GENERATING BAYESIAN GRAPH USING RUBY AND GRAPHVIZ

Lot of folks, especially those taking probabilistic graphical model class, might have realized the need for a simple tool/software that can quickly generate a directed acyclic graph (DAG) and the associated conditional probability distribution (CPD) tables for each node of the graph. Below is a quick hack that I pulled together using Ruby and AT&T’s GraphViz library to generate a directed acyclic graph along with CPD (as shown below)

There are two aspects of this hack. First is the ruby script that reads a simple input file and generates a graph using dot notation. Next, we use the graphviz library to convert this dot notation output into an actual graph.

Below is a sample input file related to the student example used in the PGM class. There are few things to note about the input file. Each line above “#Edges” represents node label and the possible values (separated by commas). After “#Edges” represents connections between nodes. Make sure the edges use exactly same label as used for nodes.

Course Difficulty, High, Low
Intelligence, High, Low
Grade, A, B, C
SAT Score, High, Low
Letter, Yes, No
#Edges
Course Difficulty -> Grade -> Letter
Intelligence -> SAT Score
Intelligence -> Grade

Once you have everything in place (install graphviz library, download ruby script from here and a sample data file from here), run the following command from terminal/command line

>ruby generator.rb data.txt > graph.txt
>dot -T png graph.txt > graph.png
>open graph.png
About these ads

About Ritesh Agrawal

I am a applied researcher who enjoys anything related to statistics, large data analysis, data mining, machine learning and data visualization.
This entry was posted in Programming, Ruby and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s