1
$\begingroup$

I am searching for a way to calculate the $z$-score for a given percentile $p$. I found this site doing such a calculation. They have given a formula for calculating $P(Z\leq z)$:

$$ \Phi(z) = P(Z \le z) = \int_{-\infty}^z \frac{1}{\sqrt{2\pi}}e^{\frac{-u^2}{2}}du $$

Now I have the value of $P(Z\leq z)$ and I tried solving it to find out $z$, but it was really complex to solve for $z$.

Is there any function in JAVA or PHP which can do this? Or is there any mathematical way in which we can solve for $z$? (I am considering hard-coding the $z$ table as my last choice because I would have to make some approximations in that case.)

EDIT

I forgot to mention this, the main problem I am trying to solve is to draw a random point from a normal distribution. I tried using inversecdf function of Normal Distribution. Due to the generalized implementation of that function, it is taking lot of time.

Say I want to calculate the point at $0.75$ we can do that in a quick way by finding $z$-value at that point followed by a simple math $\bar{x}+\sigma\times z$. So I wanted to find some way to get $z$-score for a given percentile.

$\endgroup$
  • $\begingroup$ Hi and welcome to the site! I think you want the inverse CDF or quantile function of the normal distribution. I've found this page which features the implementations in several languages (PHP and JAVA are among them). $\endgroup$ – COOLSerdash Oct 3 '13 at 11:31
  • $\begingroup$ @COOLSerdash Thank you for the reply, i forgot to mention one point. I mentioned it in the edit. $\endgroup$ – Karthik Oct 3 '13 at 11:45
  • 2
    $\begingroup$ See, for example, here or here $\endgroup$ – Glen_b -Reinstate Monica Oct 3 '13 at 12:03
5
$\begingroup$

The z-table contains the area to the left of the z-number under a normal distribution with $\mu = 0$ and $\sigma = 1$. Your p-value will generally represent one minus that area (for a one tailed test). Transforming this p-value into it's corresponding z-score should not be that hard under these circumstances.

Your normal distribution with $\mu = 0$ and $\sigma = 1$ is defined by the function

$$ f(z) = \frac{1}{\sqrt{2\pi}}e^{-\frac{z^2}{2}} $$

Your p-value is

$$ p = \int_z^\infty{\frac{1}{\sqrt{2\pi}}e^{-\frac{z^2}{2}}} $$

All you have to do is plug in your numbers and solve for $z$.

Note how the integral starts at $z$ and not at $-\infty$ because we're calculating $p$ which is one minus the area to the left of $z$ (This is true for one-tailed tests. For two-tailed tests divide $p$ by two before plugging it in the above formula).

Solving for $z$ yields:

$$ \frac{1}{2} \text{erfc}\left(\frac{z}{\sqrt{2}}\right) = p $$

$$ \text{erfc}\left(\frac{z}{\sqrt{2}}\right) = 2p $$

$$ \frac{z}{\sqrt{2}} = \text{erfc}^{-1}(2p) $$

$$ z = \sqrt{2} \cdot \text{erfc}^{-1}(2p) $$

Don't know much java but after reading these docs I suspect the code would be as easy as

double pToZ(double p) {
    double z = Math.sqrt(2) * Erf.erfcInv(2*p);
    return(z);
}

Note $\text{erfc()}$ stands for $1 - \text{erf()}$ where $\text{erf}$ is the error function.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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