Skip to content

Commit 63a2e54

Browse files
Merge pull request #92 from bayesml/release-0.3.1
Release 0.3.1
2 parents c2f457c + 437e333 commit 63a2e54

Some content is hidden

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

48 files changed

+4479
-1007
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--
22
Document Author
3-
Yuta Nakahara <yuta.nakahara@aoni.waseda.jp>
3+
Yuta Nakahara <y.nakahara@waseda.jp>
44
Shota Saito <shota.s@gunma-u.ac.jp>
55
-->
66
# How to contribute

CONTRIBUTING_jp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--
22
Document Author
3-
Yuta Nakahara <yuta.nakahara@aoni.waseda.jp>
3+
Yuta Nakahara <y.nakahara@waseda.jp>
44
-->
55
# コントリビューションの方法
66

README.md

Lines changed: 162 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--
22
Document Author
3-
Yuta Nakahara <yuta.nakahara@aoni.waseda.jp>
3+
Yuta Nakahara <y.nakahara@waseda.jp>
44
Shota Saito <shota.s@gunma-u.ac.jp>
55
-->
66

@@ -14,29 +14,34 @@ Shota Saito <shota.s@gunma-u.ac.jp>
1414

1515
<img src="./doc/logos/BayesML_logo.png" width="600">
1616

17-
## Purpose
17+
# Your First Library for Bayesian Machine Learning
1818

19-
BayesML is a library designed for promoting research, education, and application of machine learning based on Bayesian statistics and Bayesian decision theory. Through these activities, BayesML aims to contribute to society.
19+
BayesML contributes to wide society thourgh promoting education, research, and application of machine learning based on Bayesian statistics and Bayesian decision theory.
2020

2121
## Characteristics
2222

23-
BayesML has the following characteristics.
23+
* **Easy-to-use:**
24+
* You can use pre-defined Bayesian statistical models by simply importing it. You don't need to define models yourself like PyMC or Stan.
25+
* **Bayesian Decision Theoretic API:**
26+
* BayesML's API corresponds to the structure of decision-making based on Bayesian decision theory. Bayesian decision theory is a unified framework for handling various decision-making processes, such as parameter estimation and prediction of new data. Therefore, BayesML enables intuitive operations for a wider range of decision-making compared to the fit-predict type API adopted in libraries like scikit-learn. Moreover, many of our models also implement fit-predict functions.
27+
* **Model Visuialization Functions:**
28+
* All packages have methods to visualize the probabilistic data generative model, generated data from that model, and the posterior distribution learned from the data in 2~3 dimensional space. Thus, you can effectively understand the characteristics of probabilistic data generative models and algorithms through the generation of synthetic data and learning from them.
29+
* **Fast Algorithms Using Conjugate Prior Distributions:**
30+
* Many of our learning algorithms adopt exact calculation methods or variational Bayesian methods that effectively use the conjugacy between probabilistic data generative models and prior distributions. Therefore, they are much faster than general-purpose MCMC methods and are also suitable for online learning. Although some algorithms adopt MCMC methods, but they use MCMC methods specialized for each model, taking advantage of conjugacy.
2431

