File: test_poisson_distribution.cpp

package info (click to toggle)
rocrand 5.3.3-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 37,268 kB
  • sloc: cpp: 95,463; f90: 2,847; python: 1,648; sh: 293; xml: 210; makefile: 49
file content (136 lines) | stat: -rw-r--r-- 4,334 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include <stdio.h>
#include <gtest/gtest.h>

#include <random>
#include <vector>

#include <rng/generator_type.hpp>
#include <rng/generators.hpp>
#include <rng/distribution/poisson.hpp>

template<typename T>
double get_mean(std::vector<T> values)
{
    double mean = 0.0f;
    for (auto v : values)
    {
        mean += static_cast<double>(v);
    }
    return mean / values.size();
}

template<typename T>
double get_variance(std::vector<T> values, double mean)
{
    double variance = 0.0f;
    for (auto v : values)
    {
        const double x = static_cast<double>(v) - mean;
        variance += x * x;
    }
    return variance / values.size();
}

class poisson_distribution_tests : public ::testing::TestWithParam<double> { };

TEST_P(poisson_distribution_tests, mean_var)
{
    const double lambda = GetParam();

    std::random_device rd;
    std::mt19937 gen(rd());

    rocrand_poisson_distribution<ROCRAND_DISCRETE_METHOD_ALIAS, true> dis;
    dis.set_lambda(lambda);

    const size_t samples_count = static_cast<size_t>(std::max(2.0, sqrt(lambda))) * 100000;
    std::vector<unsigned int> values(samples_count);

    for (size_t si = 0; si < samples_count; si++)
    {
        const unsigned int v = dis(gen());
        values[si] = v;
    }

    dis.deallocate();

    const double mean = get_mean(values);
    const double variance = get_variance(values, mean);

    EXPECT_NEAR(mean, lambda, std::max(1.0, lambda * 1e-2));
    EXPECT_NEAR(variance, lambda, std::max(1.0, lambda * 1e-2));
}

TEST_P(poisson_distribution_tests, histogram_compare)
{
    const double lambda = GetParam();

    std::random_device rd;
    std::mt19937 gen(rd());
    std::poisson_distribution<unsigned int> host_dis(lambda);

    rocrand_poisson_distribution<ROCRAND_DISCRETE_METHOD_ALIAS, true> dis;
    dis.set_lambda(lambda);

    const size_t samples_count = static_cast<size_t>(std::max(2.0, sqrt(lambda))) * 100000;
    const size_t bin_size = static_cast<size_t>(std::max(2.0, sqrt(lambda)));
    const size_t bins_count = static_cast<size_t>((2.0 * lambda + 10.0) / bin_size);
    std::vector<unsigned int> historgram0(bins_count);
    std::vector<unsigned int> historgram1(bins_count);

    for (size_t si = 0; si < samples_count; si++)
    {
        const unsigned int v = host_dis(gen);
        const size_t bin = v / bin_size;
        if (bin < bins_count)
        {
            historgram0[bin]++;
        }
    }

    for (size_t si = 0; si < samples_count; si++)
    {
        const unsigned int v = dis(gen());
        const size_t bin = v / bin_size;
        if (bin < bins_count)
        {
            historgram1[bin]++;
        }
    }

    dis.deallocate();

    // Very loose comparison
    for (size_t bi = 0; bi < bins_count; bi++)
    {
        const unsigned int h0 = historgram0[bi];
        const unsigned int h1 = historgram1[bi];
        EXPECT_NEAR(h0, h1, std::max(samples_count * 1e-3, std::max(h0, h1) * 1e-1));
    }
}

const double lambdas[] = { 1.0, 5.5, 20.0, 100.0, 1234.5, 5000.0 };

INSTANTIATE_TEST_SUITE_P(poisson_distribution_tests,
                        poisson_distribution_tests,
                        ::testing::ValuesIn(lambdas));