-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLikelihood.h
More file actions
217 lines (184 loc) · 5.16 KB
/
Likelihood.h
File metadata and controls
217 lines (184 loc) · 5.16 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
// Implementations of the likelihoods defined in arXiv:1901.04645
#include <cmath>
#include <vector>
#include <numeric>
#include <sstream>
#include <algorithm>
namespace MCLLH {
namespace detail {
// compute the sum using the Kahan summation algorithm
template <class InIt>
typename std::iterator_traits<InIt>::value_type accumulate(InIt begin, InIt end) {
typedef typename std::iterator_traits<InIt>::value_type real;
real sum = real(0);
real running_error = real(0);
real temp;
real difference;
for (; begin != end; ++begin) {
difference = *begin;
difference -= running_error;
temp = sum;
temp += difference;
running_error = temp;
running_error -= sum;
running_error -= difference;
sum = std::move(temp);
}
return sum;
};
// compute log(1+x) without losing precision for small values of x
template<typename T>
T LogOnePlusX(T x)
{
if (x <= -1.0)
{
std::stringstream os;
os << "Invalid input argument (" << x
<< "); must be greater than -1.0";
throw std::invalid_argument( os.str() );
}
if (fabs(x) > 1e-4)
{
// x is large enough that the obvious evaluation is OK
return log(1.0 + x);
}
// Use Taylor approx. log(1 + x) = x - x^2/2 with error roughly x^3/3
// Since |x| < 10^-4, |x|^3 < 10^-12, relative error less than 10^-8
T x2 = x*x;
T x3 = x2*x;
T x4 = x3*x;
return x-x2/2.0+x3/3.0-x4/4.0;
};
template<typename DataType>
DataType get_mu(std::vector<DataType> const & wi) {
return accumulate(wi.begin(), wi.end());
};
template<typename DataType>
DataType get_sigma2(std::vector<DataType> const & wi) {
std::vector<double> w2i(wi.size());
std::transform(wi.begin(), wi.end(), w2i.begin(), [](double w)->double{return w*w;});
return accumulate(w2i.begin(), w2i.end());
};
} // namespace detail
struct poissonLikelihood{
template <typename T>
T operator()(double dataCount, T const & lambda, T const & w2_sum) const{
if(lambda==0)
return(dataCount==0?0:-std::numeric_limits<T>::infinity());
T sum(lambda);
sum+=lgamma(dataCount+1);
return(dataCount*log(lambda)-sum);
}
};
struct gammaPriorPoissonLikelihood {
template<typename T>
T operator()(double k, T const & alpha, T const & beta) {
std::vector<T> items(5);
items[0] = alpha*log(beta);
items[1] = lgamma(k+alpha);
items[2] = -lgamma(k+1);
items[3] = -(k+alpha)*detail::LogOnePlusX(beta);
items[4] = -lgamma(alpha);
return detail::accumulate(items.begin(), items.end());
}
};
struct LMean {
template<typename T>
T operator()(double k, T const & w_sum, T const & w2_sum) const {
if(w_sum <= 0 || w2_sum < 0) {
return(k==0?0:-std::numeric_limits<T>::infinity());
}
if(w2_sum == 0) {
return poissonLikelihood()(k, w_sum, w2_sum);
}
T zero(0);
if(w_sum == zero) {
if(k == 0) {
return zero;
}
else {
return T(-std::numeric_limits<double>::infinity());
}
}
T alpha = w_sum*w_sum/w2_sum;
T beta = w_sum/w2_sum;
T L = gammaPriorPoissonLikelihood()(k, alpha, beta);
return L;
}
};
struct LMode {
template<typename T>
T operator()(double k, T const & w_sum, T const & w2_sum) const {
if(w_sum <= 0 || w2_sum < 0) {
return(k==0?0:-std::numeric_limits<T>::infinity());
}
if(w2_sum == 0) {
return poissonLikelihood()(k, w_sum, w2_sum);
}
T zero(0);
if(w_sum == zero) {
if(k == 0) {
return zero;
}
else {
return T(-std::numeric_limits<double>::infinity());
}
}
const T & mu = w_sum;
T mu2 = mu*mu;
const T & sigma2 = w2_sum;
T beta = (mu + sqrt(mu2+sigma2*4.0))/(sigma2*2);
T alpha = (mu*sqrt(mu2+sigma2*4.0)/sigma2 + mu2/sigma2 + 2.0) / 2.0;
T L = gammaPriorPoissonLikelihood()(k, alpha, beta);
return L;
}
};
struct LEff {
template<typename T>
T operator()(double k, T const & w_sum, T const & w2_sum) const {
if(w_sum <= 0 || w2_sum < 0) {
return(k==0?0:-std::numeric_limits<T>::infinity());
}
if(w2_sum == 0) {
return poissonLikelihood()(k, w_sum, w2_sum);
}
T zero(0);
if(w_sum == zero) {
if(k == 0) {
return zero;
}
else {
return T(-std::numeric_limits<double>::infinity());
}
}
T alpha = w_sum*w_sum/w2_sum + 1.0;
T beta = w_sum/w2_sum;
T L = gammaPriorPoissonLikelihood()(k, alpha, beta);
return L;
}
};
struct computeLMean {
template<typename T>
T operator()(unsigned int k, const std::vector<T>& wi) const{
T mu = detail::get_mu(wi);
T sigma2 = detail::get_sigma2(wi);
return LMean()(k, mu, sigma2);
}
};
struct computeLMode {
template<typename T>
T operator()(unsigned int k, const std::vector<T>& wi) const{
T mu = detail::get_mu(wi);
T sigma2 = detail::get_sigma2(wi);
return LMode()(k, mu, sigma2);
}
};
struct computeLEff {
template<typename T>
T operator()(unsigned int k, const std::vector<T>& wi) const{
T mu = detail::get_mu(wi);
T sigma2 = detail::get_sigma2(wi);
return LEff()(k, mu, sigma2);
}
};
} // namespace MCLLH