/generative-bayesian-network

Primary LanguageJavaScriptApache License 2.0Apache-2.0

(DEPRECATED) Generative bayesian network


DEPRECATED The generative-bayesian-network package now lives in the fingerprint-suite repository. This repository is no longer actively maintained.


NodeJs package containing a bayesian network capable of randomly sampling from a distribution defined by a json object.

Installation

Run the npm i generative-bayesian-network command. No further setup is needed afterwards.

Usage

To use the network, you need to create an instance of the BayesianNetwork class which is exported from this package. Constructor of this class accepts a JSON object containing the network definition. This definition can either include the probability distributions for the nodes, or these can be calculated later using data. An example of such a definition saved in a JSON file could look like:

{
    "nodes": [
        {
            "name": "ParentNode",
            "values": ["A", "B", "C"],
            "parentNames": [],
            "conditionalProbabilities": {
                "A": 0.1,
                "B": 0.8,
                "C": 0.1
            }
        },
        {
            "name": "ChildNode",
            "values": [".", ",", "!", "?"],
            "parentNames": ["ParentNode"],
            "conditionalProbabilities": {
                "A": {
                    ".": 0.7,
                    "!": 0.3
                },
                "B": {
                    ",": 0.3,
                    "?": 0.7
                },
                "C": {
                    ".": 0.5,
                    "?": 0.5
                }
            }
        }
    ]
}

Once you have the network definition ready, you can create an instance simply by executing:

let generatorNetwork = new BayesianNetwork(networkDefinition);

If the network definition didn't include the probabilities, you also need to call the setProbabilitiesAccordingToData method and provide it with a Danfo.js dataframe containing the dataset you want to be used to calculate the probabilities:

generatorNetwork.setProbabilitiesAccordingToData(dataframe);

After the setup, you can save the current network's definition by doing:

generatorNetwork.saveNetworkDefinition(networkDefinitionFilePath);

Once you have the network all set up, you can use two methods to actually generate the samples - generateSample and generateConsistentSampleWhenPossible. The first one generates a sample of all node values given (optionally) the values we already know in the form of an object. The second does much the same thing, but instead of just getting the known values of some of the attributes, the object you can give it as an argument can contain multiple possible values for each node, not just one. You could run them for example like this:

let sample = generatorNetwork.generateSample({ "ParentNode": "A" });
let consistentSample = generatorNetwork.generateSample({
    "ParentNode": ["A","B"], "ChildNode": [",","!"]
});

API Reference

All public classes, methods and their parameters can be inspected in this API reference.

BayesianNetwork

BayesianNetwork is an implementation of a bayesian network capable of randomly sampling from the distribution represented by the network.


new BayesianNetwork(networkDefinition)

Param Type Description
networkDefinition object object defining the network structure and distributions

bayesianNetwork.generateSample(inputValues)

Randomly samples from the distribution represented by the bayesian network.

Param Type Description
inputValues object node values that are known already

bayesianNetwork.generateConsistentSampleWhenPossible(valuePossibilities)

Randomly samples from the distribution represented by the bayesian network, making sure the sample is consistent with the provided restrictions on value possibilities. Returns false if no such sample can be generated.

Param Type Description
valuePossibilities object a dictionary of lists of possible values for nodes (if a node isn't present in the dictionary, all values are possible)

bayesianNetwork.setProbabilitiesAccordingToData(dataframe)

Sets the conditional probability distributions of this network's nodes to match the given data.

Param Type Description
dataframe object a Danfo.js dataframe containing the data

bayesianNetwork.saveNetworkDefinition(networkDefinitionFilePath)

Saves the network definition to the specified file path to be used later.

Param Type Description
networkDefinitionFilePath string a file path where the network definition should be saved