# Using Apache Commons T Distribution cumulative probability to find p given DF and t Value

#### johnjackson

##### New Member
[org.apache.commons.math3.distribution.TDistribution]

Hello, I am writing a program for statistics and am don't have a very in depth knowledge of p value; I understand what it is so maybe it is that I don't understand cumulative probability and how it gets me a T Value. I've only taken elementary statistics years ago. From the code I can draw conclusions but I need to make sure it's right first.

I need help understanding if these methods would work for all inputs. This is mainly just due diligence because of my lack of understanding of the subject. I had to work backwards by taking answers and comparing them with negative and positive t values and 0 to write the code below and finding that you could do this from some post somewhere (I'm cheating!).

Also, am I correct in assuming that for a Z test you would simply use normal distribution for this?

Code:
public double getPLessThan(double DF, double tValue) {
TDistribution t = new TDistribution(DF);
double a = (t.cumulativeProbability(tValue));
return a;
}

public double getPMoreThan(double DF, double tValue) {
TDistribution t = new TDistribution(DF);
double a = (1-t.cumulativeProbability(tValue));
return a;
}

public double getPTwoTailed(double DF, double tValue) {
if (tValue > 0) {
TDistribution t = new TDistribution(DF);
double a = (1-t.cumulativeProbability(tValue));
return a*2;
} else {
TDistribution t = new TDistribution(DF);
double a = (t.cumulativeProbability(tValue));
return a*2;
}
}

#### Dason

All of your code is correct. I would offer a few suggestions though. You already have the getPLessThan and getPMoreThan functions - why not utilize those in your getPTwoTailed function instead of duplicating the logic? You can also take the absolute value of the tValue and just do 2*getPMoreThan(abs(tValue)) so you don't need to use an if statement.

Also, am I correct in assuming that for a Z test you would simply use normal distribution for this?
That is correct. You don't need to worry about degrees of freedom for the Z-test either so it's a bit simpler.

#### johnjackson

##### New Member
Great! Thanks!

Here are the edits

Code:
    public double getPLessThan(double DF, double tValue) {
return new TDistribution(DF).cumulativeProbability(tValue);
}

public double getPMoreThan(double DF, double tValue) {
return 1 - new TDistribution(DF).cumulativeProbability(tValue);
}

public double getPTwoTailed(double DF, double tValue) {
return 2*getPMoreThan(DF,abs(tValue));
}