-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistribution.cpp
More file actions
307 lines (276 loc) · 7.56 KB
/
Distribution.cpp
File metadata and controls
307 lines (276 loc) · 7.56 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
*
* Distribution.cpp
* ---------------------
*
* Author: Michael Dickens <mdickens93@gmail.com>
* Created: 2016-05-18
*
* Functionality for the Distribution class.
*
*/
#include "QuantitativeModel.h"
using namespace std;
function<double(double)> lomax_pdf(double x_m, double alpha)
{
return [x_m, alpha](double x){
return alpha / x_m / pow(1 + x / x_m, alpha + 1);
};
}
function<double(double)> lognorm_pdf(double p_m, double p_s)
{
double mu = log(p_m);
double s = log(10) * p_s;
return [mu, s](double x){
return 1 / (x * s * sqrt(2 * M_PI)) *
exp(-0.5 * pow((log(x) - mu) / s, 2));
};
}
/*
* Returns the bucket containing the given x value.
*/
int bucket_index(double x)
{
// return (int) (log(x) / log(STEP) - 0.5) + EXP_OFFSET; // I believe this is more mathematically accurate but Excel does in the other way
return (int) (log(x) / log(STEP)) + EXP_OFFSET;
}
/*
* Gives probability density in the (geometric) middle of the
* bucket. Sort-of the opposite of `bucket_index`.
*/
double bucket_prob(int index)
{
return pow(STEP, index - EXP_OFFSET + 0.5);
}
/*
* Gives probability density at the bottom of the bucket.
*/
double bucket_min_prob(int index)
{
return pow(STEP, index - EXP_OFFSET);
}
/*
* Gets the difference in x-values from the beginning to the end of a bucket.
*/
double get_delta(int index)
{
return pow(STEP, index - EXP_OFFSET + 1) - pow(STEP, index - EXP_OFFSET);
}
/*
* Combine an array of distributions using some binary operator (folds
* left).
*/
// Distribution *foldl_dists(
// function<Distribution *(const Distribution *, const Distribution *)> f,
// const Distribution *dists[], int length)
// {
// Distribution *curr = NULL;
// Distribution *next = NULL;
// for (int i = 1; i < length; i++) {
// if (i == 1) {
// next = f(dists[0], dists[i]);
// } else {
// next = f(curr, dists[i]);
// delete curr;
// }
// curr = next;
// }
// return curr;
// }
// Distribution *sum_dists(const Distribution *dists[], int length)
// {
// return foldl_dists([](const Distribution *x, const Distribution *y) {
// return *x + y;
// }, dists, length);
// }
// Distribution *product_dists(const Distribution *dists[], int length)
// {
// return foldl_dists([](const Distribution *x, const Distribution *y) {
// return *x * y;
// }, dists, length);
// }
Distribution::Distribution()
{
type = Type::empty;
}
Distribution::Distribution(Type type) : buckets(NUM_BUCKETS, 0)
{
this->type = type;
}
/*
* Takes p_m as exp(mu) and p_s and base-10 standard deviation.
*/
Distribution::Distribution(double p_m, double p_s)
{
this->type = Type::lognorm;
this->p_m = p_m;
this->p_s = p_s;
this->pdf = lognorm_pdf(p_m, p_s);
}
/*
* Constructs buckets given a probability density function (PDF).
*/
Distribution::Distribution(function<double(double)> pdf) :
Distribution(Type::buckets)
{
for (int i = 0; i < NUM_BUCKETS; i++) {
buckets[i] = pdf(bucket_prob(i));
}
}
double Distribution::operator[](int index) const
{
return get(index);
}
double Distribution::get(int index) const
{
if (type == Type::empty) {
throw "Cannot use empty Distribution. Did you try to extract a non-existent value?";
} else if (type == Type::lognorm) {
return pdf(bucket_prob(index));
} else {
return buckets[index];
}
}
void Distribution::check_empty() const
{
if (type == Type::empty) {
throw "Cannot use empty Distribution. Did you try to extract a non-existent value?";
}
}
/*
* Calculates the sum of two probability distributions.
*/
Distribution Distribution::operator+(const Distribution& other) const
{
check_empty();
Distribution res;
for (int i = 0; i < NUM_BUCKETS; i++) {
for (int j = 0; j < NUM_BUCKETS; j++) {
int index = bucket_index(bucket_prob(i) + bucket_prob(j));
double mass = get(i) * get_delta(i) * other.get(j) * get_delta(j);
if (index >= NUM_BUCKETS) {
index = NUM_BUCKETS - 1;
}
res.buckets[index] += mass / get_delta(index);
}
}
return res;
}
/*
* Calculates the product of two probability distributions.
*/
Distribution Distribution::operator*(const Distribution& other) const
{
check_empty();
if (type == Type::lognorm
&& other.type == Type::lognorm) {
double new_p_m = p_m * other.p_m;
double new_p_s = sqrt(pow(p_s, 2) + pow(other.p_s, 2));
Distribution res(new_p_m, new_p_s);
return res;
}
Distribution res(Type::buckets);
for (int i = 0; i < NUM_BUCKETS; i++) {
for (int j = 0; j < NUM_BUCKETS; j++) {
int index = bucket_index(bucket_prob(i) * bucket_prob(j));
double mass = get(i) * get_delta(i) * other.get(j) * get_delta(j);
if (index >= NUM_BUCKETS) {
index = NUM_BUCKETS - 1;
} else if (index < 0) {
index = 0;
}
res.buckets[index] += mass / get_delta(index);
}
}
return res;
}
/*
* TODO: test this
*/
Distribution Distribution::operator*(double scalar) const
{
check_empty();
if (this->type == Type::lognorm) {
Distribution res(p_m * scalar, p_s);
return res;
} else {
// TODO: idk
Distribution res;
return res;
}
}
Distribution Distribution::reciprocal() const
{
check_empty();
if (this->type == Type::lognorm) {
Distribution res(1 / p_m, p_s);
return res;
} else {
// TODO: do something idk
Distribution res;
return res;
}
}
double Distribution::mean() const
{
check_empty();
// TODO: implement for lognorm
double mu = 0;
for (int i = 0; i < NUM_BUCKETS; i++) {
mu += bucket_prob(i) * get(i) * get_delta(i);
}
return mu;
}
double Distribution::variance(double mean1) const
{
check_empty();
// TODO: implement for lognorm
double sigma2 = 0;
for (int i = 0; i < NUM_BUCKETS; i++) {
sigma2 += pow(bucket_prob(i), 2) * get(i) * get_delta(i);
}
return sigma2 - pow(mean1, 2);
}
double Distribution::integrand(const Distribution& measurement, int index, bool ev) const
{
check_empty();
double u = bucket_min_prob(index);
double prior = this->get(index);
double update = 0;
if (measurement.type == Type::buckets) {
/* approximate `measurement` with log-normal dist */
// TODO: just do this directly numerically
double mean1 = measurement.mean();
double var = measurement.variance(mean1);
double mu = log(mean1 / sqrt(1 + var / pow(mean1, 2)));
double sigma = sqrt(log(1 + var / pow(mean1, 2)));
update = lognorm_pdf(u, sigma / log(10))(exp(mu));
} else {
update = lognorm_pdf(u, measurement.p_s)(measurement.p_m);
}
double res = prior * update;
if (ev) {
res *= u;
}
return res;
}
double Distribution::integral(const Distribution& measurement, bool ev) const
{
check_empty();
double total = 0;
double x_lo = pow(STEP, -EXP_OFFSET);
double x_hi = x_lo * STEP;
double y_lo = integrand(measurement, 0, ev);
double y_hi;
double avg, delta;
for (int i = 1; i <= NUM_BUCKETS; i++) {
y_hi = integrand(measurement, i, ev);
avg = (y_lo + y_hi) / 2;
delta = x_hi - x_lo;
total += avg * delta;
x_lo = x_hi;
x_hi = x_hi * STEP;
y_lo = y_hi;
}
return total;
}