# How to calculate Chi-Square density value only known P-value?

Everywhere online there is how to calculate the Chi-Square density value given a confidence level: $$\alpha$$/p value; but I can not find how one calculates the inverse? How to calculate the $$\alpha$$/p-value knowing only the density value?

For example, if df = 4, and $$\alpha$$=0.95, then how does one calculate the $$\chi^2_4$$?

In R this would be qchisq(0.95, 4)= 9.4877. What is the math behind this?

Given the degrees of freedom and an alpha value, qchisq essetnialy finds the point $$x$$ such that $$F(x)=\alpha$$, where $$F$$ is the CDF.
This is known as the $$\alpha^{th}$$ quantile. The q in the q* functions stands for quantile, and so this class of R functions are the quantile functions for teir respective distributions. You feed them a quantile, $$q$$, then return the $$x$$ such that $$F(x)=q$$.
$$F(x)-q=0$$
for $$x$$ which should yield the correct quantile with sufficiently many iterations.
• One way would be to write down the CDF and then invert it algebraically. That isn't likely possible with the chi-square density, so the other option is to use root finding methods to solve $F(x)-q=$ for $x$. Finally, you can actually see how qchisq is implemented by looking up the source code (qchisq calls the method for the quantile function for the gamma distirbution. Find the code here) – Demetri Pananos May 22 at 21:44