【问题标题】:Goodness of fit always being zero despite taking random data?尽管采用随机数据,拟合优度始终为零?
【发布时间】:2019-06-14 17:39:13
【问题描述】:

我正在尝试编写生成随机数据并计算拟合优度的代码,但我不明白为什么卡方检验总是为零,我可以解决这个问题吗?对于尝试的修复,我尝试使用不同的类型来查看是否在初始输出中得到任何结果更改,我还尝试将参数更改为有问题的循环。

from scipy import stats
import math
import random
import numpy 
import scipy 
import numpy as np

def Linear_Chi2_Generate(observed_values = [], expected_values = []):
    #===============================================================#
    #            !!!!!!! Generation of Data  !!!!!!!!!!             #
    #===============================================================#

    for i in range(0,12):
        a = random.randint(-10,10)
        b = random.randint(-10,10)
        y = a * (b + i)
        observed_values.append(y)


    #######################################################################################
    #                               !!! Array Setup !!!!                                  #
    #              ***Had the Array types converted to floats before computing Chi2***    #
    #                                                                                     #
    #######################################################################################
    t_s = 0
    o_v = np.array(observed_values)
    e_v = np.array(expected_values)
    o_v_f = o_v.astype(float)
    e_v_f = o_v.astype(float)
    z_o_e_v_f = zip(o_v.astype(float), e_v.astype(float))

    ######################################################################################
    for i in z_o_e_v_f:
        t_s += [((o_v_f)-(e_v_f))]**2/(e_v_f) # Computs the Chi2 Stat !
    ######################################################################################
    print("Observed Values ", o_v_f)
    print("Expected Values" , e_v_f)
    df=len(o_v_f)-1
    print("Our goodness of fit for our linear function", stats.chi2.cdf(t_s,df))
    return t_s

Linear_Chi2_Generate()

【问题讨论】:

    标签: python-3.x chi-squared


    【解决方案1】:

    在您的原始代码中,e_v_f = o_v.astype(float) 使 o_v_fe_v_f 最终相同。 for 循环中也存在一些问题。我已经编辑了你的代码。看看你在找什么:

    from scipy import stats
    import math
    import random
    import numpy
    import scipy
    import numpy as np
    
    def Linear_Chi2_Generate(observed_values = [], expected_values = []):
        #===============================================================#
        #            !!!!!!! Generation of Data  !!!!!!!!!!             #
        #===============================================================#
    
        for i in range(0,12):
            a_o = random.randint(-10,10)
            b_o = random.randint(-10,10)
            y_o = a_o * (b_o + i)
            observed_values.append(y_o)
    
    #        a_e = random.randint(-10,10)
    #        b_e = random.randint(-10,10)
    #        y_e = a_e * (b_e + i)
            expected_values.append(y_o + 5)
    
    
        #######################################################################################
        #                               !!! Array Setup !!!!                                  #
        #              ***Had the Array types converted to floats before computing Chi2***    #
        #                                                                                     #
        #######################################################################################
        t_s = 0
        o_v = np.array(observed_values)
        e_v = np.array(expected_values)
        o_v_f = o_v.astype(float)
        e_v_f = e_v.astype(float)
        z_o_e_v_f = zip(o_v.astype(float), e_v.astype(float))
    
        ######################################################################################
        for o, e in z_o_e_v_f:
            t_s += (o - e) **2 / e  # Computs the Chi2 Stat !
        ######################################################################################
        print("Observed Values ", o_v_f)
        print("Expected Values" , e_v_f)
        df=len(o_v_f)-1
        print("Our goodness of fit for our linear function", stats.chi2.cdf(t_s,df))
        return t_s
    
    Linear_Chi2_Generate()
    
    

    【讨论】:

    • 不幸的是,我按照您的建议运行,拟合优度出现在多次运行 0.0、1.0 和最后是 NaN
    • @Zophikel,代码本身没有问题。结果不同的原因是因为我用来生成随机生成的期望值和观察值的方式。如果期望值中有一个值恰好为 0,则 t_s 将变为 inf。为了避免这些边缘情况,您可以将 expected_values 设为 observed_values + 一个常数。比如我把代码改成expected_values.append(y_o + 10)
    • Ahhh 好的我明白我注意到我创建的数组基本相同的事实,并将您的修复调整为我写的原始数据。我没想到 numpy 在随机数生成方面会很棘手
    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 2014-03-06
    • 1970-01-01
    • 2014-03-05
    • 2016-06-07
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多