diff --git a/GPy/examples/state_space.py b/GPy/examples/state_space.py index 6d96a927..071fb628 100644 --- a/GPy/examples/state_space.py +++ b/GPy/examples/state_space.py @@ -4,12 +4,12 @@ import matplotlib.pyplot as plt import GPy.models.state_space_new as SS_new -#X = np.linspace(0, 10, 2000)[:, None] -#Y = np.sin(X) + np.random.randn(*X.shape)*0.1 +X = np.linspace(0, 10, 4000)[:, None] +Y = np.sin(X) + np.random.randn(*X.shape)*0.1 # Need to run these lines when X and Y are imported -> -X.shape = (X.shape[0],1) -Y.shape = (Y.shape[0],1) +#X.shape = (X.shape[0],1) +#Y.shape = (Y.shape[0],1) # Need to run these lines when X and Y are imported <- ## Generation of minimal example data -> diff --git a/GPy/kern/__init__.py b/GPy/kern/__init__.py index 62796d93..3c3de65c 100644 --- a/GPy/kern/__init__.py +++ b/GPy/kern/__init__.py @@ -29,3 +29,11 @@ from .src.splitKern import SplitKern,DEtime from .src.splitKern import DEtime as DiffGenomeKern from .src.spline import Spline from .src.basis_funcs import LogisticBasisFuncKernel, LinearSlopeBasisFuncKernel, BasisFuncKernel, ChangePointBasisFuncKernel, DomainKernel + +from .src.sde_matern import sde_Matern32 +from .src.sde_matern import sde_Matern52 +from .src.sde_linear import sde_Linear +from .src.sde_standard_periodic import sde_StdPeriodic +from .src.sde_static import sde_White, sde_Bias +from .src.sde_stationary import sde_RBF,sde_Exponential,sde_RatQuad +from .src.sde_brownian import sde_Brownian diff --git a/GPy/kern/_src/sde_linear.py b/GPy/kern/_src/sde_linear.py new file mode 100644 index 00000000..48c4a169 --- /dev/null +++ b/GPy/kern/_src/sde_linear.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +""" +Classes in this module enhance Matern covariance functions with the +Stochastic Differential Equation (SDE) functionality. +""" +from .linear import Linear + +import numpy as np + +class sde_Linear(Linear): + """ + + Class provide extra functionality to transfer this covariance function into + SDE form. + + Linear kernel: + + .. math:: + + k(x,y) = \sum_{i=1}^{input dim} \sigma^2_i x_iy_i + + """ + + def sde(self): + """ + Return the state space representation of the covariance. + """ + + # Arno, insert your code here + + # Params to use: + + # self.variances + + #return (F, L, Qc, H, Pinf, dF, dQc, dPinf) diff --git a/GPy/kern/_src/sde_Matern.py b/GPy/kern/_src/sde_matern.py similarity index 68% rename from GPy/kern/_src/sde_Matern.py rename to GPy/kern/_src/sde_matern.py index 7a019c23..102691b4 100644 --- a/GPy/kern/_src/sde_Matern.py +++ b/GPy/kern/_src/sde_matern.py @@ -4,6 +4,7 @@ Classes in this module enhance Matern covariance functions with the Stochastic Differential Equation (SDE) functionality. """ from .stationary import Matern32 +from .stationary import Matern52 import numpy as np class sde_Matern32(Matern32): @@ -16,7 +17,7 @@ class sde_Matern32(Matern32): .. math:: - k(r) = \\sigma^2 (1 + \\sqrt{3} r) \exp(- \sqrt{3} r) \\ \\ \\ \\ \\text{ where } r = \sqrt{\sum_{i=1}^input_dim \\frac{(x_i-y_i)^2}{\ell_i^2} } + k(r) = \sigma^2 (1 + \sqrt{3} r) \exp(- \sqrt{3} r) \\ \\ \\ \\ \text{ where } r = \sqrt{\sum_{i=1}^{input dim} \frac{(x_i-y_i)^2}{\ell_i^2} } """ @@ -55,4 +56,31 @@ class sde_Matern32(Matern32): dPinf[:,:,0] = dPinfvariance dPinf[:,:,1] = dPinflengthscale - return (F, L, Qc, H, Pinf, dF, dQc, dPinf) \ No newline at end of file + return (F, L, Qc, H, Pinf, dF, dQc, dPinf) + +class sde_Matern52(Matern52): + """ + + Class provide extra functionality to transfer this covariance function into + SDE forrm. + + Matern 5/2 kernel: + + .. math:: + + k(r) = \sigma^2 (1 + \sqrt{5} r + \frac{5}{3}r^2) \exp(- \sqrt{5} r) \\ \\ \\ \\ \text{ where } r = \sqrt{\sum_{i=1}^{input dim} \frac{(x_i-y_i)^2}{\ell_i^2} } + + """ + + def sde(self): + """ + Return the state space representation of the covariance. + """ + + # Arno, insert your code here + + # Params to use: + # self.lengthscale + # self.variance + + #return (F, L, Qc, H, Pinf, dF, dQc, dPinf) \ No newline at end of file diff --git a/GPy/kern/_src/sde_standard_periodic.py b/GPy/kern/_src/sde_standard_periodic.py new file mode 100644 index 00000000..d72c22af --- /dev/null +++ b/GPy/kern/_src/sde_standard_periodic.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +""" +Classes in this module enhance Matern covariance functions with the +Stochastic Differential Equation (SDE) functionality. +""" +from .standard_periodic import StdPeriodic + +import numpy as np + +class sde_StdPeriodic(StdPeriodic): + """ + + Class provide extra functionality to transfer this covariance function into + SDE form. + + Standard Periodic kernel: + + .. math:: + + k(x,y) = \theta_1 \exp \left[ - \frac{1}{2} {}\sum_{i=1}^{input\_dim} + \left( \frac{\sin(\frac{\pi}{\lambda_i} (x_i - y_i) )}{l_i} \right)^2 \right] } + + """ + + def sde(self): + """ + Return the state space representation of the covariance. + """ + + # Arno, insert your code here + + # Params to use: + #self.variance + #self.wavelengths + #self.lengthscales + + # Arno, you could visualize the Latex version of the kernel formula + # and assume inputs are 1D, so no ARD is used. Then use parameters aboove. + + #return (F, L, Qc, H, Pinf, dF, dQc, dPinf) \ No newline at end of file diff --git a/GPy/kern/_src/sde_static.py b/GPy/kern/_src/sde_static.py new file mode 100644 index 00000000..8d50e5eb --- /dev/null +++ b/GPy/kern/_src/sde_static.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +""" +Classes in this module enhance Matern covariance functions with the +Stochastic Differential Equation (SDE) functionality. +""" +from .static import White +from .static import Bias + +import numpy as np + +class sde_White(White): + """ + + Class provide extra functionality to transfer this covariance function into + SDE forrm. + + Linear kernel: + + .. math:: + + k(x,y) = \alpha + + """ + + def sde(self): + """ + Return the state space representation of the covariance. + """ + + # Arno, insert your code here + + # Params to use: + # self.variance + + #return (F, L, Qc, H, Pinf, dF, dQc, dPinf) + +class sde_Bias(Bias): + """ + + Class provide extra functionality to transfer this covariance function into + SDE forrm. + + Linear kernel: + + .. math:: + + k(x,y) = \alpha*\delta(x-y) + + """ + + def sde(self): + """ + Return the state space representation of the covariance. + """ + + # Arno, insert your code here + + # Params to use: + # self.variance + + #return (F, L, Qc, H, Pinf, dF, dQc, dPinf) \ No newline at end of file diff --git a/GPy/kern/_src/sde_stationary.py b/GPy/kern/_src/sde_stationary.py new file mode 100644 index 00000000..0194ffb5 --- /dev/null +++ b/GPy/kern/_src/sde_stationary.py @@ -0,0 +1,103 @@ +# -*- coding: utf-8 -*- +""" +Classes in this module enhance several stationary covariance functions with the +Stochastic Differential Equation (SDE) functionality. +""" +from .rbf import RBF +from .stationary import Exponential +from .stationary import RatQuad + +import numpy as np + +class sde_RBF(RBF): + """ + + Class provide extra functionality to transfer this covariance function into + SDE form. + + Radial Basis Function kernel: + + .. math:: + + k(r) = \sigma^2 \exp \\bigg(- \\frac{1}{2} r^2 \\bigg) \\ \\ \\ \\ \text{ where } r = \sqrt{\sum_{i=1}^{input dim} \frac{(x_i-y_i)^2}{\ell_i^2} } + + """ + + def sde(self): + """ + Return the state space representation of the covariance. + """ + + # Arno, insert your code here + + # Params to use: + + # self.lengthscale + # self.variance + + #return (F, L, Qc, H, Pinf, dF, dQc, dPinf) + +class sde_Exponential(Exponential): + """ + + Class provide extra functionality to transfer this covariance function into + SDE form. + + Exponential kernel: + + .. math:: + + k(r) = \sigma^2 \exp \\bigg(- \\frac{1}{2} r \\bigg) \\ \\ \\ \\ \text{ where } r = \sqrt{\sum_{i=1}^{input dim} \frac{(x_i-y_i)^2}{\ell_i^2} } + + """ + + def sde(self): + """ + Return the state space representation of the covariance. + """ + F = np.array([[-1/self.lengthscale]]) + L = np.array([[1]]) + Qc = np.array([[2*self.variance/self.lengthscale]]) + H = np.array([[1]]) + Pinf = np.array([[self.variance]]) + # TODO: return the derivatives as well + + return (F, L, Qc, H, Pinf) + + # Arno, insert your code here + + # Params to use: + + # self.lengthscale + # self.variance + + #return (F, L, Qc, H, Pinf, dF, dQc, dPinf) + +class sde_RatQuad(RatQuad): + """ + + Class provide extra functionality to transfer this covariance function into + SDE form. + + Rational Quadratic kernel: + + .. math:: + + k(r) = \sigma^2 \\bigg( 1 + \\frac{r^2}{2} \\bigg)^{- \alpha} \\ \\ \\ \\ \text{ where } r = \sqrt{\sum_{i=1}^{input dim} \frac{(x_i-y_i)^2}{\ell_i^2} } + + """ + + def sde(self): + """ + Return the state space representation of the covariance. + """ + + # Arno, insert your code here + + # Params to use: + + # self.lengthscale + # self.variance + #self.power + + #return (F, L, Qc, H, Pinf, dF, dQc, dPinf) \ No newline at end of file diff --git a/GPy/kern/src/stationary.py b/GPy/kern/src/stationary.py index f2b4c2cc..019cd968 100644 --- a/GPy/kern/src/stationary.py +++ b/GPy/kern/src/stationary.py @@ -320,18 +320,18 @@ class Exponential(Stationary): def dK_dr(self, r): return -0.5*self.K_of_r(r) - def sde(self): - """ - Return the state space representation of the covariance. - """ - F = np.array([[-1/self.lengthscale]]) - L = np.array([[1]]) - Qc = np.array([[2*self.variance/self.lengthscale]]) - H = np.array([[1]]) - Pinf = np.array([[self.variance]]) - # TODO: return the derivatives as well - - return (F, L, Qc, H, Pinf) +# def sde(self): +# """ +# Return the state space representation of the covariance. +# """ +# F = np.array([[-1/self.lengthscale]]) +# L = np.array([[1]]) +# Qc = np.array([[2*self.variance/self.lengthscale]]) +# H = np.array([[1]]) +# Pinf = np.array([[self.variance]]) +# # TODO: return the derivatives as well +# +# return (F, L, Qc, H, Pinf)