I have a random variable $X$ which follows the following probability density function,

$$ p(x) = \frac{1}{4\pi} \Big[ \operatorname{erf}\Big(\frac{k\mu-x+2\pi}{\sqrt{2}k\sigma}\Big) - \operatorname{erf}\Big(\frac{k\mu-x}{\sqrt{2}k\sigma}\Big) \Big]$$

I wanted to find the probability density function for the function of the random variable $Y = \cos(X)$.

I followed the CDF method to find this.

$$ F_Y(y) = p(Y \leq y) = p(\cos(X) \leq y) = p(X \leq \cos^{-1}(y))$$

$$ F(y) = \int_{-\infty}^{\cos^{-1}(y)} \frac{1}{4\pi} \Big[ \operatorname{erf}\Big(\frac{k\mu-x+2\pi}{\sqrt{2}k\sigma}\Big) - \operatorname{erf}\Big(\frac{k\mu-x}{\sqrt{2}k\sigma}\Big) \Big] dx $$

The terms $\mu$, $\sigma$ and $k$ are constants.

$$ k \quad \epsilon \quad Z; k \geq 0$$ $$ \sigma \quad \epsilon \quad R; \sigma > 0 $$

Mathematica Output: $$ F_Y(y) = \frac{1}{8 \pi ^{3/2}} \Bigg[ -\sqrt{\pi } \left(2 i \log \left(\sqrt{1-y^2}+i y\right)-2 k \mu+\pi \right) \text{erf}\left(\frac{-2 i \log \left(\sqrt{1-y^2}+i y\right)+2 k \mu-\pi }{2 \sqrt{2} k \sigma}\right) \\ +\sqrt{\pi } \left(2 i \log \left(\sqrt{1-y^2}+i y\right) -2 k \mu-3 \pi \right) \text{erf}\left(\frac{-2 i \log \left(\sqrt{1-y^2}+i y\right) +2 k \mu+3 \pi }{2 \sqrt{2} k \sigma}\right)- \\ 2 \sqrt{2} k \sigma \left(\sqrt{1-y^2}+i y\right)^{\frac{i (2 k \mu+3 \pi )}{2 k^2 \sigma^2}} \exp \left(-\frac{(2 k \mu+3 \pi )^2-4 \log ^2\left(\sqrt{1-y^2}+i y\right)}{8 k^2 \sigma^2}\right)+ \\ 2 \sqrt{2} k \sigma \left(\sqrt{1-y^2}+i y\right)^{-\frac{i (\pi -2 k \mu)}{2 k^2 \sigma^2}} \exp \left(-\frac{(\pi -2 k \mu)^2-4 \log ^2\left(\sqrt{1-y^2}+i y\right)}{8 k^2 \sigma^2}\right)+4 \pi ^{3/2} \Bigg] $$

When I do this integral, it gives me a complex valued function on mathematica. How can this be possible that the CDF is complex valued? If it is so, what should I consider as the CDF? Should I consider the real part only?

============== EDIT ==========

How I found $p(x)$ ?

It comes from another problem of finding the PDF of a random variable $ X = k U + \beta $, where k is an integer. $U$ comes from a Gaussian distribution and $\beta$ comes from a uniform distribution.

$$ U \sim \mathcal{N}(\mu, \sigma) $$ and $$ \beta \sim \mathcal{U}(0, 2\pi) $$

As it is an addition, I found the PDF of $X$ as the convolution of the other 2 PDFs.

$$ p(x) = \int_{0}^{2\pi} \frac{1}{2\pi \sqrt{2 \pi k^2 \sigma^2}} e^{-(kx - k\mu - \tau)/(2k^2\sigma^2)} d\tau $$

$$ p(x) = \frac{1}{4\pi} \Big[ \operatorname{erf}\Big(\frac{k\mu-x+2\pi}{\sqrt{2}k\sigma}\Big) - \operatorname{erf}\Big(\frac{k\mu-x}{\sqrt{2}k\sigma}\Big) \Big] $$

