初始能量差:inf。使用pymc3中的基本线性回归模型可能会错误地指定模型

时间:2018-04-17 00:07:08

标签: python-3.x pymc3

我正在尝试在pymc3中构建一个贝叶斯推理模型,我收到以下错误:

data = [[24, 38.7], [25, 38.6], [26, 38.9], [27, 41.4], [28, 39.7], [29, 41.1], [30, 38.7], [31, 37.6],
        [32, 36.3], [33, 36.9], [34, 35.7], [35, 33.8], [36, 33.2], [37, 30.1], [38, 27.8], [39, 22.8],
        [40, 21.4], [41, 15.4], [42, 11.2], [43, 9.2], [44, 5.4], [45, 3.0], [46, 1.6]]


data = np.array(data)

x = data[:, 0]
y = data[:, 1]
plt.scatter(x, y, color="red")

with pm.Model() as change_point_model:

    switchpoint = pm.DiscreteUniform('switchpoint', lower=x.min(), upper=x.max())

    beta0 = pm.Normal('beta0', mu=40, sd=10)
    beta1 = pm.Normal('beta1', mu=90, sd=10)

    gamma0 = pm.Normal('gamma0', mu=0, sd=5)
    gamma1 = pm.Normal('gamma1', mu=0, sd=5)

    epsilon = pm.Normal('epsilon', mu=0, sd=1)

    intercept = pm.math.switch(switchpoint <= x, beta0, gamma0)
    x_coeff = pm.math.switch(switchpoint <= x, beta1, gamma1)

    y_pred = pm.Normal('y_pred', mu=intercept + x_coeff * x, sd=epsilon, observed=y)

    step1 = pm.NUTS([beta0, beta1, gamma0, gamma1])
    step2 = pm.Metropolis([switchpoint])

    # In this example we are deliberativelly choosing the metropolis sampler
    trace = pm.sample(2000, step=[step1, step2], progressbar=True)

pm.traceplot(trace[100:])

我得到的错误如下:

ValueError: Bad initial energy: inf. The model might be misspecified.

因此,在做了一些阅读后,我发现model.logp(model.test_point)正在返回-inf。因此,我该如何解决这个错误。非常感谢任何帮助!!

1 个答案:

答案 0 :(得分:3)

您正在使用法线建模正态分布的标准偏差。该测试点为0.0,其发生概率为0。

如果您将epsilon更改为Gamma('epsilon', alpha=2.0, beta=0.5)或类似,那么您应该没问题。