MIDSX 0.1
A x-ray transport code system for dosimetry
Loading...
Searching...
No Matches
probability_dist.h
1#ifndef PROBABILITY_DIST_H
2#define PROBABILITY_DIST_H
3
4#include <random>
5#include <utility>
6#include <vector>
7#include <string>
8#include <algorithm>
9#include <functional>
10#include <stdexcept>
11#include <Eigen/Core>
12#include <unsupported/Eigen/CXX11/Tensor>
13
14namespace ProbabilityDistHelpers {
15 int findIndexOfNextSmallestValue(double x, const Eigen::VectorXd &vector);
16}
17
18namespace ProbabilityDist {
19
23 class Uniform {
24 public:
25 Uniform() = default;
32 Uniform(double min, double max) : dist_(min, max) {}
33
39 double sample() const {
40 return dist_(generator_);
41 }
42
49 void setRange(double min, double max) {
50 dist_ = std::uniform_real_distribution<double>(min, max);
51 }
52 private:
53 mutable std::uniform_real_distribution<double> dist_;
54 static thread_local std::mt19937 generator_;
55 };
56
57
62 public:
68 explicit DiscreteInversion(const Eigen::Matrix<double, Eigen::Dynamic, 2> &probabilities_matrix);
69
75 double sample() const;
76
82 double getExpectationValue() const;
83 private:
84 Eigen::Matrix<double, Eigen::Dynamic, 2> probabilities_matrix_;
85 Eigen::Matrix<double, Eigen::Dynamic, 2> cdf_matrix_;
86 std::string sampling_algorithm_;
87 ProbabilityDist::Uniform uniform_dist_;
88
89 // Checks if discrete distribution is normalized. If not, normalizes it.
90 void normalize();
91
92 // Performs cumulative sum on an Eigen vector
93 static Eigen::VectorXd cumsum(const Eigen::VectorXd &vector);
94
95 // generate cdf for sampling
96 Eigen::Matrix<double, Eigen::Dynamic, 2> generateCDF() const;
97
98 };
99
101 double x_i;
102 double x_i_1;
103 double y_i;
104 double y_i_1;
105 double a_i;
106 double b_i;
107 };
108
113 public:
114 ContinuousInversion() = default;
115
125 explicit ContinuousInversion(std::function<double(double, double)> &PDF, Eigen::VectorXd &energies, double x_min, double x_max, double err_thresh = 1E-4);
126
133 double sample(double E) const;
134
135 private:
136 std::function<double(double, double)> PDF_;
137 std::function<double(double, double)> normalized_PDF_;
138 std::vector<Eigen::Array<double, Eigen::Dynamic, 2>> CDF_;
139 Eigen::VectorXd energies_;
140 std::vector<Eigen::Array<double, Eigen::Dynamic, 2>> interp_parameters_;
141 double x_min_;
142 double x_max_;
143 double err_thresh_;
144 ProbabilityDist::Uniform uniform_dist_;
145
146 void normalizePDF(double E);
147
148 double getXFromY(int energy_index, double y) const;
149
150 void initializeCDFAndInterpolationParameters();
151
152 Eigen::Array<double, Eigen::Dynamic, 2> calculateInterpolationParametersPerEnergy(
153 double E, Eigen::Array<double, Eigen::Dynamic, 2> CDF_RITA_PER_ENERGY);
154
155 Eigen::Array<double, Eigen::Dynamic, 2> getMinimizedErrorCDFPerEnergy(double E, double err_thresh);
156
157 Eigen::Array<double, Eigen::Dynamic, 2> generateCDFPerEnergy(double E, Eigen::VectorXd x_grid);
158
159 double getInterpErrorOverInterval(double E, IntervalData id) const;
160
161 double getPDFFromCDFOverInterval(double E, double x, IntervalData id) const;
162
163 double getEta(double x, IntervalData id) const;
164 };
165}
166#endif
Class which uses PENELOPE's RITA inversion sampling algorithm to sample from a continuous distributio...
double sample(double E) const
Returns a sample from the continuous distribution.
ContinuousInversion(std::function< double(double, double)> &PDF, Eigen::VectorXd &energies, double x_min, double x_max, double err_thresh=1E-4)
Constructor for the ContinuousInversion class.
Class which uses inversion sampling to sample from a discrete distribution.
double getExpectationValue() const
Returns the expectation value of the discrete distribution.
double sample() const
Returns a sample from the discrete distribution.
DiscreteInversion(const Eigen::Matrix< double, Eigen::Dynamic, 2 > &probabilities_matrix)
Constructor for the DiscreteInversion class.
Class which represents a uniform distribution.
void setRange(double min, double max)
Sets the range of the uniform distribution.
double sample() const
Returns a sample from the uniform distribution.
Uniform(double min, double max)
Constructor for the Uniform class.