GM : a graphical models library in SML

Requirements

GM requires a recent version of SML/NJ. It was developed and tested on version 110.44. The SML/NJ installed on the CS machines is outdated. A recent version, which is known to work with GM, is available at: /cs/SCRATCH/clint/sml/bin/sml

Source

Download the source tarball here: gm-sml.tar.gz

Installation

  • Expand the tarball: [bash]$ tar xzvf gm-sml.tar.gz
  • Navigate to the source directory: [bash]$ cd gm-sml/src
  • Start SML: [bash]$ sml
  • Build the library (this exports the GM structure) [sml] - CM.make "gm.cm";
  • Run the tests [sml] - CM.make "tests.cm";
  • Build the examples [sml] - CM.make "examples.cm";

    Usage

    This section illustrates the sprinkler example. The source code is packaged with the library at gm-sml/sr/examples/Sprinkler.sml . First, the random variables are created, and given names:
    val [FALSE, TRUE] = List.map RVar.intToValue [0,1] 
    val [C,S,R,W] = List.map RVar.newVar [2,2,2,2]
    val _ = RVar.giveName C "Cloudy"
    val _ = RVar.giveName S "Sprinkler"
    val _ = RVar.giveName R "Raining"
    val _ = RVar.giveName W "Wet Grass"
    
    Variable naming (RVar.giveName) uses imperative assignment to update a table of identifier/name pairs. This list is used by the RVar structure when printing names. Next, the local factors are created as conditional probability distributions:
    val cpds = List.map CPD.make [ ([0.5, 0.5], C, []),
    			       ([0.8, 0.2, 0.2, 0.8], R, [C]),
    			       ([0.5, 0.5, 0.9, 0.1], S, [C]),
    			       ([1.0, 0.0, 0.1, 0.9, 
    				 0.1, 0.9, 0.01, 0.99], 
    				W, [S,R]) ]
    val local_factors = List.map LocalFactor.CPD cpds
    
    Finally, we make the graphical model, enter some evidence, and perform inference:
    val gmodel = GModel.make local_factors
    val _ = Util.showDot (GModel.toDotString gmodel)
    val engine = Eng.makeEngine (gmodel, [],[] )
    val engine = Eng.enterEvidence engine (Eng.Hard (W, TRUE))
    val P_S = Eng.query engine [S]
    val P_SR = Eng.query engine [R, S]
    
    This creates the graphical model, and an engine for performing inference in the model. The inference engine allows evidence to be entered, and queries to be made. GM displays graphs using the dot language, a member of the graphviz toolbox. Structures define a toDotString method (where appropriate) which can be displayed with the Util.showDot method.