pymc3中的移位Beta几何(sBG)分布不起作用

时间:2017-10-12 18:29:47

标签: python theano pymc3

我是贝叶斯建模的新手,也是Python的新手。我试图像java那样模拟移位的β几何分布(sBG)。 该模型基于Fader和Hardie发表的论文。 问题是我无法接近与Daniel相同的结果,这与本文中的结果非常相似。他使用pymc和我使用pymc3(使用DensityDist函数)。我的beta后验分布增长到我设定的更高边界。 我可能会在模型环境中看到一些内容,但无法弄明白。任何帮助将不胜感激!

%matplotlib inline
import pymc3 as pm
import numpy as np
import scipy.stats as stats
from scipy import optimize
import matplotlib.pyplot as plt
import pymc3 as pm
import theano.tensor as tt
from theano.tensor import _shared
example_data = np.array([1000, 869, 743, 653, 593, 551, 517, 491])
def n_lost(data):
    lost = [None]
    for i in range(1, len(data)):
        lost.append(data[i - 1] - data[i])
    return lost

example_data_n_lost = n_lost(example_data)
n = len(example_data)
data = np.asarray((example_data, example_data_n_lost))
with pm.Model() as model:
    alpha = pm.Uniform('alpha', 0.00001, 1000.0, testval=1)
    beta = pm.Uniform('beta', 0.00001, 1000.0, testval=1)

    def P_T_is_t(alpha=alpha, beta=beta, num_periods=n):
        p = np.array([None, alpha / (alpha + beta)])
        for t in range(2, num_periods):
            pt = ((beta + t - 2) / (alpha + beta + t - 1)) * p[t-1]
            p = np.append(p,pt)
        return p

    def survival_function(num_periods=n):
        p = P_T_is_t()
        s = np.array([None, 1 - p[1]])
        for t in range(2, num_periods):
            s = np.append(s, s[t-1] - p[t])
        return s

    def logp(value):

        active = value[0,:]
        lost = value[1,:]

        # Those who've churned along the way...
        p = P_T_is_t()
        died = np.multiply(np.log(p[1:]), lost[1:]) 


        # and those still active in last period
        sf = survival_function()
        still_active = np.log(sf[-1]) * active[-1]
        return  sum(died) + still_active


    retention = pm.DensityDist('retention', logp, observed=data)
    trace = pm.sample(10000, step=pm.Metropolis())
    burned_trace = trace[3000:]

burned_trace['alpha'].mean()
burned_trace['beta'].mean()

0 个答案:

没有答案