# How to transform normally distributed random sequence N(0,1) to uniformly distributed U(0,1)?

Everybody knows how to convert U(0,1) to N(0,1). However does anybody know an efficient algorithm solving the opposite task? I mean how to generate U(0,1) sequence from N(0,1) one? Asking because a group of voice and speech researchers with whom I work are routinely trying to represent results of their measurements which they believe to be normally distributed using a linear 0 to 100 scale. As a result they are getting negative values outside of their linear scale obviously because they are dealing with random noise that can be roughly assumed to be normal and therefore this noise theoretically spans the whole real axis. Though I can imagine that I need to take a logarithm of the normal distribution, then multiply the negative quadratic term by -1 and then take a square root of it to get a linear function, a question is: does anybody know an efficient algorithm for doing that, I mean for generating high quality U(0,1) random numbers from N(0,1). I would highly appreciate any feedback on this!!

-
If $X\sim \mathcal{N}(0,1)$ then $\Phi(X)\sim U(0,1)$, where $\Phi$ is the CDF of $X$. –  Stefan Hansen Jun 4 '12 at 16:02
Thanks, however doesn't the graph of Ф(X) show that it is nonlinear, while U(0,1) is a linear function? I mean for normal distribution Ф(X) is slowly growing first then very quickly growing in the region of the bell and then its growth considerably slows down again, right? Therefore it is not uniformly distributed, even though its values are really conveniently in the range from 0 to 1. So an additional transformation for Ф(X) will be required. To get Ф(X) I will need to integrate N(0,1) so my question is if there is a known efficient algorithm to convert N(0,1) to U(0,1). Thanks! –  Anthony Green Jun 4 '12 at 16:35
$U(0,1)$ is a random variable that has a CDF that is linear. $\Phi(X)$ is another random variable that has exactly the same CDF as a $U(0,1)$ distribution. So suppose you have a draw $u\sim U(0,1)$, then you simply compute $\Phi(u)$ which is a draw from an $\mathcal{N}(0,1)$ distribution ($\Phi$ is available in most statistical packages). –  Stefan Hansen Jun 4 '12 at 16:54
As you can see from graphs given at en.wikipedia.org/wiki/Cumulative_distribution_function the Ф(X) (that is itself a CDF, i.e. an integral of normal distribution density from -∞ to x) is non-linear for normal distribution, while the CDF for U(0,1) is linear in the interval from 0 to 1. Therefore these two functions are different. If you disagree could you please provide a confirming link? Thanks a lot! –  Anthony Green Jun 4 '12 at 17:50
No $\Phi(X)$ is not a CDF. It is not a function defined on $\mathbb{R}$. Instead it is indeed a random variable $\Omega\ni \omega\mapsto \Phi(X(\omega))$ with the property that $P(\Phi(X)\leq x)=x$, $x\in (0,1)$, i.e. it is a $U(0,1)$-variable. See for example the first lines of the definition here: en.wikipedia.org/wiki/Inverse_transform_sampling#Definition or here aiaccess.net/English/Glossaries/GlosMod/… –  Stefan Hansen Jun 4 '12 at 18:08

According to Box-Muller method, if $(U_1,U_2)\sim \mathcal{U}([0,1])$ i.i.d. then $$Z_1 := \sqrt{-2\ln U_1} \cos(2\pi U_2)$$ $$Z_2 := \sqrt{-2\ln U_1} \sin(2\pi U_2)$$ are i.i.d. $\mathcal{N}(0,1)$. You can go the opposte way and set $$U_1 := \exp\left(-\frac{1}{2}(Z_1^2+Z_2^2)\right)$$ $$U_2 := \frac{1}{2\pi} \arccos \left( \frac{Z_1}{Z_1^2+Z_2^2} \right)$$ given i.i.d. normal random variables $(Z_1,Z_2)\sim \mathcal{N}(0,1)$. You will get i.i.d. uniforms on $[0,1]$.

The proof is straightforward by writing the cummulative distribution function of $U_1$ and $U_2$ and applying the appropriate change of variable. Indeed for $z \in [0,1]$,

\begin{eqnarray} P(U_1\leq z) &=& P\left(\exp\left(-\frac{1}{2}(Z_1^2+Z_2^2)\right)<z\right) \\ & = & \frac{1}{2\pi} \int_{e^{-\frac{1}{2}(x^2+y^2)}<z} e^{-\frac{1}{2}x^2}e^{-\frac{1}{2} y^2}dxdy \\ & = & \frac{1}{2\pi} \int_0^{2\pi} \int_{\sqrt{-2\ln (z)}}^\infty e^{-\frac{1}{2}r^2} r dr d\theta \\ & = & [-e^{-\frac{1}{2}r^2}]_{\sqrt{-2\ln (z)}}^\infty \\ & = & z \end{eqnarray}

It is clear also that $P(U_1 \leq z)=0$ when $z\leq0$ and $P(U_1 \leq z)=1$ when $z\geq1$. The same method will show that $U_2$ is uniform. Note that the $\arccos$ has to return values in $[0,2\pi]$.

Finally to return $n$ uniforms given $Z_1,...,Z_n$ i.i.d. $\mathcal{N}(0,1)$ just work by pair. $U_1,U_2$ generated with $Z_1,Z_2$, $U_3,U_4$ with $Z_3,Z_4$, etc. Pairwise independency of the $U_i$'s follows from the pairwise independency of the $Z_i$'s.

-
I do not understand this answer, since the OP has only one Gaussian variate, not two. –  adavid Jan 16 '14 at 3:33

Naively, this seems to just be a problem of remapping the probability densities. I do not understand the answer by vanna since that requires two variates.

Going from Gaussian to Uniform requires going from an infinite support $[-\infty,+\infty]$ to a finite support, like $[0,1]$. I think that the simplest way to achieve this is along the lines of what stefan-hansen was suggesting:

• normalize the data to Gaussian(0,1) by subtracting the average and dividing by the standard deviation: $y = \frac{x - \hat\mu}{\hat\sigma}$, and
• transform the values using the CDF: $z=\frac{1}{2}Erfc(-\frac{y}{\sqrt{2}})$, where $Erfc$ is the complementary error function.

If $y$ is distributed with Gaussian(0,1), $z$ will be distributed as Uniform(0,1).

This requires having estimates of $\hat\mu$ and $\hat\sigma$ before proceeding with the transform, but that should not be a problem.

(I would post histograms illustrating the procedure, but I do not have enough points.)

-