npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bayesjs

v0.6.5

Published

Inference on Bayesian Networks

Downloads

49

Readme

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.

It's important to remember that junction tree uses WeakMap to cache some internal results, if you are mutating the network or given object is advisable to shallow clone both objects before infer. Read more about JT cache here

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

inferAll(network: INetwork, given?: ICombinations, options?: IInferAllOptions): INetworkResult)

Calculate all probabilities from a network by receiving the network, knowing states, and options. It returns an object with all results.

This method will execute the junction tree algorithm on each node's state.

Options
force

default: false

Enforces to clear junction tree cache before inferring all network. The junction tree uses WeakMap to store the cliques and potentials that are used at the algorithm.

  • cliques weak stored by network
  • potentials weak stored by cliques and given

This option is only necessary if you are mutation your network or given object instead of creating a new object before inferring each time.

precision

default: 8

Rounds the network results according to this value. To round the value we are using round-to.

Some rounds examples:

  • 0.30000000000000004
    • 8 precision -> 0.3
    • 4 precision -> 0.3
    • 2 precision -> 0.3
  • 0.3333333333333333
    • 8 precision -> 0.33333333
    • 4 precision -> 0.3333
    • 2 precision -> 0.33
  • 0.9802979902088171
    • 8 precision -> 0.98029799
    • 4 precision -> 0.9803
    • 2 precision -> 0.98
Example
const network = {
  'Node 1': {
    id: 'Node 1',
    states: ['True', 'False'],
    parents: [],
    cpt: { True: 0.5, False: 0.5 },
  },
  'Node 2': {
    id: 'Node 2',
    states: ['True', 'False'],
    parents: [],
    cpt: { True: 0.5, False: 0.5 },
  },
  'Node 3': {
    id: 'Node 3',
    states: ['True', 'False'],
    parents: ['Node 2', 'Node 1'],
    cpt: [
      {
        when: { 'Node 2': 'True', 'Node 1': 'True' },
        then: { True: 0.5, False: 0.5 },
      },
      {
        when: { 'Node 2': 'False', 'Node 1': 'True' },
        then: { True: 0.5, False: 0.5 },
      },
      {
        when: { 'Node 2': 'True', 'Node 1': 'False' },
        then: { True: 0.5, False: 0.5 },
      },
      {
        when: { 'Node 2': 'False', 'Node 1': 'False' },
        then: { True: 0.5, False: 0.5 },
      },
    ],
  },
};

const given = { 'Node 1': 'True' }

inferAll(network, given)
// {
//   'Node 1': { True: 1, False: 0 },
//   'Node 2': { True: 0.5, False: 0.5 },
//   'Node 3': { True: 0.5, False: 0.5 },
// }

// Mutating the network...
network["Node 3"].cpt[0].then = { True: 0.95, False: 0.05 };

inferAll(network, given);
// Cached result - wrong
// {
//   'Node 1': { True: 1, False: 0 },
//   'Node 2': { True: 0.5, False: 0.5 },
//   'Node 3': { True: 0.5, False: 0.5 },
// }

inferAll(network, given, { force: true });
// {
//   'Node 1': { True: 1, False: 0 },
//   'Node 2': { True: 0.5, False: 0.5 },
//   'Node 3': { True: 0.725, False: 0.275 }
// }

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