如何在pymc3中使用StudentT发布?

时间:2016-02-18 22:32:09

标签: pymc pymc3

我不确定这是一个问题还是错误报告。我在这里发布了一个GitHub要点:https://gist.github.com/jbwhit/a9012e04b0f48e582c22

我发现这个问题(pymc3: hierarchical model with multiple obsesrved variables)是我自己的分层模型的一个很好的起点,但是一旦我试图以任何实质性的方式修改它就会遇到困难。

首先,有效的模型和设置:

import numpy as np
import pymc3 as pm

n_individuals = 200
points_per_individual = 10
means = np.random.normal(30, 12, n_individuals)
observed = np.random.normal(means, 1, (points_per_individual, n_individuals))

model = pm.Model()
with model:
    hyper_mean = pm.Normal('hyper_mean', mu=0, sd=100)
    hyper_sigma = pm.HalfNormal('hyper_sigma', sd=3)

    means = pm.Normal('means', mu=hyper_mean, sd=hyper_sigma, shape=n_individuals)
    sigmas = pm.HalfNormal('sigmas', sd=100)

    ye = pm.Normal('ye', mu=means, sd=sigmas, observed=observed)

    trace = pm.sample(10000)

以上所有工作都符合预期(并且痕迹看起来很棒)。下一段代码进行了一次更改(交换了Normal的T分布):

model = pm.Model()
with model:
    hyper_mean = pm.Normal('hyper_mean', mu=0, sd=100)
    hyper_sigma = pm.HalfNormal('hyper_sigma', sd=3)

    ### Changed to a T distribution ###
    means = pm.StudentT('means', nu=hyper_mean, sd=hyper_sigma, shape=n_individuals)

    sigmas = pm.HalfNormal('sigmas', sd=100)

    ye = pm.Normal('ye', mu=means, sd=sigmas, observed=observed)

    trace = pm.sample(10000)

以下是输出:

Assigned NUTS to hyper_mean
Assigned NUTS to hyper_sigma_log
Assigned NUTS to means
Assigned NUTS to sigmas_log
---------------------------------------------------------------------------
PositiveDefiniteError                     Traceback (most recent call last)
<ipython-input-12-69f59e2f3d47> in <module>()
     18     ye = pm.Normal('ye', mu=means, sd=sigmas, observed=observed)
     19 
---> 20     trace = pm.sample(10000)

/Users/jonathan/miniconda2/envs/pymc3/lib/python3.5/site-packages/pymc3/sampling.py in sample(draws, step, start, trace, chain, njobs, tune, progressbar, model, random_seed)
    121     """
    122     model = modelcontext(model)
--> 123 
    124     step = assign_step_methods(model, step)
    125 

/Users/jonathan/miniconda2/envs/pymc3/lib/python3.5/site-packages/pymc3/sampling.py in assign_step_methods(model, step, methods)
     66             selected_steps[selected].append(var)
     67 
---> 68     # Instantiate all selected step methods
     69     steps += [s(vars=selected_steps[s]) for s in selected_steps if selected_steps[s]]
     70 

/Users/jonathan/miniconda2/envs/pymc3/lib/python3.5/site-packages/pymc3/sampling.py in <listcomp>(.0)
     66             selected_steps[selected].append(var)
     67 
---> 68     # Instantiate all selected step methods
     69     steps += [s(vars=selected_steps[s]) for s in selected_steps if selected_steps[s]]
     70 

/Users/jonathan/miniconda2/envs/pymc3/lib/python3.5/site-packages/pymc3/step_methods/nuts.py in __init__(self, vars, scaling, step_scale, is_cov, state, Emax, target_accept, gamma, k, t0, model, profile, **kwargs)
     76 
     77 
---> 78         self.potential = quad_potential(scaling, is_cov, as_cov=False)
     79 
     80         if state is None:

/Users/jonathan/miniconda2/envs/pymc3/lib/python3.5/site-packages/pymc3/step_methods/quadpotential.py in quad_potential(C, is_cov, as_cov)
     33         return QuadPotential_SparseInv(C)
     34 
---> 35     partial_check_positive_definite(C)
     36     if C.ndim == 1:
     37         if is_cov != as_cov:

/Users/jonathan/miniconda2/envs/pymc3/lib/python3.5/site-packages/pymc3/step_methods/quadpotential.py in partial_check_positive_definite(C)
     56     if len(i):
     57         raise PositiveDefiniteError(
---> 58             "Simple check failed. Diagonal contains negatives", i)
     59 
     60 

PositiveDefiniteError: Scaling is not positive definite. Simple check failed. Diagonal contains negatives. Check indexes [202]

有关如何使其发挥作用的任何建议吗?

2 个答案:

答案 0 :(得分:4)

尝试查找MAP估算并将其用作MCMC运行的起点:

start = pm.find_MAP()
trace = pm.sample(10000, start=start)

答案 1 :(得分:2)

正如我在评论中提到的,尝试运行:

model = pm.Model()
with model:
    hyper_mean = pm.Normal('hyper_mean', mu = 0, sd = 100)
    hyper_sigma = pm.HalfNormal('hyper_sigma', sd = 3)
    nu = pm.Exponential('nu', 1./10, testval = 5.)

    ### Changed to a T distribution ###
    means = pm.StudentT('means', nu = nu, mu = hyper_mean, sd = hyper_sigma, shape = n_individuals)

    sigmas = pm.HalfNormal('sigmas', sd = 100)

    ye = pm.Normal('ye', mu = means, sd = sigmas, observed = observed)

    trace = pm.sample(10000)

换句话说:使用mu方法的pm.StudentT参数hyper_meannu来获取自由度。

一旦开始工作,您可能还会尝试添加pm.find_MAP方法(由@Chris Fonnesbeck建议)。