Loading [MathJax]/extensions/ams.js
ALMaSS  1.2 (after EcoStack, March 2024)
The Animal, Landscape and Man Simulation System
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
probability_distribution Class Reference

#include <ALMaSS_Random.h>

Public Member Functions

 probability_distribution (const std::string &prob_type, std::string prob_params)
 
double Get () const
 
int Geti () const
 
 ~probability_distribution ()
 

Private Attributes

const std::string DISCRETE_DIST_S { "DISCRETE" }
 
const std::string BETABINOMIAL_DIST_S { "BETABINOMIAL" }
 
const std::string NORMAL_DIST_S { "NORMAL" }
 
const std::string UNIREAL_DIST_S { "UNIREAL" }
 
const std::string UNIINT_DIST_S { "UNIINT" }
 
const std::string BETA_DIST_S { "BETA" }
 
const std::string GAMMA_DIST_S { "GAMMA" }
 
const std::string CAUCHY_DIST_S { "CAUCHY" }
 
const std::string EXPONENTIAL_DIST_S { "EXPONENTIAL" }
 
void * m_probDistrib
 
void * m_probDistrib1
 
void * m_probDistrib2
 
int m_type
 

Static Private Attributes

static const int DISCRETE_DIST_T = 1
 
static const int NORMAL_DIST_T = 2
 
static const int UNIREAL_DIST_T = 3
 
static const int UNIINT_DIST_T = 4
 
static const int BETA_DIST_T = 5
 
static const int GAMMA_DIST_T = 6
 
static const int CAUCHY_DIST_T = 7
 
static const int EXPONENTIAL_DIST_T = 8
 

Detailed Description

ALMaSS Probability Distribution class, wrapping around the Boost-library random distributions classes

Constructor & Destructor Documentation

◆ probability_distribution()

probability_distribution::probability_distribution ( const std::string &  prob_type,
std::string  prob_params 
)

Constructor of the class, version 1

