# A complicated integral that Mathematica can't compute

I have a very complicated distribution function of which I want to find the expected value.

The distribution I got for a function having the cosine of the samples taken from a Gaussian distribution.

$$y = \cos(x)$$

Where $$x$$ values are drawn from a Gaussian distribution and we assume $$x \in [-\pi, \pi]$$.

$$p(x) = \frac{1}{\sqrt{2\pi\sigma_x^2}}e^{-\frac{(x - \mu_x)^2}{2\sigma_x^2}}$$

The distribution of $$y$$ hence can be written as below:

So, now the distribution of $$y$$ becomes:

$$p(y) = \frac{2}{\sqrt{1 - y^2}} \frac{1}{\sqrt{2\pi\sigma_x^2}}e^{-\frac{(\cos^{-1}(y) - \mu_x)^2}{2\sigma_x^2}}$$

I referred this article for the formulation. They do it when $$x$$ follows a uniform distribution. However, here I have a Gaussian distribution.

The main question that I want to ask is to sum the random numbers one would generate using this distribution.

$$\sum_i y_i$$

Where $$y_i$$ are the samples drawn from $$p(y)$$. I thought of solving this by the following integral. I thought like a Frequentist and found that the sum of a number of variables is the sum of the independent variables (samples) I have multiplied with the probability of those samples occurring out of some number of draws from the distribution. Please let me know if this thinking is not adequate enough.

So, at the end it becomes:

$$\sum_i y_i = \sum_{i = 1}^{N_s} y_i p(y_i)$$

Where $$N_s$$ is the number of independent samples. This can be written in integral form as:

$$\int_{-1}^{+1} \frac{2y}{\sqrt{1 - y^2}} \frac{1}{\sqrt{2\pi\sigma_x^2}}e^{-\frac{(\cos^{-1}(y) - \mu_x)^2}{2\sigma_x^2}} dy$$

I say $$-1$$ to $$+1$$ instead of a finite length here because I assume $$N_s$$ in the previous expression to be large enough. This looks like the expected value of the distribution. Is there a way to find a closed form solution to this equation (Or approximate closed form)? When it comes to Gaussian distribution, this quite nicely explained before and I have seen it here. When I tried this integral with the complicated distribution, it said that it can't converge. Is there way to do this?

EDIT:

I implemented a wrong function on mathematica. Now, I do the correct one. It doesn't say it can't converge anymore, however, it doesn't compute it now. It just returns with the integral expression.

EDIT Version 2 =====================================================

I took $$y = \cos(\theta)$$ to solve the integral and I have now,

$$\int_{-\pi}^{\pi} \frac{2 \cos(\theta) e^{-\frac{\theta - \mu_x^2}{2\sigma_x^2}} }{\sqrt{2\pi \sigma_x^2}} d\theta$$

The solution I have from Mathematica looks like the following.

$$\frac{e^{-i \mu_x - \frac{1}{2\sigma_x^2}}}{2\sigma_x^2} \Bigg[ -\operatorname{erf}\Big( {\frac{\mu_x}{\sigma_x} - \frac{i \sigma_x}{2} - \frac{\pi}{\sigma}}\Big) + \operatorname{erf}\Big( {\frac{\mu_x}{\sigma_x} - \frac{i \sigma_x}{2} + \frac{\pi}{\sigma}}\Big) + e^{i 2 \mu_x} \Bigg( -\operatorname{erf}\Big( {\frac{\mu_x}{\sigma_x} + \frac{i \sigma_x}{2} - \frac{\pi}{\sigma}}\Big) + \operatorname{erf}\Big( {\frac{\mu_x}{\sigma_x} + \frac{i \sigma_x}{2} + \frac{\pi}{\sigma}}\Big) \Bigg) \Bigg) \Bigg] \Bigg[ \operatorname{erf}[\frac{i + \sigma_x^2 (-\mu_x + \pi)}{\sqrt{2} \sigma_x}] - \operatorname{erf}[\frac{i - \sigma_x^2 (\mu_x + \pi)}{\sqrt{2} \sigma_x}] - i e^{i2 \mu_x} (\operatorname{erfi}[\frac{1 + i \sigma_x^2 (-\mu_x + \pi)}{\sqrt{2} \sigma_x}] - \operatorname{erfi}[\frac{1 - i \sigma_x^2 (\mu_x + \pi)}{\sqrt{2} \sigma_x}] \Bigg]$$

Is there a relation between this and the nice solution given by @snoop ?

• I am already lost where you claim that x has a gauss-distribution but only values in $[-\pi,\pi]$... Jun 30 at 11:47
• Yes I just realized it now. I commented on @Snoop's answer. This approximation is indeed a wrong one. As I use a $\cos{X}$ later, I thought the domain of $X$ should be between $-\pi$ to $\pi$. Jun 30 at 11:51

Use characteristic functions and the fact that $$\cos(x)=(e^{ix}+e^{-ix})/2$$: $$E[\cos(X)]=\frac{1}{2}(E[e^{iX}]+E[e^{-i X}])=\frac{e^{-\frac{\sigma^2}{2}}}{2}(e^{i\mu}+e^{-i\mu})=e^{-\frac{\sigma^2}{2}}\cos(\mu)$$
• Thank you for the answer. How did you find the expected value of $e^{iX}$ in your derivation. I saw some examples, but I can't understand it quite well. I followed up my integral a bit and tried solving it. I got an answer. I will update it in the question. I am not able to see a relation between this and my answer. Probably the way I proceeded is wrong? Jun 30 at 8:43
• I think I see the relation now. If I integrate from $-\pi$ to $+\pi$, I get my solution. However, if I do the integral from $-\infty$ to $+\infty$, I get your solution. I also did a small experiment on Matlab to see how accurate the solution you provide is. I saw that for a smaller $\sigma$ (when the values of $x$ remain in the interval $[-\pi, \pi]$, the solution is more accurate. When some values of $x$ exceed these limits, the expected value is different than the numerical one. ) Jun 30 at 11:49