Skip to content

Commit

Permalink
Merge pull request #47 from yuta-nakahara/release
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
yuta-nakahara committed Nov 20, 2022
2 parents cc2cdb2 + e1033b3 commit e76b056
Show file tree
Hide file tree
Showing 113 changed files with 12,833 additions and 1,617 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ For more details, see our [website](https://yuta-nakahara.github.io/BayesML/ "Ba

Please use the following commands to install BayesML.

``` shell
``` bash
pip install bayesml
```

Expand All @@ -33,6 +33,7 @@ The following are required.
* NumPy (>= 1.20)
* SciPy (>= 1.7)
* MatplotLib (>= 3.5)
* Scikit-learn (>= 1.1)

## Example

Expand All @@ -53,11 +54,11 @@ gen_model.visualize_model()
```

>theta:0.7
>x0:[1 1 1 1 1 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1]
>x1:[1 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1]
>x2:[0 0 1 1 0 1 0 1 1 1 1 1 1 0 1 0 1 1 1 1]
>x3:[1 0 1 1 1 1 1 0 0 0 1 0 0 1 0 1 1 0 1 0]
>x4:[1 1 0 1 0 1 1 1 0 1 1 1 0 0 1 1 1 1 1 1]
>x0:[1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1]
>x1:[1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0]
>x2:[1 0 1 1 0 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1]
>x3:[1 1 1 0 1 1 0 1 0 0 0 0 1 0 1 1 1 1 1 1]
>x4:[0 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1]
>![bernoulli_example1](./doc/images/README_ex_img1.png)
After confirming that the frequency of occurrence of 1 is around `theta=0.7`, we generate a sample and store it to variable `x`.
Expand Down Expand Up @@ -99,9 +100,9 @@ print(learn_model.estimate_params(loss='abs'))
print(learn_model.estimate_params(loss='0-1'))
```

>0.6428571428571429
>0.6474720009710451
>0.6578947368421053
>0.7380952380952381
>0.7457656349087012
>0.7631578947368421
Different settings of a loss function yield different optimal estimates.

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

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.

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

```
Y. Nakahara, N. Ichijo, K. Shimada,
K. Tajima, K. Horinouchi, L. Ruan,
N. Namegaya, R. Maniwa, T. Ishiwatari,
W. Yu, Y. Iikubo, S. Saito,
K. Kazama, T. Matsushima, ``BayesML,''
Y. Nakahara, N. Ichijo, K. Shimada, Y. Iikubo,
S. Saito, K. Kazama, T. Matsushima, ``BayesML 0.2.0,''
[Online] https://github.com/yuta-nakahara/BayesML
```

Expand All @@ -144,11 +146,9 @@ BibTeX
``` bibtex
@misc{bayesml,
author = {Nakahara Yuta and Ichijo Naoki and Shimada Koshi and
Tajima Keito and Horinouchi Kohei and Ruan Luyu and
Namegaya Noboru and Maniwa Ryota and Ishiwatari Taisuke and
Yu Wenbin and Iikubo Yuji and Saito Shota and Kazama Koki and
Matsushima Toshiyasu},
title = {BayesML},
Iikubo Yuji and Saito Shota and Kazama Koki and
Matsushima Toshiyasu}
title = {BayesML 0.2.0},
howpublished = {\url{https://github.com/yuta-nakahara/BayesML}},
year = {2022}
}
Expand Down
34 changes: 17 additions & 17 deletions README_jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BayesMLは以下の特徴を持っています.

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

``` shell
``` bash
pip install bayesml
```

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

## 実行例

Expand All @@ -52,11 +53,11 @@ gen_model.visualize_model()
```

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

>0.6428571428571429
>0.6474720009710451
>0.6578947368421053
>0.7380952380952381
>0.7457656349087012
>0.7631578947368421
損失関数の設定が異なると,そのもとでの最適な推定値も異なることがわかります.

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

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

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

```
Y. Nakahara, N. Ichijo, K. Shimada,
K. Tajima, K. Horinouchi, L. Ruan,
N. Namegaya, R. Maniwa, T. Ishiwatari,
W. Yu, Y. Iikubo, S. Saito,
K. Kazama, T. Matsushima, ``BayesML,''
Y. Nakahara, N. Ichijo, K. Shimada, Y. Iikubo,
S. Saito, K. Kazama, T. Matsushima, ``BayesML,''
[Online] https://github.com/yuta-nakahara/BayesML
```

Expand All @@ -141,9 +143,7 @@ BibTeX
``` bibtex
@misc{bayesml,
author = {Nakahara Yuta and Ichijo Naoki and Shimada Koshi and
Tajima Keito and Horinouchi Kohei and Ruan Luyu and
Namegaya Noboru and Maniwa Ryota and Ishiwatari Taisuke and
Yu Wenbin and Iikubo Yuji and Saito Shota and Kazama Koki and
Iikubo Yuji and Saito Shota and Kazama Koki and
Matsushima Toshiyasu}
title = {BayesML},
howpublished = {\url{https://github.com/yuta-nakahara/BayesML}},
Expand Down
10 changes: 9 additions & 1 deletion bayesml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from . import multivariate_normal
from . import normal
from . import poisson
from . import metatree
from . import contexttree
from . import gaussianmixture
from . import hiddenmarkovnormal

__all__ = ['bernoulli',
'categorical',
Expand All @@ -14,5 +18,9 @@
'linearregression',
'multivariate_normal',
'normal',
'poisson'
'poisson',
'metatree',
'contexttree',
'gaussianmixture',
'hiddenmarkovnormal',
]
65 changes: 65 additions & 0 deletions bayesml/_check.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Code Author
# Yuta Nakahara <yuta.nakahara@aoni.waseda.jp>
# Yuji Iikubo <yuji-iikubo.8@fuji.waseda.jp>
# Yasushi Esaki <esakiful@gmail.com>
# Jun Nishikawa <jun.b.nishikawa@gmail.com>
import numpy as np

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

def int_vec(val,val_name,exception_class):
if type(val) is np.ndarray:
if np.issubdtype(val.dtype,np.integer) and val.ndim == 1:
return val
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray whose dtype is int."))

def nonneg_int_vec(val,val_name,exception_class):
if type(val) is np.ndarray:
if np.issubdtype(val.dtype,np.integer) and val.ndim == 1 and np.all(val>=0):
return val
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray whose dtype is int. Its values must be non-negative (including 0)."))

def nonneg_int_vecs(val,val_name,exception_class):
if type(val) is np.ndarray:
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1 and np.all(val>=0):
return val
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)."))


def nonneg_float_vec(val,val_name,exception_class):
if type(val) is np.ndarray:
if np.issubdtype(val.dtype,np.floating) and val.ndim == 1 and np.all(val>=0):
return val
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray whose dtype is float. Its values must be non-negative (including 0)."))

def int_of_01(val,val_name,exception_class):
if np.issubdtype(type(val),np.integer):
if val == 0 or val ==1:
Expand Down Expand Up @@ -100,6 +121,22 @@ def pos_def_sym_mat(val,val_name,exception_class):
pass
raise(exception_class(val_name + " must be a positive definite symmetric 2-dimensional numpy.ndarray."))

def sym_mats(val,val_name,exception_class):
if type(val) is np.ndarray:
if val.ndim >= 2 and val.shape[-1] == val.shape[-2]:
if np.allclose(val, np.swapaxes(val,-1,-2)):
return val
raise(exception_class(val_name + " must be a symmetric 2-dimensional numpy.ndarray."))

def pos_def_sym_mats(val,val_name,exception_class):
sym_mats(val,val_name,exception_class)
try:
np.linalg.cholesky(val)
return val
except np.linalg.LinAlgError:
pass
raise(exception_class(val_name + " must be a positive definite symmetric 2-dimensional numpy.ndarray."))

def float_(val,val_name,exception_class):
if np.issubdtype(type(val),np.floating):
return val
Expand Down Expand Up @@ -155,6 +192,14 @@ def float_vecs(val,val_name,exception_class):
return val
raise(exception_class(val_name + " must be a numpy.ndarray whose ndim >= 1."))

def pos_float_vecs(val,val_name,exception_class):
if type(val) is np.ndarray:
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1 and np.all(val>0):
return val.astype(float)
if np.issubdtype(val.dtype,np.floating) and val.ndim >= 1 and np.all(val>0.0):
return val
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray. Its values must be positive (not including 0)"))

def float_vec_sum_1(val,val_name,exception_class):
if type(val) is np.ndarray:
if np.issubdtype(val.dtype,np.integer) and val.ndim == 1 and abs(val.sum() - 1.) <= _EPSILON:
Expand All @@ -163,6 +208,14 @@ def float_vec_sum_1(val,val_name,exception_class):
return val
raise(exception_class(val_name + " must be a 1-dimensional numpy.ndarray, and the sum of its elements must equal to 1."))

def float_vecs_sum_1(val,val_name,exception_class):
if type(val) is np.ndarray:
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1 and np.all(np.abs(np.sum(val, axis=-1) - 1.) <= _EPSILON):
return val.astype(float)
if np.issubdtype(val.dtype,np.floating) and val.ndim >= 1 and np.all(np.abs(np.sum(val, axis=-1) - 1.) <= _EPSILON):
return val
raise(exception_class(val_name + " must be a numpy.ndarray whose ndim >= 1, and the sum along the last dimension must equal to 1."))

def int_(val,val_name,exception_class):
if np.issubdtype(type(val),np.integer):
return val
Expand All @@ -189,3 +242,15 @@ def onehot_vecs(val,val_name,exception_class):
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1 and np.all(val >= 0) and np.all(val.sum(axis=-1)==1):
return val
raise(exception_class(val_name + " must be a numpy.ndarray whose dtype is int and whose last axis constitutes one-hot vectors."))

def int_vecs(val,val_name,exception_class):
if type(val) is np.ndarray:
if np.issubdtype(val.dtype,np.integer) and val.ndim >= 1:
return val
raise(exception_class(val_name + " must be a numpy.ndarray whose dtype is int and ndim >= 1."))

def shape_consistency(val: int, val_name: str, correct: int, correct_name: str, exception_class):
if val != correct:
message = (f"{val_name} must coincide with {correct_name}: "
+ f"{val_name} = {val}, {correct_name} = {correct}")
raise(exception_class(message))
Loading

0 comments on commit e76b056

Please sign in to comment.