Parameters
[in]prob_typea string containing the label of the distribution, must match the supported/predefined values
[in]prob_paramsa string containing space,comma, or tab separated numerical values of the required parameters associated with the distribution type
126  {
127  // take the argument string and split it into numerical values ( make this a function )
128  replace(prob_params.begin(), prob_params.end(), ',', ' '); // replace all commas with spaces (for easier parsing
129  std::istringstream iss(prob_params);
130  std::vector<double> number_args;
131 
132  double temp;
133  while(iss >> temp) { number_args.push_back(temp); }
134 
135  int no_args = number_args.size();
136 
137  // for each supported probability distribution type, perform initialization
138  if (prob_type == DISCRETE_DIST_S)
139  {
140  m_probDistrib = new DISCRETE_DIST(number_args.begin(), number_args.end());
142  return;
143  }
144  if (prob_type == BETABINOMIAL_DIST_S)
145  {
146  if (no_args != 4)
147  {
148  g_msg->Warn(WARN_MSG, "Number of arguments for a Beta Binomial distribution needs to be 4. It is now: ", no_args);
149  exit(1);
150  }
151  std::vector<double> temp_weights = g_beta_binomial_probabilities_fnc(static_cast<int>(number_args[0]), number_args[1], number_args[2], static_cast<bool>(number_args[3]));
152  m_probDistrib = new DISCRETE_DIST(temp_weights.begin(), temp_weights.end());
153 
154  // max value, alpha, beta, prob(outcome=0) = 0 boolean option
156  return;
157  }
158  if (prob_type == NORMAL_DIST_S)
159  {
160  if (no_args != 2)
161  {
162  g_msg->Warn(WARN_MSG, "Number of arguments for a Normal distribution needs to be 2. It is now: ", no_args);
163  exit(1);
164  }
165  if (number_args[1] < 0)
166  {
167  g_msg->Warn(WARN_MSG, "Std. deviation (sigma) parameter of normal distribution is negative:", static_cast<int>(number_args[1]));
168  exit(1);
169  }
170  m_probDistrib = new NORMAL_DIST(number_args[0], number_args[1]); // mean, sigma>0
172  return;
173  }
174  if (prob_type == UNIREAL_DIST_S)
175  {
176  if (no_args != 2)
177  {
178  g_msg->Warn(WARN_MSG, "Number of arguments for a uniform real distribution needs to be 2. It is now: ", no_args);
179  exit(1);
180  }
181  if (number_args[0] > number_args[1])
182  {
183  g_msg->Warn(WARN_MSG, "Min param > Max param of uniform real distribution :",
184  std::to_string(number_args[0]) + " " + std::to_string(number_args[1]));
185  exit(1);
186  }
187  m_probDistrib = new UNIREAL_DIST(number_args[0], number_args[1]); // [min, max)
189  return;
190  }
191  if (prob_type == UNIINT_DIST_S)
192  {
193  if (no_args != 2)
194  {
195  g_msg->Warn(WARN_MSG, "Number of arguments for a uniform int distribution needs to be 2. It is now: ", no_args);
196  exit(1);
197  }
198  if (number_args[0] > number_args[1])
199  {
200  g_msg->Warn(WARN_MSG, "Min param > Max param of uniform integer distribution :",
201  std::to_string(number_args[0]) + " " + std::to_string(number_args[1]));
202  exit(1);
203  }
204  m_probDistrib = new UNIINT_DIST(static_cast<int>(number_args[0]), static_cast<int>(number_args[1])); // [min, max]
206  return;
207  }
208  if (prob_type == BETA_DIST_S)
209  {
210  if (no_args != 2)
211  {
212  g_msg->Warn(WARN_MSG, "Number of arguments for a beta distribution needs to be 2. It is now: ", no_args);
213  exit(1);
214  }
215  if (number_args[0] <= 0 || number_args[1] <= 0)
216  {
217  g_msg->Warn(WARN_MSG, "The parameters of the beta distribution need to be strictly positive values. They are now: ",
218  std::to_string(number_args[0]) + " " + std::to_string(number_args[1]));
219  exit(1);
220  }
221  m_probDistrib1 = new GAMMA_DIST(number_args[0], 1.0); // use two gamma distributions to generate a beta distribution
222  m_probDistrib2 = new GAMMA_DIST(number_args[1], 1.0);
224  }
225  else if (prob_type == GAMMA_DIST_S)
226  {
227  if (no_args != 2)
228  {
229  g_msg->Warn(WARN_MSG, "Number of arguments for a gamma distribution needs to be 2. It is now: ", no_args);
230  exit(1);
231  }
232  if (number_args[0] <= 0 || number_args[1] <= 0)
233  {
234  g_msg->Warn(WARN_MSG, "The parameters of the gamma distribution need to be strictly positive values. They are now: ",
235  std::to_string(number_args[0]) + " " + std::to_string(number_args[1]));
236  exit(1);
237  }
238  m_probDistrib = new GAMMA_DIST(number_args[0], number_args[1]); // alpha and beta
240  return;
241  }
242  else if (prob_type == CAUCHY_DIST_S)
243  {
244  if (no_args != 2)
245  {
246  g_msg->Warn(WARN_MSG, "Number of arguments for a cauchy distribution needs to be 2. It is now: ", no_args);
247  exit(1);
248  }
249  if (number_args[1] <= 0)
250  {
251  g_msg->Warn(WARN_MSG, "The scale parameters of the cauchy distribution need to be a strictly positive value. They are now: ",
252  std::to_string(number_args[1]));
253  exit(1);
254  }
255  m_probDistrib = new CAUCHY_DIST(number_args[0], number_args[1]); // location and scale
257  return;
258  }
259  else if (prob_type == EXPONENTIAL_DIST_S)
260  {
261  if (no_args != 1)
262  {
263  g_msg->Warn(WARN_MSG, "Number of arguments for a exponential distribution needs to be 1. It is now: ", no_args);
264  exit(1);
265  }
266  if (number_args[1] <= 0)
267  {
268  g_msg->Warn(WARN_MSG, "The scale parameters of the exponential distribution need to be a strictly positive value. They are now: ",
269  std::to_string(number_args[1]));
270  exit(1);
271  }
272  m_probDistrib = new EXPONENTIAL_DIST(number_args[0]); // location and scale
274  return;
275  }
276  else
277  {
278  g_msg->Warn(WARN_UNDEF, "Unknown distribution type string value: ", prob_type);
279  exit(1);
280  }
281 }

