19 double sigma_scale = 3.0;
23 ZigguratBox(
double x_min,
double x_max,
double y_max,
double y_lower)
24 : m_x_min(x_min), m_x_max(x_max), m_y_max(y_max), m_y_lower(y_lower)
36 std::pair<double, double> samplingZiggurat(
double r,
double x_func_max,
double (*func_phi)(
double))
41 std::random_device rd;
42 std::mt19937 gen(rd());
43 std::uniform_real_distribution<double> uniformDist(0.0, 1.0);
45 double box_width = (x_func_max + r) / n_boxes;
46 std::vector<ZigguratBox> boxes;
47 std::vector<double> cum_area_vector;
49 double x_min = 0, x_max = 0, y_max = 0, y_lower = 0, cum_area_box = 0;
53 for (
size_t i = 0; i < n_boxes; ++i) {
59 if (x_func_max >= x_max) {
60 y_max = func_phi(x_max);
61 y_lower = func_phi(x_min);
62 }
else if (x_func_max > x_min && x_func_max <= x_max) {
63 y_max = func_phi(x_func_max);
64 y_lower = std::min(func_phi(x_min), func_phi(x_max));
66 y_max = func_phi(x_min);
67 y_lower = func_phi(x_max);
70 boxes.emplace_back(ZigguratBox(x_min, x_max, y_max, y_lower));
72 cum_area_box += box_width * y_max;
73 cum_area_vector.emplace_back(cum_area_box);
77 for (
size_t i = 0; i < n_boxes; ++i)
78 cum_area_vector[i] = cum_area_vector[i] / cum_area_vector.back();
82 bool solnFound(
false);
85 double random_cum_area = uniformDist(gen);
86 for (
size_t i = 0; i < n_boxes; ++i) {
87 if (random_cum_area <= cum_area_vector[i]) {
88 double random_y = uniformDist(gen) * boxes[i].m_y_max;
90 std::uniform_real_distribution<double> uniformDistAB(boxes[i].m_x_min,
92 double phi_attempt = uniformDistAB(gen);
94 if (random_y <= boxes[i].m_y_lower) {
98 if (random_y <= func_phi(phi_attempt)) {
109 double alpha = 2 *
M_PI * uniformDist(gen);
110 return std::make_pair(phi, alpha);
113 double func_phi_Cauchy(
double phi)
116 return phi * std::exp(-phi);
119 double func_phi_Cone(
double phi)
122 return 6 * (1 - phi) * phi;
132 double phi_max_Cauchy = 1.0;
134 double r = sigma_scale * std::sqrt(2);
135 std::pair<double, double> samples = samplingZiggurat(r, phi_max_Cauchy, func_phi_Cauchy);
136 return std::make_pair(
m_omega_x * samples.first * std::cos(samples.second),
137 m_omega_y * samples.first * std::sin(samples.second));
142 std::random_device rd;
143 std::mt19937 gen(rd());
144 std::uniform_real_distribution<double> uniformDist(0.0, 1.0);
146 double cdf_value_phi = uniformDist(gen);
149 double phi = std::sqrt(-2 * std::log(1 - cdf_value_phi));
150 double alpha = 2 *
M_PI * uniformDist(gen);
151 return std::make_pair(
m_omega_x * phi * std::cos(alpha),
m_omega_y * phi * std::sin(alpha));
156 std::random_device rd;
157 std::mt19937 gen(rd());
158 std::uniform_real_distribution<double> uniformDist(0.0, 1.0);
160 double cdf_value_phi = uniformDist(gen);
163 double phi = std::sqrt(cdf_value_phi);
164 double alpha = 2 *
M_PI * uniformDist(gen);
165 return std::make_pair(
m_omega_x * phi * std::cos(alpha),
m_omega_y * phi * std::sin(alpha));
172 double phi_max_Cone = 0.5;
175 std::pair<double, double> samples = samplingZiggurat(r, phi_max_Cone, func_phi_Cone);
176 return std::make_pair(
m_omega_x * samples.first * std::cos(samples.second),
177 m_omega_y * samples.first * std::sin(samples.second));
Defines interface class IFTDistribution1D, and children thereof.
std::pair< double, double > randomSample() const final
std::pair< double, double > randomSample() const final
std::pair< double, double > randomSample() const final
std::pair< double, double > randomSample() const final
virtual ~IDistribution2DSampler()