Skip to content

Commit 10a408f

Browse files
committed
Add return self
1 parent 16a9351 commit 10a408f

File tree

13 files changed

+116
-14
lines changed

13 files changed

+116
-14
lines changed

bayesml/autoregressive/_autoregressive.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def set_h_params(self,h_mu_vec,h_lambda_mat,h_alpha,h_beta):
142142
if self.degree+1 != self.theta_vec.shape[0]:
143143
self.theta_vec = np.zeros(self.degree+1)
144144
warnings.warn("theta_vec is reinitialized to [0.0, 0.0, ..., 0.0] because dimension of theta_vec and h_parms are mismatched.", ParameterFormatWarning)
145+
return self
145146

146147
def get_h_params(self):
147148
"""Get the hyperparameters of the prior distribution.
@@ -163,6 +164,7 @@ def gen_params(self):
163164
"""
164165
self.tau = self.rng.gamma(shape=self.h_alpha,scale=1.0/self.h_beta)
165166
self.theta_vec = self.rng.multivariate_normal(mean=self.h_mu_vec,cov=np.linalg.inv(self.tau*self.h_lambda_mat))
167+
return self
166168

167169
def set_params(self,theta_vec, tau):
168170
"""Set the parameter of the sthocastic data generative model.
@@ -184,6 +186,7 @@ def set_params(self,theta_vec, tau):
184186
if self.degree+1 != self.h_lambda_mat.shape[0]:
185187
self.h_lambda_mat = np.identity(self.degree+1)
186188
warnings.warn("h_lambda_mat is reinitialized to the identity matrix because dimension of h_lambda_mat and theta_vec are mismatched.", ParameterFormatWarning)
189+
return self
187190

188191
def get_params(self):
189192
"""Get the parameter of the sthocastic data generative model.
@@ -406,6 +409,7 @@ def set_h0_params(self,h0_mu_vec,h0_lambda_mat,h0_alpha,h0_beta):
406409
"dimensions of h0_mu_vec and h0_lambda_mat must be the same"))
407410

408411
self.reset_hn_params()
412+
return self
409413

410414
def get_h0_params(self):
411415
"""Get the initial values of the hyperparameters of the posterior distribution.
@@ -460,6 +464,7 @@ def set_hn_params(self,hn_mu_vec,hn_lambda_mat,hn_alpha,hn_beta):
460464
raise(ParameterFormatError(
461465
"dimensions of hn_mu_vec and hn_lambda_mat must be the same"))
462466
self.calc_pred_dist(np.zeros(self.degree))
467+
return self
463468

464469
def overwrite_h0_params(self):
465470
"""Overwrite the initial values of the hyperparameters of the posterior distribution by the learned values.
@@ -474,6 +479,7 @@ def overwrite_h0_params(self):
474479
self.h0_beta = self.hn_beta
475480

476481
self.calc_pred_dist(np.zeros(self.degree))
482+
return self
477483

478484
def reset_hn_params(self):
479485
"""Reset the hyperparameters of the posterior distribution to their initial values.
@@ -488,6 +494,7 @@ def reset_hn_params(self):
488494
self.hn_beta = self.h0_beta
489495

490496
self.calc_pred_dist(np.zeros(self.degree))
497+
return self
491498

492499
def update_posterior(self,x,padding=None):
493500
"""Update the hyperparameters of the posterior distribution using traning data.
@@ -527,6 +534,7 @@ def update_posterior(self,x,padding=None):
527534
self.hn_alpha += (x.shape[0]-self.degree) / 2.0
528535
self.hn_beta += (-self.hn_mu_vec[np.newaxis,:] @ self.hn_lambda_mat @ self.hn_mu_vec[:,np.newaxis]
529536
+ x[self.degree:] @ x[self.degree:] + mu_tmp[np.newaxis,:] @ lambda_tmp @ mu_tmp[:,np.newaxis])[0,0] / 2.0
537+
return self
530538

