构建自定义零膨胀泊松 pymc3 函数

时间:2021-05-27 06:14:20

标签: python-3.x pymc3

我想重现自定义构建的零膨胀泊松分布函数(作为编程准备以转移到不同的非预构建分布)。我被困在如何制定开关和组合分布:

import numpy as np
from matplotlib import pyplot as plt
import pymc3 as pm
from theano import tensor as tt
import arviz as az

# data
PROB_ZERO = 0.2
RATE = 1
N = 250
zero_event = np.where(np.random.random(N) < PROB_ZERO, 1., 0.)
y = (1 - zero_event) * np.random.poisson(RATE, N)

### working pre-build version
with pm.Model() as m1:
    logistic_prm = pm.Normal('logistic_prm', 0., 1.)
    logistic = pm.Deterministic('logistic', pm.math.sigmoid(logistic_prm))
    rate_prm = pm.Normal('rate_prm', 0., 10.)
    rate = pm.Deterministic('rate', tt.exp(rate_prm))
    y_obs = pm.ZeroInflatedPoisson('y_obs', 1. - logistic, rate, observed = y)
    trace_m1 = pm.sample(1000)

az.plot_trace(trace_m1, var_names = ['logistic', 'rate'])
#az.plot_energy(trace_m1)
plt.show()

问题由此开始:

### not working custom-build version
with pm.Model() as m2:
    logistic_prm = pm.Normal('logistic_prm', 0., 1.)
    logistic = pm.Deterministic('logistic', pm.math.sigmoid(logistic_prm))
    rate_prm = pm.Normal('rate_prm', 0., 10.)
    rate = pm.Deterministic('rate', tt.exp(rate_prm))
    #
    rate_zero_inflated = tt.switch(         ) # how to do the switch?
    observation = # which distribution function to choose?

如何运行第二个模型?

0 个答案:

没有答案