Skip to content

Commit 3b52aa3

Browse files
committed
Acceleration
1 parent 2bffa9f commit 3b52aa3

File tree

6 files changed

+43
-35
lines changed

6 files changed

+43
-35
lines changed

bayesml/bernoulli/_bernoulli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ def calc_pred_dist(self):
386386
"""Calculate the parameters of the predictive distribution."""
387387
self.p_theta = self.hn_alpha / (self.hn_alpha + self.hn_beta)
388388

389+
def _calc_pred_density(self,x):
390+
if x:
391+
return self.p_theta
392+
else:
393+
return 1.0-self.p_theta
394+
389395
def make_prediction(self,loss="squared"):
390396
"""Predict a new data point under the given criterion.
391397

bayesml/exponential/_exponential.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ def calc_pred_dist(self):
382382
self.p_kappa = self.hn_alpha
383383
self.p_lambda = self.hn_beta
384384

385+
def _calc_pred_density(self,x):
386+
return ss_lomax.pdf(x,c=self.p_kappa,scale=self.p_lambda)
387+
385388
def make_prediction(self,loss="squared"):
386389
"""Predict a new data point under the given criterion.
387390

bayesml/metatree/_metatree.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def _gen_sample_recursion(self,node:_Node,x_continuous,x_categorical):
413413
else:
414414
if node.k < self.c_dim_continuous:
415415
for i in range(self.c_num_children_vec[node.k]):
416-
if node.thresholds[i] < x_continuous[node.k] and x_continuous[node.k] < node.thresholds[i+1]:
416+
if x_continuous[node.k] < node.thresholds[i+1]:
417417
index = i
418418
break
419419
else:
@@ -1698,40 +1698,26 @@ def _copy_tree_from_sklearn_tree(self,new_node:_Node, original_tree,node_id):
16981698
new_node.h_g = 0.0
16991699
new_node.leaf = True
17001700

1701-
def _update_posterior_leaf(self,node:_Node,x_continuous,y):
1702-
try:
1703-
node.sub_model.calc_pred_dist(x_continuous)
1704-
except:
1705-
node.sub_model.calc_pred_dist()
1706-
pred_dist = node.sub_model.make_prediction(loss='KL') # Futurework: direct method to get marginal likelihood is better
1707-
1708-
try:
1709-
node.sub_model.update_posterior(x_continuous,y)
1710-
except:
1711-
node.sub_model.update_posterior(y)
1712-
1713-
if type(pred_dist) is np.ndarray:
1714-
return pred_dist[y]
1715-
try:
1716-
return pred_dist.pdf(y)
1717-
except:
1718-
return pred_dist.pmf(y)
1701+
def _update_posterior_leaf(self,node:_Node,y):
1702+
node.sub_model.calc_pred_dist()
1703+
node.sub_model.update_posterior(y)
1704+
return node.sub_model._calc_pred_density(y)
17191705

17201706
def _update_posterior_recursion(self,node:_Node,x_continuous,x_categorical,y):
17211707
if not node.leaf: # inner node
17221708
if node.k < self.c_dim_continuous:
17231709
for i in range(self.c_num_children_vec[node.k]):
1724-
if node.thresholds[i] < x_continuous[node.k] and x_continuous[node.k] < node.thresholds[i+1]:
1710+
if x_continuous[node.k] < node.thresholds[i+1]:
17251711
index = i
17261712
break
17271713
else:
17281714
index = x_categorical[node.k-self.c_dim_continuous]
17291715
tmp1 = self._update_posterior_recursion(node.children[index],x_continuous,x_categorical,y)
1730-
tmp2 = (1 - node.h_g) * self._update_posterior_leaf(node,x_continuous,y) + node.h_g * tmp1
1716+
tmp2 = (1 - node.h_g) * self._update_posterior_leaf(node,y) + node.h_g * tmp1
17311717
node.h_g = node.h_g * tmp1 / tmp2
17321718
return tmp2
17331719
else: # leaf node
1734-
return self._update_posterior_leaf(node,x_continuous,y)
1720+
return self._update_posterior_leaf(node,y)
17351721

