Skip to content

Instantly share code, notes, and snippets.

@sebp
Last active August 14, 2019 15:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebp/3644e2e9cc3050a5b439d56dd8ccce7d to your computer and use it in GitHub Desktop.
Save sebp/3644e2e9cc3050a5b439d56dd8ccce7d to your computer and use it in GitHub Desktop.
Bayesian correlation coefficient using PyMC3
from theano.printing import Print
import pymc3 as pm
import numpy as np
import theano.tensor as T
def covariance(sigma, rho):
C = T.fill_diagonal(T.alloc(rho, 2, 2), 1.)
S = T.diag(sigma)
M = S.dot(C).dot(S)
return M
def analyze_standard(data):
with pm.Model() as model:
# priors
sigma = pm.Uniform('sigma', lower=0, upper=1000, shape=2,
testval=[0.133434, 5.9304], # init with mad
transform=None)
rho = pm.Uniform('r', lower=-1, upper=1,
testval=-0.2144021, # init with Spearman's correlation
transform=None)
# print values for debugging
rho_p = Print('rho')(rho)
sigma_p = Print('sigma')(sigma)
cov = pm.Deterministic('cov', covariance(sigma_p, rho_p))
cov_p = Print('cov')(cov)
mult_norm = pm.MvNormal('mult_norm', mu=[10.1, 79.], # set mu to median
cov=cov_p, observed=data.T)
return model
def analyze_robust(data):
with pm.Model() as model:
# priors
mu = pm.Normal('mu', mu=0., tau=0.000001, shape=2,
testval=np.array([10.1, 79.])) # set mu to median
sigma = pm.Uniform('sigma', lower=0, upper=1000, shape=2,
testval=np.array([0.133434, 5.9304]), # init with mad
transform='interval')
rho = pm.Uniform('r', lower=-1, upper=1,
testval=-0.2144021, # init with Spearman's correlation
transform=None)
# print values for debugging
rho_p = Print('rho')(rho)
sigma_p = Print('sigma')(sigma)
cov = pm.Deterministic('cov', covariance(sigma_p, rho_p))
num = pm.Exponential('nu_minus_one', lam=1. / 29., testval=1)
nu = pm.Deterministic('nu', num + 1)
cov_p = Print('cov')(cov)
nu_p = Print('nu')(nu)
mult_norm = pm.MvStudentT('mult_norm', nu=nu_p, mu=mu,
Sigma=cov_p, observed=data.T)
return model
s = [9.92, 94, 9.94, 79, 9.97, 78, 9.93, 83, 9.90, 77, 9.93, 76, 10.00, 74, 9.97, 87, 10.00, 86, 10.01,
83, 10.08, 75, 10.09, 74, 10.15, 92, 10.15, 69, 10.17, 79, 10.17, 71, 10.19, 80, 10.30, 80, 10.31,
77, 10.34, 87]
x = [float(a) for i, a in enumerate(s) if i % 2 == 0]
y = [float(a) for i, a in enumerate(s) if i % 2 != 0]
data = np.array([x, y])
x.append(9.5)
y.append(115.)
robust_model = analyze_standard(data)
with robust_model:
step = pm.Metropolis()
robust_trace = pm.sample(500, tune=250, step=step, random_seed=21412, progressbar=False)
print(pm.summary(robust_trace))
@sebp
Copy link
Author

sebp commented Nov 30, 2016

Follow-up on pymc-devs/pymc#1501

Using pymc-devs/pymc@8a6d87e gives the following error:

rho __str__ = -0.2144021
sigma __str__ = [ 0.133434  5.9304  ]
cov __str__ = [[  1.78046324e-02  -1.69660025e-01]
 [ -1.69660025e-01   3.51696442e+01]]
sigma __str__ = [ 0.133434  5.9304  ]
rho __str__ = -0.2144021
sigma __str__ = [ 0.133434  5.9304  ]
rho __str__ = -0.2144021
rho __str__ = -0.6753367200324893
cov __str__ = [[  1.78046324e-02  -1.69660025e-01]
 [ -1.69660025e-01   3.51696442e+01]]
cov __str__ = [[  1.78046324e-02  -5.34405423e-01]
 [ -5.34405423e-01   3.51696442e+01]]
rho __str__ = -0.2144021
sigma __str__ = [ 0.133434  5.9304  ]
sigma __str__ = [-0.08992942  6.58322907]
cov __str__ = [[  1.78046324e-02  -1.69660025e-01]
 [ -1.69660025e-01   3.51696442e+01]]
cov __str__ = [[  8.08730093e-03   1.26931614e-01]
 [  1.26931614e-01   4.33389049e+01]]
sigma __str__ = [ 0.133434  5.9304  ]
rho __str__ = -0.2144021
sigma __str__ = [ 0.133434  5.9304  ]
rho __str__ = -0.2144021
rho __str__ = -1.3866936871442477
cov __str__ = [[  1.78046324e-02  -1.69660025e-01]
 [ -1.69660025e-01   3.51696442e+01]]
cov __str__ = [[  1.78046324e-02  -1.09731428e+00]
 [ -1.09731428e+00   3.51696442e+01]]
Traceback (most recent call last):
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/theano/gof/link.py", line 1012, in f
    wrapper(i, node, *thunks)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/theano/compile/nanguardmode.py", line 307, in nan_check
    do_check_on(x[0], node, fn, False)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/theano/compile/nanguardmode.py", line 272, in do_check_on
    raise AssertionError(msg)
