Skip to content

Release 0.2.0 #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 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
Iikubo Yuji and Saito Shota and Kazama Koki and
Matsushima Toshiyasu}
title = {BayesML},
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
8 changes: 7 additions & 1 deletion bayesml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from . import normal
from . import poisson
from . import metatree
from . import contexttree
from . import gaussianmixture
from . import hiddenmarkovnormal

__all__ = ['bernoulli',
'categorical',
Expand All @@ -16,5 +19,8 @@
'multivariate_normal',
'normal',
'poisson',
'metatree'
'metatree',
'contexttree',
'gaussianmixture',
'hiddenmarkovnormal',
]
2 changes: 1 addition & 1 deletion bayesml/bernoulli/_bernoulli.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def visualize_posterior(self):
p_range = np.linspace(0,1,100,endpoint=False)
fig, ax = plt.subplots()
ax.plot(p_range,self.estimate_params(loss="KL").pdf(p_range))
ax.set_xlabel("p_theta")
ax.set_xlabel("theta")
ax.set_ylabel("posterior")
plt.show()

Expand Down
2 changes: 1 addition & 1 deletion bayesml/categorical/_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def update_posterior(self, x):
2-dimensional array whose shape is ``(sample_size,degree)`` whose rows are one-hot vectors.
"""
_check.onehot_vecs(x,'x',DataFormatError)
if self.degree > 1 and x.shape[-1] != self.degree:
if x.shape[-1] != self.degree:
raise(DataFormatError(f"x.shape[-1] must be degree:{self.degree}"))
x = x.reshape(-1,self.degree)

Expand Down
10 changes: 5 additions & 5 deletions bayesml/contexttree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
where the updating rule of the hyperparameter is as follows:

.. math::
\beta_n(k|s) = \beta_0(k|s) + \sum_{i=1}^n I \left\{ \text{:math:`s` is the ancestor of :math:`s_{T_\mathrm{max}}(x^{i-1})` and :math:`x_i=k` } \right\}.
\beta_n(k|s) = \beta_0(k|s) + \sum_{i=1}^n I \left\{ s \ \mathrm{is \ the \ ancestor \ of} \ s_{T_\mathrm{max}}(x^{i-1}) \ \mathrm{and} \ x_i=k \right\}.

For :math:`T \in \mathcal{T}`,

Expand All @@ -66,18 +66,18 @@
.. math::
g_{n,s} =
\begin{cases}
g_{0,s} & \text{if :math:`n=0`}, \\
g_{0,s}, & n=0, \\
\frac{ g_{n-1,s} \tilde{q}_{s_{\mathrm{child}}} (x_n|x^{n-1}) }
{ \tilde{q}_s(x_n|x^{n-1}) } & \text{otherwise},
{ \tilde{q}_s(x_n|x^{n-1}) } & \mathrm{otherwise},
\end{cases}

where :math:`s_{\mathrm{child}}` is the child node of :math:`s` on the path from :math:`s_\lambda` to :math:`s_{T_\mathrm{max}}(x^n)` and

.. math::
\tilde{q}_s(x_n|x^{n-1}) =
\begin{cases}
q_s(x_n|x^{n-1}) & \text{if :math:`s\in\mathcal{L}(T_\mathrm{max})`}, \\
(1-g_{n-1,s}) q_s(x_n|x^{n-1}) + g_{n-1,s} \tilde{q}_{s_{\mathrm{child}}}(x_n|x^{n-1}) & \text{otherwise}.
q_s(x_n|x^{n-1}) & s\in\mathcal{L}(T_\mathrm{max}), \\
(1-g_{n-1,s}) q_s(x_n|x^{n-1}) + g_{n-1,s} \tilde{q}_{s_{\mathrm{child}}}(x_n|x^{n-1}) & \mathrm{otherwise}.
\end{cases}

Here,
Expand Down
16 changes: 8 additions & 8 deletions bayesml/contexttree/_contexttree.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _visualize_model_recursion(self,tree_graph,node,node_id,parent_id,sibling_nu
tmp_p_v = p_v

# add node information
label_string = f'h_g={node.h_g:.2f}\\lp_v={tmp_p_v:.2f}\\ltheta_vec='
label_string = f'h_g={node.h_g:.2f}\\lp_v={tmp_p_v:.2f}\\ltheta_vec\\l='
if node.leaf:
label_string += '['
for i in range(self.c_k):
Expand Down Expand Up @@ -388,16 +388,16 @@ def visualize_model(self,filename=None,format=None,sample_length=10):
Examples
--------
>>> from bayesml import contexttree
>>> model = contexttree.GenModel(c_k=2,c_d_max=3,h_g=0.75)
>>> gen_model = contexttree.GenModel(c_k=2,c_d_max=3,h_g=0.75)
>>> gen_model.gen_params()
>>> model.visualize_model()
[1 1 1 1 1 1 0 0 0 1]
>>> gen_model.visualize_model()
[1 0 1 0 0 0 1 0 0 0]

.. image:: ./images/contexttree_example.png

See Also
--------
graphbiz.Digraph
graphviz.Digraph
"""
#例外処理
_check.pos_int(sample_length,'sample_length',DataFormatError)
Expand Down Expand Up @@ -781,7 +781,7 @@ def estimate_params(self,loss="0-1",visualize=True,filename=None,format=None):

See Also
--------
graphbiz.Digraph
graphviz.Digraph
"""

if loss == "0-1":
Expand Down Expand Up @@ -820,7 +820,7 @@ def _visualize_model_recursion(self,tree_graph,node:_LearnNode,node_id,parent_id
tmp_p_v = p_v

# add node information
label_string = f'hn_g={node.hn_g:.2f}\\lp_v={tmp_p_v:.2f}\\ltheta_vec='
label_string = f'hn_g={node.hn_g:.2f}\\lp_v={tmp_p_v:.2f}\\ltheta_vec\\l='
label_string += '['
for i in range(self.c_k):
theta_vec_hat = node.hn_beta_vec / node.hn_beta_vec.sum()
Expand Down Expand Up @@ -869,7 +869,7 @@ def visualize_posterior(self,filename=None,format=None):

See Also
--------
graphbiz.Digraph
graphviz.Digraph
"""
try:
import graphviz
Expand Down
Loading