The BayesianNetwork class is used to randomly sample from a distribution defined by a JSON network definition.
Network Definition Structure
The definition is a JSON object containing a nodes array. Each node object must include:
name: The unique identifier for the node.values: An array of possible values for that node.parentNames: An array of names of nodes that this node depends on.conditionalProbabilities: The probability distribution. This can be provided upfront in the JSON, or calculated later from data.
Workflow
- Initialize: Create an instance with
new BayesianNetwork(networkDefinition). - Configure Probabilities (Optional): If the JSON doesn't include probabilities, use
.setProbabilitiesAccordingToData(dataframe) with a Danfo.js dataframe. - Persist: Save the configured network using
.saveNetworkDefinition(networkDefinitionFilePath). - Sample: Generate data using
.generateSample() or .generateConsistentSampleWhenPossible().
{
"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
}
}
}
]
}