Skip to content

Commit e76b056

Browse files
Merge pull request #47 from yuta-nakahara/release
Release 0.2.0
2 parents cc2cdb2 + e1033b3 commit e76b056

File tree

113 files changed

+12833
-1617
lines changed

Some content is hidden

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

113 files changed

+12833
-1617
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ For more details, see our [website](https://yuta-nakahara.github.io/BayesML/ "Ba
2323

2424
Please use the following commands to install BayesML.
2525

26-
``` shell
26+
``` bash
2727
pip install bayesml
2828
```
2929

@@ -33,6 +33,7 @@ The following are required.
3333
* NumPy (>= 1.20)
3434
* SciPy (>= 1.7)
3535
* MatplotLib (>= 3.5)
36+
* Scikit-learn (>= 1.1)
3637

3738
## Example
3839

@@ -53,11 +54,11 @@ gen_model.visualize_model()
5354
```
5455

5556
>theta:0.7
56-
>x0:[1 1 1 1 1 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1]
57-
>x1:[1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1]
58-
>x2:[0 0 1 1 0 1 0 1 1 1 1 1 1 0 1 0 1 1 1 1]
59-
>x3:[1 0 1 1 1 1 1 0 0 0 1 0 0 1 0 1 1 0 1 0]
60-
>x4:[1 1 0 1 0 1 1 1 0 1 1 1 0 0 1 1 1 1 1 1]
57+
>x0:[1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1]
58+
>x1:[1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0]
59+
>x2:[1 0 1 1 0 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1]
60+
>x3:[1 1 1 0 1 1 0 1 0 0 0 0 1 0 1 1 1 1 1 1]
61+
>x4:[0 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1]
6162
>![bernoulli_example1](./doc/images/README_ex_img1.png)
6263
6364
After confirming that the frequency of occurrence of 1 is around `theta=0.7`, we generate a sample and store it to variable `x`.
@@ -99,9 +100,9 @@ print(learn_model.estimate_params(loss='abs'))
99100
print(learn_model.estimate_params(loss='0-1'))
100101
```
101102

102-
>0.6428571428571429
103-
>0.6474720009710451
104-
>0.6578947368421053
103+
>0.7380952380952381
104+
>0.7457656349087012
105+
>0.7631578947368421
105106
106107
Different settings of a loss function yield different optimal estimates.
107108

@@ -115,8 +116,12 @@ The following packages are currently available. In this library, a probabilistic
115116
* [Normal model](https://yuta-nakahara.github.io/BayesML/bayesml.normal.html "BayesML Normal Model")
116117
* [Multivariate normal model](https://yuta-nakahara.github.io/BayesML/bayesml.multivariate_normal.html "BayesML Multivariate Normal Model")
117118
* [Exponential model](https://yuta-nakahara.github.io/BayesML/bayesml.exponential.html "BayesML Exponential Model")
119+
* [Gaussian mixture model](https://yuta-nakahara.github.io/BayesML/bayesml.gaussianmixture.html "BayesML Gaussian Mixture Model")
118120
* [Linear regression model](https://yuta-nakahara.github.io/BayesML/bayesml.linearregression.html "BayesML Lenear Regression Model")
121+
* [Meta-tree model](https://yuta-nakahara.github.io/BayesML/bayesml.metatree.html "BayesML Meta-tree Model")
119122
* [Autoregressive model](https://yuta-nakahara.github.io/BayesML/bayesml.autoregressive.html "BayesML Autoregressive Model")
123+
* [Hidden Markov normal model](https://yuta-nakahara.github.io/BayesML/bayesml.hiddenmarkovnormal.html "BayesML Hidden Markov Normal Model")
124+
* [Context tree model](https://yuta-nakahara.github.io/BayesML/bayesml.contexttree.html "BayesML Context Tree Model")
120125

121126
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.
122127

@@ -131,11 +136,8 @@ When you use BayesML for your academic work, please provide the following biblio
131136
Plain text
132137

133138
```
134-
Y. Nakahara, N. Ichijo, K. Shimada,
135-
K. Tajima, K. Horinouchi, L. Ruan,
136-
N. Namegaya, R. Maniwa, T. Ishiwatari,
137-
W. Yu, Y. Iikubo, S. Saito,
138-
K. Kazama, T. Matsushima, ``BayesML,''
139+
Y. Nakahara, N. Ichijo, K. Shimada, Y. Iikubo,
140+
S. Saito, K. Kazama, T. Matsushima, ``BayesML 0.2.0,''
139141
[Online] https://github.com/yuta-nakahara/BayesML
140142
```
141143

@@ -144,11 +146,9 @@ BibTeX
144146
``` bibtex
145147
@misc{bayesml,
146148
author = {Nakahara Yuta and Ichijo Naoki and Shimada Koshi and
147-
Tajima Keito and Horinouchi Kohei and Ruan Luyu and
148-
Namegaya Noboru and Maniwa Ryota and Ishiwatari Taisuke and
149-
Yu Wenbin and Iikubo Yuji and Saito Shota and Kazama Koki and
150-
Matsushima Toshiyasu},
151-
title = {BayesML},
149+
Iikubo Yuji and Saito Shota and Kazama Koki and
150+
Matsushima Toshiyasu}
151+
title = {BayesML 0.2.0},
152152
howpublished = {\url{https://github.com/yuta-nakahara/BayesML}},
153153
year = {2022}
154154
}

README_jp.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ BayesMLは以下の特徴を持っています.
2222

2323
以下のコマンドによりインストール可能です.
2424

25-
``` shell
25+
``` bash
2626
pip install bayesml
2727
```
2828

@@ -32,6 +32,7 @@ BayesMLの実行には以下が必要です.
3232
* NumPy (>= 1.20)
3333
* SciPy (>= 1.7)
3434
* MatplotLib (>= 3.5)
35+
* Scikit-learn (>= 1.1)
3536

3637
## 実行例
3738

@@ -52,11 +53,11 @@ gen_model.visualize_model()
5253
```
5354

5455
>theta:0.7
55-
>x0:[1 1 1 1 1 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1]
56-
>x1:[1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1]
57-
>x2:[0 0 1 1 0 1 0 1 1 1 1 1 1 0 1 0 1 1 1 1]
58-
>x3:[1 0 1 1 1 1 1 0 0 0 1 0 0 1 0 1 1 0 1 0]
59-
>x4:[1 1 0 1 0 1 1 1 0 1 1 1 0 0 1 1 1 1 1 1]
56+
>x0:[1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1]
57+
>x1:[1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0]
58+
>x2:[1 0 1 1 0 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1]
59+
>x3:[1 1 1 0 1 1 0 1 0 0 0 0 1 0 1 1 1 1 1 1]
60+
>x4:[0 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1]
6061
>![bernoulli_example1](./doc/images/README_ex_img1.png)
6162
6263
1の出現頻度が`theta=0.7`程度であることを確認したら,サンプルを生成し変数`x`に保存します.
@@ -96,9 +97,9 @@ print(learn_model.estimate_params(loss='abs'))
9697
print(learn_model.estimate_params(loss='0-1'))
9798
```
9899

99-
>0.6428571428571429
100-
>0.6474720009710451
101-
>0.6578947368421053
100+
>0.7380952380952381
101+
>0.7457656349087012
102+
>0.7631578947368421
102103
103104
損失関数の設定が異なると,そのもとでの最適な推定値も異なることがわかります.
104105

@@ -112,8 +113,12 @@ print(learn_model.estimate_params(loss='0-1'))
112113
* [正規モデル](https://yuta-nakahara.github.io/BayesML/bayesml.normal.html "BayesML Normal Model")
113114
* [多変量正規モデル](https://yuta-nakahara.github.io/BayesML/bayesml.multivariate_normal.html "BayesML Multivariate Normal Model")
114115
* [指数モデル](https://yuta-nakahara.github.io/BayesML/bayesml.exponential.html "BayesML Exponential Model")
116+
* [混合正規モデル](https://yuta-nakahara.github.io/BayesML/bayesml.gaussianmixture.html "BayesML Gaussian Mixture Model")
115117
* [線形回帰モデル](https://yuta-nakahara.github.io/BayesML/bayesml.linearregression.html "BayesML Lenear Regression Model")
118+
* [メタツリーモデル](https://yuta-nakahara.github.io/BayesML/bayesml.metatree.html "BayesML Meta-tree Model")
116119
* [自己回帰モデル](https://yuta-nakahara.github.io/BayesML/bayesml.autoregressive.html "BayesML Autoregressive Model")
120+
* [隠れマルコフモデル](https://yuta-nakahara.github.io/BayesML/bayesml.hiddenmarkovnormal.html "BayesML Hidden Markov Normal Model")
121+
* [文脈木モデル](https://yuta-nakahara.github.io/BayesML/bayesml.contexttree.html "BayesML Context Tree Model")
117122

118123
また,今後は混合正規モデルや隠れマルコフモデルなどの厳密なベイズ推論が困難なモデルを変分ベイズ法で学習するパッケージが追加される予定です.
119124

@@ -128,11 +133,8 @@ BayesMLへのコントリビューションを考えてくださってありが
128133
プレーンテキスト
129134

130135
```
131-
Y. Nakahara, N. Ichijo, K. Shimada,
132-
K. Tajima, K. Horinouchi, L. Ruan,
133-
N. Namegaya, R. Maniwa, T. Ishiwatari,
134-
W. Yu, Y. Iikubo, S. Saito,
135-
K. Kazama, T. Matsushima, ``BayesML,''
136+
Y. Nakahara, N. Ichijo, K. Shimada, Y. Iikubo,
137+
S. Saito, K. Kazama, T. Matsushima, ``BayesML,''
136138
[Online] https://github.com/yuta-nakahara/BayesML
137139
```
138140

@@ -141,9 +143,7 @@ BibTeX
141143
``` bibtex
142144
@misc{bayesml,
143145
author = {Nakahara Yuta and Ichijo Naoki and Shimada Koshi and
144-
Tajima Keito and Horinouchi Kohei and Ruan Luyu and
145-
Namegaya Noboru and Maniwa Ryota and Ishiwatari Taisuke and
146-
Yu Wenbin and Iikubo Yuji and Saito Shota and Kazama Koki and
146+
Iikubo Yuji and Saito Shota and Kazama Koki and
147147
Matsushima Toshiyasu}
148148
title = {BayesML},
149149
howpublished = {\url{https://github.com/yuta-nakahara/BayesML}},

bayesml/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
from . import multivariate_normal
77
from . import normal
88
from . import poisson
9+
from . import metatree
10+
from . import contexttree
11+
from . import gaussianmixture
12+
from . import hiddenmarkovnormal
913

1014
__all__ = ['bernoulli',
1115
'categorical',
@@ -14,5 +18,9 @@
1418
'linearregression',
1519
'multivariate_normal',
1620
'normal',
17-
'poisson'
21+
'poisson',
22+
'metatree',
23+
'contexttree',
24+
'gaussianmixture',
25+
'hiddenmarkovnormal',
1826
]

bayesml/_check.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Code Author
22
# Yuta Nakahara <yuta.nakahara@aoni.waseda.jp>
33
# Yuji Iikubo <yuji-iikubo.8@fuji.waseda.jp>
4+
# Yasushi Esaki <esakiful@gmail.com>
5+
# Jun Nishikawa <jun.b.nishikawa@gmail.com>
46
import numpy as np
57

68
_EPSILON = np.sqrt(np.finfo(np.float64).eps)
@@ -45,12 +47,31 @@ def nonneg_ints(val,val_name,exception_class):
4547
return val
4648
raise(exception_class(val_name + " must be int or a numpy.ndarray whose dtype is int. Its values must be non-negative (including 0)."))
4749

50+
def int_vec(val,val_name,exception_class):
51+
if type(val) is np.ndarray:
52+
if np.issubdtype(val.dtype,np.integer) and val.ndim == 1:
53+
return val
54+
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray whose dtype is int."))
55+
4856
def nonneg_int_vec(val,val_name,exception_class):
4957
if type(val) is np.ndarray:
5058
if np.issubdtype(val.dtype,np.integer) and val.ndim == 1 and np.all(val>=0):
5159
return val
5260
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray whose dtype is int. Its values must be non-negative (including 0)."))
5361

62+
def nonneg_int_vecs(val,val_name,exception_class):
63+
if type(val) is np.ndarray:
64+
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1 and np.all(val>=0):
65+
return val
66+
raise(exception_class(val_name + " must be a numpy.ndarray whose ndim >= 1 and dtype is int. Its values must be non-negative (including 0)."))
67+
68+
69+
def nonneg_float_vec(val,val_name,exception_class):
70+
if type(val) is np.ndarray:
71+
if np.issubdtype(val.dtype,np.floating) and val.ndim == 1 and np.all(val>=0):
72+
return val
73+
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray whose dtype is float. Its values must be non-negative (including 0)."))
74+
5475
def int_of_01(val,val_name,exception_class):
5576
if np.issubdtype(type(val),np.integer):
5677
if val == 0 or val ==1:
@@ -100,6 +121,22 @@ def pos_def_sym_mat(val,val_name,exception_class):
100121
pass
101122
raise(exception_class(val_name + " must be a positive definite symmetric 2-dimensional numpy.ndarray."))
102123

124+
def sym_mats(val,val_name,exception_class):
125+
if type(val) is np.ndarray:
126+
if val.ndim >= 2 and val.shape[-1] == val.shape[-2]:
127+
if np.allclose(val, np.swapaxes(val,-1,-2)):
128+
return val
129+
raise(exception_class(val_name + " must be a symmetric 2-dimensional numpy.ndarray."))
130+
131+
def pos_def_sym_mats(val,val_name,exception_class):
132+
sym_mats(val,val_name,exception_class)
133+
try:
134+
np.linalg.cholesky(val)
135+
return val
136+
except np.linalg.LinAlgError:
137+
pass
138+
raise(exception_class(val_name + " must be a positive definite symmetric 2-dimensional numpy.ndarray."))
139+
103140
def float_(val,val_name,exception_class):
104141
if np.issubdtype(type(val),np.floating):
105142
return val
@@ -155,6 +192,14 @@ def float_vecs(val,val_name,exception_class):
155192
return val
156193
raise(exception_class(val_name + " must be a numpy.ndarray whose ndim >= 1."))
157194

195+
def pos_float_vecs(val,val_name,exception_class):
196+
if type(val) is np.ndarray:
197+
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1 and np.all(val>0):
198+
return val.astype(float)
199+
if np.issubdtype(val.dtype,np.floating) and val.ndim >= 1 and np.all(val>0.0):
200+
return val
201+
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray. Its values must be positive (not including 0)"))
202+
158203
def float_vec_sum_1(val,val_name,exception_class):
159204
if type(val) is np.ndarray:
160205
if np.issubdtype(val.dtype,np.integer) and val.ndim == 1 and abs(val.sum() - 1.) <= _EPSILON:
@@ -163,6 +208,14 @@ def float_vec_sum_1(val,val_name,exception_class):
163208
return val
164209
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray, and the sum of its elements must equal to 1."))
165210

211+
def float_vecs_sum_1(val,val_name,exception_class):
212+
if type(val) is np.ndarray:
213+
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1 and np.all(np.abs(np.sum(val, axis=-1) - 1.) <= _EPSILON):
214+
return val.astype(float)
215+
if np.issubdtype(val.dtype,np.floating) and val.ndim >= 1 and np.all(np.abs(np.sum(val, axis=-1) - 1.) <= _EPSILON):
216+
return val
217+
raise(exception_class(val_name + " must be a numpy.ndarray whose ndim >= 1, and the sum along the last dimension must equal to 1."))
218+
166219
def int_(val,val_name,exception_class):
167220
if np.issubdtype(type(val),np.integer):
168221
return val
@@ -189,3 +242,15 @@ def onehot_vecs(val,val_name,exception_class):
189242
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1 and np.all(val >= 0) and np.all(val.sum(axis=-1)==1):
190243
return val
191244
raise(exception_class(val_name + " must be a numpy.ndarray whose dtype is int and whose last axis constitutes one-hot vectors."))
245+
246+
def int_vecs(val,val_name,exception_class):
247+
if type(val) is np.ndarray:
248+
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1:
249+
return val
250+
raise(exception_class(val_name + " must be a numpy.ndarray whose dtype is int and ndim >= 1."))
251+
252+
def shape_consistency(val: int, val_name: str, correct: int, correct_name: str, exception_class):
253+
if val != correct:
254+
message = (f"{val_name} must coincide with {correct_name}: "
255+
+ f"{val_name} = {val}, {correct_name} = {correct}")
256+
raise(exception_class(message))

0 commit comments

Comments
 (0)