mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-04-27 05:46:24 +02:00
Merge pull request #1070 from SheffieldML/1069-import-gpy-fails-with-matplotlib-390
1069 import gpy fails with matplotlib 390
This commit is contained in:
commit
08d182bd88
7 changed files with 394 additions and 258 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
+ update import in `.plotting.matplot_dep.defaults` due to change in matplotlib
|
||||||
|
|
||||||
## v1.13.1 (2024-01-14)
|
## v1.13.1 (2024-01-14)
|
||||||
|
|
||||||
* limit `scipy<1.12` as macos and linux jobs install some pre-release version of `scipy==1.12` which breaks tests
|
* limit `scipy<1.12` as macos and linux jobs install some pre-release version of `scipy==1.12` which breaks tests
|
||||||
|
|
|
||||||
|
|
@ -13,213 +13,267 @@ import warnings
|
||||||
|
|
||||||
from scipy import special as special
|
from scipy import special as special
|
||||||
|
|
||||||
|
|
||||||
class sde_StdPeriodic(StdPeriodic):
|
class sde_StdPeriodic(StdPeriodic):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Class provide extra functionality to transfer this covariance function into
|
Class provide extra functionality to transfer this covariance function into
|
||||||
SDE form.
|
SDE form.
|
||||||
|
|
||||||
Standard Periodic kernel:
|
Standard Periodic kernel:
|
||||||
|
|
||||||
.. math::
|
.. math::
|
||||||
|
|
||||||
k(x,y) = \theta_1 \exp \left[ - \frac{1}{2} {}\sum_{i=1}^{input\_dim}
|
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] }
|
\left( \frac{\sin(\frac{\pi}{\lambda_i} (x_i - y_i) )}{l_i} \right)^2 \right] }
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO: write comment to the constructor arguments
|
# TODO: write comment to the constructor arguments
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Init constructior.
|
Init constructior.
|
||||||
|
|
||||||
Two optinal extra parameters are added in addition to the ones in
|
Two optinal extra parameters are added in addition to the ones in
|
||||||
StdPeriodic kernel.
|
StdPeriodic kernel.
|
||||||
|
|
||||||
:param approx_order: approximation order for the RBF covariance. (Default 7)
|
:param approx_order: approximation order for the RBF covariance. (Default 7)
|
||||||
:type approx_order: int
|
:type approx_order: int
|
||||||
|
|
||||||
:param balance: Whether to balance this kernel separately. (Defaulf False). Model has a separate parameter for balancing.
|
:param balance: Whether to balance this kernel separately. (Defaulf False). Model has a separate parameter for balancing.
|
||||||
:type balance: bool
|
:type balance: bool
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#import pdb; pdb.set_trace()
|
# import pdb; pdb.set_trace()
|
||||||
|
|
||||||
if 'approx_order' in kwargs:
|
if "approx_order" in kwargs:
|
||||||
self.approx_order = kwargs.get('approx_order')
|
self.approx_order = kwargs.get("approx_order")
|
||||||
del kwargs['approx_order']
|
del kwargs["approx_order"]
|
||||||
else:
|
else:
|
||||||
self.approx_order = 7
|
self.approx_order = 7
|
||||||
|
|
||||||
|
if "balance" in kwargs:
|
||||||
if 'balance' in kwargs:
|
self.balance = bool(kwargs.get("balance"))
|
||||||
self.balance = bool( kwargs.get('balance') )
|
del kwargs["balance"]
|
||||||
del kwargs['balance']
|
|
||||||
else:
|
else:
|
||||||
self.balance = False
|
self.balance = False
|
||||||
|
|
||||||
super(sde_StdPeriodic, self).__init__(*args, **kwargs)
|
super(sde_StdPeriodic, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def sde_update_gradient_full(self, gradients):
|
def sde_update_gradient_full(self, gradients):
|
||||||
"""
|
"""
|
||||||
Update gradient in the order in which parameters are represented in the
|
Update gradient in the order in which parameters are represented in the
|
||||||
kernel
|
kernel
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.variance.gradient = gradients[0]
|
self.variance.gradient = gradients[0]
|
||||||
self.period.gradient = gradients[1]
|
self.period.gradient = gradients[1]
|
||||||
self.lengthscale.gradient = gradients[2]
|
self.lengthscale.gradient = gradients[2]
|
||||||
|
|
||||||
def sde(self):
|
def sde(self):
|
||||||
"""
|
"""
|
||||||
Return the state space representation of the standard periodic covariance.
|
Return the state space representation of the standard periodic covariance.
|
||||||
|
|
||||||
|
|
||||||
! Note: one must constrain lengthscale not to drop below 0.2. (independently of approximation order)
|
! Note: one must constrain lengthscale not to drop below 0.2. (independently of approximation order)
|
||||||
After this Bessel functions of the first becomes NaN. Rescaling
|
After this Bessel functions of the first becomes NaN. Rescaling
|
||||||
time variable might help.
|
time variable might help.
|
||||||
|
|
||||||
! Note: one must keep period also not very low. Because then
|
! Note: one must keep period also not very low. Because then
|
||||||
the gradients wrt wavelength become ustable.
|
the gradients wrt wavelength become ustable.
|
||||||
However this might depend on the data. For test example with
|
However this might depend on the data. For test example with
|
||||||
300 data points the low limit is 0.15.
|
300 data points the low limit is 0.15.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#import pdb; pdb.set_trace()
|
# import pdb; pdb.set_trace()
|
||||||
# Params to use: (in that order)
|
# Params to use: (in that order)
|
||||||
#self.variance
|
# self.variance
|
||||||
#self.period
|
# self.period
|
||||||
#self.lengthscale
|
# self.lengthscale
|
||||||
if self.approx_order is not None:
|
if self.approx_order is not None:
|
||||||
N = int(self.approx_order)
|
N = int(self.approx_order)
|
||||||
else:
|
else:
|
||||||
N = 7 # approximation order
|
N = 7 # approximation order
|
||||||
|
|
||||||
p_period = float(self.period)
|
p_period = float(self.period)
|
||||||
p_lengthscale = 2*float(self.lengthscale)
|
p_lengthscale = 2 * float(self.lengthscale)
|
||||||
p_variance = float(self.variance)
|
p_variance = float(self.variance)
|
||||||
|
|
||||||
w0 = 2*np.pi/p_period # frequency
|
w0 = 2 * np.pi / p_period # frequency
|
||||||
# lengthscale is multiplied by 2 because of different definition of lengthscale
|
# lengthscale is multiplied by 2 because of different definition of lengthscale
|
||||||
|
|
||||||
[q2,dq2l] = seriescoeff(N, p_lengthscale, p_variance)
|
[q2, dq2l] = seriescoeff(N, p_lengthscale, p_variance)
|
||||||
|
|
||||||
dq2l = 2*dq2l # This is because the lengthscale if multiplied by 2.
|
dq2l = 2 * dq2l # This is because the lengthscale if multiplied by 2.
|
||||||
|
|
||||||
eps = 1e-12
|
eps = 1e-12
|
||||||
if np.any( np.isfinite(q2) == False) or np.any( np.abs(q2) > 1.0/eps) or np.any( np.abs(q2) < eps):
|
if (
|
||||||
warnings.warn("sde_Periodic: Infinite, too small, or too large (eps={0:e}) values in q2 :".format(eps) + q2.__format__("") )
|
np.any(np.isfinite(q2) == False)
|
||||||
|
or np.any(np.abs(q2) > 1.0 / eps)
|
||||||
if np.any( np.isfinite(dq2l) == False) or np.any( np.abs(dq2l) > 1.0/eps) or np.any( np.abs(dq2l) < eps):
|
or np.any(np.abs(q2) < eps)
|
||||||
warnings.warn("sde_Periodic: Infinite, too small, or too large (eps={0:e}) values in dq2l :".format(eps) + q2.__format__("") )
|
):
|
||||||
|
warnings.warn(
|
||||||
|
"sde_Periodic: Infinite, too small, or too large (eps={0:e}) values in q2 :".format(
|
||||||
F = np.kron(np.diag(range(0,N+1)),np.array( ((0, -w0), (w0, 0)) ) )
|
eps
|
||||||
L = np.eye(2*(N+1))
|
)
|
||||||
Qc = np.zeros((2*(N+1), 2*(N+1)))
|
+ q2.__format__("")
|
||||||
P_inf = np.kron(np.diag(q2),np.eye(2))
|
)
|
||||||
H = np.kron(np.ones((1,N+1)),np.array((1,0)) )
|
|
||||||
|
if (
|
||||||
|
np.any(np.isfinite(dq2l) == False)
|
||||||
|
or np.any(np.abs(dq2l) > 1.0 / eps)
|
||||||
|
or np.any(np.abs(dq2l) < eps)
|
||||||
|
):
|
||||||
|
warnings.warn(
|
||||||
|
"sde_Periodic: Infinite, too small, or too large (eps={0:e}) values in dq2l :".format(
|
||||||
|
eps
|
||||||
|
)
|
||||||
|
+ q2.__format__("")
|
||||||
|
)
|
||||||
|
|
||||||
|
F = np.kron(np.diag(range(0, N + 1)), np.array(((0, -w0), (w0, 0))))
|
||||||
|
L = np.eye(2 * (N + 1))
|
||||||
|
Qc = np.zeros((2 * (N + 1), 2 * (N + 1)))
|
||||||
|
P_inf = np.kron(np.diag(q2), np.eye(2))
|
||||||
|
H = np.kron(np.ones((1, N + 1)), np.array((1, 0)))
|
||||||
P0 = P_inf.copy()
|
P0 = P_inf.copy()
|
||||||
|
|
||||||
# Derivatives
|
# Derivatives
|
||||||
dF = np.empty((F.shape[0], F.shape[1], 3))
|
dF = np.empty((F.shape[0], F.shape[1], 3))
|
||||||
dQc = np.empty((Qc.shape[0], Qc.shape[1], 3))
|
dQc = np.empty((Qc.shape[0], Qc.shape[1], 3))
|
||||||
dP_inf = np.empty((P_inf.shape[0], P_inf.shape[1], 3))
|
dP_inf = np.empty((P_inf.shape[0], P_inf.shape[1], 3))
|
||||||
|
|
||||||
# Derivatives wrt self.variance
|
# Derivatives wrt self.variance
|
||||||
dF[:,:,0] = np.zeros(F.shape)
|
dF[:, :, 0] = np.zeros(F.shape)
|
||||||
dQc[:,:,0] = np.zeros(Qc.shape)
|
dQc[:, :, 0] = np.zeros(Qc.shape)
|
||||||
dP_inf[:,:,0] = P_inf / p_variance
|
dP_inf[:, :, 0] = P_inf / p_variance
|
||||||
|
|
||||||
# Derivatives self.period
|
# Derivatives self.period
|
||||||
dF[:,:,1] = np.kron(np.diag(range(0,N+1)),np.array( ((0, w0), (-w0, 0)) ) / p_period );
|
dF[:, :, 1] = np.kron(
|
||||||
dQc[:,:,1] = np.zeros(Qc.shape)
|
np.diag(range(0, N + 1)), np.array(((0, w0), (-w0, 0))) / p_period
|
||||||
dP_inf[:,:,1] = np.zeros(P_inf.shape)
|
)
|
||||||
|
dQc[:, :, 1] = np.zeros(Qc.shape)
|
||||||
# Derivatives self.lengthscales
|
dP_inf[:, :, 1] = np.zeros(P_inf.shape)
|
||||||
dF[:,:,2] = np.zeros(F.shape)
|
|
||||||
dQc[:,:,2] = np.zeros(Qc.shape)
|
# Derivatives self.lengthscales
|
||||||
dP_inf[:,:,2] = np.kron(np.diag(dq2l),np.eye(2))
|
dF[:, :, 2] = np.zeros(F.shape)
|
||||||
|
dQc[:, :, 2] = np.zeros(Qc.shape)
|
||||||
|
dP_inf[:, :, 2] = np.kron(np.diag(dq2l), np.eye(2))
|
||||||
dP0 = dP_inf.copy()
|
dP0 = dP_inf.copy()
|
||||||
|
|
||||||
if self.balance:
|
if self.balance:
|
||||||
# Benefits of this are not very sound.
|
# Benefits of this are not very sound.
|
||||||
import GPy.models.state_space_main as ssm
|
import GPy.models.state_space_main as ssm
|
||||||
(F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf,dP0) = ssm.balance_ss_model(F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0 )
|
|
||||||
|
(F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0) = ssm.balance_ss_model(
|
||||||
|
F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0
|
||||||
|
)
|
||||||
|
|
||||||
return (F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0)
|
return (F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0)
|
||||||
|
|
||||||
|
|
||||||
|
def seriescoeff(m=6, lengthScale=1.0, magnSigma2=1.0, true_covariance=False):
|
||||||
|
|
||||||
def seriescoeff(m=6,lengthScale=1.0,magnSigma2=1.0, true_covariance=False):
|
|
||||||
"""
|
"""
|
||||||
Calculate the coefficients q_j^2 for the covariance function
|
Calculate the coefficients q_j^2 for the covariance function
|
||||||
approximation:
|
approximation:
|
||||||
|
|
||||||
k(\tau) = \sum_{j=0}^{+\infty} q_j^2 \cos(j\omega_0 \tau)
|
k(\tau) = \sum_{j=0}^{+\infty} q_j^2 \cos(j\omega_0 \tau)
|
||||||
|
|
||||||
Reference is:
|
Reference is:
|
||||||
|
|
||||||
[1] Arno Solin and Simo Särkkä (2014). Explicit link between periodic
|
[1] Arno Solin and Simo Särkkä (2014). Explicit link between periodic
|
||||||
covariance functions and state space models. In Proceedings of the
|
covariance functions and state space models. In Proceedings of the
|
||||||
Seventeenth International Conference on Artifcial Intelligence and
|
Seventeenth International Conference on Artifcial Intelligence and
|
||||||
Statistics (AISTATS 2014). JMLR: W&CP, volume 33.
|
Statistics (AISTATS 2014). JMLR: W&CP, volume 33.
|
||||||
|
|
||||||
Note! Only the infinite approximation (through Bessel function)
|
Note! Only the infinite approximation (through Bessel function)
|
||||||
is currently implemented.
|
is currently implemented.
|
||||||
|
|
||||||
Input:
|
Input:
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
m: int
|
m: int
|
||||||
Degree of approximation. Default 6.
|
Degree of approximation. Default 6.
|
||||||
lengthScale: float
|
lengthScale: float
|
||||||
Length scale parameter in the kerenl
|
Length scale parameter in the kerenl
|
||||||
magnSigma2:float
|
magnSigma2:float
|
||||||
Multiplier in front of the kernel.
|
Multiplier in front of the kernel.
|
||||||
|
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
coeffs: array(m+1)
|
coeffs: array(m+1)
|
||||||
Covariance series coefficients
|
Covariance series coefficients
|
||||||
|
|
||||||
coeffs_dl: array(m+1)
|
coeffs_dl: array(m+1)
|
||||||
Derivatives of the coefficients with respect to lengthscale.
|
Derivatives of the coefficients with respect to lengthscale.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if true_covariance:
|
if true_covariance:
|
||||||
|
|
||||||
bb = lambda j,m: (1.0 + np.array((j != 0), dtype=np.float64) ) / (2**(j)) *\
|
bb = (
|
||||||
sp.special.binom(j, sp.floor( (j-m)/2.0 * np.array(m<=j, dtype=np.float64) ))*\
|
lambda j, m: (1.0 + np.array((j != 0), dtype=np.float64))
|
||||||
np.array(m<=j, dtype=np.float64) *np.array(sp.mod(j-m,2)==0, dtype=np.float64)
|
/ (2 ** (j))
|
||||||
|
* sp.special.binom(
|
||||||
M,J = np.meshgrid(range(0,m+1),range(0,m+1))
|
j, sp.floor((j - m) / 2.0 * np.array(m <= j, dtype=np.float64))
|
||||||
|
)
|
||||||
coeffs = bb(J,M) / sp.misc.factorial(J) * sp.exp( -lengthScale**(-2) ) *\
|
* np.array(m <= j, dtype=np.float64)
|
||||||
(lengthScale**(-2))**J *magnSigma2
|
* np.array(sp.mod(j - m, 2) == 0, dtype=np.float64)
|
||||||
|
)
|
||||||
coeffs_dl = np.sum( coeffs*lengthScale**(-3)*(2.0-2.0*J*lengthScale**2),0)
|
|
||||||
|
M, J = np.meshgrid(range(0, m + 1), range(0, m + 1))
|
||||||
coeffs = np.sum(coeffs,0)
|
|
||||||
|
coeffs = (
|
||||||
|
bb(J, M)
|
||||||
|
/ sp.misc.factorial(J)
|
||||||
|
* np.exp(-(lengthScale ** (-2)))
|
||||||
|
* (lengthScale ** (-2)) ** J
|
||||||
|
* magnSigma2
|
||||||
|
)
|
||||||
|
|
||||||
|
coeffs_dl = np.sum(
|
||||||
|
coeffs * lengthScale ** (-3) * (2.0 - 2.0 * J * lengthScale**2), 0
|
||||||
|
)
|
||||||
|
|
||||||
|
coeffs = np.sum(coeffs, 0)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
coeffs = 2*magnSigma2*sp.exp( -lengthScale**(-2) ) * special.iv(range(0,m+1),1.0/lengthScale**(2))
|
coeffs = (
|
||||||
if np.any( np.isfinite(coeffs) == False):
|
2
|
||||||
|
* magnSigma2
|
||||||
|
* np.exp(-(lengthScale ** (-2)))
|
||||||
|
* special.iv(range(0, m + 1), 1.0 / lengthScale ** (2))
|
||||||
|
)
|
||||||
|
if np.any(np.isfinite(coeffs) == False):
|
||||||
raise ValueError("sde_standard_periodic: Coefficients are not finite!")
|
raise ValueError("sde_standard_periodic: Coefficients are not finite!")
|
||||||
#import pdb; pdb.set_trace()
|
# import pdb; pdb.set_trace()
|
||||||
coeffs[0] = 0.5*coeffs[0]
|
coeffs[0] = 0.5 * coeffs[0]
|
||||||
#print(coeffs)
|
# print(coeffs)
|
||||||
# Derivatives wrt (lengthScale)
|
# Derivatives wrt (lengthScale)
|
||||||
coeffs_dl = np.zeros(m+1)
|
coeffs_dl = np.zeros(m + 1)
|
||||||
coeffs_dl[1:] = magnSigma2*lengthScale**(-3) * sp.exp(-lengthScale**(-2))*\
|
coeffs_dl[1:] = (
|
||||||
(-4*special.iv(range(0,m),lengthScale**(-2)) + 4*(1+np.arange(1,m+1)*lengthScale**(2))*special.iv(range(1,m+1),lengthScale**(-2)) )
|
magnSigma2
|
||||||
|
* lengthScale ** (-3)
|
||||||
|
* np.exp(-(lengthScale ** (-2)))
|
||||||
|
* (
|
||||||
|
-4 * special.iv(range(0, m), lengthScale ** (-2))
|
||||||
|
+ 4
|
||||||
|
* (1 + np.arange(1, m + 1) * lengthScale ** (2))
|
||||||
|
* special.iv(range(1, m + 1), lengthScale ** (-2))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# The first element
|
# The first element
|
||||||
coeffs_dl[0] = magnSigma2*lengthScale**(-3) * np.exp(-lengthScale**(-2))*\
|
coeffs_dl[0] = (
|
||||||
(2*special.iv(0,lengthScale**(-2)) - 2*special.iv(1,lengthScale**(-2)) )
|
magnSigma2
|
||||||
|
* lengthScale ** (-3)
|
||||||
|
* np.exp(-(lengthScale ** (-2)))
|
||||||
|
* (
|
||||||
|
2 * special.iv(0, lengthScale ** (-2))
|
||||||
|
- 2 * special.iv(1, lengthScale ** (-2))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return coeffs.squeeze(), coeffs_dl.squeeze()
|
return coeffs.squeeze(), coeffs_dl.squeeze()
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,14 @@ from .stationary import RatQuad
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import scipy as sp
|
import scipy as sp
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from scipy.linalg import solve_continuous_lyapunov as lyap
|
from scipy.linalg import solve_continuous_lyapunov as lyap
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from scipy.linalg import solve_lyapunov as lyap
|
from scipy.linalg import solve_lyapunov as lyap
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
class sde_RBF(RBF):
|
class sde_RBF(RBF):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
@ -30,37 +32,35 @@ class sde_RBF(RBF):
|
||||||
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} }
|
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 __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Init constructior.
|
Init constructior.
|
||||||
|
|
||||||
Two optinal extra parameters are added in addition to the ones in
|
Two optinal extra parameters are added in addition to the ones in
|
||||||
RBF kernel.
|
RBF kernel.
|
||||||
|
|
||||||
:param approx_order: approximation order for the RBF covariance. (Default 10)
|
:param approx_order: approximation order for the RBF covariance. (Default 10)
|
||||||
:type approx_order: int
|
:type approx_order: int
|
||||||
|
|
||||||
:param balance: Whether to balance this kernel separately. (Defaulf True). Model has a separate parameter for balancing.
|
:param balance: Whether to balance this kernel separately. (Defaulf True). Model has a separate parameter for balancing.
|
||||||
:type balance: bool
|
:type balance: bool
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if 'balance' in kwargs:
|
if "balance" in kwargs:
|
||||||
self.balance = bool( kwargs.get('balance') )
|
self.balance = bool(kwargs.get("balance"))
|
||||||
del kwargs['balance']
|
del kwargs["balance"]
|
||||||
else:
|
else:
|
||||||
self.balance = True
|
self.balance = True
|
||||||
|
|
||||||
|
if "approx_order" in kwargs:
|
||||||
if 'approx_order' in kwargs:
|
self.approx_order = kwargs.get("approx_order")
|
||||||
self.approx_order = kwargs.get('approx_order')
|
del kwargs["approx_order"]
|
||||||
del kwargs['approx_order']
|
|
||||||
else:
|
else:
|
||||||
self.approx_order = 6
|
self.approx_order = 6
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
super(sde_RBF, self).__init__(*args, **kwargs)
|
super(sde_RBF, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def sde_update_gradient_full(self, gradients):
|
def sde_update_gradient_full(self, gradients):
|
||||||
"""
|
"""
|
||||||
Update gradient in the order in which parameters are represented in the
|
Update gradient in the order in which parameters are represented in the
|
||||||
|
|
@ -73,86 +73,111 @@ class sde_RBF(RBF):
|
||||||
def sde(self):
|
def sde(self):
|
||||||
"""
|
"""
|
||||||
Return the state space representation of the covariance.
|
Return the state space representation of the covariance.
|
||||||
|
|
||||||
Note! For Sparse GP inference too small or two high values of lengthscale
|
Note! For Sparse GP inference too small or two high values of lengthscale
|
||||||
lead to instabilities. This is because Qc are too high or too low
|
lead to instabilities. This is because Qc are too high or too low
|
||||||
and P_inf are not full rank. This effect depends on approximatio order.
|
and P_inf are not full rank. This effect depends on approximatio order.
|
||||||
For N = 10. lengthscale must be in (0.8,8). For other N tests must be conducted.
|
For N = 10. lengthscale must be in (0.8,8). For other N tests must be conducted.
|
||||||
N=6: (0.06,31)
|
N=6: (0.06,31)
|
||||||
Variance should be within reasonable bounds as well, but its dependence is linear.
|
Variance should be within reasonable bounds as well, but its dependence is linear.
|
||||||
|
|
||||||
The above facts do not take into accout regularization.
|
The above facts do not take into accout regularization.
|
||||||
"""
|
"""
|
||||||
#import pdb; pdb.set_trace()
|
# import pdb; pdb.set_trace()
|
||||||
if self.approx_order is not None:
|
if self.approx_order is not None:
|
||||||
N = self.approx_order
|
N = self.approx_order
|
||||||
else:
|
else:
|
||||||
N = 10# approximation order ( number of terms in exponent series expansion)
|
N = 10 # approximation order ( number of terms in exponent series expansion)
|
||||||
|
|
||||||
roots_rounding_decimals = 6
|
roots_rounding_decimals = 6
|
||||||
|
|
||||||
fn = np.math.factorial(N)
|
fn = np.math.factorial(N)
|
||||||
|
|
||||||
p_lengthscale = float( self.lengthscale )
|
p_lengthscale = float(self.lengthscale)
|
||||||
p_variance = float(self.variance)
|
p_variance = float(self.variance)
|
||||||
kappa = 1.0/2.0/p_lengthscale**2
|
kappa = 1.0 / 2.0 / p_lengthscale**2
|
||||||
|
|
||||||
|
Qc = np.array(((p_variance * np.sqrt(np.pi / kappa) * fn * (4 * kappa) ** N,),))
|
||||||
|
|
||||||
Qc = np.array( ((p_variance*np.sqrt(np.pi/kappa)*fn*(4*kappa)**N,),) )
|
|
||||||
|
|
||||||
eps = 1e-12
|
eps = 1e-12
|
||||||
if (float(Qc) > 1.0/eps) or (float(Qc) < eps):
|
if (float(Qc) > 1.0 / eps) or (float(Qc) < eps):
|
||||||
warnings.warn("""sde_RBF kernel: the noise variance Qc is either very large or very small.
|
warnings.warn(
|
||||||
It influece conditioning of P_inf: {0:e}""".format(float(Qc)) )
|
"""sde_RBF kernel: the noise variance Qc is either very large or very small.
|
||||||
|
It influece conditioning of P_inf: {0:e}""".format(
|
||||||
|
float(Qc)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
pp1 = np.zeros((2*N+1,)) # array of polynomial coefficients from higher power to lower
|
pp1 = np.zeros(
|
||||||
|
(2 * N + 1,)
|
||||||
|
) # array of polynomial coefficients from higher power to lower
|
||||||
|
|
||||||
for n in range(0, N+1): # (2N+1) - number of polynomial coefficients
|
for n in range(0, N + 1): # (2N+1) - number of polynomial coefficients
|
||||||
pp1[2*(N-n)] = fn*(4.0*kappa)**(N-n)/np.math.factorial(n)*(-1)**n
|
pp1[2 * (N - n)] = (
|
||||||
|
fn * (4.0 * kappa) ** (N - n) / np.math.factorial(n) * (-1) ** n
|
||||||
pp = sp.poly1d(pp1)
|
)
|
||||||
roots = sp.roots(pp)
|
|
||||||
|
|
||||||
neg_real_part_roots = roots[np.round(np.real(roots) ,roots_rounding_decimals) < 0]
|
pp = np.poly1d(pp1)
|
||||||
aa = sp.poly1d(neg_real_part_roots, r=True).coeffs
|
roots = np.roots(pp)
|
||||||
|
|
||||||
F = np.diag(np.ones((N-1,)),1)
|
neg_real_part_roots = roots[
|
||||||
F[-1,:] = -aa[-1:0:-1]
|
np.round(np.real(roots), roots_rounding_decimals) < 0
|
||||||
|
]
|
||||||
|
aa = np.poly1d(neg_real_part_roots, r=True).coeffs
|
||||||
|
|
||||||
L= np.zeros((N,1))
|
F = np.diag(np.ones((N - 1,)), 1)
|
||||||
L[N-1,0] = 1
|
F[-1, :] = -aa[-1:0:-1]
|
||||||
|
|
||||||
H = np.zeros((1,N))
|
L = np.zeros((N, 1))
|
||||||
H[0,0] = 1
|
L[N - 1, 0] = 1
|
||||||
|
|
||||||
|
H = np.zeros((1, N))
|
||||||
|
H[0, 0] = 1
|
||||||
|
|
||||||
# Infinite covariance:
|
# Infinite covariance:
|
||||||
Pinf = lyap(F, -np.dot(L,np.dot( Qc[0,0],L.T)))
|
Pinf = lyap(F, -np.dot(L, np.dot(Qc[0, 0], L.T)))
|
||||||
Pinf = 0.5*(Pinf + Pinf.T)
|
Pinf = 0.5 * (Pinf + Pinf.T)
|
||||||
# Allocating space for derivatives
|
# Allocating space for derivatives
|
||||||
dF = np.empty([F.shape[0],F.shape[1],2])
|
dF = np.empty([F.shape[0], F.shape[1], 2])
|
||||||
dQc = np.empty([Qc.shape[0],Qc.shape[1],2])
|
dQc = np.empty([Qc.shape[0], Qc.shape[1], 2])
|
||||||
dPinf = np.empty([Pinf.shape[0],Pinf.shape[1],2])
|
dPinf = np.empty([Pinf.shape[0], Pinf.shape[1], 2])
|
||||||
|
|
||||||
# Derivatives:
|
# Derivatives:
|
||||||
dFvariance = np.zeros(F.shape)
|
dFvariance = np.zeros(F.shape)
|
||||||
dFlengthscale = np.zeros(F.shape)
|
dFlengthscale = np.zeros(F.shape)
|
||||||
dFlengthscale[-1,:] = -aa[-1:0:-1]/p_lengthscale * np.arange(-N,0,1)
|
dFlengthscale[-1, :] = -aa[-1:0:-1] / p_lengthscale * np.arange(-N, 0, 1)
|
||||||
|
|
||||||
dQcvariance = Qc/p_variance
|
dQcvariance = Qc / p_variance
|
||||||
dQclengthscale = np.array(( (p_variance*np.sqrt(2*np.pi)*fn*2**N*p_lengthscale**(-2*N)*(1-2*N),),))
|
dQclengthscale = np.array(
|
||||||
|
(
|
||||||
dPinf_variance = Pinf/p_variance
|
(
|
||||||
|
p_variance
|
||||||
|
* np.sqrt(2 * np.pi)
|
||||||
|
* fn
|
||||||
|
* 2**N
|
||||||
|
* p_lengthscale ** (-2 * N)
|
||||||
|
* (1 - 2 * N),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
dPinf_variance = Pinf / p_variance
|
||||||
|
|
||||||
lp = Pinf.shape[0]
|
lp = Pinf.shape[0]
|
||||||
coeff = np.arange(1,lp+1).reshape(lp,1) + np.arange(1,lp+1).reshape(1,lp) - 2
|
coeff = (
|
||||||
coeff[np.mod(coeff,2) != 0] = 0
|
np.arange(1, lp + 1).reshape(lp, 1)
|
||||||
dPinf_lengthscale = -1/p_lengthscale*Pinf*coeff
|
+ np.arange(1, lp + 1).reshape(1, lp)
|
||||||
|
- 2
|
||||||
|
)
|
||||||
|
coeff[np.mod(coeff, 2) != 0] = 0
|
||||||
|
dPinf_lengthscale = -1 / p_lengthscale * Pinf * coeff
|
||||||
|
|
||||||
dF[:,:,0] = dFvariance
|
dF[:, :, 0] = dFvariance
|
||||||
dF[:,:,1] = dFlengthscale
|
dF[:, :, 1] = dFlengthscale
|
||||||
dQc[:,:,0] = dQcvariance
|
dQc[:, :, 0] = dQcvariance
|
||||||
dQc[:,:,1] = dQclengthscale
|
dQc[:, :, 1] = dQclengthscale
|
||||||
dPinf[:,:,0] = dPinf_variance
|
dPinf[:, :, 0] = dPinf_variance
|
||||||
dPinf[:,:,1] = dPinf_lengthscale
|
dPinf[:, :, 1] = dPinf_lengthscale
|
||||||
|
|
||||||
P0 = Pinf.copy()
|
P0 = Pinf.copy()
|
||||||
dP0 = dPinf.copy()
|
dP0 = dPinf.copy()
|
||||||
|
|
@ -161,10 +186,14 @@ class sde_RBF(RBF):
|
||||||
# Benefits of this are not very sound. Helps only in one case:
|
# Benefits of this are not very sound. Helps only in one case:
|
||||||
# SVD Kalman + RBF kernel
|
# SVD Kalman + RBF kernel
|
||||||
import GPy.models.state_space_main as ssm
|
import GPy.models.state_space_main as ssm
|
||||||
(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf,dP0) = ssm.balance_ss_model(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0 )
|
|
||||||
|
(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0) = ssm.balance_ss_model(
|
||||||
|
F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0
|
||||||
|
)
|
||||||
|
|
||||||
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
|
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
|
||||||
|
|
||||||
|
|
||||||
class sde_Exponential(Exponential):
|
class sde_Exponential(Exponential):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
@ -195,30 +224,31 @@ class sde_Exponential(Exponential):
|
||||||
variance = float(self.variance.values)
|
variance = float(self.variance.values)
|
||||||
lengthscale = float(self.lengthscale)
|
lengthscale = float(self.lengthscale)
|
||||||
|
|
||||||
F = np.array(((-1.0/lengthscale,),))
|
F = np.array(((-1.0 / lengthscale,),))
|
||||||
L = np.array(((1.0,),))
|
L = np.array(((1.0,),))
|
||||||
Qc = np.array( ((2.0*variance/lengthscale,),) )
|
Qc = np.array(((2.0 * variance / lengthscale,),))
|
||||||
H = np.array(((1.0,),))
|
H = np.array(((1.0,),))
|
||||||
Pinf = np.array(((variance,),))
|
Pinf = np.array(((variance,),))
|
||||||
P0 = Pinf.copy()
|
P0 = Pinf.copy()
|
||||||
|
|
||||||
dF = np.zeros((1,1,2));
|
dF = np.zeros((1, 1, 2))
|
||||||
dQc = np.zeros((1,1,2));
|
dQc = np.zeros((1, 1, 2))
|
||||||
dPinf = np.zeros((1,1,2));
|
dPinf = np.zeros((1, 1, 2))
|
||||||
|
|
||||||
dF[:,:,0] = 0.0
|
dF[:, :, 0] = 0.0
|
||||||
dF[:,:,1] = 1.0/lengthscale**2
|
dF[:, :, 1] = 1.0 / lengthscale**2
|
||||||
|
|
||||||
dQc[:,:,0] = 2.0/lengthscale
|
dQc[:, :, 0] = 2.0 / lengthscale
|
||||||
dQc[:,:,1] = -2.0*variance/lengthscale**2
|
dQc[:, :, 1] = -2.0 * variance / lengthscale**2
|
||||||
|
|
||||||
dPinf[:,:,0] = 1.0
|
dPinf[:, :, 0] = 1.0
|
||||||
dPinf[:,:,1] = 0.0
|
dPinf[:, :, 1] = 0.0
|
||||||
|
|
||||||
dP0 = dPinf.copy()
|
dP0 = dPinf.copy()
|
||||||
|
|
||||||
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
|
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
|
||||||
|
|
||||||
|
|
||||||
class sde_RatQuad(RatQuad):
|
class sde_RatQuad(RatQuad):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
@ -238,12 +268,12 @@ class sde_RatQuad(RatQuad):
|
||||||
Return the state space representation of the covariance.
|
Return the state space representation of the covariance.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
assert False, 'Not Implemented'
|
assert False, "Not Implemented"
|
||||||
|
|
||||||
# Params to use:
|
# Params to use:
|
||||||
|
|
||||||
# self.lengthscale
|
# self.lengthscale
|
||||||
# self.variance
|
# self.variance
|
||||||
#self.power
|
# self.power
|
||||||
|
|
||||||
#return (F, L, Qc, H, Pinf, dF, dQc, dPinf)
|
# return (F, L, Qc, H, Pinf, dF, dQc, dPinf)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#===============================================================================
|
# ===============================================================================
|
||||||
# Copyright (c) 2015, Max Zwiessele
|
# Copyright (c) 2015, Max Zwiessele
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
|
|
@ -26,12 +26,12 @@
|
||||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
#===============================================================================
|
# ===============================================================================
|
||||||
|
|
||||||
from matplotlib import cm
|
from matplotlib import pyplot
|
||||||
from .. import Tango
|
from .. import Tango
|
||||||
|
|
||||||
'''
|
"""
|
||||||
This file is for defaults for the gpy plot, specific to the plotting library.
|
This file is for defaults for the gpy plot, specific to the plotting library.
|
||||||
|
|
||||||
Create a kwargs dictionary with the right name for the plotting function
|
Create a kwargs dictionary with the right name for the plotting function
|
||||||
|
|
@ -40,36 +40,55 @@ the plotting library will be used.
|
||||||
|
|
||||||
In the code, always ise plotting.gpy_plots.defaults to get the defaults, as
|
In the code, always ise plotting.gpy_plots.defaults to get the defaults, as
|
||||||
it gives back an empty default, when defaults are not defined.
|
it gives back an empty default, when defaults are not defined.
|
||||||
'''
|
"""
|
||||||
|
|
||||||
# Data plots:
|
# Data plots:
|
||||||
data_1d = dict(lw=1.5, marker='x', color='k')
|
data_1d = dict(lw=1.5, marker="x", color="k")
|
||||||
data_2d = dict(s=35, edgecolors='none', linewidth=0., cmap=cm.get_cmap('hot'), alpha=.5)
|
data_2d = dict(
|
||||||
inducing_1d = dict(lw=0, s=500, color=Tango.colorsHex['darkRed'])
|
s=35, edgecolors="none", linewidth=0.0, cmap=pyplot.get_cmap("hot"), alpha=0.5
|
||||||
inducing_2d = dict(s=17, edgecolor='k', linewidth=.4, color='white', alpha=.5, marker='^')
|
)
|
||||||
inducing_3d = dict(lw=.3, s=500, color=Tango.colorsHex['darkRed'], edgecolor='k')
|
inducing_1d = dict(lw=0, s=500, color=Tango.colorsHex["darkRed"])
|
||||||
xerrorbar = dict(color='k', fmt='none', elinewidth=.5, alpha=.5)
|
inducing_2d = dict(
|
||||||
yerrorbar = dict(color=Tango.colorsHex['darkRed'], fmt='none', elinewidth=.5, alpha=.5)
|
s=17, edgecolor="k", linewidth=0.4, color="white", alpha=0.5, marker="^"
|
||||||
|
)
|
||||||
|
inducing_3d = dict(lw=0.3, s=500, color=Tango.colorsHex["darkRed"], edgecolor="k")
|
||||||
|
xerrorbar = dict(color="k", fmt="none", elinewidth=0.5, alpha=0.5)
|
||||||
|
yerrorbar = dict(
|
||||||
|
color=Tango.colorsHex["darkRed"], fmt="none", elinewidth=0.5, alpha=0.5
|
||||||
|
)
|
||||||
|
|
||||||
# GP plots:
|
# GP plots:
|
||||||
meanplot_1d = dict(color=Tango.colorsHex['mediumBlue'], linewidth=2)
|
meanplot_1d = dict(color=Tango.colorsHex["mediumBlue"], linewidth=2)
|
||||||
meanplot_2d = dict(cmap='hot', linewidth=.5)
|
meanplot_2d = dict(cmap="hot", linewidth=0.5)
|
||||||
meanplot_3d = dict(linewidth=0, antialiased=True, cstride=1, rstride=1, cmap='hot', alpha=.3)
|
meanplot_3d = dict(
|
||||||
samples_1d = dict(color=Tango.colorsHex['mediumBlue'], linewidth=.3)
|
linewidth=0, antialiased=True, cstride=1, rstride=1, cmap="hot", alpha=0.3
|
||||||
samples_3d = dict(cmap='hot', alpha=.1, antialiased=True, cstride=1, rstride=1, linewidth=0)
|
)
|
||||||
confidence_interval = dict(edgecolor=Tango.colorsHex['darkBlue'], linewidth=.5, color=Tango.colorsHex['lightBlue'],alpha=.2)
|
samples_1d = dict(color=Tango.colorsHex["mediumBlue"], linewidth=0.3)
|
||||||
density = dict(alpha=.5, color=Tango.colorsHex['lightBlue'])
|
samples_3d = dict(
|
||||||
|
cmap="hot", alpha=0.1, antialiased=True, cstride=1, rstride=1, linewidth=0
|
||||||
|
)
|
||||||
|
confidence_interval = dict(
|
||||||
|
edgecolor=Tango.colorsHex["darkBlue"],
|
||||||
|
linewidth=0.5,
|
||||||
|
color=Tango.colorsHex["lightBlue"],
|
||||||
|
alpha=0.2,
|
||||||
|
)
|
||||||
|
density = dict(alpha=0.5, color=Tango.colorsHex["lightBlue"])
|
||||||
|
|
||||||
# GPLVM plots:
|
# GPLVM plots:
|
||||||
data_y_1d = dict(linewidth=0, cmap='RdBu', s=40)
|
data_y_1d = dict(linewidth=0, cmap="RdBu", s=40)
|
||||||
data_y_1d_plot = dict(color='k', linewidth=1.5)
|
data_y_1d_plot = dict(color="k", linewidth=1.5)
|
||||||
|
|
||||||
# Kernel plots:
|
# Kernel plots:
|
||||||
ard = dict(edgecolor='k', linewidth=1.2)
|
ard = dict(edgecolor="k", linewidth=1.2)
|
||||||
|
|
||||||
# Input plots:
|
# Input plots:
|
||||||
latent = dict(aspect='auto', cmap='Greys', interpolation='bicubic')
|
latent = dict(aspect="auto", cmap="Greys", interpolation="bicubic")
|
||||||
gradient = dict(aspect='auto', cmap='RdBu', interpolation='nearest', alpha=.7)
|
gradient = dict(aspect="auto", cmap="RdBu", interpolation="nearest", alpha=0.7)
|
||||||
magnification = dict(aspect='auto', cmap='Greys', interpolation='bicubic')
|
magnification = dict(aspect="auto", cmap="Greys", interpolation="bicubic")
|
||||||
latent_scatter = dict(s=20, linewidth=.2, edgecolor='k', alpha=.9)
|
latent_scatter = dict(s=20, linewidth=0.2, edgecolor="k", alpha=0.9)
|
||||||
annotation = dict(fontdict=dict(family='sans-serif', weight='light', fontsize=9), zorder=.3, alpha=.7)
|
annotation = dict(
|
||||||
|
fontdict=dict(family="sans-serif", weight="light", fontsize=9),
|
||||||
|
zorder=0.3,
|
||||||
|
alpha=0.7,
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
from matplotlib import pyplot as pb, numpy as np
|
from matplotlib import pyplot as pb
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
def plot(parameterized, fignum=None, ax=None, colors=None, figsize=(12, 6)):
|
def plot(parameterized, fignum=None, ax=None, colors=None, figsize=(12, 6)):
|
||||||
"""
|
"""
|
||||||
|
|
@ -17,6 +19,7 @@ def plot(parameterized, fignum=None, ax=None, colors=None, figsize=(12, 6)):
|
||||||
if colors is None:
|
if colors is None:
|
||||||
from ..Tango import mediumList
|
from ..Tango import mediumList
|
||||||
from itertools import cycle
|
from itertools import cycle
|
||||||
|
|
||||||
colors = cycle(mediumList)
|
colors = cycle(mediumList)
|
||||||
pb.clf()
|
pb.clf()
|
||||||
else:
|
else:
|
||||||
|
|
@ -33,21 +36,30 @@ def plot(parameterized, fignum=None, ax=None, colors=None, figsize=(12, 6)):
|
||||||
a = ax[i]
|
a = ax[i]
|
||||||
else:
|
else:
|
||||||
raise ValueError("Need one ax per latent dimension input_dim")
|
raise ValueError("Need one ax per latent dimension input_dim")
|
||||||
bg_lines.append(a.plot(means, c='k', alpha=.3))
|
bg_lines.append(a.plot(means, c="k", alpha=0.3))
|
||||||
lines.extend(a.plot(x, means.T[i], c=next(colors), label=r"$\mathbf{{X_{{{}}}}}$".format(i)))
|
lines.extend(
|
||||||
fills.append(a.fill_between(x,
|
a.plot(
|
||||||
means.T[i] - 2 * np.sqrt(variances.T[i]),
|
x, means.T[i], c=next(colors), label=r"$\mathbf{{X_{{{}}}}}$".format(i)
|
||||||
means.T[i] + 2 * np.sqrt(variances.T[i]),
|
)
|
||||||
facecolor=lines[-1].get_color(),
|
)
|
||||||
alpha=.3))
|
fills.append(
|
||||||
a.legend(borderaxespad=0.)
|
a.fill_between(
|
||||||
|
x,
|
||||||
|
means.T[i] - 2 * np.sqrt(variances.T[i]),
|
||||||
|
means.T[i] + 2 * np.sqrt(variances.T[i]),
|
||||||
|
facecolor=lines[-1].get_color(),
|
||||||
|
alpha=0.3,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
a.legend(borderaxespad=0.0)
|
||||||
a.set_xlim(x.min(), x.max())
|
a.set_xlim(x.min(), x.max())
|
||||||
if i < means.shape[1] - 1:
|
if i < means.shape[1] - 1:
|
||||||
a.set_xticklabels('')
|
a.set_xticklabels("")
|
||||||
pb.draw()
|
pb.draw()
|
||||||
a.figure.tight_layout(h_pad=.01) # , rect=(0, 0, 1, .95))
|
a.figure.tight_layout(h_pad=0.01) # , rect=(0, 0, 1, .95))
|
||||||
return dict(lines=lines, fills=fills, bg_lines=bg_lines)
|
return dict(lines=lines, fills=fills, bg_lines=bg_lines)
|
||||||
|
|
||||||
|
|
||||||
def plot_SpikeSlab(parameterized, fignum=None, ax=None, colors=None, side_by_side=True):
|
def plot_SpikeSlab(parameterized, fignum=None, ax=None, colors=None, side_by_side=True):
|
||||||
"""
|
"""
|
||||||
Plot latent space X in 1D:
|
Plot latent space X in 1D:
|
||||||
|
|
@ -62,45 +74,60 @@ def plot_SpikeSlab(parameterized, fignum=None, ax=None, colors=None, side_by_sid
|
||||||
"""
|
"""
|
||||||
if ax is None:
|
if ax is None:
|
||||||
if side_by_side:
|
if side_by_side:
|
||||||
fig = pb.figure(num=fignum, figsize=(16, min(12, (2 * parameterized.mean.shape[1]))))
|
fig = pb.figure(
|
||||||
|
num=fignum, figsize=(16, min(12, (2 * parameterized.mean.shape[1])))
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
fig = pb.figure(num=fignum, figsize=(8, min(12, (2 * parameterized.mean.shape[1]))))
|
fig = pb.figure(
|
||||||
|
num=fignum, figsize=(8, min(12, (2 * parameterized.mean.shape[1])))
|
||||||
|
)
|
||||||
if colors is None:
|
if colors is None:
|
||||||
from ..Tango import mediumList
|
from ..Tango import mediumList
|
||||||
from itertools import cycle
|
from itertools import cycle
|
||||||
|
|
||||||
colors = cycle(mediumList)
|
colors = cycle(mediumList)
|
||||||
pb.clf()
|
pb.clf()
|
||||||
else:
|
else:
|
||||||
colors = iter(colors)
|
colors = iter(colors)
|
||||||
plots = []
|
plots = []
|
||||||
means, variances, gamma = parameterized.mean, parameterized.variance, parameterized.binary_prob
|
means, variances, gamma = (
|
||||||
|
parameterized.mean,
|
||||||
|
parameterized.variance,
|
||||||
|
parameterized.binary_prob,
|
||||||
|
)
|
||||||
x = np.arange(means.shape[0])
|
x = np.arange(means.shape[0])
|
||||||
for i in range(means.shape[1]):
|
for i in range(means.shape[1]):
|
||||||
if side_by_side:
|
if side_by_side:
|
||||||
sub1 = (means.shape[1],2,2*i+1)
|
sub1 = (means.shape[1], 2, 2 * i + 1)
|
||||||
sub2 = (means.shape[1],2,2*i+2)
|
sub2 = (means.shape[1], 2, 2 * i + 2)
|
||||||
else:
|
else:
|
||||||
sub1 = (means.shape[1]*2,1,2*i+1)
|
sub1 = (means.shape[1] * 2, 1, 2 * i + 1)
|
||||||
sub2 = (means.shape[1]*2,1,2*i+2)
|
sub2 = (means.shape[1] * 2, 1, 2 * i + 2)
|
||||||
|
|
||||||
# mean and variance plot
|
# mean and variance plot
|
||||||
a = fig.add_subplot(*sub1)
|
a = fig.add_subplot(*sub1)
|
||||||
a.plot(means, c='k', alpha=.3)
|
a.plot(means, c="k", alpha=0.3)
|
||||||
plots.extend(a.plot(x, means.T[i], c=next(colors), label=r"$\mathbf{{X_{{{}}}}}$".format(i)))
|
plots.extend(
|
||||||
a.fill_between(x,
|
a.plot(
|
||||||
means.T[i] - 2 * np.sqrt(variances.T[i]),
|
x, means.T[i], c=next(colors), label=r"$\mathbf{{X_{{{}}}}}$".format(i)
|
||||||
means.T[i] + 2 * np.sqrt(variances.T[i]),
|
)
|
||||||
facecolor=plots[-1].get_color(),
|
)
|
||||||
alpha=.3)
|
a.fill_between(
|
||||||
a.legend(borderaxespad=0.)
|
x,
|
||||||
|
means.T[i] - 2 * np.sqrt(variances.T[i]),
|
||||||
|
means.T[i] + 2 * np.sqrt(variances.T[i]),
|
||||||
|
facecolor=plots[-1].get_color(),
|
||||||
|
alpha=0.3,
|
||||||
|
)
|
||||||
|
a.legend(borderaxespad=0.0)
|
||||||
a.set_xlim(x.min(), x.max())
|
a.set_xlim(x.min(), x.max())
|
||||||
if i < means.shape[1] - 1:
|
if i < means.shape[1] - 1:
|
||||||
a.set_xticklabels('')
|
a.set_xticklabels("")
|
||||||
# binary prob plot
|
# binary prob plot
|
||||||
a = fig.add_subplot(*sub2)
|
a = fig.add_subplot(*sub2)
|
||||||
a.bar(x,gamma[:,i],bottom=0.,linewidth=1.,width=1.0,align='center')
|
a.bar(x, gamma[:, i], bottom=0.0, linewidth=1.0, width=1.0, align="center")
|
||||||
a.set_xlim(x.min(), x.max())
|
a.set_xlim(x.min(), x.max())
|
||||||
a.set_ylim([0.,1.])
|
a.set_ylim([0.0, 1.0])
|
||||||
pb.draw()
|
pb.draw()
|
||||||
fig.tight_layout(h_pad=.01) # , rect=(0, 0, 1, .95))
|
fig.tight_layout(h_pad=0.01) # , rect=(0, 0, 1, .95))
|
||||||
return fig
|
return fig
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ class TestGridModel:
|
||||||
self.dim = self.X.shape[1]
|
self.dim = self.X.shape[1]
|
||||||
|
|
||||||
def test_alpha_match(self):
|
def test_alpha_match(self):
|
||||||
|
self.setup()
|
||||||
kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True)
|
kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True)
|
||||||
m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel)
|
m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel)
|
||||||
|
|
||||||
|
|
@ -38,6 +39,7 @@ class TestGridModel:
|
||||||
np.testing.assert_almost_equal(m.posterior.alpha, m2.posterior.woodbury_vector)
|
np.testing.assert_almost_equal(m.posterior.alpha, m2.posterior.woodbury_vector)
|
||||||
|
|
||||||
def test_gradient_match(self):
|
def test_gradient_match(self):
|
||||||
|
self.setup()
|
||||||
kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True)
|
kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True)
|
||||||
m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel)
|
m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel)
|
||||||
|
|
||||||
|
|
@ -55,6 +57,7 @@ class TestGridModel:
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_prediction_match(self):
|
def test_prediction_match(self):
|
||||||
|
self.setup()
|
||||||
kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True)
|
kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True)
|
||||||
m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel)
|
m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -544,6 +544,7 @@ class TestKernelGradientContinuous:
|
||||||
assert check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose)
|
assert check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose)
|
||||||
|
|
||||||
def test_OU(self):
|
def test_OU(self):
|
||||||
|
self.setup()
|
||||||
k = GPy.kern.OU(self.D - 1, ARD=True)
|
k = GPy.kern.OU(self.D - 1, ARD=True)
|
||||||
k.randomize()
|
k.randomize()
|
||||||
assert check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose)
|
assert check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue