You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
20
20
21
21
## Characteristics
22
22
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.
24
31
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/).
30
33
31
34
## News
32
35
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.
Please use the following commands to install BayesML.
38
43
39
-
```bash
44
+
```bash
40
45
pip install bayesml
41
46
```
42
47
@@ -48,76 +53,181 @@ The following are required.
48
53
* MatplotLib (>= 3.5)
49
54
* Scikit-learn (>= 1.1)
50
55
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`
52
61
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.
54
63
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
+
```
56
68
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.
59
70
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
+
)
61
77
```
62
78
63
79
You can visualize the characteristics of the created model by the following method.
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.
84
109
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
+
)
87
116
```
88
117
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.
To update the posterior distribution through learning from data, we use the following method.
96
128
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
+
```
98
132
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`.
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.
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.
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.)
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.
107
185
108
-
In BayesML, the above calclulation is performed by the following methods.
186
+
### Sampling from Posterior Distribution Using `GenModel`
`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.
By passing these to `GenModel`, you can sample parameters from the posterior distribution.
119
201
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.)
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.
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.
0 commit comments