Added Daniel's Bayes Net code. Converted example code to unit tests. Minor code clean-up.
This commit is contained in:
176
test/dkohl/bayes/example/builders/AlarmNetBuilderTable.java
Normal file
176
test/dkohl/bayes/example/builders/AlarmNetBuilderTable.java
Normal file
@@ -0,0 +1,176 @@
|
||||
package dkohl.bayes.example.builders;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
import dkohl.bayes.bayesnet.BayesNet;
|
||||
import dkohl.bayes.probability.Assignment;
|
||||
import dkohl.bayes.probability.Probability;
|
||||
import dkohl.bayes.probability.Variable;
|
||||
import dkohl.bayes.probability.distribution.ProbabilityTable;
|
||||
|
||||
/**
|
||||
* The alarm example for baysian nets.
|
||||
*
|
||||
* Stuart Russel, Peter Norvig: Artificial Intelligence: A Modern Approach, 3ed
|
||||
* Edition, Prentice Hall, 2010
|
||||
*
|
||||
* @author Daniel Kohlsdorf
|
||||
*/
|
||||
public class AlarmNetBuilderTable {
|
||||
|
||||
// Variable names
|
||||
public static final String BURGLARY = "Burglary";
|
||||
public static final String EARTHQUAKE = "Earthquake";
|
||||
public static final String ALARM = "Alarm";
|
||||
public static final String JOHN = "John";
|
||||
public static final String MARRY = "Marry";
|
||||
|
||||
// Possible outcomes
|
||||
public static final String TRUE = "true";
|
||||
public static final String FALSE = "false";
|
||||
|
||||
// A variables domain
|
||||
public static final String DOMAIN[] = { TRUE, FALSE };
|
||||
|
||||
// A set of variables
|
||||
public static final String VARIABLES[] = { BURGLARY, EARTHQUAKE, ALARM,
|
||||
JOHN, MARRY };
|
||||
|
||||
/**
|
||||
* Builds the query from the book: P(B| j, m)
|
||||
*/
|
||||
public static LinkedList<Assignment> completeQueryBulgary() {
|
||||
LinkedList<Assignment> assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(JOHN, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(MARRY, DOMAIN), TRUE));
|
||||
return new LinkedList<Assignment>(assignment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build burglary prior
|
||||
*
|
||||
* @param alarmNet
|
||||
*/
|
||||
private static void burglary(BayesNet alarmNet) {
|
||||
Probability p_bulglary = new Probability(0.001);
|
||||
String names[] = { BURGLARY };
|
||||
ProbabilityTable burglary = new ProbabilityTable(names);
|
||||
burglary.setProbabilityForAssignment("true;", p_bulglary);
|
||||
burglary.setProbabilityForAssignment("false;", p_bulglary.rest());
|
||||
alarmNet.setDistribution(new Variable(BURGLARY, DOMAIN), burglary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build earthquake prior
|
||||
*
|
||||
* @param alarmNet
|
||||
*/
|
||||
private static void earthquake(BayesNet alarmNet) {
|
||||
Probability p_earthquake = new Probability(0.002);
|
||||
String names[] = { EARTHQUAKE };
|
||||
ProbabilityTable earthquake = new ProbabilityTable(names);
|
||||
earthquake.setProbabilityForAssignment("true;", p_earthquake);
|
||||
earthquake.setProbabilityForAssignment("false;", p_earthquake.rest());
|
||||
|
||||
alarmNet.setDistribution(new Variable(EARTHQUAKE, DOMAIN), earthquake);
|
||||
}
|
||||
|
||||
/**
|
||||
* Jhon calls!
|
||||
*
|
||||
* @param alarmNet
|
||||
*/
|
||||
private static void jhon(BayesNet alarmNet) {
|
||||
// P(ALARM) == true
|
||||
Probability t = new Probability(.90);
|
||||
|
||||
// P(ALARM) == false
|
||||
Probability f = new Probability(.05);
|
||||
String names[] = { ALARM, JOHN };
|
||||
ProbabilityTable jhon = new ProbabilityTable(names);
|
||||
jhon.setProbabilityForAssignment("true;true;", t);
|
||||
jhon.setProbabilityForAssignment("false;true;", f);
|
||||
jhon.setProbabilityForAssignment("true;false", t.rest());
|
||||
jhon.setProbabilityForAssignment("false;false;", f.rest());
|
||||
|
||||
alarmNet.setDistribution(new Variable(JOHN, DOMAIN), jhon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marry calls!
|
||||
*
|
||||
* @param alarmNet
|
||||
*/
|
||||
private static void mary(BayesNet alarmNet) {
|
||||
// P(ALARM) == true
|
||||
Probability t = new Probability(.70);
|
||||
|
||||
// P(ALARM) == false
|
||||
Probability f = new Probability(.01);
|
||||
String names[] = { ALARM, MARRY };
|
||||
ProbabilityTable marry = new ProbabilityTable(names);
|
||||
marry.setProbabilityForAssignment("true;true;", t);
|
||||
marry.setProbabilityForAssignment("false;true;", f);
|
||||
marry.setProbabilityForAssignment("true;false", t.rest());
|
||||
marry.setProbabilityForAssignment("false;false;", f.rest());
|
||||
alarmNet.setDistribution(new Variable(MARRY, DOMAIN), marry);
|
||||
}
|
||||
|
||||
/**
|
||||
* The alarm goes off!
|
||||
*
|
||||
* @param alarmNet
|
||||
*/
|
||||
private static void alarm(BayesNet alarmNet) {
|
||||
// P(ALARM | BURGLARY = true, EARTHQUAKE = true)
|
||||
Probability tt = new Probability(.95);
|
||||
|
||||
// P(ALARM | BURGLARY = true, EARTHQUAKE = false)
|
||||
Probability tf = new Probability(.94);
|
||||
|
||||
// P(ALARM | BURGLARY = false, EARTHQUAKE = true)
|
||||
Probability ft = new Probability(.29);
|
||||
|
||||
// P(ALARM | BURGLARY = false, EARTHQUAKE = false)
|
||||
Probability ff = new Probability(.001);
|
||||
String names[] = { BURGLARY, EARTHQUAKE, ALARM };
|
||||
ProbabilityTable alarm = new ProbabilityTable(names);
|
||||
alarm.setProbabilityForAssignment(TRUE + ";" + TRUE + ";" + TRUE + ";",
|
||||
tt);
|
||||
alarm.setProbabilityForAssignment(
|
||||
TRUE + ";" + FALSE + ";" + TRUE + ";", tf);
|
||||
alarm.setProbabilityForAssignment(
|
||||
FALSE + ";" + TRUE + ";" + TRUE + ";", ft);
|
||||
alarm.setProbabilityForAssignment(FALSE + ";" + FALSE + ";" + TRUE
|
||||
+ ";", ff);
|
||||
|
||||
alarm.setProbabilityForAssignment(
|
||||
TRUE + ";" + TRUE + ";" + FALSE + ";", tt.rest());
|
||||
alarm.setProbabilityForAssignment(TRUE + ";" + FALSE + ";" + FALSE
|
||||
+ ";", tf.rest());
|
||||
alarm.setProbabilityForAssignment(FALSE + ";" + TRUE + ";" + FALSE
|
||||
+ ";", ft.rest());
|
||||
alarm.setProbabilityForAssignment(FALSE + ";" + FALSE + ";" + FALSE
|
||||
+ ";", ff.rest());
|
||||
alarmNet.setDistribution(new Variable(ALARM, DOMAIN), alarm);
|
||||
}
|
||||
|
||||
public static BayesNet alarm() {
|
||||
BayesNet alarmNet = new BayesNet(VARIABLES);
|
||||
|
||||
// set probability tables and priors
|
||||
burglary(alarmNet);
|
||||
earthquake(alarmNet);
|
||||
alarm(alarmNet);
|
||||
jhon(alarmNet);
|
||||
mary(alarmNet);
|
||||
|
||||
// construct the graph
|
||||
alarmNet.connect(ALARM, BURGLARY);
|
||||
alarmNet.connect(ALARM, EARTHQUAKE);
|
||||
alarmNet.connect(JOHN, ALARM);
|
||||
alarmNet.connect(MARRY, ALARM);
|
||||
|
||||
return alarmNet;
|
||||
}
|
||||
}
|
||||
244
test/dkohl/bayes/example/builders/AlarmNetBuilderTree.java
Normal file
244
test/dkohl/bayes/example/builders/AlarmNetBuilderTree.java
Normal file
@@ -0,0 +1,244 @@
|
||||
package dkohl.bayes.example.builders;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
import dkohl.bayes.bayesnet.BayesNet;
|
||||
import dkohl.bayes.probability.Assignment;
|
||||
import dkohl.bayes.probability.Probability;
|
||||
import dkohl.bayes.probability.Variable;
|
||||
import dkohl.bayes.probability.distribution.ProbabilityTree;
|
||||
|
||||
public class AlarmNetBuilderTree {
|
||||
|
||||
// Variable names
|
||||
public static final String BURGLARY = "Burglary";
|
||||
public static final String EARTHQUAKE = "Earthquake";
|
||||
public static final String ALARM = "Alarm";
|
||||
public static final String JOHN = "John";
|
||||
public static final String MARRY = "Marry";
|
||||
|
||||
// Possible outcomes
|
||||
public static final String TRUE = "true";
|
||||
public static final String FALSE = "false";
|
||||
|
||||
// A variables domain
|
||||
public static final String DOMAIN[] = { TRUE, FALSE };
|
||||
|
||||
// A set of variables
|
||||
public static final String VARIABLES[] = { BURGLARY, EARTHQUAKE, ALARM,
|
||||
JOHN, MARRY };
|
||||
|
||||
/**
|
||||
* Builds the query from the book: P(B| j, m)
|
||||
*/
|
||||
public static LinkedList<Assignment> completeQueryBulgary() {
|
||||
LinkedList<Assignment> assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(JOHN, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(MARRY, DOMAIN), TRUE));
|
||||
return new LinkedList<Assignment>(assignment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build burglary prior
|
||||
*
|
||||
* @param sprinklerNet
|
||||
*/
|
||||
private static void burglary(BayesNet sprinklerNet) {
|
||||
Probability p_bulglary = new Probability(0.001);
|
||||
|
||||
ProbabilityTree burglary = new ProbabilityTree();
|
||||
|
||||
LinkedList<Assignment> assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), TRUE));
|
||||
burglary.setProbabilityForAssignment(assignment, p_bulglary);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), FALSE));
|
||||
burglary.setProbabilityForAssignment(assignment, p_bulglary.rest());
|
||||
|
||||
sprinklerNet.setDistribution(new Variable(BURGLARY, DOMAIN), burglary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build earthquake prior
|
||||
*
|
||||
* @param sprinklerNet
|
||||
*/
|
||||
private static void earthquake(BayesNet sprinklerNet) {
|
||||
Probability p_earthquake = new Probability(0.002);
|
||||
|
||||
ProbabilityTree earthquake = new ProbabilityTree();
|
||||
|
||||
LinkedList<Assignment> assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), TRUE));
|
||||
earthquake.setProbabilityForAssignment(assignment, p_earthquake);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), FALSE));
|
||||
earthquake.setProbabilityForAssignment(assignment, p_earthquake.rest());
|
||||
|
||||
sprinklerNet.setDistribution(new Variable(EARTHQUAKE, DOMAIN),
|
||||
earthquake);
|
||||
}
|
||||
|
||||
/**
|
||||
* Jhon calls!
|
||||
*
|
||||
* @param sprinklerNet
|
||||
*/
|
||||
private static void jhon(BayesNet sprinklerNet) {
|
||||
// P(ALARM) == true
|
||||
Probability t = new Probability(.90);
|
||||
|
||||
// P(ALARM) == false
|
||||
Probability f = new Probability(.05);
|
||||
|
||||
ProbabilityTree jhon = new ProbabilityTree();
|
||||
|
||||
LinkedList<Assignment> assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(JOHN, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), TRUE));
|
||||
jhon.setProbabilityForAssignment(assignment, t);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(JOHN, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), FALSE));
|
||||
jhon.setProbabilityForAssignment(assignment, f);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(JOHN, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), TRUE));
|
||||
jhon.setProbabilityForAssignment(assignment, t.rest());
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(JOHN, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), FALSE));
|
||||
jhon.setProbabilityForAssignment(assignment, f.rest());
|
||||
|
||||
sprinklerNet.setDistribution(new Variable(JOHN, DOMAIN), jhon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marry calls!
|
||||
*
|
||||
* @param sprinklerNet
|
||||
*/
|
||||
private static void mary(BayesNet sprinklerNet) {
|
||||
// P(ALARM) == true
|
||||
Probability t = new Probability(.70);
|
||||
|
||||
// P(ALARM) == false
|
||||
Probability f = new Probability(.01);
|
||||
ProbabilityTree mary = new ProbabilityTree();
|
||||
|
||||
LinkedList<Assignment> assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(MARRY, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), TRUE));
|
||||
mary.setProbabilityForAssignment(assignment, t);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(MARRY, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), FALSE));
|
||||
mary.setProbabilityForAssignment(assignment, f);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(MARRY, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), TRUE));
|
||||
mary.setProbabilityForAssignment(assignment, t.rest());
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(MARRY, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), FALSE));
|
||||
mary.setProbabilityForAssignment(assignment, f.rest());
|
||||
sprinklerNet.setDistribution(new Variable(MARRY, DOMAIN), mary);
|
||||
}
|
||||
|
||||
/**
|
||||
* The alarm goes off!
|
||||
*
|
||||
* @param sprinklerNet
|
||||
*/
|
||||
private static void alarm(BayesNet sprinklerNet) {
|
||||
// P(ALARM | BURGLARY = true, EARTHQUAKE = true)
|
||||
Probability tt = new Probability(.95);
|
||||
|
||||
// P(ALARM | BURGLARY = true, EARTHQUAKE = false)
|
||||
Probability tf = new Probability(.94);
|
||||
|
||||
// P(ALARM | BURGLARY = false, EARTHQUAKE = true)
|
||||
Probability ft = new Probability(.29);
|
||||
|
||||
// P(ALARM | BURGLARY = false, EARTHQUAKE = false)
|
||||
Probability ff = new Probability(.001);
|
||||
ProbabilityTree alarm = new ProbabilityTree();
|
||||
|
||||
LinkedList<Assignment> assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), TRUE));
|
||||
alarm.setProbabilityForAssignment(assignment, tt);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), TRUE));
|
||||
alarm.setProbabilityForAssignment(assignment, tf);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), TRUE));
|
||||
alarm.setProbabilityForAssignment(assignment, ft);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), TRUE));
|
||||
alarm.setProbabilityForAssignment(assignment, ff);
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), FALSE));
|
||||
alarm.setProbabilityForAssignment(assignment, tt.rest());
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), FALSE));
|
||||
alarm.setProbabilityForAssignment(assignment, tf.rest());
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), TRUE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), FALSE));
|
||||
alarm.setProbabilityForAssignment(assignment, ft.rest());
|
||||
|
||||
assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(BURGLARY, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(EARTHQUAKE, DOMAIN), FALSE));
|
||||
assignment.add(new Assignment(new Variable(ALARM, DOMAIN), FALSE));
|
||||
alarm.setProbabilityForAssignment(assignment, ff.rest());
|
||||
|
||||
sprinklerNet.setDistribution(new Variable(ALARM, DOMAIN), alarm);
|
||||
}
|
||||
|
||||
public static BayesNet sprinkler() {
|
||||
BayesNet sprinklerNet = new BayesNet(VARIABLES);
|
||||
|
||||
// set probability tables and priors
|
||||
burglary(sprinklerNet);
|
||||
earthquake(sprinklerNet);
|
||||
alarm(sprinklerNet);
|
||||
jhon(sprinklerNet);
|
||||
mary(sprinklerNet);
|
||||
|
||||
// construct the graph
|
||||
sprinklerNet.connect(ALARM, BURGLARY);
|
||||
sprinklerNet.connect(ALARM, EARTHQUAKE);
|
||||
sprinklerNet.connect(JOHN, ALARM);
|
||||
sprinklerNet.connect(MARRY, ALARM);
|
||||
|
||||
return sprinklerNet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package dkohl.bayes.example.builders;
|
||||
|
||||
import dkohl.bayes.bayesnet.BayesNet;
|
||||
import dkohl.bayes.probability.Assignment;
|
||||
import dkohl.bayes.probability.Probability;
|
||||
import dkohl.bayes.probability.Variable;
|
||||
import dkohl.bayes.probability.distribution.ProbabilityTable;
|
||||
import dkohl.bayes.statistic.DataPoint;
|
||||
import dkohl.bayes.statistic.DataSet;
|
||||
|
||||
/**
|
||||
* The Sprinkler net example
|
||||
*
|
||||
* http://www.cs.ubc.ca/~murphyk/Bayes/bnintro.html
|
||||
*
|
||||
* @author Daniel Kohlsdorf
|
||||
*/
|
||||
public class EstimateSprinklerNetBuilderTable {
|
||||
|
||||
// Variable names
|
||||
public static final String CLOUDY = "Cloudy";
|
||||
public static final String SPRINKLER = "Sprinkler";
|
||||
public static final String GRASS_WET = "GrassWet";
|
||||
public static final String RAIN = "Rain";
|
||||
|
||||
// Possible outcomes
|
||||
public static final String TRUE = "true";
|
||||
public static final String FALSE = "false";
|
||||
|
||||
// A variables domain
|
||||
public static final String DOMAIN[] = { TRUE, FALSE };
|
||||
|
||||
// A set of variables
|
||||
public static final String VARIABLES[] = { CLOUDY, SPRINKLER, GRASS_WET,
|
||||
RAIN };
|
||||
|
||||
private static void cloudy(BayesNet sprinklerNet) {
|
||||
Probability p_cloudy = new Probability(0.5);
|
||||
String names[] = { CLOUDY };
|
||||
ProbabilityTable cloudy = new ProbabilityTable(names);
|
||||
cloudy.setProbabilityForAssignment("true;", p_cloudy);
|
||||
cloudy.setProbabilityForAssignment("false;", p_cloudy.rest());
|
||||
sprinklerNet.setDistribution(new Variable(CLOUDY, DOMAIN), cloudy);
|
||||
}
|
||||
|
||||
private static void rain(BayesNet sprinklerNet) {
|
||||
Probability p_cloudy = new Probability(0.8);
|
||||
Probability p_notcloudy = new Probability(0.2);
|
||||
|
||||
String names[] = { CLOUDY, RAIN };
|
||||
ProbabilityTable rain = new ProbabilityTable(names);
|
||||
rain.setProbabilityForAssignment("true;true;", p_cloudy);
|
||||
rain.setProbabilityForAssignment("false;false;", p_notcloudy);
|
||||
|
||||
rain.setProbabilityForAssignment("false;true;", p_notcloudy.rest());
|
||||
rain.setProbabilityForAssignment("true;false;", p_cloudy.rest());
|
||||
sprinklerNet.setDistribution(new Variable(RAIN, DOMAIN), rain);
|
||||
}
|
||||
|
||||
private static void sprinkler(BayesNet sprinklerNet) {
|
||||
Probability p_cloudy = new Probability(0.1);
|
||||
Probability p_notcloudy = new Probability(0.5);
|
||||
|
||||
String names[] = { CLOUDY, SPRINKLER };
|
||||
ProbabilityTable sprinkler = new ProbabilityTable(names);
|
||||
sprinkler.setProbabilityForAssignment("true;true;", p_cloudy);
|
||||
sprinkler.setProbabilityForAssignment("false;false;", p_notcloudy);
|
||||
|
||||
sprinkler
|
||||
.setProbabilityForAssignment("false;true;", p_notcloudy.rest());
|
||||
sprinkler.setProbabilityForAssignment("true;false;", p_cloudy.rest());
|
||||
sprinklerNet
|
||||
.setDistribution(new Variable(SPRINKLER, DOMAIN), sprinkler);
|
||||
}
|
||||
|
||||
private static void grass(BayesNet sprinklerNet) {
|
||||
String names[] = { RAIN, SPRINKLER, GRASS_WET };
|
||||
ProbabilityTable sprinkler = new ProbabilityTable(names);
|
||||
sprinklerNet
|
||||
.setDistribution(new Variable(GRASS_WET, DOMAIN), sprinkler);
|
||||
}
|
||||
|
||||
public static BayesNet sprinkler() {
|
||||
BayesNet sprinkler = new BayesNet(VARIABLES);
|
||||
|
||||
cloudy(sprinkler);
|
||||
rain(sprinkler);
|
||||
sprinkler(sprinkler);
|
||||
grass(sprinkler);
|
||||
|
||||
sprinkler.connect(RAIN, CLOUDY);
|
||||
sprinkler.connect(SPRINKLER, CLOUDY);
|
||||
sprinkler.connect(GRASS_WET, RAIN);
|
||||
sprinkler.connect(GRASS_WET, SPRINKLER);
|
||||
|
||||
return sprinkler;
|
||||
}
|
||||
|
||||
private static Assignment build(String varible, String value) {
|
||||
return new Assignment(new Variable(varible, DOMAIN), value);
|
||||
}
|
||||
|
||||
public static DataSet dataSet() {
|
||||
DataSet dataSet = new DataSet();
|
||||
|
||||
/**
|
||||
* If rain is false and sprinkler is false, grass is never wet.
|
||||
*/
|
||||
DataPoint one = new DataPoint();
|
||||
one.add(build(RAIN, FALSE));
|
||||
one.add(build(SPRINKLER, FALSE));
|
||||
one.add(build(GRASS_WET, FALSE));
|
||||
dataSet.add(one);
|
||||
|
||||
/**
|
||||
* 1 / 10 times the grass is not wet when the sprinkler is on
|
||||
*/
|
||||
DataPoint two = new DataPoint();
|
||||
two.add(build(RAIN, FALSE));
|
||||
two.add(build(SPRINKLER, TRUE));
|
||||
two.add(build(GRASS_WET, FALSE));
|
||||
dataSet.add(two);
|
||||
|
||||
/**
|
||||
* 9 / 10 times the sprinkler is on and the grass is wet
|
||||
*/
|
||||
for (int i = 0; i < 9; i++) {
|
||||
DataPoint point = new DataPoint();
|
||||
point.add(build(RAIN, FALSE));
|
||||
point.add(build(SPRINKLER, TRUE));
|
||||
point.add(build(GRASS_WET, TRUE));
|
||||
dataSet.add(point);
|
||||
}
|
||||
|
||||
/**
|
||||
* 1 / 10 times the grass is not wet when it rains
|
||||
*/
|
||||
DataPoint three = new DataPoint();
|
||||
three.add(build(RAIN, TRUE));
|
||||
three.add(build(SPRINKLER, FALSE));
|
||||
three.add(build(GRASS_WET, FALSE));
|
||||
dataSet.add(three);
|
||||
|
||||
/**
|
||||
* 9 / 10 times it rains and the grass is wet
|
||||
*/
|
||||
for (int i = 0; i < 9; i++) {
|
||||
DataPoint point = new DataPoint();
|
||||
point.add(build(RAIN, TRUE));
|
||||
point.add(build(SPRINKLER, FALSE));
|
||||
point.add(build(GRASS_WET, TRUE));
|
||||
dataSet.add(point);
|
||||
}
|
||||
|
||||
/**
|
||||
* 1 / 100 times the grass is not wet when it rains and the sprinkler is
|
||||
* on
|
||||
*/
|
||||
DataPoint four = new DataPoint();
|
||||
four.add(build(RAIN, TRUE));
|
||||
four.add(build(SPRINKLER, TRUE));
|
||||
four.add(build(GRASS_WET, FALSE));
|
||||
dataSet.add(four);
|
||||
|
||||
/**
|
||||
* 99 / 100 times it rains and the grass is wet
|
||||
*/
|
||||
for (int i = 0; i < 99; i++) {
|
||||
DataPoint point = new DataPoint();
|
||||
point.add(build(RAIN, TRUE));
|
||||
point.add(build(SPRINKLER, TRUE));
|
||||
point.add(build(GRASS_WET, TRUE));
|
||||
dataSet.add(point);
|
||||
}
|
||||
|
||||
return dataSet;
|
||||
}
|
||||
|
||||
}
|
||||
266
test/dkohl/bayes/example/builders/FoodExampleBuilder.java
Normal file
266
test/dkohl/bayes/example/builders/FoodExampleBuilder.java
Normal file
@@ -0,0 +1,266 @@
|
||||
package dkohl.bayes.example.builders;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import dkohl.bayes.bayesnet.BayesNet;
|
||||
import dkohl.bayes.estimation.MaximumLikelihoodEstimation;
|
||||
import dkohl.bayes.probability.Assignment;
|
||||
import dkohl.bayes.probability.Probability;
|
||||
import dkohl.bayes.probability.Variable;
|
||||
import dkohl.bayes.probability.distribution.ContinousDistribution;
|
||||
import dkohl.bayes.probability.distribution.ProbabilityDistribution;
|
||||
import dkohl.bayes.probability.distribution.ProbabilityTable;
|
||||
import dkohl.bayes.statistic.DataPoint;
|
||||
import dkohl.bayes.statistic.DataSet;
|
||||
import dkohl.onthology.Ontology;
|
||||
|
||||
public class FoodExampleBuilder {
|
||||
|
||||
public static final String TASTE = "Taste";
|
||||
public static final String SOMEONE_VEGETARIAN = "Vegetarian";
|
||||
public static final String CONTAINS_MEET = "Meet";
|
||||
public static final String CONTAINS_VEGETABLE = "Vegetable";
|
||||
public static final String CONTAINS_BEEF = "Beef";
|
||||
public static final String CONTAINS_PORK = "Pork";
|
||||
public static final String CONTAINS_TOMATOS = "Tomatos";
|
||||
public static final String CONTAINS_POTATOS = "Potatos";
|
||||
|
||||
public static final String TRUE_VALUE = "true";
|
||||
public static final String FALSE_VALUE = "false";
|
||||
|
||||
public static final String DOMAIN[] = { TRUE_VALUE, FALSE_VALUE };
|
||||
|
||||
public static final String RATING_DOMAIN[] = { "1", "2", "3", "4", "5",
|
||||
"6", "7", "8", "9", "10" };
|
||||
|
||||
private static final String[] VARIABLES = { SOMEONE_VEGETARIAN,
|
||||
CONTAINS_BEEF, CONTAINS_MEET, CONTAINS_PORK, CONTAINS_POTATOS,
|
||||
CONTAINS_TOMATOS, CONTAINS_VEGETABLE, TASTE };
|
||||
|
||||
private static final String[] OBSERVED = { CONTAINS_BEEF, CONTAINS_PORK,
|
||||
CONTAINS_POTATOS, CONTAINS_TOMATOS, };
|
||||
|
||||
public static LinkedList<Assignment> completeQueryTasteBeef() {
|
||||
LinkedList<Assignment> assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(CONTAINS_BEEF, DOMAIN),
|
||||
TRUE_VALUE));
|
||||
assignment.add(new Assignment(new Variable(CONTAINS_TOMATOS, DOMAIN),
|
||||
TRUE_VALUE));
|
||||
return new LinkedList<Assignment>(assignment);
|
||||
}
|
||||
|
||||
public static LinkedList<Assignment> completeQueryTastePork() {
|
||||
LinkedList<Assignment> assignment = new LinkedList<Assignment>();
|
||||
assignment.add(new Assignment(new Variable(CONTAINS_PORK, DOMAIN),
|
||||
TRUE_VALUE));
|
||||
assignment.add(new Assignment(new Variable(CONTAINS_POTATOS, DOMAIN),
|
||||
TRUE_VALUE));
|
||||
return new LinkedList<Assignment>(assignment);
|
||||
}
|
||||
|
||||
public static Ontology onto() {
|
||||
HashSet<String> classes = new HashSet<String>();
|
||||
classes.add(CONTAINS_MEET);
|
||||
classes.add(CONTAINS_VEGETABLE);
|
||||
Ontology onthology = new Ontology(classes);
|
||||
onthology.define(CONTAINS_PORK, CONTAINS_MEET);
|
||||
onthology.define(CONTAINS_BEEF, CONTAINS_MEET);
|
||||
|
||||
onthology.define(CONTAINS_TOMATOS, CONTAINS_VEGETABLE);
|
||||
onthology.define(CONTAINS_POTATOS, CONTAINS_VEGETABLE);
|
||||
return onthology;
|
||||
}
|
||||
|
||||
private static Assignment build(String varible, String value) {
|
||||
return new Assignment(new Variable(varible, DOMAIN), value);
|
||||
}
|
||||
|
||||
public static DataPoint beefPotatoDish(int weight) {
|
||||
DataPoint point = new DataPoint();
|
||||
point.add(build(CONTAINS_BEEF, TRUE_VALUE));
|
||||
point.add(build(CONTAINS_POTATOS, TRUE_VALUE));
|
||||
point.add(build(TASTE, "" + weight));
|
||||
return point;
|
||||
}
|
||||
|
||||
public static DataPoint beefTomatoDish(int weight) {
|
||||
DataPoint point = new DataPoint();
|
||||
point.add(build(CONTAINS_BEEF, TRUE_VALUE));
|
||||
point.add(build(CONTAINS_TOMATOS, TRUE_VALUE));
|
||||
point.add(build(TASTE, "" + weight));
|
||||
return point;
|
||||
}
|
||||
|
||||
public static DataPoint porkBeefDish(int weight) {
|
||||
DataPoint point = new DataPoint();
|
||||
point.add(build(CONTAINS_BEEF, TRUE_VALUE));
|
||||
point.add(build(CONTAINS_PORK, TRUE_VALUE));
|
||||
point.add(build(TASTE, "" + weight));
|
||||
return point;
|
||||
}
|
||||
|
||||
public static DataPoint porkPotatoDish(int weight) {
|
||||
DataPoint point = new DataPoint();
|
||||
point.add(build(CONTAINS_PORK, TRUE_VALUE));
|
||||
point.add(build(CONTAINS_POTATOS, TRUE_VALUE));
|
||||
point.add(build(TASTE, "" + weight));
|
||||
return point;
|
||||
}
|
||||
|
||||
public static DataPoint porkTomatoDish(int weight) {
|
||||
DataPoint point = new DataPoint();
|
||||
point.add(build(CONTAINS_PORK, TRUE_VALUE));
|
||||
point.add(build(CONTAINS_TOMATOS, TRUE_VALUE));
|
||||
point.add(build(TASTE, "" + weight));
|
||||
return point;
|
||||
}
|
||||
|
||||
public static DataPoint potatoTomato(int weight) {
|
||||
DataPoint point = new DataPoint();
|
||||
point.add(build(CONTAINS_POTATOS, TRUE_VALUE));
|
||||
point.add(build(CONTAINS_TOMATOS, TRUE_VALUE));
|
||||
point.add(build(TASTE, "" + weight));
|
||||
return point;
|
||||
}
|
||||
|
||||
public static DataPoint normalize(DataPoint point, Ontology onto) {
|
||||
// resolve onthology
|
||||
DataPoint normPoint = new DataPoint(point);
|
||||
for (String key : point.keySet()) {
|
||||
if (onto.getInheritance().containsKey(key)) {
|
||||
normPoint
|
||||
.add(build(onto.getInheritance().get(key), TRUE_VALUE));
|
||||
}
|
||||
}
|
||||
|
||||
// implement closed world assumption
|
||||
// everything unknown is false
|
||||
for (String variable : VARIABLES) {
|
||||
if (!normPoint.containsKey(variable)) {
|
||||
normPoint.add(build(variable, FALSE_VALUE));
|
||||
}
|
||||
}
|
||||
return normPoint;
|
||||
}
|
||||
|
||||
public static DataSet examples() {
|
||||
DataSet data = new DataSet();
|
||||
Ontology onto = onto();
|
||||
// user one ratings
|
||||
data.add(normalize(porkTomatoDish(10), onto));
|
||||
data.add(normalize(porkPotatoDish(9), onto));
|
||||
data.add(normalize(beefTomatoDish(3), onto));
|
||||
data.add(normalize(beefPotatoDish(0), onto));
|
||||
data.add(normalize(potatoTomato(0), onto));
|
||||
data.add(normalize(porkBeefDish(0), onto));
|
||||
|
||||
// user two ratings
|
||||
data.add(normalize(porkTomatoDish(10), onto));
|
||||
data.add(normalize(porkTomatoDish(8), onto));
|
||||
data.add(normalize(porkPotatoDish(10), onto));
|
||||
data.add(normalize(beefTomatoDish(0), onto));
|
||||
data.add(normalize(beefPotatoDish(1), onto));
|
||||
data.add(normalize(potatoTomato(7), onto));
|
||||
data.add(normalize(porkBeefDish(10), onto));
|
||||
|
||||
// user three ratings
|
||||
data.add(normalize(porkTomatoDish(10), onto));
|
||||
data.add(normalize(porkPotatoDish(10), onto));
|
||||
data.add(normalize(beefTomatoDish(3), onto));
|
||||
data.add(normalize(beefPotatoDish(3), onto));
|
||||
data.add(normalize(potatoTomato(3), onto));
|
||||
data.add(normalize(porkBeefDish(4), onto));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public static ProbabilityDistribution vegi() {
|
||||
String names[] = { SOMEONE_VEGETARIAN };
|
||||
ProbabilityTable table = new ProbabilityTable(names);
|
||||
table.setProbabilityForAssignment("true;", new Probability(0));
|
||||
table.setProbabilityForAssignment("false;", new Probability(1));
|
||||
return table;
|
||||
}
|
||||
|
||||
public static ProbabilityDistribution beef() {
|
||||
String names[] = { CONTAINS_MEET, CONTAINS_BEEF };
|
||||
ProbabilityTable table = new ProbabilityTable(names);
|
||||
return table;
|
||||
}
|
||||
|
||||
public static ProbabilityDistribution pork() {
|
||||
String names[] = { CONTAINS_MEET, CONTAINS_PORK };
|
||||
ProbabilityTable table = new ProbabilityTable(names);
|
||||
return table;
|
||||
}
|
||||
|
||||
public static ProbabilityDistribution meet() {
|
||||
String names[] = { SOMEONE_VEGETARIAN, CONTAINS_MEET };
|
||||
ProbabilityTable table = new ProbabilityTable(names);
|
||||
return table;
|
||||
}
|
||||
|
||||
public static ProbabilityDistribution tomatos() {
|
||||
String names[] = { CONTAINS_VEGETABLE, CONTAINS_TOMATOS };
|
||||
ProbabilityTable table = new ProbabilityTable(names);
|
||||
return table;
|
||||
}
|
||||
|
||||
public static ProbabilityDistribution potatos() {
|
||||
String names[] = { CONTAINS_VEGETABLE, CONTAINS_POTATOS };
|
||||
ProbabilityTable table = new ProbabilityTable(names);
|
||||
return table;
|
||||
}
|
||||
|
||||
public static ProbabilityDistribution vegetables() {
|
||||
String names[] = { CONTAINS_VEGETABLE, SOMEONE_VEGETARIAN };
|
||||
ProbabilityTable table = new ProbabilityTable(names);
|
||||
return table;
|
||||
}
|
||||
|
||||
public static ProbabilityDistribution taste() {
|
||||
String names[] = { TASTE, CONTAINS_BEEF, CONTAINS_PORK,
|
||||
CONTAINS_POTATOS, CONTAINS_TOMATOS };
|
||||
ContinousDistribution distribution = new ContinousDistribution(names, 0);
|
||||
return distribution;
|
||||
}
|
||||
|
||||
public static BayesNet dishNet() {
|
||||
BayesNet net = new BayesNet(VARIABLES);
|
||||
net.setDistribution(new Variable(SOMEONE_VEGETARIAN, DOMAIN), vegi());
|
||||
net.setDistribution(new Variable(CONTAINS_MEET, DOMAIN), meet());
|
||||
net.setDistribution(new Variable(CONTAINS_VEGETABLE, DOMAIN),
|
||||
vegetables());
|
||||
net.setDistribution(new Variable(CONTAINS_BEEF, DOMAIN), beef());
|
||||
net.setDistribution(new Variable(CONTAINS_PORK, DOMAIN), pork());
|
||||
net.setDistribution(new Variable(CONTAINS_POTATOS, DOMAIN), potatos());
|
||||
net.setDistribution(new Variable(CONTAINS_TOMATOS, DOMAIN), tomatos());
|
||||
net.setDistribution(new Variable(TASTE, RATING_DOMAIN), taste());
|
||||
|
||||
Ontology onthology = onto();
|
||||
for (String category : onthology.getClasses()) {
|
||||
net.connect(category, SOMEONE_VEGETARIAN);
|
||||
}
|
||||
|
||||
for (String thing : OBSERVED) {
|
||||
net.connect(thing, onthology.getInheritance().get(thing));
|
||||
net.connect(TASTE, thing);
|
||||
}
|
||||
|
||||
for (String category : onthology.getClasses()) {
|
||||
MaximumLikelihoodEstimation.estimate(examples(), net, category);
|
||||
for (String thing : onthology.getClasses2thing().get(category)) {
|
||||
MaximumLikelihoodEstimation.estimate(examples(), net, thing);
|
||||
}
|
||||
}
|
||||
|
||||
MaximumLikelihoodEstimation.estimate(examples(), net, TASTE);
|
||||
ContinousDistribution distribturion = (ContinousDistribution) net
|
||||
.getNodes().get(TASTE);
|
||||
distribturion.estimate();
|
||||
|
||||
return net;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user