References BETA_DIST_S, BETA_DIST_T, BETABINOMIAL_DIST_S, CAUCHY_DIST_S, CAUCHY_DIST_T, DISCRETE_DIST_S, DISCRETE_DIST_T, EXPONENTIAL_DIST_S, EXPONENTIAL_DIST_T, g_beta_binomial_probabilities_fnc(), g_msg, GAMMA_DIST_S, GAMMA_DIST_T, m_probDistrib, m_probDistrib1, m_probDistrib2, m_type, NORMAL_DIST_S, NORMAL_DIST_T, UNIINT_DIST_S, UNIINT_DIST_T, UNIREAL_DIST_S, UNIREAL_DIST_T, MapErrorMsg::Warn(), WARN_MSG, and WARN_UNDEF.

◆ ~probability_distribution()

probability_distribution::~probability_distribution ( )
353  {
354  switch (m_type)
355  {
356  case DISCRETE_DIST_T:
357  {
358  delete static_cast<DISCRETE_DIST*>(m_probDistrib);
359  return;
360  }
361  case NORMAL_DIST_T:
362  {
363  delete static_cast<NORMAL_DIST*>(m_probDistrib);
364  return;
365  }
366  case UNIREAL_DIST_T:
367  {
368  delete static_cast<UNIREAL_DIST*>(m_probDistrib);
369  return;
370  }
371  case UNIINT_DIST_T:
372  {
373  delete static_cast<UNIINT_DIST*>(m_probDistrib);
374  return;
375  }
376  case BETA_DIST_T:
377  {
378  delete static_cast<GAMMA_DIST*>(m_probDistrib1);
379  delete static_cast<GAMMA_DIST*>(m_probDistrib2);
380  return;
381  }
382  case GAMMA_DIST_T:
383  {
384  delete static_cast<GAMMA_DIST*>(m_probDistrib);
385  return;
386  }
387  case CAUCHY_DIST_T:
388  {
389  delete static_cast<CAUCHY_DIST*>(m_probDistrib);
390  return;
391  }
392  case EXPONENTIAL_DIST_T:
393  {
394  delete static_cast<EXPONENTIAL_DIST*>(m_probDistrib);
395  return;
396  }
397  default:
398  g_msg->Warn(WARN_BUG, "Unknown distribution type number: ", std::to_string(m_type));
399  }
400 };

References BETA_DIST_T, CAUCHY_DIST_T, DISCRETE_DIST_T, EXPONENTIAL_DIST_T, g_msg, GAMMA_DIST_T, m_probDistrib, m_probDistrib1, m_probDistrib2, m_type, NORMAL_DIST_T, UNIINT_DIST_T, UNIREAL_DIST_T, MapErrorMsg::Warn(), and WARN_BUG.

Member Function Documentation

◆ Get()

double probability_distribution::Get ( ) const

Returns a numerical random variate extracted from the defined probability distribution

