TITLE: Integral (Tanh and Normal)
QUESTION [15 upvotes]: I am trying to evaluate the following: The expectation of the hyperbolic tangent of an arbitrary normal random variable.
$\mathbb{E}[\mathrm{tanh}(\phi)]; \phi \sim N(\mu, \sigma^2)$
Equivalently:
$\int_{-\infty}^{\infty} \mathrm{tanh}(\phi)\frac{1}{\sqrt{2\pi\sigma^2}}\exp(-\frac{1}{2\sigma^2}(\phi-\mu)^2) d\phi$
I've resorted to Wolfram Alpha, and I can sometimes (!) get it to evaluate the integral for $\sigma^2 = 1$. It gives:
$\exp(\mu) - 1$ for negative $\mu$ and $1-\exp(-\mu)$ for positive mu.
I have no idea how it got this, but it seems plausible as I've done some simulations (i.e. lots of draws from the normal distribution and then the mean of the tanh of the draws) and the formula it gives for the $\sigma^2 = 1$ case seems pretty close. 
I cannot get it to evalute anything with a different variance. Mathematica also seems unwilling to compute the integral or the Fourier transform (which I thought might be a way forward) but this is tricker than the ones I know how to deal with.
Thanks!

REPLY [2 votes]: $\def\f{\varphi}
\def\s{\sigma}
\def\m{\mu}
\def\p{\pi}$Let
$$f(\f) = \frac{1}{\sqrt{2\p}\s} e^{-(\f-\m)^2/(2\s^2)}.$$
We have
\begin{align*}
I &= \int_{-\infty}^\infty  f(\f)\tanh\f\, d\f 
= \int_0^\infty f(\f)\tanh\f\, d\f 
+ \int_{-\infty}^0 f(\f)\tanh\f\, d\f \\
&= \int_0^\infty (f(\f)-f(-\f))\tanh\f\, d\f.
\end{align*}
But
\begin{align*}
\tanh\f 
&= \frac{1-e^{-2\f}}{1+e^{-2\f}} 
= (1-e^{-2\f})\sum_{k=0}^\infty (-1)^k e^{-2k\f} \\
&= 1+2\sum_{k=1}^\infty (-1)^k e^{-2k\f}.
\end{align*}
It is a straightforward exercise to show that
\begin{align*}
J_k &= \int_0^\infty (f(\f)-f(-\f)e^{-2k\f} d\f \\
&= \frac12 e^{-2k(\m-k\s^2)} 
\left(
1+\mathrm{erf}
\left(\frac{\m-2k\s^2}{\sqrt2 \s}\right)
\right)
- \frac12 e^{2k(\m+k\s^2)} 
\mathrm{erfc}\left(\frac{\m+2k\s^2}{\sqrt2\s}\right).
\end{align*}
One can show for large $k$ that $J_k$ is of order $1/k^2$.
Let
$$I_n = J_0 + 2\sum_{k=1}^n (-1)^k J_k.$$
Thus, $I = \lim_{n\to\infty}I_n.$
The zeroth approximation is
$$I_0 = J_0 = \mathrm{erf}\left(\frac{\m}{\sqrt{2}\s}\right).$$
(This amounts to approximating $\tanh\f$ by $\mathrm{sign}(\f)$ in the defining integral.)
Below we plot $I_0$, $I_1 = J_0 - 2J_1$, and $\hat I$, an independent numerical approximation to $I$, as functions of $\m$ for $\s=1$.
On this scale $I_2$ would be indistinguishable from $\hat I$.

Figure 1. $I_0$, $I_1$, and $\hat I$ (in red, orange, and black, respectively) vs $\m$ for $\s=1$.
Addendum: Interchanging summation and integration
Let $g(\f) = f(\f)-f(-\f)$.
Note that
$$g(\f)\sum_{k=1}^\infty (-1)^k e^{-2k \f}$$
converges pointwise to
$$-\frac{g(\f)}{1+e^{2\f}}$$
on $(0,\infty)$.
Also,
$$\left|
g(\f)\sum_{k=1}^n (-1)^k e^{-2k \f}
\right| 
\le \left|g(\f)\right|.$$
Since $|g(\f)|$ is integrable, interchanging summation and integration is justified by the dominated convergence theorem.