Chr*_*ser 26 python statistics confidence-interval python-2.7
我正在寻找一个Python函数(或编写我自己的函数,如果没有一个)来获取t统计量,以便在置信区间计算中使用.
我已经找到了表格,可以像这样给出各种概率/自由度的答案,但我希望能够针对任何给定的概率计算出这个.对于那些不熟悉这种自由度的人来说,样本-1中的数据点数(n)和顶部列标题的数字是概率(p),例如,如果使用0.05的双尾显着性水平,则使用0.05你正在查找用于计算95分置信度的t分数,如果你重复n次测试,结果将落在平均值+/-置信区间内.
我已经研究过在scipy.stats中使用各种函数,但是我看不到任何函数似乎允许我上面描述的简单输入.
Excel有一个简单的实现,例如获得1000的样本的t分数,我需要95%的信心我会使用:=TINV(0.05,999)得到分数~1.96
这是我到目前为止用于实现置信区间的代码,因为你可以看到我正在使用一种非常粗略的方法来获得目前的t分数(只是为perc_conf允许一些值并警告它不准确样本<1000):
# -*- coding: utf-8 -*-
from __future__ import division
import math
def mean(lst):
# ? = 1/N ?(xi)
return sum(lst) / float(len(lst))
def variance(lst):
"""
Uses standard variance formula (sum of each (data point - mean) squared)
all divided by number of data points
"""
# ?² = 1/N ?((xi-?)²)
mu = mean(lst)
return 1.0/len(lst) * sum([(i-mu)**2 for i in lst])
def conf_int(lst, perc_conf=95):
"""
Confidence interval - given a list of values compute the square root of
the variance of the list (v) divided by the number of entries (n)
multiplied by a constant factor of (c). This means that I can
be confident of a result +/- this amount from the mean.
The constant factor can be looked up from a table, for 95% confidence
on a reasonable size sample (>=500) 1.96 is used.
"""
if perc_conf == 95:
c = 1.96
elif perc_conf == 90:
c = 1.64
elif perc_conf == 99:
c = 2.58
else:
c = 1.96
print 'Only 90, 95 or 99 % are allowed for, using default 95%'
n, v = len(lst), variance(lst)
if n < 1000:
print 'WARNING: constant factor may not be accurate for n < ~1000'
return math.sqrt(v/n) * c
Run Code Online (Sandbox Code Playgroud)
以下是上述代码的示例调用:
# Example: 1000 coin tosses on a fair coin. What is the range that I can be 95%
# confident the result will f all within.
# list of 1000 perfectly distributed...
perc_conf_req = 95
n, p = 1000, 0.5 # sample_size, probability of heads for each coin
l = [0 for i in range(int(n*(1-p)))] + [1 for j in range(int(n*p))]
exp_heads = mean(l) * len(l)
c_int = conf_int(l, perc_conf_req)
print 'I can be '+str(perc_conf_req)+'% confident that the result of '+str(n)+ \
' coin flips will be within +/- '+str(round(c_int*100,2))+'% of '+\
str(int(exp_heads))
x = round(n*c_int,0)
print 'i.e. between '+str(int(exp_heads-x))+' and '+str(int(exp_heads+x))+\
' heads (assuming a probability of '+str(p)+' for each flip).'
Run Code Online (Sandbox Code Playgroud)
这个输出是:
我可以95%确信1000次硬币翻转的结果将在500的+/- 3.1%之内,即在469和531个头之间(假设每次翻转的概率为0.5).
我还研究了计算范围的t分布,然后返回得到最接近所需概率的t分数,但是我在实现公式时遇到了问题.让我知道这是否相关,你想看到代码,但我认为不是因为可能有一个更简单的方法.
提前致谢.
hen*_*rso 42
你试过scipy吗?
您将需要安装scipy库...有关在此处安装它的更多信息:http: //www.scipy.org/install.html
安装后,您可以复制Excel功能,如下所示:
from scipy import stats
#Studnt, n=999, p<0.05, 2-tail
#equivalent to Excel TINV(0.05,999)
print stats.t.ppf(1-0.025, 999)
#Studnt, n=999, p<0.05%, Single tail
#equivalent to Excel TINV(2*0.05,999)
print stats.t.ppf(1-0.05, 999)
Run Code Online (Sandbox Code Playgroud)
您还可以在这里阅读有关安装库的信息:如何为python安装scipy?
| 归档时间: |
|
| 查看次数: |
33549 次 |
| 最近记录: |