Returns
the random number, returned as a double type
283  {
284  switch (m_type)
285  {
286  case DISCRETE_DIST_T:
287  {
288  const int x = (*static_cast<DISCRETE_DIST*>(m_probDistrib))(g_generator);
289  return x;
290  }
291  case NORMAL_DIST_T:
292  {
293  const double x = (*static_cast<NORMAL_DIST*>(m_probDistrib))(g_generator);
294  return x;
295  }
296  case UNIREAL_DIST_T:
297  {
298  const double x = (*static_cast<UNIREAL_DIST*>(m_probDistrib))(g_generator);
299  return x;
300  }
301  case UNIINT_DIST_T:
302  {
303  const int x = (*static_cast<UNIINT_DIST*>(m_probDistrib))(g_generator);
304  return x;
305  }
306  case BETA_DIST_T:
307  {
308  const double x_1 = (*static_cast<GAMMA_DIST*>(m_probDistrib1))(g_generator);
309  const double x_2 = (*static_cast<GAMMA_DIST*>(m_probDistrib2))(g_generator);
310  const double x = x_1 / (x_1 + x_2); // beta distribution
311  return x;
312  }
313  case GAMMA_DIST_T:
314  {
315  const double x = (*static_cast<GAMMA_DIST*>(m_probDistrib))(g_generator);
316  return x;
317  }
318  case CAUCHY_DIST_T:
319  {
320  const double x = (*static_cast<CAUCHY_DIST*>(m_probDistrib))(g_generator);
321  return x;
322  }
323 
324  case EXPONENTIAL_DIST_T:
325  {
326  const double x = (*static_cast<EXPONENTIAL_DIST*>(m_probDistrib))(g_generator);
327  return x;
328  }
329  default:
330  g_msg->Warn(WARN_BUG, "Unknown distribution type number: ", std::to_string(m_type));
331  return 0; // should never happen, all supported types are included here
332  }
333 }

References BETA_DIST_T, CAUCHY_DIST_T, DISCRETE_DIST_T, EXPONENTIAL_DIST_T, g_generator(), g_msg, GAMMA_DIST_T, m_probDistrib, m_probDistrib1, m_probDistrib2, m_type, NORMAL_DIST_T, UNIINT_DIST_T, UNIREAL_DIST_T, MapErrorMsg::Warn(), and WARN_BUG.

Referenced by Osmia_Female::FindNestLocation(), Osmia_Population_Manager::GetFirstCocoonProvisioningMass(), Osmia_Female::LayEgg(), Osmia_Female::PlanEggsPerNest(), and Osmia_Female::st_Dispersal().

◆ Geti()

int probability_distribution::Geti ( ) const

Returns an integer random variate extracted from the defined probability distribution Maybe marginally faster than get(), as it has less types to check

Returns
the random number, returned as an integer; returns zero if the underlying prob is a continuous and not a discrete one
335  {
336  switch (m_type)
337  {
338  case DISCRETE_DIST_T:
339  {
340  const int x = (*static_cast<DISCRETE_DIST*>(m_probDistrib))(g_generator);
341  return x;
342  }
343  case UNIINT_DIST_T:
344  {
345  const int x = (*static_cast<UNIINT_DIST*>(m_probDistrib))(g_generator);
346  return x;
347  }
348  default:
349  return 0; // can happen, not all types are integer
350  }
351 }

References DISCRETE_DIST_T, g_generator(), m_probDistrib, m_type, and UNIINT_DIST_T.

Referenced by Osmia_InCocoon::st_Develop().

Member Data Documentation

◆ BETA_DIST_S

const std::string probability_distribution::BETA_DIST_S { "BETA" }
private

BETA, the continuous/real-valued beta distribution. BETA distribution is implemented using two gamma distributions since it is not included in the standard library.

Parametres are alpha and beta. The outcomes belong to [0,1]

Referenced by probability_distribution().

◆ BETA_DIST_T

const int probability_distribution::BETA_DIST_T = 5
staticprivate

◆ BETABINOMIAL_DIST_S

const std::string probability_distribution::BETABINOMIAL_DIST_S { "BETABINOMIAL" }
private

BETABINOMIAL, a beta-binomial discrete distribution with outcomes {0,1,2,..,N}

It needs 4 parameters: N (int) - max value of the outcome alpha, beta (real) - the beta binomial parameters, determine the shape of probabilities over the outcome space no_zero (numerical boolean) - extra parameter to cancel out the probability for the zero value

