Skip to content

Instantly share code, notes, and snippets.

@ShuaiGuo16
Last active January 26, 2021 10:16
Show Gist options
  • Save ShuaiGuo16/932e0288cfc6903b4d81857773d1255a to your computer and use it in GitHub Desktop.
Save ShuaiGuo16/932e0288cfc6903b4d81857773d1255a to your computer and use it in GitHub Desktop.
Calculate negative log-likelihood
def Neglikelihood(self, theta):
"""Negative likelihood function
Input
-----
theta: array, logarithm of the correlation legnths for different dimensions
Output
------
LnLike: likelihood value"""
theta = 10**theta # Correlation length
n = self.X.shape[0] # Number of training instances
one = np.ones((n,1)) # Vector of ones
# Construct correlation matrix
K = self.Corr(self.X, self.X, theta) + np.eye(n)*1e-10
inv_K = np.linalg.inv(K) # Inverse of correlation matrix
# Mean estimation
mu = (one.T @ inv_K @ self.y)/ (one.T @ inv_K @ one)
# Variance estimation
SigmaSqr = (self.y-mu*one).T @ inv_K @ (self.y-mu*one) / n
# Compute log-likelihood
DetK = np.linalg.det(K)
LnLike = -(n/2)*np.log(SigmaSqr) - 0.5*np.log(DetK)
# Update attributes
self.K, self.inv_K , self.mu, self.SigmaSqr = K, inv_K, mu, SigmaSqr
return -LnLike.flatten()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment