001    /*
002     * Created on Dec 28, 2004
003     *
004     */
005    package aima.probability;
006    
007    import java.util.ArrayList;
008    import java.util.Hashtable;
009    import java.util.Iterator;
010    import java.util.List;
011    
012    import aima.logic.propositional.algorithms.Model;
013    
014    /**
015     * @author Ravi Mohan
016     * 
017     */
018    
019    public class ProbabilityDistribution {
020            private List<Row> rows;
021    
022            String[] variableNames;
023    
024            public ProbabilityDistribution(String variableNameOne) {
025                    this(new String[] { variableNameOne });
026            }
027    
028            public ProbabilityDistribution(String variableNameOne,
029                            String variableNameTwo) {
030                    this(new String[] { variableNameOne, variableNameTwo });
031            }
032    
033            public ProbabilityDistribution(String variableNameOne,
034                            String variableNameTwo, String variableNameThree) {
035                    this(
036                                    new String[] { variableNameOne, variableNameTwo,
037                                                    variableNameThree });
038            }
039    
040            public ProbabilityDistribution(String variableNameOne,
041                            String variableNameTwo, String variableNameThree,
042                            String variableNameFour) {
043                    this(new String[] { variableNameOne, variableNameTwo,
044                                    variableNameThree, variableNameFour });
045            }
046    
047            public ProbabilityDistribution(String[] variableNames) {
048                    this.variableNames = variableNames;
049                    rows = new ArrayList<Row>();
050            }
051    
052            public void set(boolean[] values, double probability) {
053                    Model m = new Model();
054                    for (int i = 0; i < variableNames.length; i++) {
055                            m = m.extend(variableNames[i], values[i]);
056                    }
057                    rows.add(new Row(m, probability));
058            }
059    
060            public void set(boolean value1, double probability) {
061                    set(new boolean[] { value1 }, probability);
062            }
063    
064            public void set(boolean value1, boolean value2, double probability) {
065                    set(new boolean[] { value1, value2 }, probability);
066            }
067    
068            public void set(boolean value1, boolean value2, boolean value3,
069                            double probability) {
070                    set(new boolean[] { value1, value2, value3 }, probability);
071            }
072    
073            public void set(boolean value1, boolean value2, boolean value3,
074                            boolean value4, double probability) {
075                    set(new boolean[] { value1, value2, value3, value4 }, probability);
076            }
077    
078            @Override
079            public String toString() {
080                    StringBuffer buf = new StringBuffer();
081                    for (Row row : rows) {
082                            buf.append(row.toString() + "\n");
083                    }
084                    return buf.toString();
085    
086            }
087    
088            public double probabilityOf(Hashtable conditions) {
089                    double prob = 0.0;
090                    for (Row row : rows) {
091                            Iterator iter = conditions.keySet().iterator();
092                            boolean rowMeetsAllConditions = true;
093                            while (iter.hasNext()) {
094                                    String variable = (String) iter.next();
095                                    boolean value = ((Boolean) conditions.get(variable))
096                                                    .booleanValue();
097                                    if (!(row.model.matches(variable, value))) {
098                                            rowMeetsAllConditions = false;
099                                            break;
100                                            // return false;
101                                    }
102                            }
103                            if (rowMeetsAllConditions) {
104                                    prob += row.probability;
105                            }
106                    }
107    
108                    return prob;
109            }
110    
111            class Row {
112                    Model model;
113    
114                    double probability;
115    
116                    Row(Model m, double probability) {
117                            model = m;
118                            this.probability = probability;
119                    }
120    
121                    @Override
122                    public String toString() {
123                            return model.toString() + " => " + probability;
124                    }
125            }
126    
127            public double probabilityOf(String variableName, boolean b) {
128                    Hashtable<String, Boolean> h = new Hashtable<String, Boolean>();
129                    h.put(variableName, new Boolean(b));
130                    return probabilityOf(h);
131            }
132    
133    }