I thought this was correct, because when I integrated $p(x)$ from $-\infty$ to $+\infty$, I got $1$.

  • 1
    $\begingroup$ $p(\cos(X) \leq y) = p(X \leq \cos^{-1}(y))$ looks a dubious statement if $X$ can take any real value. Even if $X$ is restricted to $[0,\pi]$ and if $y \in [-1,1]$, then $\cos^{-1}(y)$ is a decreasing function $\endgroup$
    – Henry
    Commented Aug 11, 2022 at 12:25
  • $\begingroup$ I just applied cosine inverse on both sides to see how can I compute the CDF with $p(x)$. If this step is wrong, how to proceed? $X$ an take any value but a cosine function limits it from -1 to 1. $\endgroup$
    – CfourPiO
    Commented Aug 11, 2022 at 12:29
  • 3
    $\begingroup$ Answer to the question in title: No, it is not possible. Mathemathica treats erf as a complex function, can you tell mathematica not to do that? $\endgroup$ Commented Aug 11, 2022 at 13:08
  • 3
    $\begingroup$ @Kjetil Yes, you can. It's important to simplify complex-looking output when you know it's real. Mathematica has gradually introduced some bugs in integration (especially after version 7 or so) and one has to take care to test the output. $\endgroup$
    – whuber
    Commented Aug 11, 2022 at 15:37

1 Answer 1


It is not obvious to me that your $p(x)$ is actually a density, but let's suppose it is. Meanwhile $\cos^{-1}$ is not defined on all the reals and is usually a decreasing function $[-1,1] \to [0,\pi]$.

For $y \le -1$ you have $F_Y(y) = \mathbb P(Y \leq y) = 0$

and similarly for $y \ge 1$ you have $F_Y(y) = \mathbb P(Y \leq y) = 1$.

Otherwise $-1<y<1$, and with $\cos^{-1}: [-1,1] \to [0,\pi]$ you have

$$F_Y(y) = \mathbb P(Y \leq y) = \mathbb P(\cos(X) \leq y) \\= \mathbb P(2n\pi +\cos^{-1}(y)\le X \le 2n\pi+2\pi-\cos^{-1}(y))$$ over integer $n$ and so

$$F_Y(y) = \sum_{n=-\infty}^{\infty} \int_{x=2n\pi +\cos^{-1}(y)}^{x=2n\pi+2\pi-\cos^{-1}(y)} p(x)\, dx$$

all of which should be real, though not necessarily simple

  • $\begingroup$ I see! I understand it now clearly. Thank you. I was wondering why you said $p(x)$ doesn't look like a density. Is there a reason for it? I want to know because I derived it from another problem. I will update in the question now. $\endgroup$
    – CfourPiO
    Commented Aug 11, 2022 at 14:41
  • 1
    $\begingroup$ I can see $p(x)$ is positive, but it seems to involve the error function which is already an integral. I would not want to try to integrate it again to show $\int_{-\infty}^{\infty} p(x)\, dx =1$. But it might be OK $\endgroup$
    – Henry
    Commented Aug 11, 2022 at 15:01
  • 1
    $\begingroup$ Re "not obviously a density:" When $F$ is any distribution function and $a$ is any positive constant, $x\to F(x+a)-F(x)$ is a distribution function, too. It is the distribution of $X+U$ where $X$ is a random variable with distribution $F$ and $U$ is a Uniform$(0,a)$ variable. See the section on "Intuition from Probability" at stats.stackexchange.com/a/43075/919. $\endgroup$
    – whuber
    Commented Aug 12, 2022 at 12:52
  • 1
    $\begingroup$ @whuber Did you intend to say $\frac1a (F(x)-F(x-a))$ is a density? I am prepared to accept this, and also that the original expression is an example of this - I was just saying that it was not obvious $\endgroup$
    – Henry
    Commented Aug 12, 2022 at 13:10
  • 1
    $\begingroup$ Yes, I neglected the $1/a$ factor. (I always think in terms of convenient units of measurement, which in this case makes $a=1,$ and forgot to adjust for that. Thanks for catching the error!) $\endgroup$
    – whuber
    Commented Aug 12, 2022 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.