# 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 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!")