erdogant / bnlearn

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

Metrics for model testing in pgmpy but not accessible in bnlearn #61

Closed briangriner closed 1 year ago

briangriner commented 1 year ago

Want to be able to obtain a structure score for the entire model to measure improvement from adding/deleting edges directly. This is possible using pgmpy.metrics.metrics.structure_score which will return the model score for different scoring methods (k2, bdeu, bds, bic). I can do it by recreating the DAG in pgmpy and using the metrics method but it would be nice to be able to stay in bnlearn. I have been in touch with the program author and I believe he has made this available in the newest update of bnlearn but I don't see it in the documentation yet. Many thanks, Brian

briangriner commented 1 year ago

I just found the documentation for the structure_score method in help(bnlearn.bnlearn.structure_scores). See code snip below. Thank you Ergodan. Greatly appreciated. Brian

code snip

import bnlearn as bn help(bn.structure_scores)

Help on function structure_scores in module bnlearn.bnlearn:

structure_scores(model, df, scoring_method=['k2', 'bds', 'bic', 'bdeu'], verbose=3, **kwargs) Compute structure scores.

Description
-----------
Uses the standard model scoring methods to give a score for each structure.
The score doesn't have very straight forward interpretebility but can be
used to compare different models. A higher score represents a better fit.
This method only needs the model structure to compute the score and parameters
aren't required.

Parameters
----------
model: pgmpy.base.DAG or pgmpy.models.BayesianNetwork instance
    The model whose score needs to be computed.

df: pd.DataFrame instance
    The dataset against which to score the model.

scoring_method: str ( k2 | bdeu | bds | bic )
    The following four scoring methods are supported currently: 1) K2Score
    2) BDeuScore 3) BDsScore 4) BicScore

kwargs: kwargs
    Any additional parameters parameters that needs to be passed to the
    scoring method. Check pgmpy.estimators.StructureScore for details.

Returns
-------
Model score: float
    A score value for the model.

Examples
--------
>>> from pgmpy.utils import get_example_model
>>> from pgmpy.metrics import structure_score
>>> model = get_example_model('alarm')
>>> data = model.simulate(int(10000))
>>> structure_score(model, data, scoring_method="bic")
-106665.9383064447
erdogant commented 1 year ago

I updated the docstrings and this functionality to the documentation pages.