Skip to content

Fix multivariate metropolis proposal #1791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2017
Merged
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
24 changes: 21 additions & 3 deletions pymc3/step_methods/metropolis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import numpy.random as nr
import theano
import scipy.linalg

from ..distributions import draw_values
from .arraystep import ArrayStepShared, ArrayStep, metrop_select, Competence
Expand Down Expand Up @@ -41,8 +42,16 @@ def __call__(self):


class MultivariateNormalProposal(Proposal):
def __init__(self, s):
n, m = s.shape
if n != m:
raise ValueError("Covariance matrix is not symmetric.")
self.n = n
self.chol = scipy.linalg.cholesky(s, lower=True)

def __call__(self, num_draws=None):
return nr.multivariate_normal(mean=np.zeros(self.s.shape[0]), cov=self.s, size=num_draws)
b = np.random.randn(self.n)
return np.dot(self.chol, b)


class Metropolis(ArrayStepShared):
Expand Down Expand Up @@ -76,7 +85,7 @@ class Metropolis(ArrayStepShared):
'tune': np.bool,
}]

def __init__(self, vars=None, S=None, proposal_dist=NormalProposal, scaling=1.,
def __init__(self, vars=None, S=None, proposal_dist=None, scaling=1.,
tune=True, tune_interval=100, model=None, mode=None, **kwargs):

model = pm.modelcontext(model)
Expand All @@ -87,7 +96,16 @@ def __init__(self, vars=None, S=None, proposal_dist=NormalProposal, scaling=1.,

if S is None:
S = np.ones(sum(v.dsize for v in vars))
self.proposal_dist = proposal_dist(S)

if proposal_dist is not None:
self.proposal_dist = proposal_dist(S)
elif S.ndim == 1:
self.proposal_dist = NormalProposal(S)
elif S.ndim == 2:
self.proposal_dist = MultivariateNormalProposal(S)
else:
raise ValueError("Invalid rank for variance: %s" % S.ndim)

self.scaling = np.atleast_1d(scaling)
self.tune = tune
self.tune_interval = tune_interval
Expand Down
26 changes: 25 additions & 1 deletion pymc3/tests/test_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from pymc3.sampling import assign_step_methods, sample
from pymc3.model import Model
from pymc3.step_methods import (NUTS, BinaryGibbsMetropolis, CategoricalGibbsMetropolis,
Metropolis, Slice, CompoundStep,
Metropolis, Slice, CompoundStep, NormalProposal,
MultivariateNormalProposal, HamiltonianMC)
from pymc3.distributions import Binomial, Normal, Bernoulli, Categorical

from numpy.testing import assert_array_almost_equal
import numpy as np
import numpy.testing as npt
from tqdm import tqdm


Expand Down Expand Up @@ -187,6 +188,29 @@ def test_step_categorical(self):
yield self.check_stat, check, trace, step.__class__.__name__


class TestMetropolisProposal(unittest.TestCase):
def test_proposal_choice(self):
_, model, _ = mv_simple()
with model:
s = np.ones(model.ndim)
sampler = Metropolis(S=s)
assert isinstance(sampler.proposal_dist, NormalProposal)
s = np.diag(s)
sampler = Metropolis(S=s)
assert isinstance(sampler.proposal_dist, MultivariateNormalProposal)
s[0, 0] = -s[0, 0]
with self.assertRaises(np.linalg.LinAlgError):
sampler = Metropolis(S=s)

def test_mv_proposal(self):
np.random.seed(42)
cov = np.random.randn(5, 5)
cov = cov.dot(cov.T)
prop = MultivariateNormalProposal(cov)
samples = np.array([prop() for _ in range(10000)])
npt.assert_allclose(np.cov(samples.T), cov, rtol=0.2)


class TestCompoundStep(unittest.TestCase):
samplers = (Metropolis, Slice, HamiltonianMC, NUTS)

Expand Down