Skip to main content

Bayes Nets and Sampling

About:

Developed code from scratch to compute an input probability query on a given Bayes net on discrete random variables using Prior sampling, Rejection Sampling, Likelihood weighting and Gibbs sampling.
I had also written helper function to generate samples from any input univariate discrete distribution and then use it in your Bayes Net sampling code.

BayesNets Class:

I implemented a BayesNets Class to do all the required task and computations.
Basically I created a single class to do all the calculation, extractions of details from the file as well as implement all the sampling methods and sample generation.

Install the Python Package

pip install bayes-nets-sample

Import the Python Package

from bayes import bayesNets

To see the Sample .txt file

from bayes.sample_input_txt import SampleInputTXT
SampleInputTXT().generate_sample()

To Get more information about the BayesNets Class as well as its methods:

run the following command:

help(BayesNets)

Lets Start By Creating an object of BayesNets and extract the information from the file:

bayesNet = BayesNets(filepath = "example_bayesnet.txt")

Let's Print The Extracted Information:

print(bayesNet)

Let's See how a single sample is generated:

bayesNet.generateSample()

Now Lets Calculate the Query result using Prior Sampling Method:

With verbose = True

bayesNet.doPriorSampling(TOTAL_SAMPLE = 10000, verbose = True)

Now Lets Calculate the Query result using Prior Sampling Method:

With verbose = False

  • Just to see the final result.
bayesNet.doPriorSampling(TOTAL_SAMPLE = 10000, verbose = False)

Now Lets Calculate the Query result using Rejection Sampling Method:

With verbose = True

bayesNet.doRejectionSampling(TOTAL_SAMPLE_REQUIRED = 10000, verbose = True)

Now Lets Calculate the Query result using Rejection Sampling Method:

With verbose = False

  • Just to see the final result.
bayesNet.doRejectionSampling(TOTAL_SAMPLE_REQUIRED = 10000, verbose = False)

Let's See how a single weighted sample is generated:

bayesNet.generateWeightedSample(verbose = True)

Now Lets Calculate the Query result using Likelihood Weighting Method:

With verbose = True

bayesNet.doLikelihoodWeighting(TOTAL_SAMPLE = 10000, verbose = True)

Now Lets Calculate the Query result using Likelihood Weighting Method:

With verbose = False

  • Just to see the final result.
bayesNet.doLikelihoodWeighting(TOTAL_SAMPLE = 10000, verbose = False)

Sample File

5, B, E, A, J, M
B, +b, -b
E, +e, -e
A, +a, -a
J, +j, -j
M, +m, -m
B |
+b, 0.001
-b, 0.999
E |
+e, 0.002
-e, 0.998
A | B,E
+b, +e, +a, 0.95
+b, +e, -a, 0.05
+b, -e, +a, 0.94
+b, -e, -a, 0.06
-b, +e, +a, 0.29
-b, +e, -a, 0.71
-b, -e, +a, 0.001
-b, -e, -a, 0.999
J | A
+a, +j, 0.9
+a, -j, 0.1
-a, +j, 0.05
-a, -j, 0.95
M | A
+a, +m, 0.7
+a, -m, 0.3
-a, +m, 0.01
-a, -m, 0.99
Query: P( B=+b| J=+j)

Release files for bayes-nets-sample 1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bayes-nets-sample 1.5
File Size Uploaded
bayes_nets_sample-1.5.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bayes-nets-sample 1.5
File Interpreter ABI Platform
bayes_nets_sample-1.5-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / bayes_nets_sample-1.5.tar.gz

Download URL bayes_nets_sample-1.5.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
be0c7f0a861a2095fde9b821d3974f2ea414a45aaa5732aebd3918135be02a9f
BLAKE2b-256 checksum
How to use checksums
ecd24d51cf103e6d222b601154eaf925a3180b4b9290cb58b4263a7e7cc71296
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Release files / bayes_nets_sample-1.5-py3-none-any.whl

Download URL bayes_nets_sample-1.5-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2bec3b52c2b6d9976cf7ccb4bf11aa483d3c3d2e9e99bca1bef5face8f9dbe27
BLAKE2b-256 checksum
How to use checksums
5fa72564b73937ca54f1264f3791d86765fa8fb461f596847b9abb48b2501c94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Release history Release notifications | RSS feed

This release

1.5 This release

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1.1

2 release files

1.1

2 release files

1.0

2 release files

0.8

2 release files

0.7

2 release files

0.6

2 release files

0.5

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page