25-
* The structure of the library reflects the philosophy of Bayesian statistics and Bayesian decision theory: updating the posterior distribution learned from the data and outputting the optimal estimate based on the Bayes criterion.
26-
* Many of our learning algorithms are much faster than general-purpose Bayesian learning algorithms such as MCMC methods because they effectively use the conjugate property of a probabilistic data generative model and a prior distribution. Moreover, they are suitable for online learning.
27-
* All packages have methods to visualize the probabilistic data generative model, generated data from that model, and the posterior distribution learned from the data in 2~3 dimensional space. Thus, you can effectively understand the characteristics of probabilistic data generative models and algorithms through the generation of synthetic data and learning from them.
28-
29-
For more details, see our [website](https://bayesml.github.io/BayesML/ "BayesML's Documentation").
32+
For more details, see our [website](https://bayesml.github.io/BayesML/).
3033

3134
## News
3235

33-
* Our algorithm for the meta-tree model is accepted at AISTATS 2025! A sample code is [here](https://bayesml.github.io/BayesML/examples/metatree_prediction_interval.html).
36+
* Our algorithm for the meta-tree model has been accepted to AISTATS 2025! For more details, please see the links below.
37+
* [Paper](https://proceedings.mlr.press/v258/nakahara25a.html)
38+
* [Code Example](https://bayesml.github.io/BayesML/examples/metatree_prediction_interval.html)
3439

3540
## Installation
3641

3742
Please use the following commands to install BayesML.
3843

39-
``` bash
44+
```bash
4045
pip install bayesml
4146
```
4247

@@ -48,76 +53,181 @@ The following are required.
4853
* MatplotLib (>= 3.5)
4954
* Scikit-learn (>= 1.1)
5055

51-
## Example
56+
## Tutorial
57+
58+
Each model in BayesML has two classes. One is `GenModel`, which can be used for parameter generation from prior or posterior distributions, and data generation. The other is `LearnModel`, which can be used for estimating posterior distributions from data and calculating predictive distributions. Each has an API that aligns with Bayesian decision theory. Let's look at how to use each with the `linearregression` model as an example.
59+
60+
### Synthetic Data Generation with `GenModel`
5261

53-
We show an example of generating data drawn according to the Bernoulli distribution and learning from them.
62+
First, let's import the library.
5463

55-
First, we create an instance of a probabilistic data generative model. Here, the parameter `theta`, which represents an occurrence probability of 1, is set to 0.7.
64+
```python
65+
import numpy as np
66+
from bayesml import linearregression
67+
```
5668

57-
``` python
58-
from bayesml import bernoulli
69+
Next, we create an instance of the probabilistic data generative model. Here, we specify the dimension of the regression coefficients (including the constant term) as `c_degree=2` as a constant of the model, and we specify the regression coefficients as `theta_vec = np.array([1,1])` and the precision (inverse of variance) of the noise term as `tau = 10` as parameters.
5970

60-
gen_model = bernoulli.GenModel(theta=0.7)
71+
```python
72+
gen_model = linearregression.GenModel(
73+
c_degree = 2, # degree
74+
theta_vec = np.array([1,1]), # coefficients
75+
tau = 10, # noise precision
76+
)
6177
```
6278

6379
You can visualize the characteristics of the created model by the following method.
6480

65-
``` python
81+
```python
6682
gen_model.visualize_model()
6783
```
6884

69-
>theta:0.7
70-
>x0:[1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1]
71-
>x1:[1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0]
72-
>x2:[1 0 1 1 0 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1]
73-
>x3:[1 1 1 0 1 1 0 1 0 0 0 0 1 0 1 1 1 1 1 1]
74-
>x4:[0 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1]
75-
>![bernoulli_example1](./doc/images/README_ex_img1.png)
85+
>Output:
86+
>theta_vec:
87+
>[1. 1.]
88+
>tau:
89+
>10.0
90+
>![png](./doc/images/README_LR1.png)
91+
92+
To generate a sample and save it to variables `x` and `y`, we use the following method:
93+
94+
```python
95+
x,y = gen_model.gen_sample(sample_size=100)
96+
```
7697

77-
After confirming that the frequency of occurrence of 1 is around `theta=0.7`, we generate a sample and store it to variable `x`.
98+
Let's also generate test data for later use.
7899

79-
``` python
80-
x = gen_model.gen_sample(sample_size=20)
100+
```python
101+
x_test,y_test = gen_model.gen_sample(sample_size=100)
81102
```
82103

83-
Next, we create an instance of a model for learning posterior distribution.
104+
### Learning and Decision Making with `LearnModel`
105+
106+
Let's use `LearnModel` to learn a model from the data we just generated.
107+
108+
Of course, the data that can be used with `LearnModel` is not limited to data generated from `GenModel`. You can analyze various real-world data.
84109

85-
``` python
86-
learn_model = bernoulli.LearnModel()
110+
First, let's create an instance of the learning model. Here, we only specify the degree `c_degree = 2` as a constant of the model, but you can also specify hyperparameters for the prior distribution.
111+
112+
```python
113+
learn_model = linearregression.LearnModel(
114+
c_degree = 2, # degree
115+
)
87116
```
88117

89-
A method to visualize the posterior distribution also exists (the prior distribution is shown here because learning from data has not been performed yet).
118+
A method for visualizing the posterior distribution of parameters is implemented in `LearnModel`. If you visualize the posterior distribution at this point, the prior distribution will be displayed since learning from data has not yet been performed.
90119

91-
``` python
120+
```python
92121
learn_model.visualize_posterior()
93122
```
94123

95-
>![bernoulli_example2](./doc/images/README_ex_img2.png)
124+
>Output:
125+
>![png](./doc/images/README_LR2.png)
126+
127+
To update the posterior distribution through learning from data, we use the following method.
96128

97-
After learning from the data, we can see that the density of the posterior distribution is concentrated around the true parameter `theta=0.7`.
129+
```python
130+
learn_model.update_posterior(x,y)
131+
```
98132

99-
``` python
100-
learn_model.update_posterior(x)
133+
If you visualize the updated posterior distribution, you can see that the density of the posterior distribution has moved closer to the true parameters used to generate `x` and `y`.
134+
135+
```python
101136
learn_model.visualize_posterior()
102137
```
103138

104-
>![bernoulli_example3](./doc/images/README_ex_img3.png)
139+
>Output:
140+
>![png](./doc/images/README_LR3.png)
141+
142+
To make decisions such as parameter estimation and prediction of new data based on the learned model, we proceed as follows.
143+
144+
For parameter estimation, we use the `estimate_params` method. By specifying the `loss` option as `squared`, you can obtain an estimate that minimizes the Bayes risk function based on the squared error loss function. The resulting value is the expected value of the posterior distribution.
145+
146+
```python
147+
learn_model.estimate_params(loss="squared",dict_out=True)
148+
```
149+
150+
>Output:
151+
>{'theta_vec': array([0.99846525, 0.96263024]), 'tau': 6.9036925167513195}
152+
153+
If you specify the `loss` option as `abs`, you can obtain an estimate that minimizes the Bayes risk function based on the absolute error loss function. The resulting value is the median of the posterior distribution, which is why the estimated value of `tau` differs from the previous one.
154+
155+
```python
156+
learn_model.estimate_params(loss="abs",dict_out=True)
157+
```
158+
159+
>Output:
160+
>{'theta_vec': array([0.99846525, 0.96263024]), 'tau': 6.858623148933392}
161+
162+
To predict new data, we first use the following method to calculate the predictive distribution for new explanatory variables.
163+
164+
```python
165+
learn_model.calc_pred_dist(x_test)
166+
```
167+
168+
Next, we use the `make_prediction` method to obtain predicted values. Similar to parameter estimation, you can specify the loss function using the `loss` option. (In this example, the same predicted values will be returned whether you assume squared error loss or absolute error loss since the posterior predictive distribution is symmetrical.)
169+
170+
```python
171+
y_pred = learn_model.make_prediction(loss="squared")
172+
```
173+
174+
Let's calculate the mean squared error.
105175

106-
In Bayesian decision theory, the optimal estimator under the Bayes criterion is derived as follows. First, we set a loss function, e.g., a squared-error loss, absolute-error loss, and 0-1 loss. Then, the Bayes risk function is defined by taking the expectation of the loss function with respect to the distribution of data and parameters. By minimizing the Bayes risk function, we obtain the optimal estimator under the Bayes criterion. For example, if we set a squared-error loss, the optimal estimator under the Bayes criterion of the parameter `theta` is the mean of the posterior distribution.
176+
```python
177+
mse = np.sum((y_test - y_pred)**2) / len(y_test)
178+
print(f"MSE: {mse}")
179+
```
180+
181+
>Output:
182+
>MSE: 0.09020880284291456
183+
184+
Taking into account that the precision (inverse of variance) of the noise term used for data generation was 10, we can see that the predictions are achieved with sufficient accuracy.
107185

108-
In BayesML, the above calclulation is performed by the following methods.
186+
### Sampling from Posterior Distribution Using `GenModel`
109187

110-
``` python
111-
print(learn_model.estimate_params(loss='squared'))
112-
print(learn_model.estimate_params(loss='abs'))
113-
print(learn_model.estimate_params(loss='0-1'))
188+
`GenModel` can also be used to sample parameters from the posterior distribution learned by `LearnModel`, or to sample new data from the posterior predictive distribution.
189+
190+
First, the hyperparameters of the posterior distribution learned by `LearnModel` can be obtained as follows.
191+
192+
```python
193+
hn_params = learn_model.get_hn_params()
194+
print(hn_params)
114195
```
115196

116-
>0.7380952380952381
117-
>0.7457656349087012
118-
>0.7631578947368421
197+
>Output:
198+
>{'hn_mu_vec': array([0.99846525, 0.96263024]), 'hn_lambda_mat': array(\[[ 99.87503339, 5.96145913],[ 5.96145913, 101. ]]), 'hn_alpha': 51.0, 'hn_beta': 7.387351026461872}
199+
200+
By passing these to `GenModel`, you can sample parameters from the posterior distribution.
119201

120-
Different settings of a loss function yield different optimal estimates.
202+
We create a new `GenModel` instance for parameter sampling and pass the hyperparameters through the `set_h_params` method. (In the example below, we are unpacking the values of the dictionary `hn_params` using `*` for `hn_params.values()`. This is a Python feature, not a BayesML functionality.)
203+
204+
```python
205+
posterior_gen_model = linearregression.GenModel(
206+
c_degree = 2, # degree
207+
)
208+
posterior_gen_model.set_h_params(*hn_params.values())
209+
```
210+
211+
We use the `gen_params` method to generate parameters and the `get_params` method to retrieve the generated parameters. If you want to perform multiple samplings, please repeat the following in a `for` loop.
212+
213+
```python
214+
posterior_gen_model.gen_params()
215+
print(posterior_gen_model.get_params())
216+
```
217+
218+
>Output:
219+
>{'theta_vec': array([1.00935782, 0.93804208]), 'tau': 5.50775630793475}
220+
221+
To sample new data from the posterior predictive distribution, we generate data after sampling parameters. When we generated the synthetic data, we did not provide explanatory variables as arguments to `gen_sample` (see [here](#synthetic-data-generation-with-genmodel)), but you can also specify them explicitly as follows.
222+
223+
```python
224+
posterior_gen_model.gen_params()
225+
_,y_new = posterior_gen_model.gen_sample(x=x_test[:10])
226+
print(f"y_new: {y_new}")
227+
```
228+
229+
>Output:
230+
>y_new: [-0.49532975 2.03473075 1.13758759 -0.46735058 -0.71902336 -0.09288005 0.89463227 2.07886012 2.81211771 1.60020635]
121231
122232
## Package list
123233

@@ -148,22 +258,22 @@ When you use BayesML for your academic work, please provide the following biblio
148258

149259
Plain text
150260

151-
```
261+
```text
152262
Y. Nakahara, N. Ichijo, K. Shimada, Y. Iikubo,
153263
S. Saito, K. Kazama, T. Matsushima, BayesML Developers, ``BayesML,''
154-
Python package version 0.3.0,
264+
Python package version 0.3.1,
155265
[Online] https://github.com/bayesml/BayesML
156266
```
157267

158268
BibTeX
159269

160-
``` bibtex
270+
```bibtex
161271
@misc{bayesml,
162272
author = {Nakahara, Yuta and Ichijo, Naoki and Shimada, Koshi and
163273
Iikubo, Yuji and Saito, Shota and Kazama, Koki and
164274
Matsushima, Toshiyasu and {BayesML Developers}},
165275
title = {{BayesML}},
166-
howpublished = {Python package version 0.3.0},
276+
howpublished = {Python package version 0.3.1},
167277
note = {\url{https://github.com/bayesml/BayesML}},
168278
year = {2025}
169279
}

0 commit comments

Comments
 (0)