My problem is the inconsistency between the definition and the computation of the Approximate entropy (`ApEn`). 


Suppose $u = (u\_i:1\leq i \leq N)$ is a sequence of positive real numbers and $x = (x\_i:1\leq i\leq N - m + 1)$ the sequence of components of $u$ of length $m$ (i.e. $x\_i = (u\_i,\ldots,x\_{i + m -1})$). Let also $C^m\_i$ be the number of elements of $x$ which have Chebyshev distance less than a threshold $r$ from $x\_i$. The two different definitions of `ApEn` I have run into are the following:


(A) ApEn = $\frac{1}{N - m + 1}\sum\log C^m\_i - \frac{1}{N - m}\sum\log C^{m + 1}\_i = \log(\prod C^m\_i)^{\frac{1}{N - m + 1}} - \log(\prod C^{m + 1}\_i)^{\frac{1}{N - m}}$


(B) ApEn = $\log(\frac{1}{N - m + 1}\sum C^m\_i) - \log(\frac{1}{N - m}\sum C^{m + 1}\_i)$


It is direct to see that `(A)` computes the logarithm of the arithmetical mean of variations of components of lengths $m, m+1$, while `(B)` computes the logarithm of their geometrical mean. In practice they return different results and I cannot find a legitimate reason to pick one of the two -apart from `(B)` being computationally more efficient.


What makes things even more confusing, is the fact that up to now I have seen `(A)` in definitions of ApEn and `(B)` in implementations. In wikipedia, <http://en.wikipedia.org/wiki/Approximate_entropy>, ApEn is defined by `(A)` and computed in the example by `(B)`. Pincus defines it using `(A)` in <http://www.pnas.org/content/88/6/2297.full.pdf>. The implementation in R's package `pracma` is according to `(B)`.


Are those two definitions interchangeable for some reason, or is one of the two preferred in practice? I would really appreciate a justified answer. 



Which is the right way to compute the Approximate Entropy (ApEn)?
From a computational perspective it is more advantageous to take the logarithm of the average (definition B) rather than the average of the logarithm (definition A). In particular if your time series is long, taking the logarithm just once at the end (definition B) rather than for each data point (definition A) can speed things up a lot. The difference is likely to be insignificant: ApEn is a qualitative indicator anyway, and unless the time series has great dispersion interchanging average and logarithm will not make a qualitative difference.


More precisely, if $C\_i^m$ has a relatively narrow distribution ($\epsilon=$ variance/mean$^2$ $\ll 1$), the difference between the logarithm of the average of $C\_i^m$ and the average of the logarithm can be approximated by
$$\log\left(\frac{1}{N-m+1}\sum\_i C\_i^m\right)-\frac{1}{N-m+1}\sum\_i \log C\_i^m=
\frac{(N-m+1)\sum\_i (C\_i^m)^2}{2\left(\sum\_i C\_i^m\right)^2}-\frac{1}{2}+{\rm order}(\epsilon^2).$$


