Coder Social home page Coder Social logo

bayesiannetworks's Introduction

BayesianNetworks

This library is designed to easily perform inference on belief networks

Getting started

  • Include bayesian_inference in your source files
  • Add this line to your executable #include "baynet/Graph.h"
  • Add the library in the CMakeLists.txt like this
target_link_libraries(${PROJECT_NAME} baynet)

Usage

Loading the network

Put the xdsl file containing the network in the data folder, then load the network by passing the file name to the constructor

// load the network contained in Credit.xdsl
baynet::Graph network("data/Credit.xdsl");

See the initial state of the network

To see the prior probabilities of each node, just call the inference method like this

int num_samples = 10000;
std::unordered_map<std::string, std::vector<float>> results = network.inference(num_samples);

// use this to print the network in a nice format
baynet::Graph::pretty_print(results);

Set the evidence

Write the evidence variables, separating them with a comma, then pass the string to the inference method. You can also decide to pass 1 if you want to use rejection sampling (default is likelihood weighting)

std::string evidence = "CreditWorthiness=Negative,Assets=wealthy"; // if there is a space after the comma it doesn't matter
results = network.inference(num_samples, evidence); // network.inference(num_samples, evidence, 1) to use rejection sampling
baynet::Graph::pretty_print(results); // see the results

If you are interested only in a single node, you can save a lot of computational time by calling single_node_inference.

// Let's calculate P(VisitToAsia|Tuberculosis=Present)
std::string query = "VisitToAsia|Tuberculosis=Present"; // set the query
std::vector<float> results = network.single_node_inference(query, num_samples); // obtain the conditional probabilities
baynet::Graph::pretty_print_query(results, query); // print the result

Edit the network

If you want, you can change the CPT of a node, given its name

// make sure the number of probabilities matches the CPT of the node you want to edit
std::string problist = "0.5 0.42 0.08";
network.edit_cpt("Income", problist);

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.