17361722
def _compare_metatree_recursion(self,node1:_Node,node2:_Node):
17371723
if node1.leaf:
@@ -2302,7 +2288,7 @@ def _calc_pred_dist_recursion(self,node:_Node,x_continuous,x_categorical):
23022288
if not node.leaf: # inner node
23032289
if node.k < self.c_dim_continuous:
23042290
for i in range(self.c_num_children_vec[node.k]):
2305-
if node.thresholds[i] < x_continuous[node.k] and x_continuous[node.k] < node.thresholds[i+1]:
2291+
if x_continuous[node.k] < node.thresholds[i+1]:
23062292
index = i
23072293
break
23082294
else:
@@ -2377,7 +2363,7 @@ def _make_prediction_recursion_squared(self,node:_Node):
23772363
else: # inner node
23782364
if node.k < self.c_dim_continuous:
23792365
for i in range(self.c_num_children_vec[node.k]):
2380-
if node.thresholds[i] < self._tmp_x_continuous[node.k] and self._tmp_x_continuous[node.k] < node.thresholds[i+1]:
2366+
if self._tmp_x_continuous[node.k] < node.thresholds[i+1]:
23812367
index = i
23822368
break
23832369
else:
@@ -2391,7 +2377,7 @@ def _make_prediction_recursion_kl(self,node:_Node):
23912377
else: # inner node
23922378
if node.k < self.c_dim_continuous:
23932379
for i in range(self.c_num_children_vec[node.k]):
2394-
if node.thresholds[i] < self._tmp_x_continuous[node.k] and self._tmp_x_continuous[node.k] < node.thresholds[i+1]:
2380+
if self._tmp_x_continuous[node.k] < node.thresholds[i+1]:
23952381
index = i
23962382
break
23972383
else:

bayesml/metatree/metatree_test.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,35 @@
22
from bayesml import normal
33
from bayesml import poisson
44
from bayesml import bernoulli
5+
from bayesml import exponential
56
import numpy as np
6-
import copy
7+
import time
78

8-
dim_continuous = 0
9-
dim_categorical = 2
9+
dim_continuous = 2
10+
dim_categorical = 0
1011

1112
gen_model = metatree.GenModel(
1213
c_dim_continuous=dim_continuous,
1314
c_dim_categorical=dim_categorical,
14-
h_g=0.75,
15-
sub_h_params={'h_alpha':0.1,'h_beta':0.1})
15+
h_g=1.0,
16+
SubModel=normal,
17+
sub_h_params={'h_kappa':0.1})
18+
# sub_h_params={'h_alpha':0.1,'h_beta':0.1})
1619
gen_model.gen_params(threshold_type='even')
17-
gen_model.visualize_model(filename='tree.pdf')
20+
# gen_model.visualize_model(filename='tree.pdf')
1821

1922
x_continuous,x_categorical,y = gen_model.gen_sample(200)
20-
x_continuous_test,x_categorical_test,y_test = gen_model.gen_sample(10)
2123

2224
learn_model = metatree.LearnModel(
2325
c_dim_continuous=dim_continuous,
2426
c_dim_categorical=dim_categorical,
2527
c_num_children_vec=2,
26-
sub_h0_params={'h0_alpha':0.1,'h0_beta':0.1})
28+
SubModel=normal,
29+
sub_h0_params={'h0_kappa':0.1})
30+
# sub_h0_params={'h0_alpha':0.1,'h0_beta':0.1})
31+
32+
start = time.time()
2733
learn_model.update_posterior(x_continuous,x_categorical,y)
28-
for i in range(10):
29-
print(learn_model.pred_and_update(x_continuous_test[i],x_categorical_test[i],y_test[i],loss='0-1'))
34+
end = time.time()
35+
36+
print(end-start)

bayesml/normal/_normal.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ def calc_pred_dist(self):
466466
self.p_nu = 2*self.hn_alpha
467467
self.p_lambda = self.hn_kappa / (self.hn_kappa+1) * self.hn_alpha / self.hn_beta
468468

469+
def _calc_pred_density(self,x):
470+
return ss_t.pdf(x,loc=self.p_mu,scale=1.0/np.sqrt(self.p_lambda),df=self.p_nu)
471+
469472
def make_prediction(self,loss="squared"):
470473
"""Predict a new data point under the given criterion.
471474

bayesml/poisson/_poisson.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ def calc_pred_dist(self):
377377
self.p_r = self.hn_alpha
378378
self.p_theta = 1.0 / (1.0+self.hn_beta)
379379

380+
def _calc_pred_density(self,x):
381+
return ss_nbinom.pmf(x,n=self.p_r,p=(1.0-self.p_theta))
382+
380383
def make_prediction(self,loss="squared"):
381384
"""Predict a new data point under the given criterion.
382385

0 commit comments

Comments
 (0)