Start up the python interpreter. You will be displaying a factored distribution so first create a scrolled frame object in which to display it:
>>> from Tkinter import Tk >>> root = Tk() >>> from gPy.Utils import scrolled_frame >>> sf = scrolled_frame(root)
We will use the 'Asia' Bayesian network (BN) as our factored distribution. The factors of a BN are of a special sort: conditional probability tables (CPTs), but here we want to treat them as normal factors. This can be effected in gPy by multiplying each by 1. Don't forget to indent the line inside the for loop.
>>> from gPy.Examples import asia >>> for cpt in asia: ... cpt *= 1 ... >>>
Ignore the message about not being able to use the Data class, you will never need that class, so no problem! Now take a look at the distribution.
>>> asia.gui_display(sf)
OK, your first task is write down the hypergraph associated with this factored distribution. Recall that a hypergraph is a set of hyperedges, and each hyperedge is a set of vertices. Here each vertex is the name of a random variable. Once you think you have it right, check you have the right answer.
>>> print asia.hypergraph()
Is this hypergraph reduced? Once you have decided check your answer.
>>> asia.hypergraph().is_reduced()
Now work out the reduction (reduced version) of this hypergraph. (If it is already reduced then the reduction is the same as the original hypegraph.) Check your answer.
>>> hg = asia.hypergraph() >>> hg.red() >>> print hg
If you want to understand how the red method works do:
>>> help(hg.red)
Don't kill your Python session, it will save you time to keep it around for the next question ...
I'm assuming you have a Python session with the objects from the last question. Firstly make a (new) copy of the 'Asia' hypergraph.
>>> asia_hg = asia.hypergraph()
Since asia_hg is a copy of the hypergraph
associated with 'Asia' we can alter it without altering the
asia object. To reassure yourself that this is the case
look at the documentation for the hypergraph method. (Hit
'q' to quit the help.)
>>> help(asia.hypergraph)
Write down the 2-section of the 'Asia' hypergraph. Now check your answer. If you want to rearrange the graph's vertices in the GUI: just move them by dragging with the left mouse button.
>>> print asia_hg.two_section() >>> asia_hg.two_section().gui_display(sf)
Now remove a hyperedge from the hypergraph:
>>> asia_hg.remove_hyperedge(['Cancer', 'TbOrCa', 'Tuberculosis'])
What is the 2-section graph of the (altered) hypergraph? Check your answer with:
>>> asia_hg.two_section().gui_display(sf)
First we need a graph. Save the
following 4 lines of Python source to a file stuff.py in
your working directory.
from Tkinter import Tk
from gPy.Graphs import UGraph
root = Tk()
gr = UGraph('abcdef')
Restart a new Python session. Grab all the objects defined in
stuff.py and start up gPy's rudimentary graph
editor.
>>> from stuff import * >>> gr.gui_edit(root)
To select a node or edge click the mouse's left button while the cursor is over the object. To draw an edge to a selected node place the cursor above another node and click the right button. Clicking the left button when over empty space provides the opportunity to add a vertex. (Just hit the generated "OK" button, if you do this by mistake.) Draw yourself some graph and click "Done" when finished.
Work out the clique hypergraph of your graph and check your answer by doing
>>> print gr.hypergraph()
We haven't covered an algorithm for pulling out cliques, but if your graphs are not too big you should be able to do it 'by eye'. Repeat this exercise for a few graphs that you create.
OK, this is a rather tedious exercise, but since factor multiplication and marginalisation are so central you have to do it by hand at least once. Firstly, for multiplication do the following:
>>> from gPy.Examples import asia >>> f1 = asia['Bronchitis'] * asia['Cancer'] >>> f2 = asia['Bronchitis'] * 1 >>> print f1 >>> print f2
Now compute the product of f1 and f2
by hand. (OK, you can use a calculator to do the
multiplications.) Check your answer with:
>>> from gPy.Demos import * >>> prod_gui(f1,f2)
Now for marginalisation. Do:
>>> f = f1*f2 >>> marginalise_gui(f)
Compute by hand the factor which results from marginalising away
the variable Cancer. Check your answer by selecting
the Cancer variable by clicking it with the left
mouse button and then 'pressing' the Sum out
selected button.
Grab a graph to play with and display it:
>>> from gPy.Examples import asia >>> gr = asia.adg().moralise() >>> from Tkinter import Tk >>> gr.gui_display(Tk())
Let P be some probability distribution which factorises according to this graph. Work out which of the following (conditional) independence statements must be true about P.
Check your answers using reachable or separates
This question is harder than the others.
A loop (or cycle) is a path in an undirected graph from a vertex back to itself. Write a Python module defining a class MyUGraph which extends gPy's UGraph class for undirected graphs with one extra method which checks to see if a given vertex is part of a cycle. Naturally you will need to check the gPy API documentation to see which methods are available from UGraph, the neighbours method will definitely be useful. Your module will look like this:
from gPy.Graphs import UGraph
class MyUGraph(UGraph):
def in_loop(self,vertex):
.....
Last modified: Thu Nov 3 17:01:08 GMT 2011