A inference library for Bayesian Networks made with TypeScript.
Currently there are three inferences algorithms:
- Junction tree algorithm
- Variable elimination
- Enumeration
infer(network: INetwork, nodes?: ICombinations, given?: ICombinations): number
Calculate the probability of a node's state.
This function receives a network, a node's state, and the knowing states and will return the probability of the node's state give.
As mentioned above, there are three inferences engines, by default the junction tree algorithm is used to execute the infer function.
import { infer, inferences } from 'bayesjs';
infer(network, nodes, give); // Junction tree algorithm
inferences.enumeration.infer(network, nodes, give);
inferences.variableElimination.infer(network, nodes, give);
inferences.junctionTree.infer(network, nodes, give);
Given the rain-sprinkler-grasswet network. Image here.
import { infer } from 'bayesjs';
const network = // ...
// What is the probability that it is raining (RAIN = T)?
infer(network, { 'RAIN': 'T' }).toFixed(4) // 0.2000
// What is the probability that it is raining (RAIN = T), given the sprinkler is off (SPRINKLER = F)?
infer(network, { 'RAIN': 'T' }, { 'SPRINKLER': 'F' }).toFixed(4) // 0.2920
Add a node in a Bayesian Network.
This function receives a network and a node, check if the node can be appended on the network. If something is wrong an exception will be thrown, otherwise, a new network will return with the node added.
import { addNode } from 'bayesjs';
const networkWithRainAndSprinkler = // ...
const grassWet = {
id: 'GRASS_WET',
states: [ 'T', 'F' ],
parents: [ 'RAIN', 'SPRINKLER' ],
cpt: [
{ when: { 'RAIN': 'T', 'SPRINKLER': 'T' }, then: { 'T': 0.99, 'F': 0.01 } },
{ when: { 'RAIN': 'T', 'SPRINKLER': 'F' }, then: { 'T': 0.8, 'F': 0.2 } },
{ when: { 'RAIN': 'F', 'SPRINKLER': 'T' }, then: { 'T': 0.9, 'F': 0.1 } },
{ when: { 'RAIN': 'F', 'SPRINKLER': 'F' }, then: { 'T': 0, 'F': 1 } }
]
};
const newtwork = addNode(networkWithRainAndSprinkler, grassWet);
MIT