-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTDistribution.cpp
executable file
·422 lines (360 loc) · 8.9 KB
/
TDistribution.cpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* TDistribution.cpp
*
* Created on: May 4, 2012
* Author: nhatuan
*/
#include "TDistribution.h"
#include <cstring>
/**
* constructor
*/
TDistribution::TDistribution() {
dof = 0;
determinant = 0.0;
aveCDistance = 0.0;
//cor = new float*[2];
cov = new float*[2];
inv = new float*[2];
locParam = new float[2];
for(int i=0; i<2; i++) {
//cor[i] = new float[2];
cov[i] = new float[2];
inv[i] = new float[2];
locParam[i] = 0;
for(int j = 0; j<2; j++) {
//cor[i][j] = 0;
cov[i][j] = 0;
inv[i][j] = 0;
}
}
}
/**
* copy constructor
*/
TDistribution::TDistribution(const TDistribution &t) {
//printf("COPY CONSTRUCTOR IS CALLED\n");
dof = t.dof;
determinant = t.determinant;
aveCDistance = t.aveCDistance;
//cor = new float*[2];
cov = new float*[2];
inv = new float*[2];
locParam = new float[2];
for(int i=0; i<2; i++) {
//cor[i] = new float[2];
cov[i] = new float[2];
inv[i] = new float[2];
locParam[i] = t.locParam[i];
for(int j = 0; j<2; j++) {
//cor[i][j] = t.cor[i][j];
cov[i][j] = t.cov[i][j];
inv[i][j] = t.inv[i][j];
}
}
//memcpy(locParam, t.locParam, sizeof(t.locParam));
//memcpy(cor, t.cor, sizeof(t.cor));
//memcpy(cov, t.cov, sizeof(t.cov));
//memcpy(inv, t.inv, sizeof(t.inv));
samples.clear();
for(unsigned int i=0; i<t.samples.size(); i++) {
Sample tmp = t.samples[i];
addNewSample(tmp);
}
}
/**
* destructor
*/
TDistribution::~TDistribution() {
if (*cov != NULL && *inv != NULL && locParam != NULL) {
for(int i=0; i<2; i++) {
//delete[] cor[i];
delete[] cov[i];
delete[] inv[i];
}
//delete [] cor;
delete [] cov;
delete [] inv;
delete [] locParam;
}
}
/**
* set degree of freedom for distribution
*/
void TDistribution::setDegreeOfFreedom(float d) {
dof = d;
}
/**
* add new sample to distribution
*/
void TDistribution::addNewSample(Sample sample) {
samples.push_back(sample);
}
/**
* remove a sample from distribution
*/
void TDistribution::removeSampleWithName(string name) {
for(unsigned int i=0; i<samples.size(); i++) {
if(samples[i].getName() == name) {
samples.erase(samples.begin() + i);
break;
}
}
}
/**
* calculate the parameters of distribution
*/
void TDistribution::calculateParams() {
try {
float *x = new float[samples.size()];
float *y = new float[samples.size()];
float xTotal = 0, yTotal = 0;
if (samples.empty()) {
determinant = 0;
for(int i=0; i<2; i++) {
locParam[i] = 0;
for(int j=0; j<2; j++) {
cov[i][j] = 0;
//cor[i][j] = 0;
inv[i][j] = 0;
}
}
return;
}
for(unsigned int i=0; i<samples.size(); i++) {
x[i] = samples[i].getContrast();
y[i] = samples[i].getStrength();
xTotal += x[i];
yTotal += y[i];
}
locParam[0] = xTotal / samples.size();
locParam[1] = yTotal / samples.size();
updateAveCDistance();
if (samples.size() < 4) throw "too less samples";
cov = calculateCov(x, y, locParam[0], locParam[1], samples.size());
//cor = calculateCor(cov);
//cor = cov;
//inv = calculateInv(cor);
inv = calculateInv(cov);
determinant = calculateDet(inv);
//printf("loc: %f, %f\n", locParam[0], locParam[1]);
//printf("cov: %f %f %f %f\n", cov[0][0], cov[0][1], cov[1][0], cov[1][1]);
//printf("cor: %f %f %f %f\n", cor[0][0], cor[0][1], cor[1][0], cor[1][1]);
//printf("inv: %f %f %f %f\n", inv[0][0], inv[0][1], inv[1][0], inv[1][1]);
delete [] x;
delete [] y;
} catch(...) {
cov[0][0] = 0.05;
cov[0][1] = cov[1][0] = 0.00;
cov[1][1] = 0.17;
//cor = calculateCor(cov);
//inv = calculateInv(cor);
inv = calculateInv(cov);
determinant = calculateDet(inv);
}
}
/**
* calculate the density of X = (x,y)^T
*
* The formula is:
* f(X) = (|Sigma^-1| / (2 * PI)) * (1 + ((X - mu)^T * Sigma^(-1) * (X - mu))/dof)^(-(dof + 2)/2)
*
* where Sigma is the inverse matrix of correlation matrix
* PI = 3.14159265
* X = (x,y)^T
* mu is the location parameter
* dof is the degree of freedom
* for bivariate student distribution, p = 2
*/
float TDistribution::calculateProb(float x, float y) {
try {
// A = |\Sigma^-1| / (2 * PI)
float returnVal = sqrt(determinant) / (2 * PI);
// B = (x - \mu)^T * \Sigma^(-1) * (x - \mu)
float t1 = x - locParam[0];
float t2 = y - locParam[1];
float inVal = 0;
inVal += inv[0][0] * t1 * t1;
inVal += inv[1][1] * t2 * t2;
inVal += inv[1][0] * t1 * t2;
inVal += inv[0][1] * t1 * t2;
// C = (1 + B / dof)^(-(dof + 2)/2)
float newVal = 1 + (inVal / dof);
inVal = pow(newVal, ((- dof - 2) / 2.0));
// f(X) = A * C
returnVal *= inVal;
return returnVal;
} catch (...) {
return 0;
}
}
/**
* calculate the density for list of point
*/
float * TDistribution::calculateProbArray(float *x, float *y) {
float * returnArray = new float[length(x)];
for(unsigned int i=0; i<length(x); i++) {
returnArray[i] = calculateProb(x[i], y[i]);
}
return returnArray;
}
/**
* for debug, convert distribution's parameters to string
*/
string TDistribution::toString() {
char output[1000];
sprintf(output, "numberOfSamples: %d, degreeOfFreedom: %.2f, locationParams: (%.2f, %.2f), determinant: %.3f, average Distance: %.3f",
samples.size(), dof, locParam[0], locParam[1], determinant, aveCDistance);
return output;
}
/**
* write sample data of distribution to file
*/
void TDistribution::toFile(string fileName) {
ofstream of;
of.open(fileName.c_str(), ios::out);
for(unsigned int i=0; i<samples.size(); i++) {
of << samples[i].getContrast() << " " << samples[i].getStrength() << " " << calculateProb(samples[i].getContrast(), samples[i].getStrength())<< "\n";
}
of << locParam[0] << " " << locParam[1] << " " << calculateProb(locParam[0], locParam[1]) << "\n";
of.close();
}
/**
* calculate the weights for each sample
*
* wi = (dof + p) / (dof + ((X - mu)^T * Sigma^(-1) * (X - mu)))
*
* where Sigma is the inverse matrix of correlation matrix
* X = (x,y)^T
* mu is the location parameter
* dof is the degree of freedom
* for bivariate student distribution, p = 2
*/
vector<float> TDistribution::calculateWeights() {
vector<float> returnVec;
for(unsigned int i=0; i<samples.size(); i++) {
int x = samples[i].getContrast();
int y = samples[i].getStrength();
int t1 = x - locParam[0];
int t2 = y - locParam[1];
float inVal = 0;
inVal += inv[0][0] * t1 * t1;
inVal += inv[1][1] * t2 * t2;
inVal += inv[1][0] * t1 * t2;
inVal += inv[0][1] * t1 * t2;
float wi = (dof + 2) / (dof + inVal);
returnVec.push_back(wi);
}
return returnVec;
}
/**
* update parameters with weights for each sample
* if there is an error when update params, keep them as the original
*/
void TDistribution::updateParams() {
// try to update params
try {
float *x = new float[samples.size()];
float *y = new float[samples.size()];
if (samples.size() < 4) throw "too less samples";
vector<float> weights = calculateWeights();
for(unsigned int i=0; i<samples.size(); i++) {
x[i] = samples[i].getContrast();
y[i] = samples[i].getStrength();
}
locParam = updateLocParam(x, y, weights);
updateAveCDistance();
cov = updateCov(x, y, locParam[0], locParam[1], weights);
if(cov[0][0] == 0 || cov[1][1] == 0) throw "can't calculate correlation";
//cor = calculateCor(cov);
//cor = cov;
//inv = calculateInv(cor);
inv = calculateInv(cov);
determinant = calculateDet(inv);
delete [] x;
delete [] y;
}
catch (...) {
cov[0][0] = 0.05;
cov[0][1] = cov[1][0] = 0.00;
cov[1][1] = 0.17;
//cor = calculateCor(cov);
//inv = calculateInv(cor);
inv = calculateInv(cov);
determinant = calculateDet(inv);
}
}
/**
* get number of samples in distribution
*/
int TDistribution::getNumberOfSamples() {
return samples.size();
}
/**
* get the determinant
*/
float TDistribution::getDeterminant() {
return determinant;
}
/**
* get the location parameter
*/
float * TDistribution::getLocParam() {
return locParam;
}
/**
* get the covariance matrix
*/
float ** TDistribution::getCov() {
return cov;
}
/**
* get the correlation matrix
*/
//float ** TDistribution::getCor() {
// return cor;
//}
/**
* get the inverse matrix of correlation matrix
*/
float ** TDistribution::getInv() {
return inv;
}
/**
* get degree of freedom
*/
int TDistribution::getDOF() {
return dof;
}
/**
* get list of samples
*/
vector<Sample> TDistribution::getSamples() {
return samples;
}
/**
* compare two distributions
*/
bool TDistribution::isEqual(TDistribution t) {
if(getNumberOfSamples() != t.getNumberOfSamples()) return false;
if(getDeterminant() != t.getDeterminant()) return false;
float * tLoc = t.getLocParam();
float ** tCov = t.getCov();
for(int i=0; i<2; i++) {
if(locParam[i] != tLoc[i]) return false;
for(int j=0; j<2; j++) {
if(cov[i][j] != tCov[i][j]) return false;
}
}
return true;
}
void TDistribution::removeAll() {
samples.clear();
}
void TDistribution::updateAveCDistance() {
float c = locParam[0];
for(unsigned int i=0; i<samples.size(); i++) {
aveCDistance += mabs(c - samples[i].getContrast());
}
aveCDistance /= samples.size();
}