-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmpiricalDistribution.C
180 lines (148 loc) · 4.25 KB
/
EmpiricalDistribution.C
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
/**************************************************************
EmpiricalDistribution.C
Copyright (C)2013 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***************************************************************/
#include "EmpiricalDistribution.H"
#include "BOOM/Random.H"
#include "BOOM/Constants.H"
#include <math.h>
#include <iostream>
#include <fstream>
using namespace BOOM;
EmpiricalDistribution::EmpiricalDistribution(const BOOM::String &filename,
bool shouldInterpolate)
: useInterpolation(shouldInterpolate)
{
// ctor
load(filename);
normalize();
smallestElemLogP=log(v[0]->second);
largestElemLogP=log(v[v.size()-1]->second);
smallestValue=NEGATIVE_INFINITY;
for(Vector<EmpiricalDistributionElement*>::iterator cur=v.begin(),
end=v.end() ; cur!=end ; ++cur) {
EmpiricalDistributionElement *elem=*cur;
const double &value=elem->second;
if(value==0.0) continue;
if(!isFinite(smallestValue) || value<smallestValue) smallestValue=value;
}
}
EmpiricalDistribution::~EmpiricalDistribution()
{
BOOM::Vector<EmpiricalDistributionElement*>::iterator cur=v.begin(),
end=v.end();
for(; cur!=end ; ++cur) delete *cur;
}
void EmpiricalDistribution::normalize()
{
// Sum the values (even those not explicitly represented)
int n=v.size(), halfBinSize=binSize/2;
double sum=0;
for(int i=0 ; i<n ; ++i)
{
EmpiricalDistributionElement &elem=*v[i];
int x=elem.first+halfBinSize;
double y=elem.second;
sum+=y*binSize; // *binSize is necessary so all probabilities are
// comparable, even across different histograms and
// feature types (i.e., coding & noncoding)
}
// Divide by the sum
for(int i=0 ; i<n ; ++i) v[i]->second/=sum;
}
unsigned EmpiricalDistribution::binarySearch(unsigned elem)
{
unsigned begin=0, end=v.size();
while(begin<end)
{
unsigned mid=unsigned((begin+end)/2);
unsigned midElem=v[mid]->first;
if(elem>midElem) begin=mid+1;
else end=mid;
}
return begin;
}
void EmpiricalDistribution::load(const BOOM::String &filename)
{
ifstream is(filename.c_str());
if(!is.good()) throw BOOM::String("Error opening file ")+filename+
" in EmpiricalDistribution::load()";
while(!is.eof())
{
unsigned x;
double y;
is >> x;
if(is.eof()) break;
is >> y;
v.push_back(new EmpiricalDistributionElement(x,y));
}
binSize=v[1]->first-v[0]->first;
}
double EmpiricalDistribution::getLogP(unsigned x)
{
// Perform binary search to find nearest neighbor
unsigned index=binarySearch(x);
if(index<0) return smallestElemLogP;
if(index>=v.size()) return largestElemLogP;
EmpiricalDistributionElement &elem=*v[index];
unsigned foundX=elem.first, x1, x2;
double foundY=elem.second, y1, y2;
if(x==foundX) {
if(foundY==0.0) return log(smallestValue/2);
return log(foundY); // ### this log could be cached
}
if(useInterpolation)
{
// Perform linear interpolation between two surrounding neighbors
if(x<foundX)
{
if(index==0) return log(interpolate(0,0,foundX,foundY,x));
EmpiricalDistributionElement &prevElem=*v[index-1];
x1=prevElem.first;
y1=prevElem.second;
x2=foundX;
y2=foundY;
}
else // x>foundX
{
if(index==v.size()-1) return largestElemLogP;
EmpiricalDistributionElement &nextElem=*v[index+1];
x1=foundX;
y1=foundY;
x2=nextElem.first;
y2=nextElem.second;
}
double y=interpolate(x1,y1,x2,y2,x);
if(y==0.0) return log(smallestValue/2);
return log(y);
}
else {
if(foundY==0.0) return log(smallestValue/2);
return log(foundY);
}
}
double EmpiricalDistribution::interpolate(unsigned x1,double y1,unsigned x2,
double y2,unsigned x)
{
return (y2-y1)/double(x2-x1)*(x-x1)+y1;
}
void EmpiricalDistribution::useLogLikelihoodRatios()
{
int n=v.size();
double mean=0;
double debug=0;
for(int i=0 ; i<n ; ++i)
{
int x=v[i]->first;
double y=v[i]->second;
debug+=y*binSize;
mean+=(x+binSize/2)*y*binSize;
}
double Pmean=exp(getLogP(int(mean)));
for(int i=0 ; i<n ; ++i)
{
v[i]->second/=Pmean;
}
}