more massive and destructive changes everywhere

This commit is contained in:
James Hensman 2013-12-05 15:09:31 -05:00
parent 881800126f
commit 5ec64d2279
18 changed files with 202 additions and 166 deletions

View file

@ -22,3 +22,6 @@ use this posterior object for making predictions, optimizing hyper-parameters,
etc.
"""
from exact_gaussian_inference import ExactGaussianInference
expectation_propagation = 'foo' # TODO

View file

@ -2,11 +2,12 @@
# Licensed under the BSD 3-clause license (see LICENSE.txt)
from posterior import Posterior
from .../util.linalg import pdinv, dpotrs, tdot
from ...util.linalg import pdinv, dpotrs, tdot
import numpy as np
log_2_pi = np.log(2*np.pi)
class exact_gaussian_inference(object):
class ExactGaussianInference(object):
"""
An object for inference when the likelihood is Gaussian.
@ -17,9 +18,9 @@ class exact_gaussian_inference(object):
"""
def __init__(self):
self._YYTfactor_cache = caching.cache()
pass#self._YYTfactor_cache = caching.cache()
def get_YYTfactor(self, Y):
def get_YYTfactor(self, Y):
"""
find a matrix L which satisfies LLT = YYT.
@ -38,16 +39,16 @@ class exact_gaussian_inference(object):
"""
YYT_factor = self.get_YYTfactor(Y)
Wi, LW, LWi, W_logdet = pdinv(K + likelhood.covariance(Y, Y_metadata))
Wi, LW, LWi, W_logdet = pdinv(K + likelihood.covariance_matrix(Y, Y_metadata))
alpha, _ = dpotrs(LW, YYT_factor, lower=1)
dL_dK = 0.5 * (tdot(alpha) - Y.shape[1] * Wi)
log_marginal = 0.5*(-Y.size * log_2_pi - Y.shape[1] * W_logdet - np.sum(alpha * YYT_factor.T)
log_marginal = 0.5*(-Y.size * log_2_pi - Y.shape[1] * W_logdet - np.sum(alpha * YYT_factor))
dL_dtheta_lik = likelihood.dL_dtheta(np.diag(dL_dK))
dL_dtheta_lik = likelihood._gradients(np.diag(dL_dK))
return Posterior(log_marginal, dL_DK, dL_dtheta_lik, LW, alpha, K):
return Posterior(log_marginal, dL_dK, dL_dtheta_lik, LW, alpha, K)

View file

@ -6,27 +6,27 @@ import numpy as np
class Posterior(object):
"""
An object to represent a Gaussian posterior over latent function values.
this may be computed exactly for Gaussian likelihoods, or approximated for
This may be computed exactly for Gaussian likelihoods, or approximated for
non-Gaussian likelihoods.
The purpose of this clas is to serve as an interface between the inference
The purpose of this class is to serve as an interface between the inference
schemes and the model classes.
"""
def __init__(self, log_marginal, dLM_DK, dLM_dtheta_lik, woodbury_chol, woodbury_mean, K):
def __init__(self, log_marginal, dL_dK, dL_dtheta_lik, woodbury_chol, woodbury_vector, K):
"""
log_marginal: log p(Y|X)
DLM_dK: d/dK log p(Y|X)
dLM_dtheta_lik : d/dtheta log p(Y|X) (where theta are the parameters of the likelihood)
DL_dK: d/dK log p(Y|X)
dL_dtheta_lik : d/dtheta log p(Y|X) (where theta are the parameters of the likelihood)
woodbury_chol : a lower triangular matrix L that satisfies posterior_covariance = K - K L^{-T} L^{-1} K
woodbury_mean : a matrix (or vector, as Nx1 matrix) M which satisfies posterior_mean = K M
woodbury_vector : a matrix (or vector, as Nx1 matrix) M which satisfies posterior_mean = K M
K : the proir covariance (required for lazy computation of various quantities)
"""
self.log_marginal = log_marginal
self.dLM_DK = dLM_DK
self.dLM_dtheta_lik = _dLM_dtheta_lik
self.dL_dK = dL_dK
self.dL_dtheta_lik = dL_dtheta_lik
self._woodbury_chol = woodbury_chol
self._woodbury_mean = woodbury_mean
self._woodbury_vector = woodbury_vector
self._K = K
#these are computed lazily below
@ -37,13 +37,13 @@ class Posterior(object):
@property
def mean(self):
if self._mean is None:
self._mean = np.dot(self._K, self._woodbury_mean)
self._mean = np.dot(self._K, self._woodbury_vector)
return self._mean
@property
def covariance(self):
if self._covariance is None:
LiK, _ = dpotrs
LiK, _ = dpotrs(self._woodbury_chol, self._K)
self._covariance = self._K - tdot(LiK.T)
return self._covariance

View file

@ -0,0 +1,2 @@
from scg import SCG
from optimization import *