Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

student t confidence interval in python

I am interested in using python to compute a confidence interval from a student t.

I am using the StudentTCI() function in Mathematica and now need to code the same function in python http://reference.wolfram.com/mathematica/HypothesisTesting/ref/StudentTCI.html

I am not quite sure how to build this function myself, but before I embark on that, is this function in python somewhere? Like numpy? (I haven't used numpy and my advisor advised not using numpy if possible).

What would be the easiest way to solve this problem? Can I copy the source code from the StudentTCI() in numpy (if it exists) into my code as a function definition?

edit: I'm going to need to build the Student TCI using python code (if possible). Installing scipy has turned into a dead end. I am having the same problem everyone else is having, and there is no way I can require Scipy for the code I distribute if it takes this long to set up.

Anyone know how to look at the source code for the algorithm in the scipy version? I'm thinking I'll refactor it into a python definition.

like image 397
SwimBikeRun Avatar asked Jun 20 '13 00:06

SwimBikeRun


People also ask

How do you find the T interval in Python?

This approach is used to calculate confidence Intervals for the small dataset where the n<=30 and for this, the user needs to call the t. interval() function from the scipy. stats library to get the confidence interval for a population means of the given dataset in python.

How do you create a 95 confidence interval in Python?

Confidence Intervals with T-statistic So, now you wish to construct a 95% confidence interval. So, we have sample std deviation, the number of samples, and sample mean. All we need is t*. So, t* for a 95% confidence interval with a degree of freedom of 19(n-1 = 20-1) is 2.093.

What is the T value for a 95% confidence level?

The sample size is n=10, the degrees of freedom (df) = n-1 = 9. The t value for 95% confidence with df = 9 is t = 2.262.


1 Answers

I guess you could use scipy.stats.t and it's interval method:

In [1]: from scipy.stats import t
In [2]: t.interval(0.95, 10, loc=1, scale=2)  # 95% confidence interval
Out[2]: (-3.4562777039298762, 5.4562777039298762)
In [3]: t.interval(0.99, 10, loc=1, scale=2)  # 99% confidence interval
Out[3]: (-5.338545334351676, 7.338545334351676)

Sure, you can make your own function if you like. Let's make it look like in Mathematica:

from scipy.stats import t


def StudentTCI(loc, scale, df, alpha=0.95):
    return t.interval(alpha, df, loc, scale)

print StudentTCI(1, 2, 10)
print StudentTCI(1, 2, 10, 0.99)

Result:

(-3.4562777039298762, 5.4562777039298762)
(-5.338545334351676, 7.338545334351676)
like image 134
abudis Avatar answered Sep 28 '22 05:09

abudis