@@ -230,34 +230,36 @@ class MvNormal(Continuous):
230
230
Define a multivariate normal variable for a given covariance
231
231
matrix::
232
232
233
- cov = np.array([[1., 0.5], [0.5, 2]])
233
+ cov = np.array([[1.0 , 0.5], [0.5, 2]])
234
234
mu = np.zeros(2)
235
- vals = pm.MvNormal(' vals' , mu=mu, cov=cov, shape=(5, 2))
235
+ vals = pm.MvNormal(" vals" , mu=mu, cov=cov, shape=(5, 2))
236
236
237
237
Most of the time it is preferable to specify the cholesky
238
238
factor of the covariance instead. For example, we could
239
239
fit a multivariate outcome like this (see the docstring
240
240
of `LKJCholeskyCov` for more information about this)::
241
241
242
242
mu = np.zeros(3)
243
- true_cov = np.array([[1.0, 0.5, 0.1],
244
- [0.5, 2.0, 0.2],
245
- [0.1, 0.2, 1.0]])
243
+ true_cov = np.array(
244
+ [
245
+ [1.0, 0.5, 0.1],
246
+ [0.5, 2.0, 0.2],
247
+ [0.1, 0.2, 1.0],
248
+ ],
249
+ )
246
250
data = np.random.multivariate_normal(mu, true_cov, 10)
247
251
248
252
sd_dist = pm.Exponential.dist(1.0, shape=3)
249
- chol, corr, stds = pm.LKJCholeskyCov('chol_cov', n=3, eta=2,
250
- sd_dist=sd_dist, compute_corr=True)
251
- vals = pm.MvNormal('vals', mu=mu, chol=chol, observed=data)
253
+ chol, corr, stds = pm.LKJCholeskyCov("chol_cov", n=3, eta=2, sd_dist=sd_dist, compute_corr=True)
254
+ vals = pm.MvNormal("vals", mu=mu, chol=chol, observed=data)
252
255
253
256
For unobserved values it can be better to use a non-centered
254
257
parametrization::
255
258
256
259
sd_dist = pm.Exponential.dist(1.0, shape=3)
257
- chol, _, _ = pm.LKJCholeskyCov('chol_cov', n=3, eta=2,
258
- sd_dist=sd_dist, compute_corr=True)
259
- vals_raw = pm.Normal('vals_raw', mu=0, sigma=1, shape=(5, 3))
260
- vals = pm.Deterministic('vals', pt.dot(chol, vals_raw.T).T)
260
+ chol, _, _ = pm.LKJCholeskyCov("chol_cov", n=3, eta=2, sd_dist=sd_dist, compute_corr=True)
261
+ vals_raw = pm.Normal("vals_raw", mu=0, sigma=1, shape=(5, 3))
262
+ vals = pm.Deterministic("vals", pt.dot(chol, vals_raw.T).T)
261
263
"""
262
264
263
265
rv_op = multivariate_normal
@@ -1806,13 +1808,12 @@ class MatrixNormal(Continuous):
1806
1808
Define a matrixvariate normal variable for given row and column covariance
1807
1809
matrices::
1808
1810
1809
- colcov = np.array([[1., 0.5], [0.5, 2]])
1811
+ colcov = np.array([[1.0 , 0.5], [0.5, 2]])
1810
1812
rowcov = np.array([[1, 0, 0], [0, 4, 0], [0, 0, 16]])
1811
1813
m = rowcov.shape[0]
1812
1814
n = colcov.shape[0]
1813
1815
mu = np.zeros((m, n))
1814
- vals = pm.MatrixNormal('vals', mu=mu, colcov=colcov,
1815
- rowcov=rowcov)
1816
+ vals = pm.MatrixNormal("vals", mu=mu, colcov=colcov, rowcov=rowcov)
1816
1817
1817
1818
Above, the ith row in vals has a variance that is scaled by 4^i.
1818
1819
Alternatively, row or column cholesky matrices could be substituted for
@@ -2418,23 +2419,25 @@ class ICAR(Continuous):
2418
2419
# 4x4 adjacency matrix
2419
2420
# arranged in a square lattice
2420
2421
2421
- W = np.array([
2422
- [0,1,0,1],
2423
- [1,0,1,0],
2424
- [0,1,0,1],
2425
- [1,0,1,0]
2426
- ])
2422
+ W = np.array(
2423
+ [
2424
+ [0, 1, 0, 1],
2425
+ [1, 0, 1, 0],
2426
+ [0, 1, 0, 1],
2427
+ [1, 0, 1, 0],
2428
+ ],
2429
+ )
2427
2430
2428
2431
# centered parameterization
2429
2432
with pm.Model():
2430
- sigma = pm.Exponential(' sigma' , 1)
2431
- phi = pm.ICAR(' phi' , W=W, sigma=sigma)
2433
+ sigma = pm.Exponential(" sigma" , 1)
2434
+ phi = pm.ICAR(" phi" , W=W, sigma=sigma)
2432
2435
mu = phi
2433
2436
2434
2437
# non-centered parameterization
2435
2438
with pm.Model():
2436
- sigma = pm.Exponential(' sigma' , 1)
2437
- phi = pm.ICAR(' phi' , W=W)
2439
+ sigma = pm.Exponential(" sigma" , 1)
2440
+ phi = pm.ICAR(" phi" , W=W)
2438
2441
mu = sigma * phi
2439
2442
2440
2443
References
0 commit comments