Exemple #1
0
def test_multivariate_normal_logpdf_batches_and_states_shared_cov_masked(D=10):
    # Test broadcasting over B batches, N datapoints, and K means, 1 covariance, with masks
    B = 3
    N = 100
    K = 5
    x = npr.randn(B, N, D)
    mask = npr.rand(B, N, D) < .5
    mu = npr.randn(K, D)
    L = npr.randn(D, D)
    Sigma = np.dot(L, L.T)

    ll1 = multivariate_normal_logpdf(x[:, :, None, :],
                                     mu,
                                     Sigma,
                                     mask=mask[:, :, None, :])
    assert ll1.shape == (B, N, K)

    ll2 = np.empty((B, N, K))
    for b in range(B):
        for n in range(N):
            m = mask[b, n]
            if m.sum() == 0:
                ll2[b, n] = 0
            else:
                for k in range(K):
                    ll2[b, n, k] = mvn.logpdf(x[b, n][m], mu[k][m],
                                              Sigma[np.ix_(m, m)])

    assert np.allclose(ll1, ll2)
Exemple #2
0
 def log_prior(self):
     beta_mean = np.zeros(np.shape(self.beta)[0])
     beta_cov = (0.1**2)*np.eye(np.shape(self.beta)[0])
     dyn_var = np.exp(self.log_sigma_scale)
     alpha = 1.1 # or 0.02
     beta = 1e-3 # or 0.02
     return np.sum(stats.multivariate_normal_logpdf(np.array([self.beta]), beta_mean, beta_cov)) \
             + np.sum(-0.5 * (self.x0 - 0.5)**2 / 1.0) \
             + alpha*np.log(beta) - gammaln(alpha) - (alpha+1)*np.log(dyn_var) - beta/dyn_var
Exemple #3
0
def test_multivariate_normal_logpdf_simple(D=10):
    # Test single datapoint log pdf
    x = npr.randn(D)
    mu = npr.randn(D)
    L = npr.randn(D, D)
    Sigma = np.dot(L, L.T)

    ll1 = multivariate_normal_logpdf(x, mu, Sigma)
    ll2 = mvn.logpdf(x, mu, Sigma)
    assert np.allclose(ll1, ll2)
Exemple #4
0
    def log_likelihoods(self, data, input, mask, tag):
        mus, Sigmas = self.mus, self.Sigmas
        if mask is not None and np.any(~mask) and not isinstance(
                mus, np.ndarray):
            raise Exception(
                "Current implementation of multivariate_normal_logpdf for masked data"
                "does not work with autograd because it writes to an array. "
                "Use DiagonalGaussian instead if you need to support missing data."
            )

        return stats.multivariate_normal_logpdf(data[:, None, :], mus, Sigmas)
Exemple #5
0
def test_multivariate_normal_logpdf_shared_params(D=10):
    # Test broadcasting over datapoints with shared parameters
    leading_ndim = npr.randint(1, 4)
    shp = npr.randint(1, 10, size=leading_ndim)
    x = npr.randn(*shp, D)
    mu = npr.randn(D)
    L = npr.randn(D, D)
    Sigma = np.dot(L, L.T)

    ll1 = multivariate_normal_logpdf(x, mu, Sigma)
    ll2 = np.reshape(mvn.logpdf(x, mu, Sigma), shp)
    assert np.allclose(ll1, ll2)
Exemple #6
0
def test_multivariate_normal_logpdf_simple_masked(D=10):
    # Test single datapoint log pdf with mask
    x = npr.randn(D)
    mask = npr.rand(D) < 0.5
    mask[0] = True
    mu = npr.randn(D)
    L = npr.randn(D, D)
    Sigma = np.dot(L, L.T)

    ll1 = multivariate_normal_logpdf(x, mu, Sigma, mask=mask)
    ll2 = mvn.logpdf(x[mask], mu[mask], Sigma[np.ix_(mask, mask)])
    assert np.allclose(ll1, ll2)
Exemple #7
0
def test_expected_multivariate_normal_logpdf_simple(D=10):
    # Test single datapoint log pdf
    x = npr.randn(D)
    mu = npr.randn(D)
    L = npr.randn(D, D)
    Sigma = np.dot(L, L.T)

    # Check that when the covariance is zero we get the regular mvn pdf
    xxT = np.outer(x, x)
    mumuT = np.outer(mu, mu)
    ll1 = expected_multivariate_normal_logpdf(x, xxT, mu, mumuT, Sigma)
    ll2 = multivariate_normal_logpdf(x, mu, Sigma)
    assert np.allclose(ll1, ll2)
