hops
GammaModels.hpp
Go to the documentation of this file.
1 #ifndef HOPS_GAMMAMODELS_HPP
2 #define HOPS_GAMMAMODELS_HPP
3 
4 #include <cmath>
5 #include <Eigen/Core>
6 #include <utility>
7 #include <vector>
8 
11 
12 namespace {
13  double gammaProbabilityDensityFunction(double x, double location, double scale, double shape) {
14  if (scale <= 0 || shape <= 0) {
15  throw std::runtime_error("scale and shape parameters have to be larger than 0.");
16  }
17  if (x - location < 0) {
18  return 0;
19  }
20  return (std::pow(x - location, shape - 1) * std::exp(-(x - location) / scale)) /
21  (std::tgamma(shape) * std::pow(scale, shape));
22  }
23 }
24 
25 namespace hops {
26 
28  public:
29  using FloatType = typename VectorType::Scalar;
30 
31  explicit FullGammaModel(std::vector<FloatType> measurements) : measurements(std::move(measurements)) {
32  A = MatrixType(6, 3);
33  A << 1, 0, 0,
34  -1, 0, 0,
35  0, 1, 0,
36  0, -1, 0,
37  0, 0, 1,
38  0, 0, -1;
39 
40  b = VectorType(6);
41  b << 0.9, 0., 10, -0.1, 10, -0.1;
42  }
43 
44  [[nodiscard]] FloatType computeNegativeLogLikelihood(const VectorType &parameters) const {
45  FloatType neglike = 0;
46  for (const auto &measurement : this->measurements) {
47  neglike -= std::log(
48  gammaProbabilityDensityFunction(measurement,
49  parameters(0),
50  parameters(1),
51  parameters(2)));
52 
53  }
54  return neglike;
55  }
56 
57  [[nodiscard]] const std::vector<std::string> &getParameterNames() const {
58  return parameterNames;
59  }
60 
61  VectorType getB() const {
62  return b;
63  }
64 
65  MatrixType getA() const {
66  return A;
67  }
68 
69  [[nodiscard]] const std::string &getModelName() const {
70  return modelName;
71  }
72 
73  private:
74  std::vector<std::string> parameterNames = {"location", "scale", "shape"};
75 
76  VectorType b;
77  MatrixType A;
78  std::vector<FloatType> measurements;
79  std::string modelName = "FullGammaModel";
80  };
81 
82 
83  class GammaModel1 {
84  public:
85  using FloatType = typename VectorType::Scalar;
86 
87  explicit GammaModel1(std::vector<FloatType> measurements) : measurements(std::move(measurements)) {
88  A = MatrixType(4, 2);
89  A << 1, 0,
90  -1, 0,
91  0, 1,
92  0, -1;
93 
94  b = VectorType(4);
95  b << 0.9, 0., 10, -0.1;
96  }
97 
99  FloatType neglike = 0;
100  for (const auto &measurement : this->measurements) {
101  neglike -= std::log(
102  gammaProbabilityDensityFunction(measurement,
103  parameters(0),
104  scale,
105  parameters(1)));
106 
107  }
108  return neglike;
109  }
110 
111  [[nodiscard]] const std::vector<std::string> &getParameterNames() const {
112  return parameterNames;
113  }
114 
115  VectorType getB() const {
116  return b;
117  }
118 
119  MatrixType getA() const {
120  return A;
121  }
122 
123  [[nodiscard]] const std::string &getModelName() const {
124  return modelName;
125  }
126 
127  private:
128  std::vector<std::string> parameterNames = {"location", "shape"};
129 
130  VectorType b;
131  MatrixType A;
132  std::vector<FloatType> measurements;
133  constexpr static const double scale = 1;
134  std::string modelName = "GammaModel1";
135  };
136 
137  class GammaModel2 {
138  public:
139  using FloatType = typename VectorType::Scalar;
140 
141  explicit GammaModel2(std::vector<FloatType> measurements) : measurements(std::move(measurements)) {
142  A = MatrixType(4, 2);
143  A << 1, 0,
144  -1, 0,
145  0, 1,
146  0, -1;
147 
148  b = VectorType(4);
149  b << 10, -0.1, 10, -0.1;
150  }
151 
152  [[nodiscard]] FloatType computeNegativeLogLikelihood(const VectorType &parameters) const {
153  FloatType neglike = 0;
154  for (const auto &measurement : this->measurements) {
155  neglike -= std::log(
156  gammaProbabilityDensityFunction(measurement,
157  location,
158  parameters(0),
159  parameters(1)));
160 
161  }
162  return neglike;
163  }
164 
165  [[nodiscard]] const std::vector<std::string> &getParameterNames() const {
166  return parameterNames;
167  }
168 
169  [[nodiscard]] VectorType getB() const {
170  return b;
171  }
172 
173  [[nodiscard]] MatrixType getA() const {
174  return A;
175  }
176 
177  [[nodiscard]] const std::string &getModelName() const {
178  return modelName;
179  }
180 
181  private:
182  std::vector<std::string> parameterNames = {"scale", "shape"};
183 
184  VectorType b;
185  MatrixType A;
186  std::vector<FloatType> measurements;
187 
188  constexpr static double location = 0;
189  std::string modelName = "GammaModel2";
190  };
191 
192 
194  public:
195  using FloatType = typename VectorType::Scalar;
196 
197  explicit MinimalGammaModel(std::vector<FloatType> measurements) : measurements(std::move(measurements)) {
198  A = MatrixType(2, 1);
199  A << 1, -1;
200  b = VectorType(2);
201  b << 10, -0.1;
202  }
203 
204  [[nodiscard]] FloatType computeNegativeLogLikelihood(const VectorType &parameters) const {
205  FloatType neglike = 0;
206  for (const auto &measurement : this->measurements) {
207  neglike -= std::log(
208  gammaProbabilityDensityFunction(measurement,
209  location,
210  scale,
211  parameters(0)));
212 
213  }
214  return neglike;
215  }
216 
217  [[nodiscard]] const std::vector<std::string> &getParameterNames() const {
218  return parameterNames;
219  }
220 
221  [[nodiscard]] VectorType getB() const {
222  return b;
223  }
224 
225  [[nodiscard]] MatrixType getA() const {
226  return A;
227  }
228 
229  [[nodiscard]] const std::string &getModelName() const {
230  return modelName;
231  }
232 
233  private:
234  std::vector<std::string> parameterNames = {"shape"};
235 
236  VectorType b;
237  MatrixType A;
238  std::vector<FloatType> measurements;
239 
240  constexpr static double location = 0;
241  constexpr static double scale = 1;
242  std::string modelName = "MinimalGammaModel";
243  };
244 }
245 
246 #endif //HOPS_GAMMAMODELS_HPP
hops::FullGammaModel
Definition: GammaModels.hpp:27
hops::GammaModel1
Definition: GammaModels.hpp:83
hops::FullGammaModel::computeNegativeLogLikelihood
FloatType computeNegativeLogLikelihood(const VectorType &parameters) const
Definition: GammaModels.hpp:44
hops::MatrixType
Eigen::MatrixXd MatrixType
Definition: MatrixType.hpp:7
hops::GammaModel2::getA
MatrixType getA() const
Definition: GammaModels.hpp:173
hops::GammaModel1::GammaModel1
GammaModel1(std::vector< FloatType > measurements)
Definition: GammaModels.hpp:87
hops::MinimalGammaModel::MinimalGammaModel
MinimalGammaModel(std::vector< FloatType > measurements)
Definition: GammaModels.hpp:197
hops::MinimalGammaModel::FloatType
typename VectorType::Scalar FloatType
Definition: GammaModels.hpp:195
hops::GammaModel2::GammaModel2
GammaModel2(std::vector< FloatType > measurements)
Definition: GammaModels.hpp:141
hops::GammaModel1::getModelName
const std::string & getModelName() const
Definition: GammaModels.hpp:123
hops::FullGammaModel::FloatType
typename VectorType::Scalar FloatType
Definition: GammaModels.hpp:29
hops::GammaModel1::getParameterNames
const std::vector< std::string > & getParameterNames() const
Definition: GammaModels.hpp:111
hops::FullGammaModel::getParameterNames
const std::vector< std::string > & getParameterNames() const
Definition: GammaModels.hpp:57
hops::FullGammaModel::FullGammaModel
FullGammaModel(std::vector< FloatType > measurements)
Definition: GammaModels.hpp:31
hops::GammaModel2::getModelName
const std::string & getModelName() const
Definition: GammaModels.hpp:177
hops::MinimalGammaModel
Definition: GammaModels.hpp:193
hops::GammaModel1::getA
MatrixType getA() const
Definition: GammaModels.hpp:119
hops::GammaModel1::computeNegativeLogLikelihood
FloatType computeNegativeLogLikelihood(const VectorType &parameters) const
Definition: GammaModels.hpp:98
hops::MinimalGammaModel::computeNegativeLogLikelihood
FloatType computeNegativeLogLikelihood(const VectorType &parameters) const
Definition: GammaModels.hpp:204
hops::FullGammaModel::getB
VectorType getB() const
Definition: GammaModels.hpp:61
hops::GammaModel1::FloatType
typename VectorType::Scalar FloatType
Definition: GammaModels.hpp:85
hops::MinimalGammaModel::getA
MatrixType getA() const
Definition: GammaModels.hpp:225
hops::GammaModel2::FloatType
typename VectorType::Scalar FloatType
Definition: GammaModels.hpp:139
hops::MinimalGammaModel::getParameterNames
const std::vector< std::string > & getParameterNames() const
Definition: GammaModels.hpp:217
hops
Definition: CsvReader.hpp:8
hops::MinimalGammaModel::getB
VectorType getB() const
Definition: GammaModels.hpp:221
hops::GammaModel2::computeNegativeLogLikelihood
FloatType computeNegativeLogLikelihood(const VectorType &parameters) const
Definition: GammaModels.hpp:152
hops::FullGammaModel::getModelName
const std::string & getModelName() const
Definition: GammaModels.hpp:69
VectorType.hpp
MatrixType.hpp
hops::FullGammaModel::getA
MatrixType getA() const
Definition: GammaModels.hpp:65
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
hops::GammaModel1::getB
VectorType getB() const
Definition: GammaModels.hpp:115
hops::VectorType
Eigen::VectorXd VectorType
Definition: VectorType.hpp:7
hops::GammaModel2
Definition: GammaModels.hpp:137
hops::GammaModel2::getParameterNames
const std::vector< std::string > & getParameterNames() const
Definition: GammaModels.hpp:165
hops::GammaModel2::getB
VectorType getB() const
Definition: GammaModels.hpp:169
hops::MinimalGammaModel::getModelName
const std::string & getModelName() const
Definition: GammaModels.hpp:229