Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pymc/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
)
from pymc.distributions.shape_utils import (
_change_dist_size,
build_icar_covariance,
change_dist_size,
get_support_shape,
implicit_size_from_params,
Expand Down Expand Up @@ -2451,7 +2452,17 @@ class ICAR(Continuous):

rv_op = icar

@classmethod

def ICAR(name, W, tau=1.0, mu=None, method="eig", **kwargs):
W = pt.as_tensor_variable(W)

if mu is None:
mu = pt.zeros(W.shape[0])

cov = build_icar_covariance(W, tau) # private python helper that gets eig pseudo inverse

return pm.MvNormal.dist(mu=mu, cov=cov, method=method, name=name, **kwargs)

def dist(cls, W, sigma=1, zero_sum_stdev=0.001, **kwargs):
# Note: These checks are forcing W to be non-symbolic
if not W.ndim == 2:
Expand Down
10 changes: 10 additions & 0 deletions pymc/distributions/shape_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
from pymc.pytensorf import PotentialShapeType


def build_icar_covariance(W, tau):
D = np.diag(W.sum(axis=1))
Q = tau*(D-W)
vals, vecs = np.linalg.eigh(Q)
tol = np.max(vals)*1e-12
inv_vals = np.where(vals > tol, 1/vals, 0.0)
cov = (vecs*inv_vals) @ vecs.T
cov = 0.5*(cov+cov.T)
return cov

def to_tuple(shape):
"""Convert ints, arrays, and Nones to tuples.
Expand Down