4336 |
14 Apr 23 |
peter |
// $Id$ |
4336 |
14 Apr 23 |
peter |
2 |
// |
4336 |
14 Apr 23 |
peter |
// Copyright (C) 2023 Peter Johansson |
4336 |
14 Apr 23 |
peter |
4 |
// |
4336 |
14 Apr 23 |
peter |
// This program is free software; you can redistribute it and/or modify |
4336 |
14 Apr 23 |
peter |
// it under the terms of the GNU General Public License as published by |
4336 |
14 Apr 23 |
peter |
// the Free Software Foundation; either version 3 of the License, or |
4336 |
14 Apr 23 |
peter |
// (at your option) any later version. |
4336 |
14 Apr 23 |
peter |
9 |
// |
4336 |
14 Apr 23 |
peter |
// This program is distributed in the hope that it will be useful, but |
4336 |
14 Apr 23 |
peter |
// WITHOUT ANY WARRANTY; without even the implied warranty of |
4336 |
14 Apr 23 |
peter |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4336 |
14 Apr 23 |
peter |
// General Public License for more details. |
4336 |
14 Apr 23 |
peter |
14 |
// |
4336 |
14 Apr 23 |
peter |
// You should have received a copy of the GNU General Public License |
4336 |
14 Apr 23 |
peter |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
4336 |
14 Apr 23 |
peter |
17 |
|
4336 |
14 Apr 23 |
peter |
18 |
|
4336 |
14 Apr 23 |
peter |
19 |
#include <config.h> |
4336 |
14 Apr 23 |
peter |
20 |
|
4336 |
14 Apr 23 |
peter |
21 |
#include "Suite.h" |
4336 |
14 Apr 23 |
peter |
22 |
#include "yat/statistics/ln_pdf.h" |
4336 |
14 Apr 23 |
peter |
23 |
#include "yat/statistics/utility.h" |
4336 |
14 Apr 23 |
peter |
24 |
|
4336 |
14 Apr 23 |
peter |
25 |
#include <gsl/gsl_randist.h> |
4336 |
14 Apr 23 |
peter |
26 |
#include <cmath> |
4336 |
14 Apr 23 |
peter |
27 |
|
4336 |
14 Apr 23 |
peter |
28 |
using namespace theplu::yat; |
4336 |
14 Apr 23 |
peter |
29 |
|
4336 |
14 Apr 23 |
peter |
30 |
void test_bernoulli_ln_pdf(test::Suite& suite, unsigned int k, double p) |
4336 |
14 Apr 23 |
peter |
31 |
{ |
4336 |
14 Apr 23 |
peter |
32 |
double ln_pdf = statistics::bernoulli_ln_pdf(k, p); |
4336 |
14 Apr 23 |
peter |
33 |
double pdf = gsl_ran_bernoulli_pdf(k, p); |
4336 |
14 Apr 23 |
peter |
34 |
suite.add(suite.equal(ln_pdf, std::log(pdf))); |
4336 |
14 Apr 23 |
peter |
35 |
} |
4336 |
14 Apr 23 |
peter |
36 |
|
4336 |
14 Apr 23 |
peter |
37 |
|
4336 |
14 Apr 23 |
peter |
38 |
void test_binomial_ln_pdf(test::Suite& suite, unsigned int k, double p, |
4336 |
14 Apr 23 |
peter |
39 |
unsigned int n) |
4336 |
14 Apr 23 |
peter |
40 |
{ |
4336 |
14 Apr 23 |
peter |
41 |
double ln_pdf = statistics::binomial_ln_pdf(k, p, n); |
4336 |
14 Apr 23 |
peter |
42 |
double pdf = gsl_ran_binomial_pdf(k, p, n); |
4336 |
14 Apr 23 |
peter |
43 |
suite.add(suite.equal(ln_pdf, std::log(pdf), 10)); |
4336 |
14 Apr 23 |
peter |
44 |
} |
4336 |
14 Apr 23 |
peter |
45 |
|
4336 |
14 Apr 23 |
peter |
46 |
|
4336 |
14 Apr 23 |
peter |
47 |
void test_exponential_ln_pdf(test::Suite& suite, double x, double m) |
4336 |
14 Apr 23 |
peter |
48 |
{ |
4336 |
14 Apr 23 |
peter |
49 |
double ln_pdf = statistics::exponential_ln_pdf(x, m); |
4336 |
14 Apr 23 |
peter |
50 |
double pdf = gsl_ran_exponential_pdf(x, m); |
4336 |
14 Apr 23 |
peter |
51 |
if (!suite.add(suite.equal(ln_pdf, std::log(pdf)))) { |
4336 |
14 Apr 23 |
peter |
52 |
suite.err() << "exponential_ln_pdf(" << x << ", " << m << "): " |
4336 |
14 Apr 23 |
peter |
53 |
<< ln_pdf << "\n"; |
4336 |
14 Apr 23 |
peter |
54 |
suite.err() << "pdf: " << pdf << " log(pdf): " << pdf << "\n"; |
4336 |
14 Apr 23 |
peter |
55 |
suite.err() << "\n"; |
4336 |
14 Apr 23 |
peter |
56 |
} |
4336 |
14 Apr 23 |
peter |
57 |
} |
4336 |
14 Apr 23 |
peter |
58 |
|
4336 |
14 Apr 23 |
peter |
59 |
|
4336 |
14 Apr 23 |
peter |
60 |
void test_gaussian_ln_pdf(test::Suite& suite, double x, double s) |
4336 |
14 Apr 23 |
peter |
61 |
{ |
4336 |
14 Apr 23 |
peter |
62 |
double ln_pdf = statistics::gaussian_ln_pdf(x, s); |
4336 |
14 Apr 23 |
peter |
63 |
double pdf = gsl_ran_gaussian_pdf(x, s); |
4336 |
14 Apr 23 |
peter |
64 |
suite.add(suite.equal(ln_pdf, std::log(pdf))); |
4336 |
14 Apr 23 |
peter |
65 |
} |
4336 |
14 Apr 23 |
peter |
66 |
|
4336 |
14 Apr 23 |
peter |
67 |
|
4336 |
14 Apr 23 |
peter |
68 |
void test_geometric_ln_pdf(test::Suite& suite, unsigned int k, double p) |
4336 |
14 Apr 23 |
peter |
69 |
{ |
4336 |
14 Apr 23 |
peter |
70 |
double ln_pdf = statistics::geometric_ln_pdf(k, p); |
4336 |
14 Apr 23 |
peter |
71 |
double pdf = gsl_ran_geometric_pdf(k, p); |
4336 |
14 Apr 23 |
peter |
72 |
suite.add(suite.equal(ln_pdf, std::log(pdf))); |
4336 |
14 Apr 23 |
peter |
73 |
} |
4336 |
14 Apr 23 |
peter |
74 |
|
4336 |
14 Apr 23 |
peter |
75 |
|
4336 |
14 Apr 23 |
peter |
76 |
void test_hypergeometric_ln_pdf(test::Suite& suite, unsigned int k, |
4336 |
14 Apr 23 |
peter |
77 |
unsigned int n1, unsigned int n2, |
4336 |
14 Apr 23 |
peter |
78 |
unsigned int t) |
4336 |
14 Apr 23 |
peter |
79 |
{ |
4336 |
14 Apr 23 |
peter |
80 |
double ln_pdf = statistics::hypergeometric_ln_pdf(k, n1, n2, t); |
4336 |
14 Apr 23 |
peter |
81 |
double pdf = gsl_ran_hypergeometric_pdf(k, n1, n2, t); |
4336 |
14 Apr 23 |
peter |
82 |
suite.add(suite.equal(ln_pdf, std::log(pdf))); |
4336 |
14 Apr 23 |
peter |
83 |
} |
4336 |
14 Apr 23 |
peter |
84 |
|
4336 |
14 Apr 23 |
peter |
85 |
|
4336 |
14 Apr 23 |
peter |
86 |
void test_negative_binomial_ln_pdf(test::Suite& suite, unsigned int k, |
4336 |
14 Apr 23 |
peter |
87 |
double p, double r) |
4336 |
14 Apr 23 |
peter |
88 |
{ |
4336 |
14 Apr 23 |
peter |
89 |
double ln_pdf = statistics::negative_binomial_ln_pdf(k, p, r); |
4336 |
14 Apr 23 |
peter |
90 |
double pdf = gsl_ran_negative_binomial_pdf(k, p, r); |
4336 |
14 Apr 23 |
peter |
91 |
suite.add(suite.equal(ln_pdf, std::log(pdf))); |
4336 |
14 Apr 23 |
peter |
92 |
} |
4336 |
14 Apr 23 |
peter |
93 |
|
4336 |
14 Apr 23 |
peter |
94 |
|
4336 |
14 Apr 23 |
peter |
95 |
void test_negative_hypergeometric_ln_pdf(test::Suite& suite, unsigned int k, |
4336 |
14 Apr 23 |
peter |
96 |
unsigned int n1, unsigned int n2, |
4336 |
14 Apr 23 |
peter |
97 |
unsigned int t) |
4336 |
14 Apr 23 |
peter |
98 |
{ |
4336 |
14 Apr 23 |
peter |
99 |
double ln_pdf = statistics::negative_hypergeometric_ln_pdf(k, n1, n2, t); |
4336 |
14 Apr 23 |
peter |
100 |
double pdf = statistics::negative_hypergeometric_pdf(k, n1, n2, t); |
4336 |
14 Apr 23 |
peter |
101 |
if (!suite.add(suite.equal(ln_pdf, std::log(pdf), 100))) { |
4336 |
14 Apr 23 |
peter |
102 |
suite.err() << "negative_hypergeometric_ln_pdf: " |
4336 |
14 Apr 23 |
peter |
103 |
<< k << " " << n1 << " " << n2 << " " << t << ": " |
4336 |
14 Apr 23 |
peter |
104 |
<< ln_pdf << "\n"; |
4336 |
14 Apr 23 |
peter |
105 |
suite.err() << "negative_hypergeometric_pdf: " |
4336 |
14 Apr 23 |
peter |
106 |
<< k << " " << n1 << " " << n2 << " " << t << ": " |
4336 |
14 Apr 23 |
peter |
107 |
<< pdf << "\n"; |
4336 |
14 Apr 23 |
peter |
108 |
suite.err() << "log(pdf): " << std::log(pdf) << "\n"; |
4336 |
14 Apr 23 |
peter |
109 |
} |
4336 |
14 Apr 23 |
peter |
110 |
} |
4336 |
14 Apr 23 |
peter |
111 |
|
4336 |
14 Apr 23 |
peter |
112 |
|
4336 |
14 Apr 23 |
peter |
113 |
void test_poisson_ln_pdf(test::Suite& suite, unsigned int k, double m) |
4336 |
14 Apr 23 |
peter |
114 |
{ |
4336 |
14 Apr 23 |
peter |
115 |
double ln_pdf = statistics::poisson_ln_pdf(k, m); |
4336 |
14 Apr 23 |
peter |
116 |
double pdf = gsl_ran_poisson_pdf(k, m); |
4336 |
14 Apr 23 |
peter |
117 |
suite.add(suite.equal(ln_pdf, std::log(pdf))); |
4336 |
14 Apr 23 |
peter |
118 |
} |
4336 |
14 Apr 23 |
peter |
119 |
|
4336 |
14 Apr 23 |
peter |
120 |
|
4336 |
14 Apr 23 |
peter |
121 |
|
4336 |
14 Apr 23 |
peter |
122 |
void test_ugaussian_ln_pdf(test::Suite& suite, double x) |
4336 |
14 Apr 23 |
peter |
123 |
{ |
4336 |
14 Apr 23 |
peter |
124 |
double ln_pdf = statistics::ugaussian_ln_pdf(x); |
4336 |
14 Apr 23 |
peter |
125 |
double pdf = gsl_ran_ugaussian_pdf(x); |
4336 |
14 Apr 23 |
peter |
126 |
suite.add(suite.equal(ln_pdf, std::log(pdf))); |
4336 |
14 Apr 23 |
peter |
127 |
} |
4336 |
14 Apr 23 |
peter |
128 |
|
4336 |
14 Apr 23 |
peter |
129 |
|
4336 |
14 Apr 23 |
peter |
// main functions |
4336 |
14 Apr 23 |
peter |
131 |
|
4336 |
14 Apr 23 |
peter |
132 |
void test_bernoulli_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
133 |
{ |
4336 |
14 Apr 23 |
peter |
134 |
suite.out() << "test bernoulli_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
135 |
test_bernoulli_ln_pdf(suite, 0, 0.3); |
4336 |
14 Apr 23 |
peter |
136 |
test_bernoulli_ln_pdf(suite, 1, 0.3); |
4336 |
14 Apr 23 |
peter |
137 |
} |
4336 |
14 Apr 23 |
peter |
138 |
|
4336 |
14 Apr 23 |
peter |
139 |
|
4336 |
14 Apr 23 |
peter |
140 |
void test_binomial_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
141 |
{ |
4336 |
14 Apr 23 |
peter |
142 |
suite.out() << "test binomial_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
143 |
test_binomial_ln_pdf(suite, 2, 0.2, 20); |
4336 |
14 Apr 23 |
peter |
144 |
test_binomial_ln_pdf(suite, 3, 0.2, 20); |
4336 |
14 Apr 23 |
peter |
145 |
test_binomial_ln_pdf(suite, 4, 0.2, 20); |
4336 |
14 Apr 23 |
peter |
146 |
} |
4336 |
14 Apr 23 |
peter |
147 |
|
4336 |
14 Apr 23 |
peter |
148 |
|
4336 |
14 Apr 23 |
peter |
149 |
void test_exponential_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
150 |
{ |
4336 |
14 Apr 23 |
peter |
151 |
suite.out() << "test exponential_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
152 |
test_exponential_ln_pdf(suite, 3.0, 2.0); |
4336 |
14 Apr 23 |
peter |
153 |
} |
4336 |
14 Apr 23 |
peter |
154 |
|
4336 |
14 Apr 23 |
peter |
155 |
|
4336 |
14 Apr 23 |
peter |
156 |
void test_gaussian_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
157 |
{ |
4336 |
14 Apr 23 |
peter |
158 |
suite.out() << "test gaussian_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
159 |
test_gaussian_ln_pdf(suite, 0, 1); |
4336 |
14 Apr 23 |
peter |
160 |
test_gaussian_ln_pdf(suite, 1, 1); |
4336 |
14 Apr 23 |
peter |
161 |
test_gaussian_ln_pdf(suite, 2, 1); |
4336 |
14 Apr 23 |
peter |
162 |
test_gaussian_ln_pdf(suite, 0, 2); |
4336 |
14 Apr 23 |
peter |
163 |
test_gaussian_ln_pdf(suite, 2, 0.5); |
4336 |
14 Apr 23 |
peter |
164 |
} |
4336 |
14 Apr 23 |
peter |
165 |
|
4336 |
14 Apr 23 |
peter |
166 |
|
4336 |
14 Apr 23 |
peter |
167 |
void test_geometric_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
168 |
{ |
4336 |
14 Apr 23 |
peter |
169 |
suite.out() << "test geometric_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
170 |
test_geometric_ln_pdf(suite, 4, 0.1); |
4336 |
14 Apr 23 |
peter |
171 |
} |
4336 |
14 Apr 23 |
peter |
172 |
|
4336 |
14 Apr 23 |
peter |
173 |
|
4336 |
14 Apr 23 |
peter |
174 |
void test_hypergeometric_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
175 |
{ |
4336 |
14 Apr 23 |
peter |
176 |
suite.out() << "test hypergeometric_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
177 |
test_hypergeometric_ln_pdf(suite, 4, 10, 20, 5); |
4336 |
14 Apr 23 |
peter |
178 |
} |
4336 |
14 Apr 23 |
peter |
179 |
|
4336 |
14 Apr 23 |
peter |
180 |
|
4336 |
14 Apr 23 |
peter |
181 |
void test_negative_binomial_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
182 |
{ |
4336 |
14 Apr 23 |
peter |
183 |
suite.out() << "test negative_binomial_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
184 |
test_negative_binomial_ln_pdf(suite, 3, 0.001, 2.0); |
4336 |
14 Apr 23 |
peter |
185 |
} |
4336 |
14 Apr 23 |
peter |
186 |
|
4336 |
14 Apr 23 |
peter |
187 |
|
4336 |
14 Apr 23 |
peter |
188 |
void test_negative_hypergeometric_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
189 |
{ |
4336 |
14 Apr 23 |
peter |
190 |
suite.out() << "test negative_hypergeometric_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
191 |
test_negative_hypergeometric_ln_pdf(suite, 4, 10, 20, 5); |
4336 |
14 Apr 23 |
peter |
192 |
} |
4336 |
14 Apr 23 |
peter |
193 |
|
4336 |
14 Apr 23 |
peter |
194 |
|
4336 |
14 Apr 23 |
peter |
195 |
void test_poisson_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
196 |
{ |
4336 |
14 Apr 23 |
peter |
197 |
suite.out() << "test poisson_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
198 |
test_poisson_ln_pdf(suite, 2, 0.1); |
4336 |
14 Apr 23 |
peter |
199 |
} |
4336 |
14 Apr 23 |
peter |
200 |
|
4336 |
14 Apr 23 |
peter |
201 |
|
4336 |
14 Apr 23 |
peter |
202 |
void test_ugaussian_ln_pdf(test::Suite& suite) |
4336 |
14 Apr 23 |
peter |
203 |
{ |
4336 |
14 Apr 23 |
peter |
204 |
suite.out() << "test ugaussian_ln_pdf\n"; |
4336 |
14 Apr 23 |
peter |
205 |
test_ugaussian_ln_pdf(suite, 0); |
4336 |
14 Apr 23 |
peter |
206 |
test_ugaussian_ln_pdf(suite, 1); |
4336 |
14 Apr 23 |
peter |
207 |
test_ugaussian_ln_pdf(suite, 2); |
4336 |
14 Apr 23 |
peter |
208 |
} |
4336 |
14 Apr 23 |
peter |
209 |
|
4336 |
14 Apr 23 |
peter |
210 |
|
4336 |
14 Apr 23 |
peter |
211 |
int main(int argc, char* argv[]) |
4336 |
14 Apr 23 |
peter |
212 |
{ |
4336 |
14 Apr 23 |
peter |
213 |
test::Suite suite(argc, argv); |
4336 |
14 Apr 23 |
peter |
214 |
|
4336 |
14 Apr 23 |
peter |
215 |
test_bernoulli_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
216 |
test_binomial_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
217 |
test_exponential_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
218 |
test_gaussian_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
219 |
test_geometric_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
220 |
test_hypergeometric_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
221 |
test_negative_binomial_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
222 |
test_negative_hypergeometric_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
223 |
test_poisson_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
224 |
test_ugaussian_ln_pdf(suite); |
4336 |
14 Apr 23 |
peter |
225 |
return suite.return_value(); |
4336 |
14 Apr 23 |
peter |
226 |
} |