Merge branch 'devel' into binomial_laplace

This commit is contained in:
Max Zwiessele 2016-09-01 14:21:27 +01:00
commit 589c5d9eac
206 changed files with 41346 additions and 4247 deletions

View file

@ -27,9 +27,6 @@ class Binomial(Likelihood):
super(Binomial, self).__init__(gp_link, 'Binomial')
def conditional_mean(self, gp, Y_metadata):
return self.gp_link(gp)*Y_metadata['trials']
def pdf_link(self, inv_link_f, y, Y_metadata):
"""
Likelihood function given inverse link of f.
@ -138,7 +135,7 @@ class Binomial(Likelihood):
inv_link_f2 = np.square(inv_link_f)
return 2*y/inv_link_f**3 - 2*(N-y)/(1.-inv_link_f)**3
def samples(self, gp, Y_metadata=None):
def samples(self, gp, Y_metadata=None, **kw):
"""
Returns a set of samples of observations based on a given value of the latent variable.
@ -152,3 +149,32 @@ class Binomial(Likelihood):
def exact_inference_gradients(self, dL_dKdiag,Y_metadata=None):
pass
def variational_expectations(self, Y, m, v, gh_points=None, Y_metadata=None):
if isinstance(self.gp_link, link_functions.Probit):
if gh_points is None:
gh_x, gh_w = self._gh_points()
else:
gh_x, gh_w = gh_points
gh_w = gh_w / np.sqrt(np.pi)
shape = m.shape
C = np.atleast_1d(Y_metadata['trials'])
m,v,Y, C = m.flatten(), v.flatten(), Y.flatten()[:,None], C.flatten()[:,None]
X = gh_x[None,:]*np.sqrt(2.*v[:,None]) + m[:,None]
p = std_norm_cdf(X)
p = np.clip(p, 1e-9, 1.-1e-9) # for numerical stability
N = std_norm_pdf(X)
#TODO: missing nchoosek coefficient! use gammaln?
F = (Y*np.log(p) + (C-Y)*np.log(1.-p)).dot(gh_w)
NoverP = N/p
NoverP_ = N/(1.-p)
dF_dm = (Y*NoverP - (C-Y)*NoverP_).dot(gh_w)
dF_dv = -0.5* ( Y*(NoverP**2 + NoverP*X) + (C-Y)*(NoverP_**2 - NoverP_*X) ).dot(gh_w)
return F.reshape(*shape), dF_dm.reshape(*shape), dF_dv.reshape(*shape), None
else:
raise NotImplementedError

View file

@ -678,7 +678,7 @@ class Likelihood(Parameterized):
burnin_cache = np.zeros(par_chains)
burnin_cache[:] = starting_loc.flatten()
burning_in = True
for i in xrange(burn_in+num_samples):
for i in range(burn_in+num_samples):
next_ind = i-burn_in
if burning_in:
old_y = burnin_cache