如何在 PyMC3 中正确定义 Beta 分布的混合
How to correctly defined mixture of Beta distributions in PyMC3
我正在尝试使用来自 PyMC3 的 Mixture
混合使用两个 Beta 分布(我不知道每个分布的权重)来拟合数据。这是代码:
model=pm.Model()
with model:
alpha1=pm.Uniform("alpha1",lower=0,upper=20)
beta1=pm.Uniform("beta1",lower=0,upper=20)
alpha2=pm.Uniform("alpha2",lower=0,upper=20)
beta2=pm.Uniform("beta2",lower=0,upper=20)
w=pm.Uniform("w",lower=0,upper=1)
b1=pm.Beta("B1",alpha=alpha1,beta=beta1)
b2=pm.Beta("B2",alpha=alpha2,beta=beta2)
mix=pm.Mixture("mix",w=[1.0,w],comp_dists=[b1,b2])
在 运行 这段代码之后,我得到以下错误:AttributeError: 'list' object has no attribute 'mean'
。有什么建议吗?
PyMC3 自带 pymc3.tests
module which contains useful examples. By searching that directory for the word "mixture" I came upon this example:
Mixture('x_obs', w,
[Normal.dist(mu[0], tau=tau[0]), Normal.dist(mu[1], tau=tau[1])],
observed=self.norm_x)
请注意 classmethod dist
is called. Googling "pymc3 dist classmethod" leads to this doc page 解释
... each Distribution
has a dist
class method that returns a stripped-down distribution object that can be used outside of a PyMC model.
除此之外,我不完全清楚为什么这里需要精简版,但它似乎有效:
import pymc3 as pm
model = pm.Model()
with model:
alpha1 = pm.Uniform("alpha1", lower=0, upper=20)
beta1 = pm.Uniform("beta1", lower=0, upper=20)
alpha2 = pm.Uniform("alpha2", lower=0, upper=20)
beta2 = pm.Uniform("beta2", lower=0, upper=20)
w = pm.Uniform("w", lower=0, upper=1)
b1 = pm.Beta.dist(alpha=alpha1, beta=beta1)
b2 = pm.Beta.dist(alpha=alpha2, beta=beta2)
mix = pm.Mixture("mix", w=[1.0, w], comp_dists=[b1, b2])
注意使用dist
类方法时,名称字符串被省略。
我正在尝试使用来自 PyMC3 的 Mixture
混合使用两个 Beta 分布(我不知道每个分布的权重)来拟合数据。这是代码:
model=pm.Model()
with model:
alpha1=pm.Uniform("alpha1",lower=0,upper=20)
beta1=pm.Uniform("beta1",lower=0,upper=20)
alpha2=pm.Uniform("alpha2",lower=0,upper=20)
beta2=pm.Uniform("beta2",lower=0,upper=20)
w=pm.Uniform("w",lower=0,upper=1)
b1=pm.Beta("B1",alpha=alpha1,beta=beta1)
b2=pm.Beta("B2",alpha=alpha2,beta=beta2)
mix=pm.Mixture("mix",w=[1.0,w],comp_dists=[b1,b2])
在 运行 这段代码之后,我得到以下错误:AttributeError: 'list' object has no attribute 'mean'
。有什么建议吗?
PyMC3 自带 pymc3.tests
module which contains useful examples. By searching that directory for the word "mixture" I came upon this example:
Mixture('x_obs', w,
[Normal.dist(mu[0], tau=tau[0]), Normal.dist(mu[1], tau=tau[1])],
observed=self.norm_x)
请注意 classmethod dist
is called. Googling "pymc3 dist classmethod" leads to this doc page 解释
... each
Distribution
has adist
class method that returns a stripped-down distribution object that can be used outside of a PyMC model.
除此之外,我不完全清楚为什么这里需要精简版,但它似乎有效:
import pymc3 as pm
model = pm.Model()
with model:
alpha1 = pm.Uniform("alpha1", lower=0, upper=20)
beta1 = pm.Uniform("beta1", lower=0, upper=20)
alpha2 = pm.Uniform("alpha2", lower=0, upper=20)
beta2 = pm.Uniform("beta2", lower=0, upper=20)
w = pm.Uniform("w", lower=0, upper=1)
b1 = pm.Beta.dist(alpha=alpha1, beta=beta1)
b2 = pm.Beta.dist(alpha=alpha2, beta=beta2)
mix = pm.Mixture("mix", w=[1.0, w], comp_dists=[b1, b2])
注意使用dist
类方法时,名称字符串被省略。