1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// REQUIRES: long_tests
10
11// <random>
12
13// template<class RealType = double>
14// class gamma_distribution
15
16// template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
17
18#include <random>
19#include <cassert>
20#include <vector>
21#include <numeric>
22
23#include "test_macros.h"
24
25template <class T>
26inline
27T
28sqr(T x)
29{
30 return x * x;
31}
32
33int main(int, char**)
34{
35 {
36 typedef std::gamma_distribution<> D;
37 typedef D::param_type P;
38 typedef std::mt19937 G;
39 G g;
40 D d(0.5, 2);
41 P p(1, .5);
42 const int N = 1000000;
43 std::vector<D::result_type> u;
44 for (int i = 0; i < N; ++i)
45 {
46 D::result_type v = d(g, p);
47 assert(d.min() < v);
48 u.push_back(v);
49 }
50 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size();
51 double var = 0;
52 double skew = 0;
53 double kurtosis = 0;
54 for (unsigned i = 0; i < u.size(); ++i)
55 {
56 double dbl = (u[i] - mean);
57 double d2 = sqr(dbl);
58 var += d2;
59 skew += dbl * d2;
60 kurtosis += d2 * d2;
61 }
62 var /= u.size();
63 double dev = std::sqrt(x: var);
64 skew /= u.size() * dev * var;
65 kurtosis /= u.size() * var * var;
66 kurtosis -= 3;
67 double x_mean = p.alpha() * p.beta();
68 double x_var = p.alpha() * sqr(p.beta());
69 double x_skew = 2 / std::sqrt(x: p.alpha());
70 double x_kurtosis = 6 / p.alpha();
71 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
72 assert(std::abs((var - x_var) / x_var) < 0.01);
73 assert(std::abs((skew - x_skew) / x_skew) < 0.01);
74 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
75 }
76 {
77 typedef std::gamma_distribution<> D;
78 typedef D::param_type P;
79 typedef std::mt19937 G;
80 G g;
81 D d(1, .5);
82 P p(2, 3);
83 const int N = 1000000;
84 std::vector<D::result_type> u;
85 for (int i = 0; i < N; ++i)
86 {
87 D::result_type v = d(g, p);
88 assert(d.min() < v);
89 u.push_back(v);
90 }
91 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size();
92 double var = 0;
93 double skew = 0;
94 double kurtosis = 0;
95 for (unsigned i = 0; i < u.size(); ++i)
96 {
97 double dbl = (u[i] - mean);
98 double d2 = sqr(dbl);
99 var += d2;
100 skew += dbl * d2;
101 kurtosis += d2 * d2;
102 }
103 var /= u.size();
104 double dev = std::sqrt(x: var);
105 skew /= u.size() * dev * var;
106 kurtosis /= u.size() * var * var;
107 kurtosis -= 3;
108 double x_mean = p.alpha() * p.beta();
109 double x_var = p.alpha() * sqr(p.beta());
110 double x_skew = 2 / std::sqrt(p.alpha());
111 double x_kurtosis = 6 / p.alpha();
112 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
113 assert(std::abs((var - x_var) / x_var) < 0.01);
114 assert(std::abs((skew - x_skew) / x_skew) < 0.01);
115 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
116 }
117 {
118 typedef std::gamma_distribution<> D;
119 typedef D::param_type P;
120 typedef std::mt19937 G;
121 G g;
122 D d(2, 3);
123 P p(.5, 2);
124 const int N = 1000000;
125 std::vector<D::result_type> u;
126 for (int i = 0; i < N; ++i)
127 {
128 D::result_type v = d(g, p);
129 assert(d.min() < v);
130 u.push_back(v);
131 }
132 double mean = std::accumulate(u.begin(), u.end(), 0.0) / u.size();
133 double var = 0;
134 double skew = 0;
135 double kurtosis = 0;
136 for (unsigned i = 0; i < u.size(); ++i)
137 {
138 double dbl = (u[i] - mean);
139 double d2 = sqr(dbl);
140 var += d2;
141 skew += dbl * d2;
142 kurtosis += d2 * d2;
143 }
144 var /= u.size();
145 double dev = std::sqrt(x: var);
146 skew /= u.size() * dev * var;
147 kurtosis /= u.size() * var * var;
148 kurtosis -= 3;
149 double x_mean = p.alpha() * p.beta();
150 double x_var = p.alpha() * sqr(p.beta());
151 double x_skew = 2 / std::sqrt(p.alpha());
152 double x_kurtosis = 6 / p.alpha();
153 assert(std::abs((mean - x_mean) / x_mean) < 0.01);
154 assert(std::abs((var - x_var) / x_var) < 0.01);
155 assert(std::abs((skew - x_skew) / x_skew) < 0.01);
156 assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
157 }
158
159 return 0;
160}
161

source code of libcxx/test/std/numerics/rand/rand.dist/rand.dist.pois/rand.dist.pois.gamma/eval_param.pass.cpp