1// Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// =============================================================================
15#include "tensorflow/contrib/boosted_trees/lib/utils/random.h"
16#include "tensorflow/core/platform/test.h"
17
18namespace tensorflow {
19namespace boosted_trees {
20namespace utils {
21namespace {
22
23TEST(RandomTest, Poisson) {
24  random::PhiloxRandom philox(77L);
25  random::SimplePhilox rng(&philox);
26  for (int trial = 0; trial < 10; ++trial) {
27    const int32 num_bootstrap = 10000;
28    double sum = 0;
29    double zeros = 0;
30    double ones = 0;
31    for (int i = 0; i < num_bootstrap; ++i) {
32      auto n = PoissonBootstrap(&rng);
33      sum += n;
34      zeros += (n == 0) ? 1 : 0;
35      ones += (n == 1) ? 1 : 0;
36    }
37
38    // Ensure mean is near expected value.
39    const double expected_mean = 1.0;  // lambda
40    const double mean_std_error = 1.0 / sqrt(num_bootstrap);
41    double mean = sum / num_bootstrap;
42    EXPECT_NEAR(mean, expected_mean, 3 * mean_std_error);
43
44    // Ensure probability mass for values 0 and 1 are near expected value.
45    const double expected_p = 0.368;
46    const double proportion_std_error =
47        sqrt(expected_p * (1 - expected_p) / num_bootstrap);
48    EXPECT_NEAR(zeros / num_bootstrap, expected_p, 3 * proportion_std_error);
49    EXPECT_NEAR(ones / num_bootstrap, expected_p, 3 * proportion_std_error);
50  }
51}
52
53}  // namespace
54}  // namespace utils
55}  // namespace boosted_trees
56}  // namespace tensorflow
57