-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathInverseCdf.cpp
More file actions
77 lines (64 loc) · 1.69 KB
/
InverseCdf.cpp
File metadata and controls
77 lines (64 loc) · 1.69 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "InverseCdf.h"
#include <cmath>
#include "Rand.h"
/**
* Function: set_neg_log
*
* Purpose: set defaults if not present in parameter file
* @param start_value - the value to use for cdf_values_[CDF_RES]
* @return void
*/
void InverseCdf::set_neg_log(double start_value)
{
cdf_values_[CDF_RES] = start_value;
for (int i = 0; i < CDF_RES; i++)
cdf_values_[i] = -log(1 - ((double)i) / CDF_RES);
}
/**
* Function: assign_exponent
*
* Purpose: exponentiate each quantile
* @return void
*/
void InverseCdf::assign_exponent()
{
for (int quantile = 0; quantile <= CDF_RES; quantile++)
{
cdf_values_[quantile] = exp(-cdf_values_[quantile]);
}
}
/**
* Function: assign_exponent
*
* Purpose: exponentiate each quantile
* @param value - value of the exponent
* @return void
*/
void InverseCdf::assign_exponent(double value)
{
for (int i = 0; i <= CDF_RES; i++)
{
cdf_values_[i] = exp(value);
}
}
/**
* Function: choose
*
* Purpose: choose a value from the InverseCdf data
* @param Mean
* @param tn - thread number
* @param timesteps_per_day
* @return value chosen from InverseCdf data
*/
unsigned short int InverseCdf::choose(double Mean, int tn, double timesteps_per_day)
{
unsigned short int Value;
int i;
double q, ti;
i = (int)floor(q = ranf_mt(tn) * CDF_RES); //// note q defined here as well as i.
q -= ((double)i); //// remainder
//// weighted average (sort of) between quartile values from CDF_RES. logged as it was previously exponentiated in ReadParams. Minus as exp(-cdf) was done in ReadParaams. Sort of
ti = -Mean * log(q * cdf_values_[i + 1] + (1.0 - q) * cdf_values_[i]);
Value = (unsigned short int) floor(0.5 + (ti * timesteps_per_day));
return Value;
}