pomp
Inference for partially observed Markov processes
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
logmeanexp.c
Go to the documentation of this file.
1// -*- C++ -*-
2
3#include "internal.h"
4
5// Compute log(mean(exp(X))) accurately,
6// optionally with one element dropped
7SEXP logmeanexp (const SEXP X, const SEXP Drop) {
8 int j, n = LENGTH(X);
9 int k = *INTEGER(Drop)-1; // zero-based index
10 double *x = REAL(X);
11 long double m = R_NegInf;
12 long double s = 0;
13 for (j = 0; j < n; j++) {
14 if (j != k)
15 m = (x[j] > m) ? (long double) x[j] : m;
16 }
17 for (j = 0; j < n; j++) {
18 if (j != k)
19 s += expl((long double) x[j] - m);
20 }
21 if (k >= 0 && k < n) n--;
22 return ScalarReal(m + log(s/n));
23}
#define X
Definition gompertz.c:14
SEXP logmeanexp(const SEXP X, const SEXP Drop)
Definition logmeanexp.c:7