Skip to content

Commit b4289bf

Browse files
committed
Add WishartBartlett example.
1 parent 0222671 commit b4289bf

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

pymc3/examples/wishart.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pymc3 as pm
2+
import numpy as np
3+
import theano
4+
import theano.tensor as T
5+
import scipy.stats
6+
import matplotlib.pyplot as plt
7+
8+
# Covariance matrix we want to recover
9+
covariance = np.matrix([[2, .5, -.5],
10+
[.5, 1., 0.],
11+
[-.5, 0., 0.5]])
12+
13+
prec = np.linalg.inv(covariance)
14+
15+
mean = [.5, 1, .2]
16+
data = scipy.stats.multivariate_normal(mean, covariance).rvs(5000)
17+
18+
plt.scatter(data[:, 0], data[:, 1])
19+
20+
with pm.Model() as model:
21+
S = np.eye(3)
22+
nu = 5
23+
mu = pm.Normal('mu', mu=0, sd=1, shape=3)
24+
25+
# Use the transformed Wishart distribution
26+
# Under the hood this will do a Cholesky decomposition
27+
# of S and add two RVs to the sampler: c and z
28+
prec = pm.WishartBartlett('prec', S, nu)
29+
30+
# To be able to compare it to truth, convert precision to covariance
31+
cov = pm.Deterministic('cov', T.nlinalg.matrix_inverse(prec))
32+
33+
lp = pm.MvNormal('likelihood', mu=mu, tau=prec, observed=data)
34+
35+
start = pm.find_MAP()
36+
step = pm.NUTS(scaling=start)
37+
trace = pm.sample(500, step)
38+
39+
pm.traceplot(trace[100:]);

0 commit comments

Comments
 (0)