student t confidence interval in python

后端 未结 1 1408
刺人心
刺人心 2021-01-02 10:16

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

1条回答
  •  囚心锁ツ
    2021-01-02 10:40

    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)
    

    0 讨论(0)
提交回复
热议问题