Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Probabilistic Graphical Models with Python

You're reading from  Mastering Probabilistic Graphical Models with Python

Product type Book
Published in Aug 2015
Publisher
ISBN-13 9781784394684
Pages 284 pages
Edition 1st Edition
Languages
Author (1):
Ankur Ankan Ankur Ankan
Profile icon Ankur Ankan
Toc

Table of Contents (14) Chapters close

Mastering Probabilistic Graphical Models Using Python
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Bayesian Network Fundamentals 2. Markov Network Fundamentals 3. Inference – Asking Questions to Models 4. Approximate Inference 5. Model Learning – Parameter Estimation in Bayesian Networks 6. Model Learning – Parameter Estimation in Markov Networks 7. Specialized Models Index

Representing joint probability distributions using pgmpy


We can also represent joint probability distributions using pgmpy's JointProbabilityDistribution class. Let's say we want to represent the joint distribution over the outcomes of tossing two fair coins. So, in this case, the probability of all the possible outcomes would be 0.25, which is shown as follows:

In [16]: from pgmpy.factors import JointProbabilityDistribution as Joint
In [17]: distribution = Joint(['coin1', 'coin2'], 
                              [2, 2], 
                              [0.25, 0.25, 0.25, 0.25])

Here, the first argument includes names of random variable. The second argument is a list of the number of states of each random variable. The third argument is a list of probability values, assuming that the first variable changes its states the slowest. So, the preceding distribution represents the following:

In [18]: print(distribution)
╒═════════╤═════════╤══════════════════╕
│ coin1   │ coin2   │   P(coin1,coin2) │
╞═════════╪═════════╪══════════════════╡
│ coin1_0 │ coin2_0 │   0.2500         │
├─────────┼─────────┼──────────────────┤
│ coin1_0 │ coin2_1 │   0.2500         │
├─────────┼─────────┼──────────────────┤
│ coin1_1 │ coin2_0 │   0.2500         │
├─────────┼─────────┼──────────────────┤
│ coin1_1 │ coin2_1 │   0.2500         │
╘═════════╧═════════╧══════════════════╛

We can also conduct independence queries over these distributions in pgmpy:

In [19]: distribution.check_independence('coin1', 'coin2')
Out[20]: True
You have been reading a chapter from
Mastering Probabilistic Graphical Models with Python
Published in: Aug 2015 Publisher: ISBN-13: 9781784394684
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime