1
votes

Well I'm programming a test in java for pseudoRandom numbers for simulation subjet, and I need to calculate inverse of Chi squared, so I have alpha and degrees as you can see here.

the book I'm reading, uses Excel function Excel ChiSQ.INV like this:

CHISQ.INV(probability,deg_freedom)

CHISQ.INV(0.025,39) = 58.12005973 <- THIS value is what i need to calculate

The thing is I'm using a table to calculate the chi squared, but I think there's some way to calculate with the computer, I found this Class but I still don't get how it works ChiSquaredDistribution Apache Commons

So the objetive is to calculate Chi Squared Inv with java or with a library for java.

ChiSquaredDistribution x2 = new ChiSquaredDistribution(1, 0.05);

System.out.println(x2.cumulativeProbability(1.96));
System.out.println(x2.getNumericalVariance());

output:

0.8384866815324579

2.0

1
Never used this class before, but if you need inverse Chi distribution, there is method ChiSquaredDistribution#inverseCumulativeProbability Inherited from interface ContinuousDistribution - Bedla
Thanks for answering, but could you show and example of code with the Class. - Palomino9r
The same way as you use x2.cumulativeProbability(1.96), you can use x2.inverseCumulativeProbability(1.96). I wrote it into comment only, because I am not sure, this solution is correct. - Bedla
Thanks so much, in order to calculate the value I asked, is 1-(alpha/2) = 1-(0.05/2) = 0.975 so in Java ChiSquaredDistribution x2 = new ChiSquaredDistribution(39); System.out.println( x2.inverseCumulativeProbability(0.975) ); - Palomino9r
Great. Feel free to post your research as answer, to make it visible to other people seeking for solution. - Bedla

1 Answers

2
votes

The problem is to calculate the chi squared function with 39 degrees of freedom and with 95% level of confidence, so alpha is 5%. as you can see here. This is the excel function :

CHISQ.INV(probability,deg_freedom)
CHISQ.INV(0.025,39) = 58.12005973

In order to calculate the value I asked in java, is 1-(alpha/2) = 1-(0.05/2) = 0.975

so in Java using Apache Commons Library:

ChiSquaredDistribution x2 = new ChiSquaredDistribution( degreesOfFreedom );
double result = x2.inverseCumulativeProbability(alpha);

ChiSquaredDistribution x2 = new ChiSquaredDistribution(39);
System.out.println( x2.inverseCumulativeProbability(0.975) );

Output:

58.12005973444771

the result seems almost the same as the excel function, may be the book is wrong (58.1200541) or is rounded.