Skip to content

Commit a5f808e

Browse files
Merge pull request #57 from yuta-nakahara/release-0.2.1
Release 0.2.1
2 parents 71ae588 + dd64ef4 commit a5f808e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1572
-1082
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ The following packages are currently available. In this library, a probabilistic
123123
* [Hidden Markov normal model](https://yuta-nakahara.github.io/BayesML/bayesml.hiddenmarkovnormal.html "BayesML Hidden Markov Normal Model")
124124
* [Context tree model](https://yuta-nakahara.github.io/BayesML/bayesml.contexttree.html "BayesML Context Tree Model")
125125

126-
In the future, we will add packages to deal with a mixture normal model and a hidden Markov model, which are difficult to perform exact Bayesian inference, by using variational Bayes methods.
126+
In the future, we will add packages to deal with more complicated hierarchical models.
127127

128128
## How to contribute
129129

@@ -137,7 +137,7 @@ Plain text
137137

138138
```
139139
Y. Nakahara, N. Ichijo, K. Shimada, Y. Iikubo,
140-
S. Saito, K. Kazama, T. Matsushima, ``BayesML 0.2.0,''
140+
S. Saito, K. Kazama, T. Matsushima, BayesML Developers, ``BayesML 0.2.1,''
141141
[Online] https://github.com/yuta-nakahara/BayesML
142142
```
143143

@@ -147,8 +147,8 @@ BibTeX
147147
@misc{bayesml,
148148
author = {Nakahara Yuta and Ichijo Naoki and Shimada Koshi and
149149
Iikubo Yuji and Saito Shota and Kazama Koki and
150-
Matsushima Toshiyasu}
151-
title = {BayesML 0.2.0},
150+
Matsushima Toshiyasu and {BayesML Developers}},
151+
title = {BayesML 0.2.1},
152152
howpublished = {\url{https://github.com/yuta-nakahara/BayesML}},
153153
year = {2022}
154154
}

README_jp.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ print(learn_model.estimate_params(loss='0-1'))
120120
* [隠れマルコフモデル](https://yuta-nakahara.github.io/BayesML/bayesml.hiddenmarkovnormal.html "BayesML Hidden Markov Normal Model")
121121
* [文脈木モデル](https://yuta-nakahara.github.io/BayesML/bayesml.contexttree.html "BayesML Context Tree Model")
122122

123-
また,今後は混合正規モデルや隠れマルコフモデルなどの厳密なベイズ推論が困難なモデルを変分ベイズ法で学習するパッケージが追加される予定です
123+
また,今後はより複雑な階層的モデルを取り扱うパッケージを追加していく予定です
124124

125125
## コントリビューションの方法
126126

@@ -134,7 +134,7 @@ BayesMLへのコントリビューションを考えてくださってありが
134134

135135
```
136136
Y. Nakahara, N. Ichijo, K. Shimada, Y. Iikubo,
137-
S. Saito, K. Kazama, T. Matsushima, ``BayesML,''
137+
S. Saito, K. Kazama, T. Matsushima, BayesML Developers, ``BayesML 0.2.1,''
138138
[Online] https://github.com/yuta-nakahara/BayesML
139139
```
140140

@@ -144,8 +144,8 @@ BibTeX
144144
@misc{bayesml,
145145
author = {Nakahara Yuta and Ichijo Naoki and Shimada Koshi and
146146
Iikubo Yuji and Saito Shota and Kazama Koki and
147-
Matsushima Toshiyasu}
148-
title = {BayesML},
147+
Matsushima Toshiyasu and {BayesML Developers}},
148+
title = {BayesML 0.2.1},
149149
howpublished = {\url{https://github.com/yuta-nakahara/BayesML}},
150150
year = {2022}
151151
}

bayesml/autoregressive/_autoregressive.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(
5858

5959
# h_params
6060
self.h_mu_vec = np.zeros(self.c_degree+1)
61-
self.h_lambda_mat = np.identity(self.c_degree+1)
61+
self.h_lambda_mat = np.eye(self.c_degree+1)
6262
self.h_alpha = 1.0
6363
self.h_beta = 1.0
6464

@@ -306,13 +306,13 @@ def __init__(
306306

307307
# h0_params
308308
self.h0_mu_vec = np.zeros(self.c_degree+1)
309-
self.h0_lambda_mat = np.identity(self.c_degree+1)
309+
self.h0_lambda_mat = np.eye(self.c_degree+1)
310310
self.h0_alpha = 1.0
311311
self.h0_beta = 1.0
312312

313313
# hn_params
314314
self.hn_mu_vec = np.zeros(self.c_degree+1)
315-
self.hn_lambda_mat = np.identity(self.c_degree+1)
315+
self.hn_lambda_mat = np.eye(self.c_degree+1)
316316
self.hn_alpha = 1.0
317317
self.hn_beta = 1.0
318318

@@ -484,8 +484,8 @@ def update_posterior(self,x,padding=None):
484484
for n in range(self.c_degree+1,x.shape[0]):
485485
x_mat[n,1:] = x[n-self.c_degree:n]
486486

487-
mu_tmp = np.copy(self.hn_mu_vec)
488-
lambda_tmp = np.copy(self.hn_lambda_mat)
487+
mu_tmp = np.array(self.hn_mu_vec)
488+
lambda_tmp = np.array(self.hn_lambda_mat)
489489
if padding == "zeros":
490490
self.hn_lambda_mat += x_mat.T @ x_mat
491491
self.hn_mu_vec[:] = np.linalg.solve(self.hn_lambda_mat,

bayesml/categorical/_categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def estimate_params(self, loss="squared",dict_out=False):
401401
else:
402402
return (self.hn_alpha_vec - 1) / (np.sum(self.hn_alpha_vec) - self.c_degree)
403403
else:
404-
warnings.warn("MAP estimate of lambda_mat doesn't exist for the current hn_alpha_vec.",ResultWarning)
404+
warnings.warn("MAP estimate of theta_vec doesn't exist for the current hn_alpha_vec.",ResultWarning)
405405
if dict_out:
406406
return {'theta_vec':None}
407407
else:

bayesml/contexttree/contexttree_test.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

bayesml/gaussianmixture/_gaussianmixture.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def __init__(
4848
self,
4949
c_num_classes,
5050
c_degree,
51-
*,
5251
pi_vec=None,
5352
mu_vecs=None,
5453
lambda_mats=None,
@@ -67,14 +66,14 @@ def __init__(
6766
# params
6867
self.pi_vec = np.ones(self.c_num_classes) / self.c_num_classes
6968
self.mu_vecs = np.zeros([self.c_num_classes,self.c_degree])
70-
self.lambda_mats = np.tile(np.identity(self.c_degree),[self.c_num_classes,1,1])
69+
self.lambda_mats = np.tile(np.eye(self.c_degree),[self.c_num_classes,1,1])
7170

7271
# h_params
7372
self.h_alpha_vec = np.ones(self.c_num_classes) / 2
7473
self.h_m_vecs = np.zeros([self.c_num_classes,self.c_degree])
7574
self.h_kappas = np.ones(self.c_num_classes)
7675
self.h_nus = np.ones(self.c_num_classes) * self.c_degree
77-
self.h_w_mats = np.tile(np.identity(self.c_degree),[self.c_num_classes,1,1])
76+
self.h_w_mats = np.tile(np.eye(self.c_degree),[self.c_num_classes,1,1])
7877

7978
self.set_params(
8079
pi_vec,
@@ -88,7 +87,18 @@ def __init__(
8887
h_nus,
8988
h_w_mats,
9089
)
91-
90+
91+
def get_constants(self):
92+
"""Get constants of GenModel.
93+
94+
Returns
95+
-------
96+
constants : dict of {str: int, numpy.ndarray}
97+
* ``"c_num_classes"`` : the value of ``self.c_num_classes``
98+
* ``"c_degree"`` : the value of ``self.c_degree``
99+
"""
100+
return {"c_num_classes":self.c_num_classes, "c_degree":self.c_degree}
101+
92102
def set_h_params(
93103
self,
94104
h_alpha_vec=None,
@@ -412,7 +422,6 @@ def __init__(
412422
self,
413423
c_num_classes,
414424
c_degree,
415-
*,
416425
h0_alpha_vec = None,
417426
h0_m_vecs = None,
418427
h0_kappas = None,
@@ -430,7 +439,7 @@ def __init__(
430439
self.h0_m_vecs = np.zeros([self.c_num_classes,self.c_degree])
431440
self.h0_kappas = np.ones(self.c_num_classes)
432441
self.h0_nus = np.ones(self.c_num_classes) * self.c_degree
433-
self.h0_w_mats = np.tile(np.identity(self.c_degree),[self.c_num_classes,1,1])
442+
self.h0_w_mats = np.tile(np.eye(self.c_degree),[self.c_num_classes,1,1])
434443
self.h0_w_mats_inv = np.linalg.inv(self.h0_w_mats)
435444

436445
self._ln_c_h0_alpha = 0.0
@@ -480,6 +489,17 @@ def __init__(
480489
h0_w_mats,
481490
)
482491

492+
def get_constants(self):
493+
"""Get constants of LearnModel.
494+
495+
Returns
496+
-------
497+
constants : dict of {str: int, numpy.ndarray}
498+
* ``"c_num_classes"`` : the value of ``self.c_num_classes``
499+
* ``"c_degree"`` : the value of ``self.c_degree``
500+
"""
501+
return {"c_num_classes":self.c_num_classes, "c_degree":self.c_degree}
502+
483503
def set_h0_params(
484504
self,
485505
h0_alpha_vec = None,
@@ -771,7 +791,7 @@ def _init_subsampling(self,x):
771791
self.hn_w_mats_inv[k] = ((_subsample - self.hn_m_vecs[k]).T
772792
@ (_subsample - self.hn_m_vecs[k])
773793
/ _size * self.hn_nus[k]
774-
+ np.identity(self.c_degree) * 1.0E-5) # avoid singular matrix
794+
+ np.eye(self.c_degree) * 1.0E-5) # avoid singular matrix
775795
self.hn_w_mats[k] = np.linalg.inv(self.hn_w_mats_inv[k])
776796
self._calc_q_lambda_char()
777797

@@ -815,12 +835,12 @@ def update_posterior(
815835
self.r_vecs = np.empty([x.shape[0],self.c_num_classes])
816836

817837
tmp_vl = 0.0
818-
tmp_alpha_vec = np.copy(self.hn_alpha_vec)
819-
tmp_m_vecs = np.copy(self.hn_m_vecs)
820-
tmp_kappas = np.copy(self.hn_kappas)
821-
tmp_nus = np.copy(self.hn_nus)
822-
tmp_w_mats = np.copy(self.hn_w_mats)
823-
tmp_w_mats_inv = np.copy(self.hn_w_mats_inv)
838+
tmp_alpha_vec = np.array(self.hn_alpha_vec)
839+
tmp_m_vecs = np.array(self.hn_m_vecs)
840+
tmp_kappas = np.array(self.hn_kappas)
841+
tmp_nus = np.array(self.hn_nus)
842+
tmp_w_mats = np.array(self.hn_w_mats)
843+
tmp_w_mats_inv = np.array(self.hn_w_mats_inv)
824844

825845
convergence_flag = True
826846
for i in range(num_init):
@@ -913,7 +933,7 @@ def estimate_params(self,loss="squared"):
913933
if np.all(self.hn_alpha_vec > 1):
914934
pi_vec_hat[:] = (self.hn_alpha_vec - 1) / (np.sum(self.hn_alpha_vec) - self.c_degree)
915935
else:
916-
warnings.warn("MAP estimate of lambda_mat doesn't exist for the current hn_alpha_vec.",ResultWarning)
936+
warnings.warn("MAP estimate of pi_vec doesn't exist for the current hn_alpha_vec.",ResultWarning)
917937
pi_vec_hat[:] = np.nan
918938

919939
lambda_mats_hat = np.empty([self.c_num_classes,self.c_degree,self.c_degree])
@@ -1164,7 +1184,7 @@ def estimate_latent_vars(self,x,loss="0-1"):
11641184
if loss == "squared":
11651185
return self.r_vecs
11661186
elif loss == "0-1":
1167-
return np.identity(self.c_num_classes,dtype=int)[np.argmax(self.r_vecs,axis=1)]
1187+
return np.eye(self.c_num_classes,dtype=int)[np.argmax(self.r_vecs,axis=1)]
11681188
elif loss == "KL":
11691189
return self.r_vecs
11701190
else:

bayesml/hiddenmarkovnormal/_hiddenmarkovnormal.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ def __init__(
9595
self.pi_vec = np.ones(self.c_num_classes) / self.c_num_classes
9696
self.a_mat = np.ones([self.c_num_classes,self.c_num_classes]) / self.c_num_classes
9797
self.mu_vecs = np.zeros([self.c_num_classes,self.c_degree])
98-
self.lambda_mats = np.tile(np.identity(self.c_degree),[self.c_num_classes,1,1])
98+
self.lambda_mats = np.tile(np.eye(self.c_degree),[self.c_num_classes,1,1])
9999

100100
# h_params
101101
self.h_eta_vec = np.ones(self.c_num_classes) / 2.0
102102
self.h_zeta_vecs = np.ones([self.c_num_classes,self.c_num_classes]) / 2.0
103103
self.h_m_vecs = np.zeros([self.c_num_classes,self.c_degree])
104104
self.h_kappas = np.ones([self.c_num_classes])
105105
self.h_nus = np.ones(self.c_num_classes) * self.c_degree
106-
self.h_w_mats = np.tile(np.identity(self.c_degree),[self.c_num_classes,1,1])
106+
self.h_w_mats = np.tile(np.eye(self.c_degree),[self.c_num_classes,1,1])
107107

108108
self.set_params(
109109
pi_vec,
@@ -119,6 +119,17 @@ def __init__(
119119
h_nus,
120120
h_w_mats)
121121

122+
def get_constants(self):
123+
"""Get constants of GenModel.
124+
125+
Returns
126+
-------
127+
constants : dict of {str: int, numpy.ndarray}
128+
* ``"c_num_classes"`` : the value of ``self.c_num_classes``
129+
* ``"c_degree"`` : the value of ``self.c_degree``
130+
"""
131+
return {"c_num_classes":self.c_num_classes, "c_degree":self.c_degree}
132+
122133
def set_params(
123134
self,
124135
pi_vec=None,
@@ -523,7 +534,7 @@ def __init__(
523534
self.h0_m_vecs = np.zeros([self.c_num_classes,self.c_degree])
524535
self.h0_kappas = np.ones([self.c_num_classes])
525536
self.h0_nus = np.ones(self.c_num_classes) * self.c_degree
526-
self.h0_w_mats = np.tile(np.identity(self.c_degree),[self.c_num_classes,1,1])
537+
self.h0_w_mats = np.tile(np.eye(self.c_degree),[self.c_num_classes,1,1])
527538
self.h0_w_mats_inv = np.linalg.inv(self.h0_w_mats)
528539

529540
self._ln_c_h0_eta_vec = 0.0
@@ -594,6 +605,17 @@ def __init__(
594605
h0_w_mats,
595606
)
596607

608+
def get_constants(self):
609+
"""Get constants of LearnModel.
610+
611+
Returns
612+
-------
613+
constants : dict of {str: int, numpy.ndarray}
614+
* ``"c_num_classes"`` : the value of ``self.c_num_classes``
615+
* ``"c_degree"`` : the value of ``self.c_degree``
616+
"""
617+
return {"c_num_classes":self.c_num_classes, "c_degree":self.c_degree}
618+
597619
def set_h0_params(
598620
self,
599621
h0_eta_vec = None,
@@ -940,7 +962,7 @@ def _init_subsampling(self,x):
940962
self.hn_w_mats_inv[k] = ((_subsample - self.hn_m_vecs[k]).T
941963
@ (_subsample - self.hn_m_vecs[k])
942964
/ _size * self.hn_nus[k]
943-
+ np.identity(self.c_degree) * 1.0E-5) # avoid singular matrix
965+
+ np.eye(self.c_degree) * 1.0E-5) # avoid singular matrix
944966
self.hn_w_mats[k] = np.linalg.inv(self.hn_w_mats_inv[k])
945967
self._calc_q_lambda_char()
946968

@@ -1051,13 +1073,13 @@ def update_posterior(
10511073
self._cs = np.ones([self._length])
10521074

10531075
tmp_vl = 0.0
1054-
tmp_eta_vec = np.copy(self.hn_eta_vec)
1055-
tmp_zeta_vecs = np.copy(self.hn_zeta_vecs)
1056-
tmp_m_vecs = np.copy(self.hn_m_vecs)
1057-
tmp_kappas = np.copy(self.hn_kappas)
1058-
tmp_nus = np.copy(self.hn_nus)
1059-
tmp_w_mats = np.copy(self.hn_w_mats)
1060-
tmp_w_mats_inv = np.copy(self.hn_w_mats_inv)
1076+
tmp_eta_vec = np.array(self.hn_eta_vec)
1077+
tmp_zeta_vecs = np.array(self.hn_zeta_vecs)
1078+
tmp_m_vecs = np.array(self.hn_m_vecs)
1079+
tmp_kappas = np.array(self.hn_kappas)
1080+
tmp_nus = np.array(self.hn_nus)
1081+
tmp_w_mats = np.array(self.hn_w_mats)
1082+
tmp_w_mats_inv = np.array(self.hn_w_mats_inv)
10611083

10621084
convergence_flag = True
10631085
for i in range(num_init):
@@ -1475,7 +1497,7 @@ def estimate_latent_vars(self,x,loss='0-1',viterbi=True):
14751497
if loss == "squared" or loss == "KL":
14761498
return self.gamma_vecs
14771499
elif loss == "0-1":
1478-
return np.identity(self.c_num_classes,dtype=int)[np.argmax(self.gamma_vecs,axis=1)]
1500+
return np.eye(self.c_num_classes,dtype=int)[np.argmax(self.gamma_vecs,axis=1)]
14791501
else:
14801502
raise(CriteriaError(f"loss=\"{loss}\" is unsupported. "
14811503
+"When viterbi == False, This function supports \"squared\", \"0-1\", and \"KL\"."))

bayesml/linearregression/_linearregression.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(
5858

5959
# h_params
6060
self.h_mu_vec = np.zeros(self.c_degree)
61-
self.h_lambda_mat = np.identity(self.c_degree)
61+
self.h_lambda_mat = np.eye(self.c_degree)
6262
self.h_alpha = 1.0
6363
self.h_beta = 1.0
6464

@@ -203,7 +203,7 @@ def gen_sample(self,sample_size=None,x=None,constant=True):
203203
sample_size = x.shape[0]
204204
elif sample_size is not None:
205205
_check.pos_int(sample_size,'sample_size',DataFormatError)
206-
x = self.rng.multivariate_normal(np.zeros(self.c_degree),np.identity(self.c_degree), size=sample_size)
206+
x = self.rng.multivariate_normal(np.zeros(self.c_degree),np.eye(self.c_degree), size=sample_size)
207207
if constant:
208208
x[:,-1] = 1.0
209209
else:
@@ -345,13 +345,13 @@ def __init__(
345345

346346
# h0_params
347347
self.h0_mu_vec = np.zeros(self.c_degree)
348-
self.h0_lambda_mat = np.identity(self.c_degree)
348+
self.h0_lambda_mat = np.eye(self.c_degree)
349349
self.h0_alpha = 1.0
350350
self.h0_beta = 1.0
351351

352352
# hn_params
353353
self.hn_mu_vec = np.zeros(self.c_degree)
354-
self.hn_lambda_mat = np.identity(self.c_degree)
354+
self.hn_lambda_mat = np.eye(self.c_degree)
355355
self.hn_alpha = 1.0
356356
self.hn_beta = 1.0
357357

0 commit comments

Comments
 (0)