Skip to content

Flowan24/bayesjs

 
 

Repository files navigation

Build Status Coverage Status npm bundle size Commitizen friendly js-standard-style semantic-release

BayesJS

A inference library for Bayesian Networks made with TypeScript.

Inferences

Currently there are three inferences algorithms:

Methods

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);
Example

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

addNode(network: INetwork, node: INode): INetwork

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.

Example
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);

License

MIT

About

Inference on Bayesian Networks

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.5%
  • JavaScript 0.5%