erdogant / bnlearn

Python library for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods.
https://erdogant.github.io/bnlearn
Other
465 stars 46 forks source link

Smooth param and aode model #49

Closed PARODBE closed 2 years ago

PARODBE commented 2 years ago

Hi @erdogant ,

One question, I don't know if we speak about smooth parameter, but could you incorporate? It is a very importante parameter, but it is possible that we spoke about this and may be you already incorporated.

And another thing could you incorporate AODE model? It is very interesting and in bnclassify or bnlearn of r is available.

Thanks for all!

Pablo

erdogant commented 2 years ago

Can you show a reference what you exactly mean with the smooth parameter and the AODE model.

PARODBE commented 2 years ago

Sure. Regarding smooth param, In bnclassify for example: https://github.com/bmihaljevic/bnclassify/blob/master/R/0bnclassify-doc.R : "The smoothing value (\eqn{\alpha}) for Bayesian #' parameter estimation. Nonnegative. \eqn{\alpha =

' 0} corresponds to maximum likelihood estimation. Returns a uniform

' distribution when \eqn{N_{ i j \cdot } + r_i \alpha = 0}{N[ ij . ] + r[i]

' \alpha = 0}. With partially observed data, the above amounts to

' \emph{available case analysis}." this part is in: https://rdrr.io/github/bmihaljevic/bnclassify/src/R/0bnclassify-doc.R

For me It is more clear implement it from scratch like this for example: https://kenzotakahashi.github.io/naive-bayes-from-scratch-in-python.html Here you have the implementation of smooth parameter in a naive bayes, multinomial naive bayes etc...

Respect AODE model: https://rdrr.io/github/bmihaljevic/bnclassify/src/R/bncs.R

Let me know if it is enough or you need more information please.

Thanks for all @erdogant !

Pablo

erdogant commented 2 years ago

Thanks for the links! A lot to read I see ;-)

PARODBE commented 2 years ago

Hi @erdogant , I made the same question in pgmpy, and I am going to paste the answer for you, because they already have a solution implemented and may be this is easier add in your library, I don't know:

@PARODBE Sorry for the late reply. If I am understanding correctly, the smooth parameter in bnclassify is equivalent to providing a pseudo_counts for each state when estimating the parameters. It should be equivalent to using BayesianEstimator with dirichlet prior in pgmpy. Here's an example with data generated from the alarm model and trying to learn the parameters of the model back from the simulated data. The pseudo_counts parameter should be equivalent to the smooth parameter in bnclassify.

In [1]: from pgmpy.estimators import BayesianEstimator

In [2]: from pgmpy.utils import get_example_model

In [3]: from pgmpy.models import BayesianNetwork

In [4]: model = get_example_model('alarm')

In [5]: df = model.simulate(int(1e4)) Generating for node: BP: 100%|████████████████████████████████████████████| 37/37 [00:00<00:00, 299.29it/s] /home/ankur/pgmpy/pgmpy/models/BayesianNetwork.py:1227: FutureWarning: Passing a set as an indexer is deprecated and will raise in a future version. Use a list instead. return samples.loc[:, set(self.nodes()) - self.latents]

In [6]: new_model = BayesianNetwork(model.edges())

In [7]: cpds = BayesianEstimator(new_model, df).get_parameters(prior_type='dirichlet', pseudo_counts=1)

In [8]: new_model.add_cpds(*cpds)

erdogant commented 2 years ago

Thank you for letting me know! I did add the smooth parameter in the latest version now.

PARODBE commented 2 years ago

Thank you !!:)