Referenced by probability_distribution().

◆ CAUCHY_DIST_S

const std::string probability_distribution::CAUCHY_DIST_S { "CAUCHY" }
private

CAUCHY, the continuous/real-valued gamma distribution

Parametres are alpha and beta. The outcomes is any real number.

Referenced by probability_distribution().

◆ CAUCHY_DIST_T

const int probability_distribution::CAUCHY_DIST_T = 7
staticprivate

◆ DISCRETE_DIST_S

const std::string probability_distribution::DISCRETE_DIST_S { "DISCRETE" }
private

DISCRETE, custom build distribution with outcomes {0,1,2,..}

Needs as parameters a (not necesarilly normalized) set of weights (real numbers). The weights correspond to the outcomes {0,1,2,...} Therefore the number of weights give the number of outcomes, where zero is included

Referenced by probability_distribution().

◆ DISCRETE_DIST_T

const int probability_distribution::DISCRETE_DIST_T = 1
staticprivate

◆ EXPONENTIAL_DIST_S

const std::string probability_distribution::EXPONENTIAL_DIST_S { "EXPONENTIAL" }
private

EXPONENTIAL, the continuous/real-valued exponential distribution

Parametres are lamda. The outcomes is any real number.

Referenced by probability_distribution().

◆ EXPONENTIAL_DIST_T

const int probability_distribution::EXPONENTIAL_DIST_T = 8
staticprivate

◆ GAMMA_DIST_S

const std::string probability_distribution::GAMMA_DIST_S { "GAMMA" }
private

GAMMA, the continuous/real-valued cauchy distribution

Parametres are location and scale. The outcomes belong to [0,max]

Referenced by probability_distribution().

◆ GAMMA_DIST_T

const int probability_distribution::GAMMA_DIST_T = 6
staticprivate

◆ m_probDistrib

void* probability_distribution::m_probDistrib
private

◆ m_probDistrib1

void* probability_distribution::m_probDistrib1
private

◆ m_probDistrib2

void* probability_distribution::m_probDistrib2
private

◆ m_type

int probability_distribution::m_type
private

◆ NORMAL_DIST_S

const std::string probability_distribution::NORMAL_DIST_S { "NORMAL" }
private

NORMAL, a continuous normal distribution

It needs two parameters: the mean (real number) and the standard deviation (real number, >=0)

Referenced by probability_distribution().

◆ NORMAL_DIST_T

const int probability_distribution::NORMAL_DIST_T = 2
staticprivate

◆ UNIINT_DIST_S

const std::string probability_distribution::UNIINT_DIST_S { "UNIINT" }
private

UNIINT, a discrete uniform distribution

Parametres are min and max, both integer values. The outcomes belong to {min, min+1, ... , max}

Referenced by probability_distribution().

◆ UNIINT_DIST_T

const int probability_distribution::UNIINT_DIST_T = 4
staticprivate

◆ UNIREAL_DIST_S

const std::string probability_distribution::UNIREAL_DIST_S { "UNIREAL" }
private

UNIREAL, a continuous uniform distribution

Parametres are min and max, both real values. The outcomes belong to [min,max)

Referenced by probability_distribution().

◆ UNIREAL_DIST_T

const int probability_distribution::UNIREAL_DIST_T = 3
staticprivate

