Skip to content

Commit

Permalink
Fix invalid escape sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
janmayer committed Oct 21, 2024
1 parent aa49220 commit 4fa858e
Show file tree
Hide file tree
Showing 45 changed files with 224 additions and 224 deletions.
30 changes: 15 additions & 15 deletions GPy/core/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def parameters_changed(self):

def log_likelihood(self):
"""
The log marginal likelihood of the model, :math:`p(\mathbf{y})`, this is the objective function of the model being optimised
The log marginal likelihood of the model, :math:`p(\\mathbf{y})`, this is the objective function of the model being optimised
"""
return self._log_marginal_likelihood

Expand All @@ -296,9 +296,9 @@ def _raw_predict(self, Xnew, full_cov=False, kern=None):
diagonal of the covariance is returned.
.. math::
p(f*|X*, X, Y) = \int^{\inf}_{\inf} p(f*|f,X*)p(f|X,Y) df
= N(f*| K_{x*x}(K_{xx} + \Sigma)^{-1}Y, K_{x*x*} - K_{xx*}(K_{xx} + \Sigma)^{-1}K_{xx*}
\Sigma := \texttt{Likelihood.variance / Approximate likelihood covariance}
p(f*|X*, X, Y) = \\int^{\\inf}_{\\inf} p(f*|f,X*)p(f|X,Y) df
= N(f*| K_{x*x}(K_{xx} + \\Sigma)^{-1}Y, K_{x*x*} - K_{xx*}(K_{xx} + \\Sigma)^{-1}K_{xx*}
\\Sigma := \texttt{Likelihood.variance / Approximate likelihood covariance}
"""
mu, var = self.posterior._raw_predict(kern=self.kern if kern is None else kern, Xnew=Xnew, pred_var=self._predictive_variable, full_cov=full_cov)
if self.mean_function is not None:
Expand Down Expand Up @@ -702,7 +702,7 @@ def log_predictive_density(self, x_test, y_test, Y_metadata=None):
Calculation of the log predictive density
.. math:
p(y_{*}|D) = p(y_{*}|f_{*})p(f_{*}|\mu_{*}\\sigma^{2}_{*})
p(y_{*}|D) = p(y_{*}|f_{*})p(f_{*}|\\mu_{*}\\sigma^{2}_{*})
:param x_test: test locations (x_{*})
:type x_test: (Nx1) array
Expand All @@ -718,7 +718,7 @@ def log_predictive_density_sampling(self, x_test, y_test, Y_metadata=None, num_s
Calculation of the log predictive density by sampling
.. math:
p(y_{*}|D) = p(y_{*}|f_{*})p(f_{*}|\mu_{*}\\sigma^{2}_{*})
p(y_{*}|D) = p(y_{*}|f_{*})p(f_{*}|\\mu_{*}\\sigma^{2}_{*})
:param x_test: test locations (x_{*})
:type x_test: (Nx1) array
Expand All @@ -734,24 +734,24 @@ def log_predictive_density_sampling(self, x_test, y_test, Y_metadata=None, num_s

def _raw_posterior_covariance_between_points(self, X1, X2):
"""
Computes the posterior covariance between points. Does not account for
Computes the posterior covariance between points. Does not account for
normalization or likelihood
:param X1: some input observations
:param X2: other input observations
:returns:
:returns:
cov: raw posterior covariance: k(X1,X2) - k(X1,X) G^{-1} K(X,X2)
"""
return self.posterior.covariance_between_points(self.kern, self.X, X1, X2)


def posterior_covariance_between_points(self, X1, X2, Y_metadata=None,
likelihood=None,
def posterior_covariance_between_points(self, X1, X2, Y_metadata=None,
likelihood=None,
include_likelihood=True):
"""
Computes the posterior covariance between points. Includes likelihood
variance as well as normalization so that evaluation at (x,x) is consistent
Computes the posterior covariance between points. Includes likelihood
variance as well as normalization so that evaluation at (x,x) is consistent
with model.predict
:param X1: some input observations
Expand All @@ -762,8 +762,8 @@ def posterior_covariance_between_points(self, X1, X2, Y_metadata=None,
the predicted underlying latent function f.
:type include_likelihood: bool
:returns:
cov: posterior covariance, a Numpy array, Nnew x Nnew if
:returns:
cov: posterior covariance, a Numpy array, Nnew x Nnew if
self.output_dim == 1, and Nnew x Nnew x self.output_dim otherwise.
"""

Expand All @@ -774,7 +774,7 @@ def posterior_covariance_between_points(self, X1, X2, Y_metadata=None,
mean, _ = self._raw_predict(X1, full_cov=True)
if likelihood is None:
likelihood = self.likelihood
_, cov = likelihood.predictive_values(mean, cov, full_cov=True,
_, cov = likelihood.predictive_values(mean, cov, full_cov=True,
Y_metadata=Y_metadata)

if self.normalizer is not None:
Expand Down
38 changes: 19 additions & 19 deletions GPy/core/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self, expressions, cacheable, derivatives=None, parameters=None, fu
self._set_derivatives(derivatives)
self._set_parameters(parameters)
# Convert the expressions to a list for common sub expression elimination
# We should find the following type of expressions: 'function', 'derivative', 'second_derivative', 'third_derivative'.
# We should find the following type of expressions: 'function', 'derivative', 'second_derivative', 'third_derivative'.
self.update_expression_list()

# Apply any global stabilisation operations to expressions.
Expand Down Expand Up @@ -86,7 +86,7 @@ def extract_vars(expr):
# object except as cached. For covariance functions this is X
# and Z, for likelihoods F and for mapping functions X.
self.cacheable_vars = [] # list of everything that's cacheable
for var in cacheable:
for var in cacheable:
self.variables[var] = [e for e in vars if e.name.split('_')[0]==var.lower()]
self.cacheable_vars += self.variables[var]
for var in cacheable:
Expand All @@ -105,7 +105,7 @@ def extract_derivative(function, derivative_arguments):
for derivative in derivatives:
derivative_arguments += self.variables[derivative]

# Do symbolic work to compute derivatives.
# Do symbolic work to compute derivatives.
for key, func in self.expressions.items():
# if func['function'].is_Matrix:
# rows = func['function'].shape[0]
Expand All @@ -126,7 +126,7 @@ def _set_parameters(self, parameters):
if theta.name in parameters:
val = parameters[theta.name]
# Add parameter.

self.link_parameters(Param(theta.name, val, None))
#self._set_attribute(theta.name, )

Expand Down Expand Up @@ -174,7 +174,7 @@ def eval_update_gradients(self, function, partial, **kwargs):
code = self.code[function]['derivative'][theta.name]
gradient[theta.name] = (partial*eval(code, self.namespace)).sum()
return gradient

def eval_gradients_X(self, function, partial, **kwargs):
if 'X' in kwargs:
gradients_X = np.zeros_like(kwargs['X'])
Expand All @@ -194,7 +194,7 @@ def code_parameters_changed(self):
for variable, code in self.variable_sort(self.code['parameters_changed']):
lcode += self._print_code(variable) + ' = ' + self._print_code(code) + '\n'
return lcode

def code_update_cache(self):
lcode = ''
for var in self.cacheable:
Expand All @@ -208,7 +208,7 @@ def code_update_cache(self):
for i, theta in enumerate(self.variables[var]):
lcode+= "\t" + var + '= np.atleast_2d(' + var + ')\n'
lcode+= "\t" + self._print_code(theta.name) + ' = ' + var + '[:, ' + str(i) + "]" + reorder + "\n"

for variable, code in self.variable_sort(self.code['update_cache']):
lcode+= self._print_code(variable) + ' = ' + self._print_code(code) + "\n"

Expand Down Expand Up @@ -250,7 +250,7 @@ def _set_attribute(self, name, value):
"""Make sure namespace gets updated when setting attributes."""
setattr(self, name, value)
self.namespace.update({name: getattr(self, name)})


def update_expression_list(self):
"""Extract a list of expressions from the dictionary of expressions."""
Expand All @@ -260,9 +260,9 @@ def update_expression_list(self):
for fname, fexpressions in self.expressions.items():
for type, texpressions in fexpressions.items():
if type == 'function':
self.expression_list.append(texpressions)
self.expression_list.append(texpressions)
self.expression_keys.append([fname, type])
self.expression_order.append(1)
self.expression_order.append(1)
elif type[-10:] == 'derivative':
for dtype, expression in texpressions.items():
self.expression_list.append(expression)
Expand All @@ -274,9 +274,9 @@ def update_expression_list(self):
elif type[:-10] == 'third_':
self.expression_order.append(5) #sym.count_ops(self.expressions[type][dtype]))
else:
self.expression_list.append(fexpressions[type])
self.expression_list.append(fexpressions[type])
self.expression_keys.append([fname, type])
self.expression_order.append(2)
self.expression_order.append(2)

# This step may be unecessary.
# Not 100% sure if the sub expression elimination is order sensitive. This step orders the list with the 'function' code first and derivatives after.
Expand Down Expand Up @@ -313,7 +313,7 @@ def extract_sub_expressions(self, cache_prefix='cache', sub_prefix='sub', prefix
sym_var = sym.var(cache_prefix + str(i))
self.variables[cache_prefix].append(sym_var)
replace_dict[expr.name] = sym_var

for i, expr in enumerate(params_change_list):
sym_var = sym.var(sub_prefix + str(i))
self.variables[sub_prefix].append(sym_var)
Expand All @@ -329,7 +329,7 @@ def extract_sub_expressions(self, cache_prefix='cache', sub_prefix='sub', prefix
for keys in self.expression_keys:
for replace, void in common_sub_expressions:
setInDict(self.expressions, keys, getFromDict(self.expressions, keys).subs(replace, replace_dict[replace.name]))

self.expressions['parameters_changed'] = {}
self.expressions['update_cache'] = {}
for var, expr in common_sub_expressions:
Expand All @@ -339,7 +339,7 @@ def extract_sub_expressions(self, cache_prefix='cache', sub_prefix='sub', prefix
self.expressions['update_cache'][replace_dict[var.name].name] = expr
else:
self.expressions['parameters_changed'][replace_dict[var.name].name] = expr


def _gen_code(self):
"""Generate code for the list of expressions provided using the common sub-expression eliminator to separate out portions that are computed multiple times."""
Expand All @@ -357,8 +357,8 @@ def match_key(expr):
return code

self.code = match_key(self.expressions)


def _expr2code(self, arg_list, expr):
"""Convert the given symbolic expression into code."""
code = lambdastr(arg_list, expr)
Expand All @@ -379,7 +379,7 @@ def _print_code(self, code):
def _display_expression(self, keys, user_substitutes={}):
"""Helper function for human friendly display of the symbolic components."""
# Create some pretty maths symbols for the display.
sigma, alpha, nu, omega, l, variance = sym.var('\sigma, \alpha, \nu, \omega, \ell, \sigma^2')
sigma, alpha, nu, omega, l, variance = sym.var(r'\sigma, \alpha, \nu, \omega, \ell, \sigma^2')
substitutes = {'scale': sigma, 'shape': alpha, 'lengthscale': l, 'variance': variance}
substitutes.update(user_substitutes)

Expand Down Expand Up @@ -416,5 +416,5 @@ def sort_key(x):
return int(digits[0])
else:
return x[0]

return sorted(var_dict.items(), key=sort_key, reverse=reverse)
2 changes: 1 addition & 1 deletion GPy/inference/latent_function_inference/laplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self):
"""
Laplace Approximation
Find the moments \hat{f} and the hessian at this point
Find the moments \\hat{f} and the hessian at this point
(using Newton-Raphson) of the unnormalised posterior
"""
Expand Down
8 changes: 4 additions & 4 deletions GPy/inference/latent_function_inference/posterior.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ def woodbury_inv(self):
"""
The inverse of the woodbury matrix, in the gaussian likelihood case it is defined as
$$
(K_{xx} + \Sigma_{xx})^{-1}
\Sigma_{xx} := \texttt{Likelihood.variance / Approximate likelihood covariance}
(K_{xx} + \\Sigma_{xx})^{-1}
\\Sigma_{xx} := \texttt{Likelihood.variance / Approximate likelihood covariance}
$$
"""
if self._woodbury_inv is None:
Expand All @@ -200,8 +200,8 @@ def woodbury_vector(self):
"""
Woodbury vector in the gaussian likelihood case only is defined as
$$
(K_{xx} + \Sigma)^{-1}Y
\Sigma := \texttt{Likelihood.variance / Approximate likelihood covariance}
(K_{xx} + \\Sigma)^{-1}Y
\\Sigma := \texttt{Likelihood.variance / Approximate likelihood covariance}
$$
"""
if self._woodbury_vector is None:
Expand Down
2 changes: 1 addition & 1 deletion GPy/kern/src/coregionalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


class Coregionalize(Kern):
"""
r"""
Covariance function for intrinsic/linear coregionalization models
This covariance has the form:
Expand Down
2 changes: 1 addition & 1 deletion GPy/kern/src/eq_ode1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EQ_ODE1(Kern):
This outputs of this kernel have the form
.. math::
\frac{\text{d}y_j}{\text{d}t} = \sum_{i=1}^R w_{j,i} u_i(t-\delta_j) - d_jy_j(t)
\frac{\text{d}y_j}{\text{d}t} = \\sum_{i=1}^R w_{j,i} u_i(t-\\delta_j) - d_jy_j(t)
where :math:`R` is the rank of the system, :math:`w_{j,i}` is the sensitivity of the :math:`j`th output to the :math:`i`th latent function, :math:`d_j` is the decay rate of the :math:`j`th output and :math:`u_i(t)` are independent latent Gaussian processes goverened by an exponentiated quadratic covariance.
Expand Down
2 changes: 1 addition & 1 deletion GPy/kern/src/eq_ode2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EQ_ODE2(Kern):
This outputs of this kernel have the form
.. math::
\frac{\text{d}^2y_j(t)}{\text{d}^2t} + C_j\frac{\text{d}y_j(t)}{\text{d}t} + B_jy_j(t) = \sum_{i=1}^R w_{j,i} u_i(t)
\frac{\text{d}^2y_j(t)}{\text{d}^2t} + C_j\frac{\text{d}y_j(t)}{\text{d}t} + B_jy_j(t) = \\sum_{i=1}^R w_{j,i} u_i(t)
where :math:`R` is the rank of the system, :math:`w_{j,i}` is the sensitivity of the :math:`j`th output to the :math:`i`th latent function, :math:`d_j` is the decay rate of the :math:`j`th output and :math:`f_i(t)` and :math:`g_i(t)` are independent latent Gaussian processes goverened by an exponentiated quadratic covariance.
Expand Down
2 changes: 1 addition & 1 deletion GPy/kern/src/grid_kerns.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GridRBF(GridKern):
.. math::
k(r) = \sigma^2 \exp \\bigg(- \\frac{1}{2} r^2 \\bigg)
k(r) = \\sigma^2 \\exp \\bigg(- \\frac{1}{2} r^2 \\bigg)
"""
_support_GPU = True
Expand Down
20 changes: 10 additions & 10 deletions GPy/kern/src/kern.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,25 @@ def Kdiag(self, X):
def psi0(self, Z, variational_posterior):
"""
.. math::
\psi_0 = \sum_{i=0}^{n}E_{q(X)}[k(X_i, X_i)]
\\psi_0 = \\sum_{i=0}^{n}E_{q(X)}[k(X_i, X_i)]
"""
return self.psicomp.psicomputations(self, Z, variational_posterior)[0]
def psi1(self, Z, variational_posterior):
"""
.. math::
\psi_1^{n,m} = E_{q(X)}[k(X_n, Z_m)]
\\psi_1^{n,m} = E_{q(X)}[k(X_n, Z_m)]
"""
return self.psicomp.psicomputations(self, Z, variational_posterior)[1]
def psi2(self, Z, variational_posterior):
"""
.. math::
\psi_2^{m,m'} = \sum_{i=0}^{n}E_{q(X)}[ k(Z_m, X_i) k(X_i, Z_{m'})]
\\psi_2^{m,m'} = \\sum_{i=0}^{n}E_{q(X)}[ k(Z_m, X_i) k(X_i, Z_{m'})]
"""
return self.psicomp.psicomputations(self, Z, variational_posterior, return_psi2_n=False)[2]
def psi2n(self, Z, variational_posterior):
"""
.. math::
\psi_2^{n,m,m'} = E_{q(X)}[ k(Z_m, X_n) k(X_n, Z_{m'})]
\\psi_2^{n,m,m'} = E_{q(X)}[ k(Z_m, X_n) k(X_n, Z_{m'})]
Thus, we do not sum out n, compared to psi2
"""
Expand All @@ -173,7 +173,7 @@ def gradients_X(self, dL_dK, X, X2):
"""
.. math::
\\frac{\partial L}{\partial X} = \\frac{\partial L}{\partial K}\\frac{\partial K}{\partial X}
\\frac{\\partial L}{\\partial X} = \\frac{\\partial L}{\\partial K}\\frac{\\partial K}{\\partial X}
"""
raise NotImplementedError
def gradients_X_X2(self, dL_dK, X, X2):
Expand All @@ -182,7 +182,7 @@ def gradients_XX(self, dL_dK, X, X2, cov=True):
"""
.. math::
\\frac{\partial^2 L}{\partial X\partial X_2} = \\frac{\partial L}{\partial K}\\frac{\partial^2 K}{\partial X\partial X_2}
\\frac{\\partial^2 L}{\\partial X\\partial X_2} = \\frac{\\partial L}{\\partial K}\\frac{\\partial^2 K}{\\partial X\\partial X_2}
"""
raise NotImplementedError("This is the second derivative of K wrt X and X2, and not implemented for this kernel")
def gradients_XX_diag(self, dL_dKdiag, X, cov=True):
Expand All @@ -203,7 +203,7 @@ def update_gradients_diag(self, dL_dKdiag, X):
def update_gradients_full(self, dL_dK, X, X2):
"""Set the gradients of all parameters when doing full (N) inference."""
raise NotImplementedError

def reset_gradients(self):
raise NotImplementedError

Expand All @@ -216,9 +216,9 @@ def update_gradients_expectations(self, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variati
.. math::
\\frac{\partial L}{\partial \\theta_i} & = \\frac{\partial L}{\partial \psi_0}\\frac{\partial \psi_0}{\partial \\theta_i}\\
& \quad + \\frac{\partial L}{\partial \psi_1}\\frac{\partial \psi_1}{\partial \\theta_i}\\
& \quad + \\frac{\partial L}{\partial \psi_2}\\frac{\partial \psi_2}{\partial \\theta_i}
\\frac{\\partial L}{\\partial \\theta_i} & = \\frac{\\partial L}{\\partial \\psi_0}\\frac{\\partial \\psi_0}{\\partial \\theta_i}\\
& \\quad + \\frac{\\partial L}{\\partial \\psi_1}\\frac{\\partial \\psi_1}{\\partial \\theta_i}\\
& \\quad + \\frac{\\partial L}{\\partial \\psi_2}\\frac{\\partial \\psi_2}{\\partial \\theta_i}
Thus, we push the different derivatives through the gradients of the psi
statistics. Be sure to set the gradients for all kernel
Expand Down
Loading

0 comments on commit 4fa858e

Please sign in to comment.