Sample from the alarm network

The Alarm Network is the Hello World of Belief Networks. The code below will create and parameterise the network and than sample from it and collect the samples in an histogram for testing.


public void main(String[] args) throws Exception {
	Value t = new Value("true");
	Value f = new Value("false");
	Variable burglary = new Variable("Burglary", t, f);
	Variable earthquake = new Variable("Earthquake", t, f);
	Variable alarm = new Variable("Alarm", t, f);
	Variable johnCalls = new Variable("John Calls", t, f);
	Variable maryCalls = new Variable("Mary Calls", t, f);
	
	DirectedGraph graph = new DirectedGraph();
	
	Function fBurglar = new FunctionV2(Arrays.asList(burglary));
	fBurglar.setValues(new double[] { .001, 1 - .001 });
	graph.add(burglary);
	graph.setDistribution(burglary, fBurglar);
	
	Function fEarthquake = new FunctionV2(Arrays.asList(earthquake));
	fEarthquake.setValues(new double[] { .002, 1 - .002 });
	graph.add(earthquake);
	graph.setDistribution(earthquake, fEarthquake);
	
	Function fAlarm = new FunctionV2(Arrays.asList(alarm, burglary, earthquake));
	fAlarm.setValues(new double[] { .95, .94, .29, .001, 1 - .95, 1 - .94, 1 - .29, 1 - .001 });
	graph.add(alarm);
	graph.setDistribution(alarm, fAlarm);
	
	Function fJohnCalls = new FunctionV2(Arrays.asList(johnCalls, alarm));
	fJohnCalls.setValues(new double[] { .9, .05, 1 - .9, 1 - .05 });
	graph.add(johnCalls);
	graph.setDistribution(johnCalls, fJohnCalls);
	
	Function fMaryCalls = new FunctionV2(Arrays.asList(maryCalls, alarm));
	fMaryCalls.setValues(new double[] { .7, .1, 1 - .7, 1 - .01 });
	graph.add(maryCalls);
	graph.setDistribution(maryCalls, fMaryCalls);
	
	Histogram histogram = new Histogram(Arrays.asList(burglary, earthquake, alarm, johnCalls, maryCalls));
	//Value[][] dataset = new Sampler(new DirichletRandomGenerator()).sample(graph, 10000);
	Value[][] dataset = graph.getSampler().sample(10000);
	for(int i = 0; i < dataset.length; i++) histogram.add(dataset[i]);	
}

An urology case on the chance of success using shockwave lithotripsy to treat kidney stones

Depending on various factors, including (P)ain, si(Z)e, si(T)e (I)mage expressiveness and previous (R)enal surgery judge the chance of (C)learance causally on stone (D)isintegration following shockwave lithotripsy as an expert model (Madigan, Mosurski and Almond).


	Value vMild = new Value("mild_0");
	Value vSevere = new Value("severe_1");
	Variable nP = new Variable("P", vMild, vSevere);
	Value vSmall = new Value("small_0");
	Value vLarge = new Value("large_1");
	Variable nZ = new Variable("Z", vSmall, vLarge);
	Value vUpperKidney = new Value("upper kidney_0");
	Value vElsewhere = new Value("elsewhere_1");
	Variable nT = new Variable("T", vUpperKidney, vElsewhere);
	Value vYes = new Value("yes_1");
	Value vNo = new Value("no_0");
	Variable nS = new Variable("S", vYes, vNo);
	Variable nR = new Variable("R", vYes, vNo);
	Value vGood = new Value("good_1");
	Value vVague = new Value("vague_0");
	Variable nI = new Variable("I", vGood, vVague);
	Variable nD = new Variable("D", vYes, vNo);
	Variable nC = new Variable("C", vYes, vNo);
	
	DirectedGraph graph = new DirectedGraph();
	
	Function fT = new FunctionV2(Arrays.asList(nT));
	fT.setValues(new double[] { .5, .5 });
	graph.add(nT);
	graph.setDistribution(nT, fT);
	
	Function fR = new FunctionV2(Arrays.asList(nR));
	fR.setValues(new double[] { .1, .9 });
	graph.add(nR);
	graph.setDistribution(nR, fR);
	
	Function fS = new FunctionV2(Arrays.asList(nS, nR));
	fS.setValues(new double[] { .95, .3, .05, .7} );
	graph.add(nS);
	graph.setDistribution(nS, fS);
	
	Function fI = new FunctionV2(Arrays.asList(nI, nZ));
	fI.setValues(new double[] { .5, .8, .5, .2 });
	graph.add(nI);
	graph.setDistribution(nI, fI);
	
	Function fD = new FunctionV2(Arrays.asList(nD, nT, nI, nS));
	fD.setValues(new double[] { .75, .9, .55, .7, .6, .8, .5, .65, .1, .25, .3, .45, .2, .4, .35, .5 } );
	graph.add(nD);
	graph.setDistribution(nD, fD);
	
	Function fP = new FunctionV2(Arrays.asList(nP, nT));
	fP.setValues(new double[] { .4, .2, .6, .8 });
	graph.add(nP);
	graph.setDistribution(nP, fP);
	
	Function fC = new FunctionV2(Arrays.asList(nC, nD));
	fC.setValues(new double[] { .95, .1, .05, .9 } );
	graph.add(nC);
	graph.setDistribution(nC, fC);
	
	Function fZ = new FunctionV2(Arrays.asList(nZ));
	fZ.setValues(new double[] { .5, .5 });
	graph.add(nZ);
	graph.setDistribution(nZ, fZ);
	
	Inference inference = new Inference(graph);
	Function beliefC = inference.getBelief(Arrays.asList(nC), new ArrayList<Variable>());
	assertArrayEquals(new double[] { 0.6922727925255111, 0.30772720747448895 }, beliefC.getValues(), EPSILON);
	nS.setValue(vYes);
	beliefC = inference.getBelief(Arrays.asList(nC), Arrays.asList(nS));
	assertArrayEquals(new double[] { 0.7346326836581708, 0.2653673163418291 }, beliefC.getValues(), EPSILON);
}

The interpretation of the result is that the observation of scarring impacts on the marginal probability of stone clearance after lithotripsy.