学生在python中的置信区间

Swi*_*Run 9 python numpy distribution

我有兴趣使用python来计算学生t的置信区间.

我在Mathematica中使用StudentTCI()函数,现在需要在python中编写相同的函数http://reference.wolfram.com/mathematica/HypothesisTesting/ref/StudentTCI.html

我不太确定如何自己构建这个函数,但在我开始之前,这个函数是在python的某个地方吗?喜欢numpy?(我没有使用numpy,我的顾问建议如果可能的话不使用numpy).

解决这个问题最简单的方法是什么?我可以将源代码从numpy中的StudentTCI()(如果存在)复制到我的代码中作为函数定义吗?

编辑:我将需要使用python代码构建Student TCI(如果可能).安装scipy已经变成了死胡同.我遇到了其他人都遇到的同样问题,如果需要很长时间才能设置,我就无法为我分发的代码提供Scipy.

任何人都知道如何在scipy版本中查看算法的源代码?我想我会把它重构为python定义.

abu*_*dis 14

我想你可以使用scipy.stats.t和它的interval方法:

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)
Run Code Online (Sandbox Code Playgroud)

当然,如果你愿意,你可以自己动手.让我们看起来像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)
Run Code Online (Sandbox Code Playgroud)

结果:

(-3.4562777039298762, 5.4562777039298762)
(-5.338545334351676, 7.338545334351676)
Run Code Online (Sandbox Code Playgroud)

  • @SwimBikeRun,loc是位置参数或平均值μ.在这种情况下,比例是方差σ.如果你不想使用`SciPy`,你可以找到发行版的源代码及其方法[这里](https://github.com/scipy/scipy/blob/master/scipy/stats/distributions. PY).然而,"SciPy"依赖于"NumPy",因此您的用户必须安装它. (2认同)