pythonbayesian-networkscausality

constructing an expertise based bayesian network in causalnex


Up to now, in causalnex package, I only encountered Bayesian networks that were constucted from data. I want to know how to create my own network with my node parameters and CPDs from expertise. Anybody has some reference to it or an example?


Solution

  • It looks like causalnex doesn't directly support setting the CPD's manually, but you can look at the underlying code and see that it's using the pgmpy BayesianModel to simultaneously represent the structure and CPD's within a causalnex BayesianNetwork.

    With that, you could add the CPD's you know via add_cpds instead of fitting them. To get at the BayesianModel object it would be: bn._model, where bn is your causalnex.BayesianNetwork object.

    I'm not sure if this would make you just want to use pgmpy instead of causalnex or not!! It seems like the big benefit from causalnex is its use of the NOTEARS algorithm, which helps you build the Weighted Adjacency Matrix for your Directed Graph. Then again, it also coordinates some plotting for you.

    Also, an important note from the docs to remind you that it's not really continuous, but discretised/binned:

    Bayesian Networks in CausalNex support only discrete distributions. Any continuous features, or features with a large number of categories, should be discretised prior to fitting the Bayesian Network. Models containing variables with many possible values will typically be badly fit, and exhibit poor performance.