-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTruncNormalDist.h
300 lines (245 loc) · 9.04 KB
/
TruncNormalDist.h
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// ==========================================================================
// truncated normal distribution
// Lior Kogan ([email protected]), 2012
// based on VC 2012 std::normal_distribution (random) as a skeleton
// and on C. H. Jackson's R's implementation of the following paper:
// Robert, C. P. Simulation of truncated normal variables. Statistics and Computing (1995) 5, 121-125
// ==========================================================================
#pragma once
#include <random>
#include "CircHelper.h" // _2Pi, Sqr
#define _NRAND(eng, resty) \
(_STD generate_canonical<resty, static_cast<size_t>(-1)>(eng))
// ==========================================================================
// TEMPLATE CLASS truncated_normal_distribution
template<class _Ty= double>
class truncated_normal_distribution
{ // template class for truncated normal distribution
public:
static_assert(_Is_RealType<_Ty>::value,
"invalid template argument for truncated_normal_distribution");
typedef truncated_normal_distribution<_Ty> _Myt;
typedef _Ty result_type;
struct param_type
{ // parameter package
typedef _Myt distribution_type;
explicit param_type(_Ty _Mean0 = 0., _Ty _Sigma0 = 1., _Ty _A0 = 0., _Ty _B0 = 0.)
{ // construct from parameters
_Init(_Mean0, _Sigma0, _A0, _B0);
}
bool operator==(const param_type& _Right) const
{ // test for equality
return _Mean == _Right._Mean &&
_Sigma == _Right._Sigma &&
_A == _Right._A &&
_B == _Right._B ;
}
bool operator!=(const param_type& _Right) const
{ // test for inequality
return !(*this == _Right);
}
_Ty mean() const
{ // return mean value
return _Mean;
}
_Ty sigma() const
{ // return sigma value
return _Sigma;
}
_Ty a() const
{ // return truncation-range lower-bound
return _A;
}
_Ty b() const
{ // return truncation-range upper-bound
return _B;
}
_Ty stddev() const
{ // return sigma value
return _Sigma;
}
int alg() const
{ // return fastest algorithm for the given parameters
return _Alg;
}
void _Init(_Ty _Mean0, _Ty _Sigma0, _Ty _A0, _Ty _B0)
{ // set internal state
_RNG_ASSERT(0. < _Sigma0, "invalid sigma argument for truncated_normal_distribution");
_RNG_ASSERT(_A0 < _B0 , "invalid truncation-range for truncated_normal_distribution");
_Mean = _Mean0 ;
_Sigma = _Sigma0;
_A = _A0 ;
_B = _B0 ;
_NA = (_A - _Mean) / _Sigma;
_NB = (_B - _Mean) / _Sigma;
// decide on the fastest algorithm for our case
_Alg= 3;
if ((_NA < 0 ) && ( _NB > 0) && (_NB - _NA > sqrt(_2Pi))) _Alg= 0;
else if ((_NA >= 0) && ( _NB > _NA + 2.*sqrt(exp(1.)) / ( _NA + sqrt(Sqr(_NA) + 4.)) * exp((_NA*2. - _NA*sqrt(Sqr(_NA) + 4.))/4.))) _Alg= 1;
else if ((_NB <= 0) && (-_NA > -_NB + 2.*sqrt(exp(1.)) / (-_NB + sqrt(Sqr(_NB) + 4.)) * exp((_NB*2. - -_NB*sqrt(Sqr(_NB) + 4.))/4.))) _Alg= 2;
}
_Ty _Mean ;
_Ty _Sigma;
_Ty _A ;
_Ty _B ;
_Ty _NA ; // _A normalized
_Ty _NB ; // _B normalized
int _Alg ; // algorithm to use
};
explicit truncated_normal_distribution(_Ty _Mean0 = 0. ,
_Ty _Sigma0 = 1. ,
_Ty _A0 = std::numeric_limits< _Ty>::min(), // truncation-range lower-bound
_Ty _B0 = std::numeric_limits< _Ty>::max() ) // truncation-range upper-bound
: _Par(_Mean0, _Sigma0, _A0, _B0)
{ // construct
}
explicit truncated_normal_distribution(param_type _Par0)
: _Par(_Par0)
{ // construct from parameter package
}
_Ty mean() const
{ // return mean value
return _Par.mean();
}
_Ty sigma() const
{ // return sigma value
return _Par.sigma();
}
_Ty a() const
{ // return truncation-range lower-bound
return _Par.a();
}
_Ty b() const
{ // return truncation-range upper-bound
return _Par.b();
}
_Ty stddev() const
{ // return sigma value
return _Par.sigma();
}
param_type param() const
{ // return parameter package
return _Par;
}
void param(const param_type& _Par0)
{ // set parameter package
_Par= _Par0;
reset();
}
result_type (min)() const
{ // get smallest possible result
return _Par._A;
}
result_type (max)() const
{ // get largest possible result
return _Par._B;
}
void reset()
{ // clear internal state
}
template<class _Engine>
result_type operator()(_Engine& _Eng) const
{ // return next value
return _Eval(_Eng, _Par);
}
template<class _Engine>
result_type operator()(_Engine& _Eng, const param_type& _Par0) const
{ // return next value, given parameter package
reset();
return _Eval(_Eng, _Par0);
}
template<class _Elem, class _Traits>
basic_istream<_Elem, _Traits>& _Read(basic_istream<_Elem, _Traits>& _Istr)
{ // read state from _Istr
_Ty _Mean0 ;
_Ty _Sigma0;
_Ty _A0 ;
_Ty _B0 ;
_In(_Istr, _Mean0 );
_In(_Istr, _Sigma0);
_In(_Istr, _A0 );
_In(_Istr, _B0 );
_Par._Init(_Mean0, _Sigma0, _A0, _B0);
return _Istr;
}
template<class _Elem, class _Traits>
basic_ostream<_Elem, _Traits>& _Write(basic_ostream<_Elem, _Traits>& _Ostr) const
{ // write state to _Ostr
_Out(_Ostr, _Par._Mean );
_Out(_Ostr, _Par._Sigma);
_Out(_Ostr, _Par._A );
_Out(_Ostr, _Par._B );
return _Ostr;
}
private:
template<class _Engine> result_type _Eval(_Engine& _Eng, const param_type& _Par0) const
{
_Ty _Res;
switch (_Par0._Alg)
{
case 0 :
{
normal_distribution<_Ty> nd;
do { _Res = nd(_Eng); }
while (_Res<_Par0._NA || _Res>_Par0._NB);
break;
}
case 1 :
{
exponential_distribution<_Ty> ed;
_Ty a,u,z;
do
{
a = (_Par0._NA + sqrt(Sqr(_Par0._NA)+4.))/2.;
z = ed(_Eng, exponential_distribution<_Ty>::param_type(a)) + _Par0._NA;
u = _NRAND(_Eng, _Ty);
}
while ((u>exp(-Sqr(z-a)/2.)) || (z>_Par0._NB));
_Res = z;
break;
}
case 2 :
{
exponential_distribution<_Ty> ed;
_Ty a,u,z;
do
{
a = (-_Par0._NB + sqrt(Sqr(_Par0._NB)+4.))/2.;
z = ed(_Eng, exponential_distribution<_Ty>::param_type(a)) - _Par0._NB;
u = _NRAND(_Eng, _Ty);
}
while ((u>exp(-Sqr(z-a)/2.)) || (z>-_Par0._NA));
_Res = -z;
break;
}
default:
{
_Ty z,u,rho;
do
{
uniform_real_distribution<_Ty> ud(_Par0._NA, _Par0._NB);
z = ud(_Eng);
u = _NRAND(_Eng, _Ty);
if (_Par0._NA>0) rho = exp((Sqr(_Par0._NA)-Sqr(z))/2.);
else if (_Par0._NB<0) rho = exp((Sqr(_Par0._NB)-Sqr(z))/2.);
else rho = exp( -Sqr(z) /2.);
}
while (u>rho);
_Res = z;
}
}
return _Res * _Par0._Sigma + _Par0._Mean; // denormalize result
}
int _Alg; // which algorithm to use
param_type _Par;
};
template<class _Elem, class _Traits, class _Ty>
basic_istream<_Elem, _Traits>& operator>>(basic_istream<_Elem, _Traits>& _Istr, truncated_normal_distribution<_Ty>& _Dist)
{ // read state from _Istr
return _Dist._Read(_Istr);
}
template<class _Elem, class _Traits, class _Ty>
basic_ostream<_Elem, _Traits>& operator<<(basic_ostream<_Elem, _Traits>& _Ostr, const truncated_normal_distribution<_Ty>& _Dist)
{ // write state to _Ostr
return _Dist._Write(_Ostr);
}