-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtail_dist.hpp
40 lines (29 loc) · 852 Bytes
/
tail_dist.hpp
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
#ifndef TAIL_DIST_HPP
#define TAIL_DIST_HPP
#include <cmath>
#include <etf/random_digits.hpp>
// Marsaglia's algorithm for tail sampling.
template <typename RealType, int W>
class NormalTailDistribution {
public:
NormalTailDistribution() = default;
NormalTailDistribution(RealType xt): xt_(xt), inv_xt_(RealType(1)/xt) {}
template<class G>
RealType operator()(G& g) const
{
RealType dx;
RealType y;
do {
dx = std::log(RealType(1.0)
- etf::generate_random_real<RealType, W>(g))
*inv_xt_;
y = std::log(RealType(1.0)
- etf::generate_random_real<RealType, W>(g));
} while (-2*y < dx*dx);
return xt_ - dx;
}
private:
RealType xt_;
RealType inv_xt_;
};
#endif // TAIL_DIST_HPP