Survival analysis in PyMC 3

孤者浪人 提交于 2019-11-30 14:15:34

This is a tricky port, and requires three new concepts:

  1. Use of the theano tensor
  2. Use of the DensityDist
  3. Passing a dict as observed

This code provides the equivalent model as the PyMC2 version you linked to above:

import pymc3 as pm
from pymc.examples import melanoma_data as data
import theano.tensor as t

times = data.t # not to be confused with the theano tensor t!
failure = (data.censored==0).astype(int)

with pm.Model() as model:

    beta0 = pm.Normal('beta0', mu=0.0, tau=0.0001)
    beta1 = pm.Normal('beta1', mu=0.0, tau=0.0001)
    lam = t.exp(beta0 + beta1*data.treat)

    def survival_like(failure, value):
        return t.sum(failure * t.log(lam) - lam * value)

    survive = pm.DensityDist('survive', survival_like,
                        observed={'failure': failure, 'value': times})

with model:

    start = pm.find_MAP()
    step = pm.NUTS(scaling=start)
    trace = pm.sample(10000, step=step, start=start)

pm.traceplot(trace);

Output as follows:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!