Finding Two-Tailed P Value from t-distribution and Degrees of Freedom in Python

hoshi picture hoshi · May 27, 2014 · Viewed 12.6k times · Source

How do I determine the P Value of a t-distrobution with n degrees of freedom.

Research on this subject points me to this stack exchange answer: https://stackoverflow.com/a/17604216

I assume np.abs(tt) is the T-value, but how do i work in degrees of freedom, is that the n-1?

Thanks in advance

Answer

Warren Weckesser picture Warren Weckesser · May 27, 2014

Yes, n-1 is the degrees of freedom in that example.

Given a t-value and a degrees of freedom, you can use the "survival function" sf of scipy.stats.t (aka the complementary CDF) to compute the one-sided p-value. The first argument is the T value, and the second is the degrees of freedom.

For example, the first entry of the table on this page says that for 1 degree of freedom, the critical T value for p=0.1 is 3.078. Here's how you can verify that with t.sf:

In [7]: from scipy.stats import t

In [8]: t.sf(3.078, 1)
Out[8]: 0.09999038172554342   # Approximately 0.1, as expected.

For the two-sided p-value, just double the one-sided p-value.