Source code for GPy.likelihoods.link_functions

# Copyright (c) 2012-2015 The GPy authors (see AUTHORS.txt)
# Licensed under the BSD 3-clause license (see LICENSE.txt)

import numpy as np
import scipy
from ..util.univariate_Gaussian import std_norm_cdf, std_norm_pdf
import scipy as sp
from ..util.misc import safe_exp, safe_square, safe_cube, safe_quad, safe_three_times

[docs]class GPTransformation(object): """ Link function class for doing non-Gaussian likelihoods approximation :param Y: observed output (Nx1 numpy.darray) .. note:: Y values allowed depend on the likelihood_function used """ def __init__(self): pass
[docs] def transf(self,f): """ Gaussian process tranformation function, latent space -> output space """ raise NotImplementedError
[docs] def dtransf_df(self,f): """ derivative of transf(f) w.r.t. f """ raise NotImplementedError
[docs] def d2transf_df2(self,f): """ second derivative of transf(f) w.r.t. f """ raise NotImplementedError
[docs] def d3transf_df3(self,f): """ third derivative of transf(f) w.r.t. f """ raise NotImplementedError
[docs] def to_dict(self): raise NotImplementedError
def _save_to_input_dict(self): return {}
[docs] @staticmethod def from_dict(input_dict): """ Instantiate an object of a derived class using the information in input_dict (built by the to_dict method of the derived class). More specifically, after reading the derived class from input_dict, it calls the method _build_from_input_dict of the derived class. Note: This method should not be overrided in the derived class. In case it is needed, please override _build_from_input_dict instate. :param dict input_dict: Dictionary with all the information needed to instantiate the object. """ import copy input_dict = copy.deepcopy(input_dict) link_class = input_dict.pop('class') import GPy link_class = eval(link_class) return link_class._build_from_input_dict(link_class, input_dict)
@staticmethod def _build_from_input_dict(link_class, input_dict): return link_class(**input_dict)
[docs]class Identity(GPTransformation): """ .. math:: g(f) = f """
[docs] def transf(self,f): return f
[docs] def dtransf_df(self,f): return np.ones_like(f)
[docs] def d2transf_df2(self,f): return np.zeros_like(f)
[docs] def d3transf_df3(self,f): return np.zeros_like(f)
[docs] def to_dict(self): """ Convert the object into a json serializable dictionary. Note: It uses the private method _save_to_input_dict of the parent. :return dict: json serializable dictionary containing the needed information to instantiate the object """ input_dict = super(Identity, self)._save_to_input_dict() input_dict["class"] = "GPy.likelihoods.link_functions.Identity" return input_dict
[docs]class Probit(GPTransformation): """ .. math:: g(f) = \\Phi^{-1} (mu) """
[docs] def transf(self,f): return std_norm_cdf(f)
[docs] def dtransf_df(self,f): return std_norm_pdf(f)
[docs] def d2transf_df2(self,f): return -f * std_norm_pdf(f)
[docs] def d3transf_df3(self,f): return (safe_square(f)-1.)*std_norm_pdf(f)
[docs] def to_dict(self): """ Convert the object into a json serializable dictionary. Note: It uses the private method _save_to_input_dict of the parent. :return dict: json serializable dictionary containing the needed information to instantiate the object """ input_dict = super(Probit, self)._save_to_input_dict() input_dict["class"] = "GPy.likelihoods.link_functions.Probit" return input_dict
[docs]class ScaledProbit(Probit): """ .. math:: g(f) = \\Phi^{-1} (nu*mu) """ def __init__(self, nu=1.): self.nu = float(nu)
[docs] def transf(self,f): return std_norm_cdf(f*self.nu)
[docs] def dtransf_df(self,f): return std_norm_pdf(f*self.nu)*self.nu
[docs] def d2transf_df2(self,f): return -(f*self.nu) * std_norm_pdf(f*self.nu)*(self.nu**2)
[docs] def d3transf_df3(self,f): return (safe_square(f*self.nu)-1.)*std_norm_pdf(f*self.nu)*(self.nu**3)
[docs] def to_dict(self): """ Convert the object into a json serializable dictionary. Note: It uses the private method _save_to_input_dict of the parent. :return dict: json serializable dictionary containing the needed information to instantiate the object """ input_dict = super(ScaledProbit, self)._save_to_input_dict() input_dict["class"] = "GPy.likelihoods.link_functions.ScaledProbit" return input_dict
[docs]class Cloglog(GPTransformation): """ Complementary log-log link .. math:: p(f) = 1 - e^{-e^f} or f = \log (-\log(1-p)) """
[docs] def transf(self,f): ef = safe_exp(f) return 1-np.exp(-ef)
[docs] def dtransf_df(self,f): ef = safe_exp(f) return np.exp(f-ef)
[docs] def d2transf_df2(self,f): ef = safe_exp(f) return -np.exp(f-ef)*(ef-1.)
[docs] def d3transf_df3(self,f): ef = safe_exp(f) ef2 = safe_square(ef) three_times_ef = safe_three_times(ef) r_val = np.exp(f-ef)*(1.-three_times_ef + ef2) return r_val
[docs]class Log(GPTransformation): """ .. math:: g(f) = \\log(\\mu) """
[docs] def transf(self,f): return safe_exp(f)
[docs] def dtransf_df(self,f): return safe_exp(f)
[docs] def d2transf_df2(self,f): return safe_exp(f)
[docs] def d3transf_df3(self,f): return safe_exp(f)
[docs]class Log_ex_1(GPTransformation): """ .. math:: g(f) = \\log(\\exp(\\mu) - 1) """
[docs] def transf(self,f): return scipy.special.log1p(safe_exp(f))
[docs] def dtransf_df(self,f): ef = safe_exp(f) return ef/(1.+ef)
[docs] def d2transf_df2(self,f): ef = safe_exp(f) aux = ef/(1.+ef) return aux*(1.-aux)
[docs] def d3transf_df3(self,f): ef = safe_exp(f) aux = ef/(1.+ef) daux_df = aux*(1.-aux) return daux_df - (2.*aux*daux_df)
[docs]class Reciprocal(GPTransformation):
[docs] def transf(self,f): return 1./f
[docs] def dtransf_df(self, f): f2 = safe_square(f) return -1./f2
[docs] def d2transf_df2(self, f): f3 = safe_cube(f) return 2./f3
[docs] def d3transf_df3(self,f): f4 = safe_quad(f) return -6./f4
[docs]class Heaviside(GPTransformation): """ .. math:: g(f) = I_{x \\geq 0} """
[docs] def transf(self,f): #transformation goes here return np.where(f>0, 1, 0)
[docs] def dtransf_df(self,f): raise NotImplementedError("This function is not differentiable!")
[docs] def d2transf_df2(self,f): raise NotImplementedError("This function is not differentiable!")