Debian Codegraph @

contrib/ms-sbvar/utilities_dw/stat/dw_rand.cc

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <boost/math/distributions/chi_squared.hpp>
19 #include <cmath>
20 #include <cstdio>
21 #include <random>
22 #include <sstream>
23
24 #include "dw_rand.h"
25
26 extern "C"
27 {
28 /*******************************************************************************/
29 /*************************** Uniform Random Numbers ****************************/
30 /*******************************************************************************/
31
32 static std::mt19937 gen;
33
34 /*
35 Initializes seed value for uniform random number generator. The seed value
36 can be any integer. A value of 0 will initialize the seed from a (hopefully)
37 non-deterministic random number generator.
38 */
39 void dw_initialize_generator(int init)
40 {
41 using seed_t = decltype(gen)::result_type;
42 if (init == 0)
43 {
44 std::random_device rd;
45 std::uniform_int_distribution<seed_t> dist {std::numeric_limits<seed_t>::min()};
46 gen.seed(dist(rd));
47 }
48 else
49 gen.seed(static_cast<seed_t>(init));
50 }
51
52 void dw_print_generator_state(FILE *f)
53 {
54 std::ostringstream s;
55 s << gen << "\n";
56 std::fprintf(f, "%s", s.str().c_str());
57 }
58
59
60 /*******************************************************************************/
61 /*******************************************************************************/
62 /*******************************************************************************/
63
64 /*
65 Generates a uniform (0,1) deviate.
66 */
67 PRECISION dw_uniform_rnd(void)
68 {
69 std::uniform_real_distribution<PRECISION> dist;
70 return dist(gen);
71 }
72
73
74 /*
75 Returns a standard gaussian deviate. The density function for the
76 standard gaussian is
77
78 1
79 ----------- exp(-0.5*x^2)
80 sqrt(2*Pi)
81
82 */
83 PRECISION dw_gaussian_rnd(void)
84 {
85 std::normal_distribution<PRECISION> dist;
86 return dist(gen);
87 }
88
89 /*
90 Returns a standard truncated gaussian deviate. Usually, a <= b, but if this
91 is not the case, then the values of a and b will be swapped.
92
93 Based on ideas in Robert's "Simulation of Truncated Normal Variables,"
94 Statistics and Computing, June 1995. (There are be eariler cites).
95
96 If a >= 0 or b <= 0, use rejection method with an exponential proposal. The
97 exponential family is
98
99 g(x) = alpha*exp(-alpha*(x-a))/(1-exp(-alpha*(b-a)))
100
101 If a >= 0, alpha is chosen to be (a+sqrt(a^2+4))/2 if this number is less than
102 or equal to b and (a+b)/2 otherwise. Emperical evidence indicates that the
103 worst case is a=0, b=infinity with the probability acceptance equal to 0.7602.
104 The case b <= 0 is similar.
105
106 If a < 0 < b, and b - a < sqrt(2*pi), use rejection method with uniform
107 proposal. Worst case: a=0, b=sqrt(2*pi) with probability of acceptance equal
108 to 0.4939. If a=0, then the exponential proposal would be used, but if a
109 is slightly less than 0, then the acceptance probability will be slightly more
110 than 0.4939.
111
112 If a < 0 < b, and b - a >= sqrt(2*pi), draw from gaussian until the constraint
113 is satisfied. Worst case: a=0, b=sqrt(2*pi) with probability of acceptance
114 equal to 0.4939. If a=0, then the exponential proposal would be used, but if
115 a is slightly less than 0, then the acceptance probability will be slightly
116 more than 0.4939.
117 */
118 PRECISION dw_truncated_gaussian_rnd(PRECISION a, PRECISION b)
119 {
120 PRECISION alpha, c, x;
121
122 if (a == b) return a;
123
124 if (a > b)
125 {
126 x=a;
127 a=b;
128 b=x;
129 }
130
131 if (a >= 0)
132 {
133 alpha=0.5*(a+sqrt(a*a+4.0));
134 if (alpha >= (x=0.5*(a+b))) alpha=x;
135 c=1-exp(-alpha*(b-a));
136 x=a-log(1-c*dw_uniform_rnd())/alpha;
137 while (dw_uniform_rnd() > exp(-0.5*(x-alpha)*(x-alpha)))
138 x=a-log(1-c*dw_uniform_rnd())/alpha;
139 return x;
140 }
141
142 if (b <= 0)
143 {
144 alpha=0.5*(-b+sqrt(b*b+4));
145 if (alpha >= (x=-0.5*(a+b))) alpha=x;
146 c=1-exp(-alpha*(b-a));
147 x=-b-log(1-c*dw_uniform_rnd())/alpha;
148 while (dw_uniform_rnd() > exp(-0.5*(x-alpha)*(x-alpha)))
149 x=-b-log(1-c*dw_uniform_rnd())/alpha;
150 return -x;
151 }
152
153 if (b-a < 2.506628274631000) // sqrt(2*pi)
154 {
155 x=a+(b-a)*dw_uniform_rnd();
156 while (dw_uniform_rnd() > exp(-x*x/2))
157 x=a+(b-a)*dw_uniform_rnd();
158 return x;
159 }
160
161 x=dw_gaussian_rnd();
162 while ((x < a) || (x > b))
163 x=dw_gaussian_rnd();
164 return x;
165 }
166
167
168 /*
169 Returns a standard gamma deviate. The density function for a standard gamma
170 distribution is
171
172 x^(a-1)*exp(-x)
173 gamma_density(x;a) = ----------------
174 gamma(a)
175
176 for a > 0. The function gamma(a) is the integral with from 0 to infinity of
177 exp(-t)*t^(a-1).
178
179 A general gamma variate can be obtained as follows. Let z=b*x. Then,
180 z is drawn from a general gamma distribution whose density is
181
182 z^(a-1)*exp(-z/b)
183 gamma_density(z;a,b) = ------------------
184 gamma(a)*b^a
185
186 Notes:
187 Does not check if a > 0.
188 */
189 PRECISION dw_gamma_rnd(PRECISION a)
190 {
191 std::gamma_distribution<PRECISION> dist {a};
192 return dist(gen);
193 }
194
195
196 /*
197 Returns a lognormal deviate. The mean and standard deviations of the
198 underlying normal distributions are passed.
199 */
200 PRECISION dw_lognormal_rnd(PRECISION mean, PRECISION standard_deviation)
201 {
202 std::lognormal_distribution<PRECISION> dist {mean, standard_deviation};
203 return dist(gen);
204 }
205
206
207
208 PRECISION dw_chi_square_cdf(PRECISION x, int df)
209 {
210 boost::math::chi_squared_distribution<PRECISION> dist {static_cast<PRECISION>(df)};
211 return cdf(dist, x);
212 }
213
214 PRECISION dw_chi_square_invcdf(PRECISION p, int df)
215 {
216 boost::math::chi_squared_distribution<PRECISION> dist {static_cast<PRECISION>(df)};
217 return quantile(dist, p);
218 }
219
220
221 /*
222 Returns the natural logrithm of the gamma function applied to x. The gamma
223 function of x is the integral from 0 to infinity of t^(x-1)*exp(-t)dt.
224 */
225 PRECISION dw_log_gamma(PRECISION x)
226 {
227 return std::lgamma(x);
228 }
229
230 } // extern "C"