Skip to content

Develop metatree categorical #65

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 2 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions bayesml/categorical/_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import warnings
import numpy as np
from scipy.stats import dirichlet as ss_dirichlet
from scipy.special import gammaln
import matplotlib.pyplot as plt

from .. import base
Expand Down Expand Up @@ -553,3 +554,16 @@ def pred_and_update(self, x, loss="squared",onehot=True):
prediction = self.make_prediction(loss,onehot)
self.update_posterior(x,onehot)
return prediction

def calc_log_marginal_likelihood(self):
"""Calculate log marginal likelihood

Returns
-------
log_marginal_likelihood : float
The log marginal likelihood.
"""
return (gammaln(self.h0_alpha_vec.sum())
-gammaln(self.h0_alpha_vec).sum()
-gammaln(self.hn_alpha_vec.sum())
+gammaln(self.hn_alpha_vec).sum())
14 changes: 13 additions & 1 deletion bayesml/metatree/metatree_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bayesml import metatree
from bayesml import linearregression
from bayesml import categorical
import numpy as np
import time

Expand All @@ -12,6 +12,8 @@
c_max_depth=5,
h_g=1.0,
seed=0,
SubModel=categorical,
sub_constants={'c_degree':3},
)
gen_model.gen_params()
true_root = gen_model.get_params()
Expand All @@ -21,10 +23,20 @@
c_dim_continuous=dim_continuous,
c_dim_categorical=dim_categorical,
c_max_depth=5,
SubModel=categorical,
sub_constants={'c_degree':3},
)
learn_model.set_h0_params(h0_metatree_list=[true_root['root']])
learn_model.set_h0_params(h0_g=0.5)

# learn_model.update_posterior(
# x_continuous,
# x_categorical,
# y,
# alg_type='given_MT',
# )
# learn_model.visualize_posterior(filename='posterior3.gv')

time_vec = np.empty(100)
for i in range(100):
learn_model.reset_hn_params()
Expand Down