简体   繁体   English

卡方检验带误差线的数据

[英]Chi-squared test for data with error bars

Suppose I have two arrays, A1 and A2 : 假设我有两个数组A1A2

A1 = [1,2,2,2,3,3,7]
A2 = [0.5,1,1.5,2,7,2,6.9]

Suppose I also have the error bar data for A1 : 假设我还有A1的错误栏数据:

err_bars = [0.1,0.6,0.9,0.2,0.01,0.8,0.1]

I want to see if there is a way to determine if the data in A2 fits with that in A1 . 我想看看是否有一种方法可以确定A2的数据是否适合A1

Is there any way to calculate the Chi-squared, F-test and p -value considering the error bars in the data? 考虑到数据中的误差线,有什么方法可以计算卡方,F检验和p值?

So far I could not find any Chi-squared function in Python that considers the error bars of the data. 到目前为止,我在Python中找不到任何考虑数据误差线的卡方函数。

Try this: 尝试这个:

   # obs -- observed value
    # exp -- expected value

def chisqr(obs, exp, error):
    chisqr = 0
    for i in range(len(obs)):
        chisqr = chisqr + ((obs[i]-exp[i])**2)/(error[i]**2)
    return chisqr

A1 = [1,2,2,2,3,3,7]
A2 = [0.5,1,1.5,2,7,2,6.9]
err_bars = [0.1,0.6,0.9,0.2,0.01,0.8,0.1]
print chisqr(A1, A2, err_bars)

If your working with numerical data, chances are you should be using the numpy package to do most of your calculations. 如果您使用数字数据,则很可能应该使用numpy软件包进行大多数计算。 For anything more than a small calculation its significantly faster, and its almost always easier to read (A good summery why is here ). 除了简单的计算之外,它的速度明显更快,并且几乎总是更容易阅读( 这里有一个很好的总结)。 Switching to to numpy lets you write this as 切换到numpy可以将其写为

A1 = np.array([1, 2, 2, 2, 3, 3, 7])
A2 = np.array([0.5, 1, 1.5, 2, 7, 2, 6.9])
err_bars = np.array([0.1, 0.6, 0.9, 0.2, 0.01, 0.8, 0.1])

def chisq(obs, exp, error):
    return np.sum((obs - exp) ** 2 / (error ** 2))

print(chisq(A1, A2, err_bars))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM