-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlikelihood.h
More file actions
317 lines (299 loc) · 10.6 KB
/
likelihood.h
File metadata and controls
317 lines (299 loc) · 10.6 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
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2018 Ghislain Durif
//
// This file is part of the `pCMF' library for R and related languages.
// It is made available under the terms of the GNU General Public
// License, version 2, or at your option, any later version,
// incorporated herein by reference.
//
// This program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA
/*!
* \brief functions for likelihood computation
* \author Ghislain Durif
* \version 1.0
* \date 06/02/2018
*/
#ifndef LIKELIHOOD_H
#define LIKELIHOOD_H
#include <math.h>
#include <RcppEigen.h>
#include "internal.h"
#include "macros.h"
// [[Rcpp::depends(RcppEigen)]]
using Eigen::MatrixXd; // variable size matrix, double precision
using Eigen::RowVectorXd; // variable size row vector, double precision
using Eigen::VectorXd; // variable size vector, double precision
/*!
* \namespace likelihood
*
* A specific namespace for all related likelihood functions
*/
namespace likelihood {
/*!
* \fn log-likelihood for the Gamma distribution Gamma(param1, param2)
*
* `param1` is the shape parameter and `param2` is the rate parameter.
* The density function is defined as
* \f[
* x \mapsto f(x; a_1, a_2) = \frac{(a_2)^{a_1} x^{a_1-1} e^{-a_2 x}}{\Gamma(a_1)}
* \f]
* where f\$ z \mapsto \Gamma(z) \f$ is the Gamma function.
*
* \param[in] x observed value
* \param[in] param1 shape parameter for the Gamma distribution
* \param[in] param2 rate parameter for the Gamma distribution
*
* \return Gamma log-likelihood value
*/
inline double gamma_loglike(double x, double param1, double param2) {
double res=0;
res = param1 * log(param2) + (param1 - 1) * log(x) - param2 * x - lgamma(param1);
return res;
};
/*!
* \fn log-likelihood for the Gamma distribution Gamma(param1, param2) matrix-wise
*
* `param1` is the shape parameter and `param2` is the rate parameter.
* The density function is defined as
* \f[
* x \mapsto f(x; a_1, a_2) = \frac{(a_2)^{a_1} x^{a_1-1} e^{-a_2 x}}{\Gamma(a_1)}
* \f]
* where f\$ z \mapsto \Gamma(z) \f$ is the Gamma function.
*
* \param[in] x matrix rows x cols of observed values
* \param[in] param1 matrix rows x cols of shape parameters
* \param[in] param2 matrix rows x cols of rate parameters
*
* \return sum of the Gamma log-likelihood values over the rows x cols matrix
*/
inline double gamma_loglike(const MatrixXd &X, const MatrixXd ¶m1, const MatrixXd ¶m2) {
double res = ( param1.array() * param2.mlog().array() + (param1.array() - 1) * X.mlog().array()
- param2.array() * X.array() - param1.mlgamma().array() ).sum();
return res;
};
/*!
* \fn log-likelihood for the Gamma distribution Gamma(param1, param2) matrix-wise
* where X and logX are given
*
* `param1` is the shape parameter and `param2` is the rate parameter.
* The density function is defined as
* \f[
* x \mapsto f(x; a_1, a_2) = \frac{(a_2)^{a_1} x^{a_1-1} e^{-a_2 x}}{\Gamma(a_1)}
* \f]
* where f\$ z \mapsto \Gamma(z) \f$ is the Gamma function.
*
* \param[in] x matrix rows x cols of observed values
* \param[in] param1 matrix rows x cols of shape parameters
* \param[in] param2 matrix rows x cols of rate parameters
*
* \return sum of the Gamma log-likelihood values over the rows x cols matrix
*/
inline double gamma_loglike(const MatrixXd &X, const MatrixXd &logX,
const MatrixXd ¶m1, const MatrixXd ¶m2) {
double res = ( param1.array() * param2.mlog().array() + (param1.array() - 1) * logX.array()
- param2.array() * X.array() - param1.mlgamma().array() ).sum();
return res;
};
/*!
* \fn log-likelihood for the Poisson distribution P(rate)
*
* `rate` is the Poisson intensity parameter.
* The density function is defined as
* \f[
* x \mapsto f(x; r) = \frac{r^x e^{-r}}{r!}
* \f]
*
* \param[in] x observed value
* \param[in] rate Poisson intensity
*
* \return Poisson log-likelihood value
*/
inline double poisson_loglike(double x, double rate) {
double res = 0;
res = x * log(rate>0 ? rate : 1) - rate - lgamma(x+1);
return res;
};
/*!
* \fn log-likelihood for the Poisson distribution P(rate) matrix-wise
*
* `rate` is the Poisson intensity parameter.
* The density function is defined as
* \f[
* x \mapsto f(x; r) = \frac{r^x e^{-r}}{r!}
* \f]
*
* \param[in] x matrix rows x cols of observed values
* \param[in] rate matrix rows x cols of Poisson intensities
*
* \return sum of the Poisson log-likelihood values over the rows x cols matrix
*/
inline double poisson_loglike(const MatrixXd &X, const MatrixXd &rate) {
// double res = ( X.array() * ((rate.array() > 0).select(rate, 1)).mlog().array()
// - rate.array() - (X.array() + 1).mlgamma() ).sum();
double res = ( X.array() * rate.mclog().array()
- rate.array() - (X.array() + 1).mlgamma() ).sum();
return res;
};
/*!
* \fn log-likelihood for the Poisson distribution P(rate) matrix-wise
* with a column-wise rate
*
* `rate` is the Poisson intensity parameter.
* The density function is defined as
* \f[
* x \mapsto f(x; r) = \frac{r^x e^{-r}}{r!}
* \f]
*
* \param[in] x matrix rows x cols of observed values
* \param[in] rate vector of size cols of Poisson intensities
*
* \return sum of the Poisson log-likelihood values over the rows x cols matrix
*/
inline double poisson_loglike_vec(const MatrixXd &X, const RowVectorXd &rate) {
// double res = ( ( X.array().rowwise()
// * ((rate.array() > 0).select(rate, 1)).mlog().array() ).rowwise() - rate.array() ).sum()
// - (X.array() + 1).mlgamma().sum();
double res = ( ( X.array().rowwise()
* rate.mclog().array() ).rowwise() - rate.array() ).sum()
- (X.array() + 1).mlgamma().sum();
return res;
};
/*!
* \fn log-likelihood for the Zero-inflated Poisson distribution matrix-wise
*
* `rate` is the Poisson intensity parameter and `prob` the
* drop-out probability. The density function is defined as
* \f[
* x \mapsto f(x; r, prob) = (1-prob) delta_0(x) + prob \frac{r^x e^{-r}}{r!}
* \f]
*
* \param[in] x matrix rows x cols of observed values
* \param[in] rate matrix rows x cols of Poisson intensities
* \param[in] prob matrix rows x cols of drop-out probabilities
*
* \return sum of the ZI Poisson log-likelihood values over the rows x cols matrix
*/
inline double zi_poisson_loglike(const MatrixXd &X, const MatrixXd &rate,
const MatrixXd &prob) {
int i, j;
VectorXd tmp = VectorXd::Zero(X.cols());
#if defined(_OPENMP)
#pragma omp parallel for private(i)
#endif
for(j=0; j<X.cols(); j++) {
for(i=0; i<X.rows(); i++) {
tmp(j) += X(i,j) > 0 ?
std::log(prob(i,j)) + poisson_loglike(X(i,j), rate(i,j)) :
std::log( (1-prob(i,j) + prob(i,j) * std::exp(-rate(i,j))));
}
}
return tmp.sum();
};
/*!
* \fn log-likelihood for the Zero-inflated Poisson distribution matrix-wise
* with column-wise parameters
*
* `rate` is the Poisson intensity parameter and `prob` the
* drop-out probability. The density function is defined as
* \f[
* x \mapsto f(x; r, prob) = (1-prob) delta_0(x) + prob \frac{r^x e^{-r}}{r!}
* \f]
*
* \param[in] x matrix rows x cols of observed values
* \param[in] rate vector of size cols of Poisson intensities
* \param[in] prob vector of size cols of drop-out probabilities
*
* \return sum of the Poisson log-likelihood values over the rows x cols matrix
*/
inline double zi_poisson_loglike_vec(const MatrixXd &X, const VectorXd &rate,
const VectorXd &prob) {
int i, j;
VectorXd tmp = VectorXd::Zero(X.cols());
#if defined(_OPENMP)
#pragma omp parallel for private(i)
#endif
for(j=0; j<X.cols(); j++) {
for(i=0; i<X.rows(); i++) {
tmp(j) += X(i,j) > 0 ?
std::log(prob(j)) + poisson_loglike(X(i,j), rate(j)) :
std::log( (1-prob(j) + prob(j) * std::exp(-rate(j))));
}
}
return tmp.sum();
};
/*!
* \fn log-likelihood for the Bernoulli distribution P(prob) matrix-wise
*
* `prob` is the Bernoulli parameter.
* The density function is defined as
* \f[
* x \mapsto f(x; prob) = prob^x * (1-prob)^{(1 - x)}
* \f]
*
* \param[in] x matrix rows x cols of observed values
* \param[in] prob matrix rows x cols of Bernoulli probabilities
*
* \return sum of the Bernnoulli log-likelihood values over the rows x cols matrix
*/
inline double bernoulli_loglike(const MatrixXd &X, const MatrixXd &prob) {
// double res = ( X.array() * prob.mclog().array()
// + (1 - X.array()).array() * (1 - prob.array()).mclog().array() ).sum();
// return res;
int i, j;
VectorXd tmp = VectorXd::Zero(X.cols());
#if defined(_OPENMP)
#pragma omp parallel for private(i)
#endif
for(j=0; j<X.cols(); j++) {
for(i=0; i<X.rows(); i++) {
// if(prob(i,j)>1e-44 && prob(i,j)<1-1e-44) {
if(prob(i,j)>0 && prob(i,j)<1) {
tmp(j) += X(i,j) * std::log(prob(i,j)) + (1-X(i,j)) * std::log(1-prob(i,j));
}
}
}
return tmp.sum();
};
/*!
* \fn log-likelihood for the Bernoulli distribution P(prob) matrix-wise
* with column-wise parameters
*
* `prob` is the Bernoulli parameter.
* The density function is defined as
* \f[
* x \mapsto f(x; prob) = prob^x * (1-prob)^{(1 - x)}
* \f]
*
* \param[in] x matrix rows x cols of observed values
* \param[in] prob prob vector of size cols of Bernoulli probabilities
*
* \return sum of the Bernnoulli log-likelihood values over the rows x cols matrix
*/
inline double bernoulli_loglike_vec(const MatrixXd &X, const RowVectorXd &prob) {
// double res = ( X.array().rowwise() * prob.mclog().array()
// + (1 - X.array()).array().rowwise() * (1 - prob.array()).mclog().array() ).sum();
// return res;
int i, j;
VectorXd tmp = VectorXd::Zero(X.cols());
#if defined(_OPENMP)
#pragma omp parallel for private(i)
#endif
for(j=0; j<X.cols(); j++) {
for(i=0; i<X.rows(); i++) {
if(prob(i,j)>0 && prob(i,j)<1) {
tmp(j) += X(i,j) * std::log(prob(j)) + (1-X(i,j)) * std::log(1-prob(j));
}
}
}
return tmp.sum();
};
}
#endif // LIKELIHOOD_H