擅长:python、mysql、java
<p>感谢Fonnesbeck在github issue tracker上回答此问题:</p>
<p><a href="https://github.com/pymc-devs/pymc3/issues/452" rel="nofollow">https://github.com/pymc-devs/pymc3/issues/452</a></p>
<p>以下是更新后的代码:</p>
<pre><code>with pm.Model() as model:
#priors
p = pm.Uniform( "p", 0 , 1) #this is the fraction that come from mean1 vs mean2
ber = pm.Bernoulli( "ber", p = p, shape=len(data)) # produces 1 with proportion p.
sigma = pm.Uniform('sigma', 0, 100)
precision = sigma**-2
mean = pm.Normal( "mean", 0, 0.01, shape=2 )
mu = pm.Deterministic('mu', mean[ber])
process = pm.Normal('process', mu=mu, tau=precision, observed=data)
with model:
step1 = pm.Metropolis([p, sigma, mean])
step2 = pm.BinaryMetropolis([ber])
trace = pm.sample(10000, [step1, step2])
</code></pre>
<p>在推断Bernoulli随机变量时,需要使用binarymetropoli</p>