531539
def estimate_params(self,loss="squared"):
532540
"""Estimate the parameter of the stochastic data generative model under the given criterion.
@@ -647,6 +655,7 @@ def calc_pred_dist(self,x):
647655
self.p_m = self.hn_mu_vec @ _explanatory_vec
648656
self.p_lambda = self.hn_alpha / self.hn_beta / (1.0 + _explanatory_vec @ np.linalg.solve(self.hn_lambda_mat,_explanatory_vec))
649657
self.p_nu = 2.0 * self.hn_alpha
658+
return self
650659

651660
def make_prediction(self,loss="squared"):
652661
"""Predict a new data point under the given criterion.

bayesml/base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def load_h_params(self,filename):
5858
tmp_h_params = pickle.load(f)
5959
if type(tmp_h_params) is dict:
6060
self.set_h_params(*tmp_h_params.values())
61-
return
61+
return self
6262

6363
raise(ParameterFormatError(
6464
filename+" must be a pickled python dictionary obtained by "
@@ -120,7 +120,7 @@ def load_params(self,filename):
120120
params = pickle.load(f)
121121
if type(params) is dict:
122122
self.set_params(*params.values())
123-
return
123+
return self
124124

125125
raise(ParameterFormatError(filename+" must be a pickled python dictionary obtained by ``GenModel.save_params()``"))
126126

@@ -189,7 +189,7 @@ def load_h0_params(self,filename):
189189
tmp_h_params = pickle.load(f)
190190
if type(tmp_h_params) is dict:
191191
self.set_h0_params(*tmp_h_params.values())
192-
return
192+
return self
193193

194194
raise(ParameterFormatError(
195195
filename+" must be a pickled python dictionary obtained by "
@@ -249,7 +249,7 @@ def load_hn_params(self,filename):
249249
tmp_h_params = pickle.load(f)
250250
if type(tmp_h_params) is dict:
251251
self.set_hn_params(*tmp_h_params.values())
252-
return
252+
return self
253253

254254
raise(ParameterFormatError(
255255
filename+" must be a pickled python dictionary obtained by "
@@ -264,6 +264,7 @@ def reset_hn_params(self):
264264
Note that the parameters of the predictive distribution are also calculated from them.
265265
"""
266266
self.set_hn_params(*self.get_h0_params().values())
267+
return self
267268

268269
def overwrite_h0_params(self):
269270
"""Overwrite the initial values of the hyperparameters of the posterior distribution by the learned values.
@@ -272,6 +273,7 @@ def overwrite_h0_params(self):
272273
Note that the parameters of the predictive distribution are also calculated from them.
273274
"""
274275
self.set_h0_params(*self.get_hn_params().values())
276+
return self
275277

276278
@abstractmethod
277279
def update_posterior(self):

bayesml/bernoulli/_bernoulli.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def set_h_params(self,h_alpha,h_beta):
4545
"""
4646
self.h_alpha = _check.pos_float(h_alpha,'h_alpha',ParameterFormatError)
4747
self.h_beta = _check.pos_float(h_beta,'h_beta',ParameterFormatError)
48+
return self
4849

4950
def get_h_params(self):
5051
"""Get the hyperparameters of the prior distribution.
@@ -63,7 +64,8 @@ def gen_params(self):
6364
The generated vaule is set at ``self.theta``.
6465
"""
6566
self.theta = self.rng.beta(self.h_alpha,self.h_beta)
66-
67+
return self
68+
6769
def set_params(self,theta):
6870
"""Set the parameter of the sthocastic data generative model.
6971
@@ -73,6 +75,7 @@ def set_params(self,theta):
7375
a real number :math:`\theta \in [0, 1]`
7476
"""
7577
self.theta = _check.float_in_closed01(theta,'theta',ParameterFormatError)
78+
return self
7679

7780
def get_params(self):
7881
"""Get the parameter of the sthocastic data generative model.
@@ -203,6 +206,7 @@ def set_h0_params(self,h0_alpha,h0_beta):
203206
self.h0_alpha = _check.pos_float(h0_alpha,'h0_alpha',ParameterFormatError)
204207
self.h0_beta = _check.pos_float(h0_beta,'h0_beta',ParameterFormatError)
205208
self.reset_hn_params()
209+
return self
206210

207211
def get_h0_params(self):
208212
"""Get the initial values of the hyperparameters of the posterior distribution.
@@ -228,6 +232,7 @@ def set_hn_params(self,hn_alpha,hn_beta):
228232
self.hn_alpha = _check.pos_float(hn_alpha,'hn_alpha',ParameterFormatError)
229233
self.hn_beta = _check.pos_float(hn_beta,'hn_beta',ParameterFormatError)
230234
self.calc_pred_dist()
235+
return self
231236

232237
def get_hn_params(self):
233238
"""Get the hyperparameters of the posterior distribution.
@@ -249,6 +254,7 @@ def reset_hn_params(self):
249254
self.hn_alpha = self.h0_alpha
250255
self.hn_beta = self.h0_beta
251256
self.calc_pred_dist()
257+
return self
252258

253259
def overwrite_h0_params(self):
254260
"""Overwrite the initial values of the hyperparameters of the posterior distribution by the learned values.
@@ -259,6 +265,7 @@ def overwrite_h0_params(self):
259265
self.h0_alpha = self.hn_alpha
260266
self.h0_beta = self.hn_beta
261267
self.calc_pred_dist()
268+
return self
262269

263270
def update_posterior(self,x):
264271
"""Update the hyperparameters of the posterior distribution using traning data.
@@ -271,6 +278,7 @@ def update_posterior(self,x):
271278
_check.ints_of_01(x,'x',DataFormatError)
272279
self.hn_alpha += np.sum(x==1)
273280
self.hn_beta += np.sum(x==0)
281+
return self
274282

275283
def estimate_params(self,loss="squared",dict_out=False):
276284
"""Estimate the parameter of the stochastic data generative model under the given criterion.
@@ -385,6 +393,7 @@ def get_p_params(self):
385393
def calc_pred_dist(self):
386394
"""Calculate the parameters of the predictive distribution."""
387395
self.p_theta = self.hn_alpha / (self.hn_alpha + self.hn_beta)
396+
return self
388397

389398
def make_prediction(self,loss="squared"):
390399
"""Predict a new data point under the given criterion.

bayesml/categorical/_categorical.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def set_h_params(self,h_alpha_vec):
9696
if self.degree != self.theta_vec.shape[0]:
9797
self.theta_vec = np.ones(self.degree) / self.degree
9898
warnings.warn("theta_vec is reinitialized to [1.0/self.degree, 1.0/self.degree, ..., 1.0/self.degree] because dimension of theta_vec and h_alpha_vec are mismatched.", ParameterFormatWarning)
99+
return self
99100

100101
def get_h_params(self):
101102
"""Get the hyperparameters of the prior distribution.
@@ -113,6 +114,7 @@ def gen_params(self):
113114
The generated vaule is set at ``self.theta_vec``.
114115
"""
115116
self.theta_vec[:] = self.rng.dirichlet(self.h_alpha_vec)
117+
return self
116118

117119
def set_params(self, theta_vec):
118120
"""Set the parameter of the sthocastic data generative model.
@@ -128,6 +130,7 @@ def set_params(self, theta_vec):
128130
if self.degree != self.h_alpha_vec.shape[0]:
129131
self.h_alpha_vec = np.ones(self.degree) / 2.0
130132
warnings.warn("h_alpha_vec is reinitialized to [1/2, 1/2, ..., 1/2] because dimension of h_m_vec and mu_vec are mismatched.", ParameterFormatWarning)
133+
return self
131134

132135
def get_params(self):
133136
"""Get the parameter of the sthocastic data generative model.
@@ -289,6 +292,7 @@ def set_h0_params(self,h0_alpha_vec):
289292
self.h0_alpha_vec = _check.pos_float_vec(h0_alpha_vec,'h0_alpha_vec',ParameterFormatError)
290293
self.degree = self.h0_alpha_vec.shape[0]
291294
self.reset_hn_params()
295+
return self
292296

293297
def get_h0_params(self):
294298
"""Get the initial values of the hyperparameters of the posterior distribution.
@@ -311,6 +315,7 @@ def set_hn_params(self,hn_alpha_vec):
311315
self.hn_alpha_vec = _check.pos_float_vec(hn_alpha_vec,'hn_alpha_vec',ParameterFormatError)
312316
self.degree = self.hn_alpha_vec.shape[0]
313317
self.calc_pred_dist()
318+
return self
314319

315320
def get_hn_params(self):
316321
"""Get the hyperparameters of the posterior distribution.
@@ -330,6 +335,7 @@ def reset_hn_params(self):
330335
"""
331336
self.hn_alpha_vec = np.copy(self.h0_alpha_vec)
332337
self.calc_pred_dist()
338+
return self
333339

334340
def overwrite_h0_params(self):
335341
"""Overwrite the initial value of the hyperparameter of the posterior distribution by the learned values.
@@ -339,6 +345,7 @@ def overwrite_h0_params(self):
339345
"""
340346
self.h0_alpha_vec = np.copy(self.hn_alpha_vec)
341347
self.calc_pred_dist()
348+
return self
342349

343350
def update_posterior(self, x):
344351
"""Update the hyperparameters of the posterior distribution using traning data.
@@ -355,6 +362,7 @@ def update_posterior(self, x):
355362

356363
for k in range(self.degree):
357364
self.hn_alpha_vec[k] += x[:,k].sum()
365+
return self
358366

359367
def estimate_params(self, loss="squared",dict_out=False):
360368
"""Estimate the parameter of the stochastic data generative model under the given criterion.
@@ -471,6 +479,7 @@ def get_p_params(self):
471479
def calc_pred_dist(self):
472480
"""Calculate the parameters of the predictive distribution."""
473481
self.p_theta_vec = self.hn_alpha_vec / self.hn_alpha_vec.sum()
482+
return self
474483

475484
def make_prediction(self, loss="squared"):
476485
"""Predict a new data point under the given criterion.

bayesml/contexttree/_contexttree.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ def set_h_params(self,
295295
))
296296
self.h_root = _Node(0,self.c_k)
297297
self._set_h_params_recursion(self.h_root,h_root)
298+
return self
298299

299300
def get_h_params(self):
300301
"""Get the hyperparameters of the prior distribution.
@@ -324,7 +325,8 @@ def gen_params(self,tree_fix=False):
324325
self._gen_params_recursion_tree_fix(self.root,self.h_root)
325326
else:
326327
self._gen_params_recursion(self.root,self.h_root)
327-
328+
return self
329+
328330
def set_params(self,root=None):
329331
"""Set the parameter of the sthocastic data generative model.
330332
@@ -339,6 +341,7 @@ def set_params(self,root=None):
339341
"root must be an instance of metatree._Node"
340342
))
341343
self._set_params_recursion(self.root,root)
344+
return self
342345

343346
def get_params(self):
344347
"""Get the parameter of the sthocastic data generative model.
@@ -614,6 +617,7 @@ def set_h0_params(self,
614617
self._set_h0_params_recursion(self.h0_root,h0_root)
615618

616619
self.reset_hn_params()
620+
return self
617621

618622
def get_h0_params(self):
619623
"""Get the hyperparameters of the prior distribution.
@@ -668,6 +672,7 @@ def set_hn_params(self,
668672
self._set_hn_params_recursion(self.hn_root,hn_root)
669673

670674
self.calc_pred_dist(np.zeros(self.c_d_max,dtype=int))
675+
return self
671676

672677
def get_hn_params(self):
673678
"""Get the hyperparameters of the posterior distribution.
@@ -727,6 +732,7 @@ def update_posterior(self,x):
727732

728733
for i in range(x.shape[0]):
729734
self._update_posterior_recursion(self.hn_root,x,i)
735+
return self
730736

731737
def _map_recursion_add_nodes(self,node:_Node):
732738
if node.depth == self.c_d_max: # leaf node
@@ -976,6 +982,7 @@ def calc_pred_dist(self,x):
976982
self.p_theta_vec[:] = self.hn_beta_vec / self.hn_beta_vec.sum()
977983
else:
978984
self.p_theta_vec[:] = self._calc_pred_dist_recursion(self.hn_root,x,i)
985+
return self
979986

980987
def make_prediction(self,loss="KL"):
981988
"""Predict a new data point under the given criterion.

0 commit comments

Comments
 (0)