AssertionError: NaN detected
NanGuardMode found an error in the output of a node in this variable:
Elemwise{log,no_inplace} [id A] ''   
 |Elemwise{true_div,no_inplace} [id B] ''   
   |TensorConstant{1.0} [id C]
   |Det [id D] ''   
     |MatrixInverse [id E] ''   
       |Print{message='cov', attrs=('__str__',), global_fn=<function _print_fn at 0x7fb7c8926488>} [id F] ''   
         |Dot22 [id G] 'cov'   
           |Dot22 [id H] ''   
           | |Diag [id I] ''   
           | | |Print{message='sigma', attrs=('__str__',), global_fn=<function _print_fn at 0x7fb7c8926488>} [id J] ''   
           | |   |sigma_shared [id K]
           | |FillDiagonal [id L] ''   
           |   |Alloc [id M] ''   
           |   | |Print{message='rho', attrs=('__str__',), global_fn=<function _print_fn at 0x7fb7c8926488>} [id N] ''   
           |   | | |Subtensor{int64} [id O] ''   
           |   | |   |Subtensor{int64:int64:} [id P] ''   
           |   | |   | |inarray1 [id Q]
           |   | |   | |Constant{0} [id R]
           |   | |   | |Constant{1} [id S]
           |   | |   |Constant{0} [id R]
           |   | |TensorConstant{2} [id T]
           |   | |TensorConstant{2} [id T]
           |   |TensorConstant{1.0} [id U]
           |Diag [id I] ''   



During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "robust_cor.py", line 78, in <module>
    robust_trace = pm.sample(500, tune=250, step=step, random_seed=21412, progressbar=False)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/sampling.py", line 175, in sample
    return sample_func(**sample_args)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/sampling.py", line 185, in _sample
    for strace in sampling:
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/sampling.py", line 267, in _iter_sample
    point = step.step(point)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/step_methods/compound.py", line 16, in step
    point = method.step(point)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/step_methods/arraystep.py", line 142, in step
    apoint = self.astep(bij.map(point))
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/step_methods/metropolis.py", line 124, in astep
    q_new = metrop_select(self.delta_logp(q, q0), q, q0)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/theano/compile/function_module.py", line 859, in __call__
    outputs = self.fn()
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/theano/gof/link.py", line 1014, in f
    raise_with_op(node, *thunks)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/theano/gof/link.py", line 314, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/theano/gof/link.py", line 1012, in f
    wrapper(i, node, *thunks)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/theano/compile/nanguardmode.py", line 307, in nan_check
    do_check_on(x[0], node, fn, False)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/theano/compile/nanguardmode.py", line 272, in do_check_on
    raise AssertionError(msg)
AssertionError: NaN detected
NanGuardMode found an error in the output of a node in this variable:
Elemwise{log,no_inplace} [id A] ''   
 |Elemwise{true_div,no_inplace} [id B] ''   
   |TensorConstant{1.0} [id C]
   |Det [id D] ''   
     |MatrixInverse [id E] ''   
       |Print{message='cov', attrs=('__str__',), global_fn=<function _print_fn at 0x7fb7c8926488>} [id F] ''   
         |Dot22 [id G] 'cov'   
           |Dot22 [id H] ''   
           | |Diag [id I] ''   
           | | |Print{message='sigma', attrs=('__str__',), global_fn=<function _print_fn at 0x7fb7c8926488>} [id J] ''   
           | |   |sigma_shared [id K]
           | |FillDiagonal [id L] ''   
           |   |Alloc [id M] ''   
           |   | |Print{message='rho', attrs=('__str__',), global_fn=<function _print_fn at 0x7fb7c8926488>} [id N] ''   
           |   | | |Subtensor{int64} [id O] ''   
           |   | |   |Subtensor{int64:int64:} [id P] ''   
           |   | |   | |inarray1 [id Q]
           |   | |   | |Constant{0} [id R]
           |   | |   | |Constant{1} [id S]
           |   | |   |Constant{0} [id R]
           |   | |TensorConstant{2} [id T]
           |   | |TensorConstant{2} [id T]
           |   |TensorConstant{1.0} [id U]
           |Diag [id I] ''   


Apply node that caused the error: Elemwise{log,no_inplace}(Elemwise{true_div,no_inplace}.0)
Toposort index: 59
Inputs types: [TensorType(float64, scalar)]
Inputs shapes: [()]
Inputs strides: [()]
Inputs values: [array(-0.5779160437553532)]
Outputs clients: [[Elemwise{add,no_inplace}(Elemwise{mul,no_inplace}.0, Elemwise{log,no_inplace}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "robust_cor.py", line 75, in <module>
    robust_model = analyze_standard(data)
  File "robust_cor.py", line 32, in analyze_standard
    cov=cov_p, observed=data.T)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/distributions/distribution.py", line 31, in __new__
    return model.Var(name, dist, data)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/model.py", line 299, in Var
    distribution=dist, model=self)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/model.py", line 582, in __init__
    self.logp_elemwiset = distribution.logp(data)
  File "/home/sebp/miniconda3/envs/pymc3-master/lib/python3.5/site-packages/pymc3/distributions/multivariate.py", line 117, in logp
    result = k * tt.log(2 * np.pi) + tt.log(1. / det(tau))

Debugprint of the apply node: 
Elemwise{log,no_inplace} [id A] <TensorType(float64, scalar)> ''   
 |Elemwise{true_div,no_inplace} [id B] <TensorType(float64, scalar)> ''   
   |TensorConstant{1.0} [id C] <TensorType(float32, scalar)>
   |Det [id D] <TensorType(float64, scalar)> ''   
     |MatrixInverse [id E] <TensorType(float64, matrix)> ''   
       |Print{message='cov', attrs=('__str__',), global_fn=<function _print_fn at 0x7fb7c8926488>} [id F] <TensorType(float64, matrix)> ''   
         |Dot22 [id G] <TensorType(float64, matrix)> 'cov' 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment