GithubHelp home page GithubHelp logo

vangj / jsbayes-lcg Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 33 KB

Simple Linear Conditional Gaussian (LCG) Bayesian Belief Network (BBN) inference library in JavaScript.

License: Apache License 2.0

Makefile 0.41% JavaScript 99.59%

jsbayes-lcg's Introduction

jsbayes-lcg

This JavaScript library is a Bayesian Belief Network (BBN) inference tool using Gibbs sampling. The form of the BBN is assumed to be Linear Gaussian (LG). A LG BBN is one where the joint distribution is defined by a multivariate normal Gaussian distribution and each local probability models is defined by a linear combination of its parents. In this library, all variables in a LG BBN are continuous (as opposed to discrete). If you are working with discrete variables, please take a look at jsbayes.

#How do I get the library?

Download the file jsbayes-lcg.js and make a reference to the JavaScript file.

<script type="text/javascript" src="jsbayes-lcg.js"></script>

Or use Bower.

bower install jsbayes-lcg --save

Or use NPM.

npm install jsbayes-lcg --save

For NPM, import the library with var jsbayeslcg = require('jsbayes-lcg');

#How do I use jsbayes-lcg?

As a quickstart, here's how you create a LCG BBN. Assume we have 3 continuous variables, then the LCG BBN may be defined with the means and covariance matrix as follows.

//means is a column vector of means
var means = [
    [ 1 ],
    [ -4.5 ],
    [ 8.5 ]
];
//sigma is a covariance matrix
var sigma = [
    [4,2,-1],
    [2, 5, -5],
    [-2, -5, 8]
];
var g = jsbayeslcg.newGraph(means, sigma);
//define the structure (or parent-child relationships)
var n3 = g.defineNode('n2', 2, [1]); //node 1 is a parent of node 2
var n2 = g.defineNode('n1', 1, [0]); //node 0 is a parent of node 1
var n1 = g.defineNode('n0', 0); //node 0 has no parents

You can then perform inference.

var T = 10000; //max iterations in gibbs sampling, here it's 10,000
g.sample(T); //actually performm the sampling

//the averages are the averages for each variable after sampling
var avg1 = n1.avg;
var avg2 = n2.avg;
var avg3 = n3.avg;

You may also observe a variable.

n1.observe(2.5);
g.sample(T);

You may also unobserve a variable.

n1.unobserve();
g.sample(T);

#Computing the means and covariance matrix There are util methods for you to compute the means and covariance matrix. Assume you have a matrix of data as follows.

var data = [
 [0, 1, 9],
 [1, 2, 8],
 [2, 3, 7],
 [3, 4, 6],
 [4, 5, 5],
 [5, 6, 4],
 [6, 7, 3],
 [7, 8, 2],
 [8, 9, 1],
 [9, 10, 0]
];

Then you can compute the means (column vector of 3 rows by 1 column) and covariance matrix (3 rows by 3 columns).

var means = jsbayeslcg.getMeans(data);
var sigma = jsbayeslcg.getCovMatrix(data);

You may then feed the means and covariance matrix directly back into jsbayes-lcg to partly specify the BBN.

var g = jsbayeslcg.newGraph(means, sigma);

#NOTICE THIS IS BETA SOFTWARE, USE AT YOUR OWN RISK.

jsbayes-lcg's People

Contributors

vangj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.