The documentation for this class was generated from the following files:
probability_distribution::UNIINT_DIST_T
static const int UNIINT_DIST_T
Definition: ALMaSS_Random.h:149
WARN_MSG
Definition: MapErrorMsg.h:38
probability_distribution::m_probDistrib1
void * m_probDistrib1
Definition: ALMaSS_Random.h:156
g_beta_binomial_probabilities_fnc
std::vector< double > g_beta_binomial_probabilities_fnc(const int a_n, const double a_alpha, const double a_beta, const bool a_noZero)
Local utility function for generating the weights-vector for the beta binomial distribution from inpu...
Definition: ALMaSS_Random.cpp:102
probability_distribution::m_type
int m_type
Definition: ALMaSS_Random.h:158
UNIINT_DIST
std::uniform_int_distribution< int > UNIINT_DIST
Definition: ALMaSS_Random.cpp:118
probability_distribution::GAMMA_DIST_T
static const int GAMMA_DIST_T
Definition: ALMaSS_Random.h:151
probability_distribution::BETABINOMIAL_DIST_S
const std::string BETABINOMIAL_DIST_S
Definition: ALMaSS_Random.h:91
probability_distribution::NORMAL_DIST_T
static const int NORMAL_DIST_T
Definition: ALMaSS_Random.h:147
probability_distribution::EXPONENTIAL_DIST_S
const std::string EXPONENTIAL_DIST_S
Definition: ALMaSS_Random.h:144
probability_distribution::BETA_DIST_T
static const int BETA_DIST_T
Definition: ALMaSS_Random.h:150
probability_distribution::m_probDistrib2
void * m_probDistrib2
Definition: ALMaSS_Random.h:157
probability_distribution::CAUCHY_DIST_S
const std::string CAUCHY_DIST_S
Definition: ALMaSS_Random.h:136
probability_distribution::UNIREAL_DIST_T
static const int UNIREAL_DIST_T
Definition: ALMaSS_Random.h:148
WARN_UNDEF
Definition: MapErrorMsg.h:36
probability_distribution::DISCRETE_DIST_S
const std::string DISCRETE_DIST_S
Definition: ALMaSS_Random.h:81
probability_distribution::m_probDistrib
void * m_probDistrib
Definition: ALMaSS_Random.h:155
probability_distribution::UNIREAL_DIST_S
const std::string UNIREAL_DIST_S
Definition: ALMaSS_Random.h:105
MapErrorMsg::Warn
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: MapErrorMsg.cpp:69
NORMAL_DIST
std::normal_distribution< double > NORMAL_DIST
Definition: ALMaSS_Random.cpp:119
GAMMA_DIST
std::gamma_distribution< double > GAMMA_DIST
Definition: ALMaSS_Random.cpp:122
DISCRETE_DIST
std::discrete_distribution<> DISCRETE_DIST
Definition: ALMaSS_Random.cpp:117
probability_distribution::GAMMA_DIST_S
const std::string GAMMA_DIST_S
Definition: ALMaSS_Random.h:128
probability_distribution::CAUCHY_DIST_T
static const int CAUCHY_DIST_T
Definition: ALMaSS_Random.h:152
g_generator
std::mt19937 g_generator(seed_seq)
g_msg
MapErrorMsg * g_msg
Definition: MapErrorMsg.cpp:41
probability_distribution::UNIINT_DIST_S
const std::string UNIINT_DIST_S
Definition: ALMaSS_Random.h:112
probability_distribution::NORMAL_DIST_S
const std::string NORMAL_DIST_S
Definition: ALMaSS_Random.h:98
EXPONENTIAL_DIST
std::exponential_distribution< double > EXPONENTIAL_DIST
Definition: ALMaSS_Random.cpp:124
UNIREAL_DIST
std::uniform_real_distribution< double > UNIREAL_DIST
Definition: ALMaSS_Random.cpp:120
probability_distribution::BETA_DIST_S
const std::string BETA_DIST_S
Definition: ALMaSS_Random.h:120
CAUCHY_DIST
std::cauchy_distribution< double > CAUCHY_DIST
Definition: ALMaSS_Random.cpp:123
probability_distribution::DISCRETE_DIST_T
static const int DISCRETE_DIST_T
Definition: ALMaSS_Random.h:146
probability_distribution::EXPONENTIAL_DIST_T
static const int EXPONENTIAL_DIST_T
Definition: ALMaSS_Random.h:153
WARN_BUG
Definition: MapErrorMsg.h:34