MCPcopy
hub / github.com/ddbourgin/numpy-ml / marginal_log_likelihood

Method marginal_log_likelihood

numpy_ml/nonparametric/gp.py:156–221  ·  view source on GitHub ↗

Compute the log of the marginal likelihood (i.e., the log model evidence), :math:`p(y \mid X, \\text{kernel_params})`. Notes ----- Under the GP regression model, the marginal likelihood is normally distributed: .. math:: y | X,

(self, kernel_params=None)

Source from the content-addressed store, hash-verified

154 return (pp_mean, conf) if not return_cov else (pp_mean, conf, pp_cov)
155
156 def marginal_log_likelihood(self, kernel_params=None):
157 """
158 Compute the log of the marginal likelihood (i.e., the log model
159 evidence), :math:`p(y \mid X, \\text{kernel_params})`.
160
161 Notes
162 -----
163 Under the GP regression model, the marginal likelihood is normally
164 distributed:
165
166 .. math::
167
168 y | X, \\theta \sim \mathcal{N}(0, K + \\alpha I)
169
170 Hence,
171
172 .. math::
173
174 \log p(y \mid X, \\theta) =
175 -0.5 \log \det(K + \\alpha I) -
176 0.5 y^\\top (K + \\alpha I)^{-1} y + \\frac{n}{2} \log 2 \pi
177
178 where :math:`K = \\text{kernel}(X, X)`, :math:`\\theta` is the set of
179 kernel parameters, and `n` is the number of dimensions in `K`.
180
181 Parameters
182 ----------
183 kernel_params : dict
184 Parameters for the kernel function. If None, calculate the
185 marginal likelihood under the kernel parameters defined at model
186 initialization. Default is None.
187
188 Returns
189 -------
190 marginal_log_likelihood : float
191 The log likelihood of the training targets given the kernel
192 parameterized by `kernel_params` and the training inputs,
193 marginalized over all functions `f`.
194 """
195 X = self.parameters["X"]
196 y = self.parameters["y"]
197 alpha = self.hyperparameters["alpha"]
198
199 K = self.parameters["GP_cov"]
200 if kernel_params is not None:
201 # create a new kernel with parameters `kernel_params` and recalc
202 # the GP covariance matrix
203 summary_dict = self.kernel.summary_dict()
204 summary_dict["parameters"].update(kernel_params)
205 kernel = KernelInitializer(summary_dict)()
206 K = kernel(X, X)
207
208 # add isotropic noise to kernel diagonal
209 K += np.eye(K.shape[0]) * alpha
210
211 Kinv = inv(K)
212 Klogdet = -0.5 * slogdet(K)[1]
213 const = K.shape[0] / 2 * np.log(2 * np.pi)

Callers 1

test_gp_regressionFunction · 0.95

Calls 2

KernelInitializerClass · 0.85
updateMethod · 0.45

Tested by 1

test_gp_regressionFunction · 0.76