Exemple #8
0
def test_multivariate_normal_logpdf_unique_params(D=10):
    # Test broadcasting over datapoints and corresponding parameters
    leading_ndim = npr.randint(1, 4)
    shp = npr.randint(1, 10, size=leading_ndim)
    x = npr.randn(*shp, D)
    mu = npr.randn(*shp, D)
    L = npr.randn(*shp, D, D)
    Sigma = np.matmul(L, np.swapaxes(L, -1, -2))

    ll1 = multivariate_normal_logpdf(x, mu, Sigma)
    ll2 = np.empty(shp)
    for inds in product(*[np.arange(s) for s in shp]):
        ll2[inds] = mvn.logpdf(x[inds], mu[inds], Sigma[inds])
    assert np.allclose(ll1, ll2)
Exemple #9
0
    def log_transition_matrices(self, data, input, mask, tag):
        assert np.all(mask), "Recurrent models require that all data are present."

        T = data.shape[0]
        assert input.shape[0] == T
        K, D = self.K, self.D

        # Previous state effect
        log_Ps = np.tile(self.log_Ps[None, :, :], (T-1, 1, 1))

        # RBF recurrent function
        rbf = multivariate_normal_logpdf(data[:-1, None, :], self.mus, self.Sigmas)
        log_Ps = log_Ps + rbf[:, None, :]

        # Input effect
        log_Ps = log_Ps + np.dot(input[1:], self.Ws.T)[:, None, :]
        return log_Ps - logsumexp(log_Ps, axis=2, keepdims=True)
Exemple #10
0
def test_multivariate_normal_logpdf_batches_and_states_shared_cov(D=10):
    # Test broadcasting over B batches, N datapoints, and K means, 1 covariance
    B = 3
    N = 100
    K = 5
    x = npr.randn(B, N, D)
    mu = npr.randn(K, D)
    L = npr.randn(D, D)
    Sigma = np.dot(L, L.T)

    ll1 = multivariate_normal_logpdf(x[:, :, None, :], mu, Sigma)
    assert ll1.shape == (B, N, K)

    ll2 = np.empty((B, N, K))
    for b in range(B):
        for n in range(N):
            for k in range(K):
                ll2[b, n, k] = mvn.logpdf(x[b, n], mu[k], Sigma)
    assert np.allclose(ll1, ll2)
Exemple #11
0
def test_multivariate_normal_logpdf_batches_and_states(D=10):
    # Test broadcasting over B batches, N datapoints, and K parameters
    B = 3
    N = 100
    K = 5
    x = npr.randn(B, N, D)
    mu = npr.randn(K, D)
    L = npr.randn(K, D, D)
    Sigma = np.matmul(L, np.swapaxes(L, -1, -2))

    ll1 = multivariate_normal_logpdf(x[:, :, None, :], mu, Sigma)
    assert ll1.shape == (B, N, K)

    ll2 = np.empty((B, N, K))
    for b in range(B):
        for n in range(N):
            for k in range(K):
                ll2[b, n, k] = mvn.logpdf(x[b, n], mu[k], Sigma[k])
    assert np.allclose(ll1, ll2)
Exemple #12
0
def test_expected_multivariate_normal_logpdf_bound(D=10):
    """
    Make sure the expected likelihood at the mode is less than
    the likelihood at the mode for nonzero Sigma.
    """
    x = npr.randn(D)
    sqrt_x_cov = npr.randn(D, D)
    x_cov = np.dot(sqrt_x_cov, sqrt_x_cov.T)

    mu = x.copy()
    sqrt_Sigma = npr.randn(D, D)
    Sigma = np.dot(sqrt_Sigma, sqrt_Sigma.T)

    # Check that when the covariance is zero we get the regular mvn pdf
    xxT = x_cov + np.outer(x, x)
    mumuT = np.outer(mu, mu)
    ell = expected_multivariate_normal_logpdf(x, xxT, mu, mumuT, Sigma)
    ll = multivariate_normal_logpdf(x, mu, Sigma)
    assert ell < ll