mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-30 14:35:15 +02:00
work on likeluhoods and likelihoods tests
This commit is contained in:
parent
5cf792504a
commit
2876e5a07a
11 changed files with 186 additions and 222 deletions
|
|
@ -1,11 +1,12 @@
|
|||
# Copyright (c) 2012, 2013 Ricardo Andrade
|
||||
# Copyright (c) 2012 - 2014, GPy authors (see AUTHORS.txt).
|
||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
|
||||
|
||||
import numpy as np
|
||||
from scipy import stats,special
|
||||
import scipy as sp
|
||||
from GPy.util.univariate_Gaussian import std_norm_pdf,std_norm_cdf
|
||||
from ..util.univariate_Gaussian import std_norm_pdf,std_norm_cdf
|
||||
from ..core.parameterization import Param
|
||||
import link_functions
|
||||
from likelihood import Likelihood
|
||||
|
||||
|
|
@ -18,14 +19,16 @@ class Gamma(Likelihood):
|
|||
\\alpha_{i} = \\beta y_{i}
|
||||
|
||||
"""
|
||||
def __init__(self,gp_link=None,analytical_mean=False,analytical_variance=False,beta=1.):
|
||||
self.beta = beta
|
||||
super(Gamma, self).__init__(gp_link,analytical_mean,analytical_variance)
|
||||
def __init__(self,gp_link=None,beta=1.):
|
||||
if gp_link is None:
|
||||
gp_link = link_functions.Log()
|
||||
super(Gamma, self).__init__(gp_link, 'Gamma')
|
||||
|
||||
def _preprocess_values(self,Y):
|
||||
return Y
|
||||
self.beta = Param('beta', beta)
|
||||
self.add_parameter(self.beta)
|
||||
self.beta.fix()#TODO: gradients!
|
||||
|
||||
def pdf_link(self, link_f, y, extra_data=None):
|
||||
def pdf_link(self, link_f, y, Y_metadata=None):
|
||||
"""
|
||||
Likelihood function given link(f)
|
||||
|
||||
|
|
@ -37,7 +40,7 @@ class Gamma(Likelihood):
|
|||
:type link_f: Nx1 array
|
||||
:param y: data
|
||||
:type y: Nx1 array
|
||||
:param extra_data: extra_data which is not used in poisson distribution
|
||||
:param Y_metadata: Y_metadata which is not used in poisson distribution
|
||||
:returns: likelihood evaluated for this point
|
||||
:rtype: float
|
||||
"""
|
||||
|
|
@ -47,7 +50,7 @@ class Gamma(Likelihood):
|
|||
objective = (y**(alpha - 1.) * np.exp(-self.beta*y) * self.beta**alpha)/ special.gamma(alpha)
|
||||
return np.exp(np.sum(np.log(objective)))
|
||||
|
||||
def logpdf_link(self, link_f, y, extra_data=None):
|
||||
def logpdf_link(self, link_f, y, Y_metadata=None):
|
||||
"""
|
||||
Log Likelihood Function given link(f)
|
||||
|
||||
|
|
@ -59,7 +62,7 @@ class Gamma(Likelihood):
|
|||
:type link_f: Nx1 array
|
||||
:param y: data
|
||||
:type y: Nx1 array
|
||||
:param extra_data: extra_data which is not used in poisson distribution
|
||||
:param Y_metadata: Y_metadata which is not used in poisson distribution
|
||||
:returns: likelihood evaluated for this point
|
||||
:rtype: float
|
||||
|
||||
|
|
@ -71,7 +74,7 @@ class Gamma(Likelihood):
|
|||
log_objective = alpha*np.log(self.beta) - np.log(special.gamma(alpha)) + (alpha - 1)*np.log(y) - self.beta*y
|
||||
return np.sum(log_objective)
|
||||
|
||||
def dlogpdf_dlink(self, link_f, y, extra_data=None):
|
||||
def dlogpdf_dlink(self, link_f, y, Y_metadata=None):
|
||||
"""
|
||||
Gradient of the log likelihood function at y, given link(f) w.r.t link(f)
|
||||
|
||||
|
|
@ -83,7 +86,7 @@ class Gamma(Likelihood):
|
|||
:type link_f: Nx1 array
|
||||
:param y: data
|
||||
:type y: Nx1 array
|
||||
:param extra_data: extra_data which is not used in gamma distribution
|
||||
:param Y_metadata: Y_metadata which is not used in gamma distribution
|
||||
:returns: gradient of likelihood evaluated at points
|
||||
:rtype: Nx1 array
|
||||
|
||||
|
|
@ -94,7 +97,7 @@ class Gamma(Likelihood):
|
|||
#return -self.gp_link.dtransf_df(gp)*self.beta*np.log(obs) + special.psi(self.gp_link.transf(gp)*self.beta) * self.gp_link.dtransf_df(gp)*self.beta
|
||||
return grad
|
||||
|
||||
def d2logpdf_dlink2(self, link_f, y, extra_data=None):
|
||||
def d2logpdf_dlink2(self, link_f, y, Y_metadata=None):
|
||||
"""
|
||||
Hessian at y, given link(f), w.r.t link(f)
|
||||
i.e. second derivative logpdf at y given link(f_i) and link(f_j) w.r.t link(f_i) and link(f_j)
|
||||
|
|
@ -108,7 +111,7 @@ class Gamma(Likelihood):
|
|||
:type link_f: Nx1 array
|
||||
:param y: data
|
||||
:type y: Nx1 array
|
||||
:param extra_data: extra_data which is not used in gamma distribution
|
||||
:param Y_metadata: Y_metadata which is not used in gamma distribution
|
||||
:returns: Diagonal of hessian matrix (second derivative of likelihood evaluated at points f)
|
||||
:rtype: Nx1 array
|
||||
|
||||
|
|
@ -122,7 +125,7 @@ class Gamma(Likelihood):
|
|||
#return -self.gp_link.d2transf_df2(gp)*self.beta*np.log(obs) + special.polygamma(1,self.gp_link.transf(gp)*self.beta)*(self.gp_link.dtransf_df(gp)*self.beta)**2 + special.psi(self.gp_link.transf(gp)*self.beta)*self.gp_link.d2transf_df2(gp)*self.beta
|
||||
return hess
|
||||
|
||||
def d3logpdf_dlink3(self, link_f, y, extra_data=None):
|
||||
def d3logpdf_dlink3(self, link_f, y, Y_metadata=None):
|
||||
"""
|
||||
Third order derivative log-likelihood function at y given link(f) w.r.t link(f)
|
||||
|
||||
|
|
@ -134,22 +137,10 @@ class Gamma(Likelihood):
|
|||
:type link_f: Nx1 array
|
||||
:param y: data
|
||||
:type y: Nx1 array
|
||||
:param extra_data: extra_data which is not used in gamma distribution
|
||||
:param Y_metadata: Y_metadata which is not used in gamma distribution
|
||||
:returns: third derivative of likelihood evaluated at points f
|
||||
:rtype: Nx1 array
|
||||
"""
|
||||
assert np.atleast_1d(link_f).shape == np.atleast_1d(y).shape
|
||||
d3lik_dlink3 = -special.polygamma(2, self.beta*link_f)*(self.beta**3)
|
||||
return d3lik_dlink3
|
||||
|
||||
def _mean(self,gp):
|
||||
"""
|
||||
Mass (or density) function
|
||||
"""
|
||||
return self.gp_link.transf(gp)
|
||||
|
||||
def _variance(self,gp):
|
||||
"""
|
||||
Mass (or density) function
|
||||
"""
|
||||
return self.gp_link.transf(gp)/self.beta
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue