Skip to content

Add calc_log_marginal_likelihood #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion bayesml/bernoulli/_bernoulli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import warnings
import numpy as np
from scipy.stats import beta as ss_beta
# from scipy.stats import betabinom as ss_betabinom
from scipy.special import gammaln
import matplotlib.pyplot as plt

from .. import base
Expand Down Expand Up @@ -486,3 +486,32 @@ def pred_and_update(self,x,loss="squared"):
prediction = self.make_prediction(loss=loss)
self.update_posterior(x)
return prediction

def calc_log_marginal_likelihood(self,x,posterior_update=True):
"""Calculate log marginal likelihood of given data

Parameters
----------
x : numpy.ndarray
All the elements must be 0 or 1.
posterior_update : bool, optional
If True, posterior distribution will be
updated at the same time, by default True.

Returns
-------
log_marginal_likelihood : float
The log margina likelihood.
"""
self.update_posterior(x)
tmp = (gammaln(self.h0_alpha+self.h0_beta)
-gammaln(self.h0_alpha)
-gammaln(self.h0_beta)
-gammaln(self.hn_alpha+self.hn_beta)
+gammaln(self.hn_alpha)
+gammaln(self.hn_beta))

if not posterior_update:
self.reset_hn_params()

return tmp