Search in sources :

Example 26 with TableVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.

the class TestCases method tree.

private static List<TableFactor> tree(int depth, int childrenPerNode, Function<ArrayList<TableVariable>, ArrayList<Double>> entryGen, int cardinality) {
    TableVariable[][] treeVars = treeVariables(depth, childrenPerNode, cardinality);
    List<TableFactor> result = new ArrayList<>();
    for (int i = 0; i < depth; i++) {
        for (int j = 0; j < pow(childrenPerNode, i); j++) {
            ArrayList<TableVariable> vars = listWithVariableNodeAndItschildren(depth, childrenPerNode, treeVars, i, j);
            TableFactor newNode = new TableFactor(vars, entryGen.apply(vars));
            result.add(newNode);
        }
    }
    return result;
}
Also used : TableFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 27 with TableVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.

the class TestCases method treeVariables.

// ------ Trees
private static TableVariable[][] treeVariables(int depth, int childrenPerNode, int cardinality) {
    int nCol = pow(childrenPerNode, depth);
    TableVariable[][] result = new TableVariable[depth][nCol];
    for (int i = 0; i < depth; i++) {
        for (int j = 0; j < pow(childrenPerNode, i); j++) {
            result[i][j] = new TableVariable("A_" + i + "_" + j, cardinality);
        }
    }
    return result;
}
Also used : TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 28 with TableVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.

the class TableBayesianModelTest method testSickSunColdModelWithDifferentDatapoints.

@Test
public void testSickSunColdModelWithDifferentDatapoints() {
    // Dataset
    List<TableVariable> variables = list(sickVariable, sunVariable, coldVariable);
    List<Integer> variableValues1 = list(1, 0, 1);
    DefaultDatapoint datapoint1 = new DefaultDatapoint(variables, variableValues1);
    List<DefaultDatapoint> datapoints = list();
    int numberOfDatapoints1 = 4;
    for (int i = 1; i <= numberOfDatapoints1; i++) {
        datapoints.add(datapoint1);
    }
    List<Integer> variableValues2 = list(0, 0, 0);
    DefaultDatapoint datapoint2 = new DefaultDatapoint(variables, variableValues2);
    int numberOfDatapoints2 = 2;
    for (int i = 1; i <= numberOfDatapoints2; i++) {
        datapoints.add(datapoint2);
    }
    List<Integer> variableValues3 = list(0, 0, 1);
    DefaultDatapoint datapoint3 = new DefaultDatapoint(variables, variableValues3);
    int numberOfDatapoints3 = 1;
    for (int i = 1; i <= numberOfDatapoints3; i++) {
        datapoints.add(datapoint3);
    }
    DefaultDataset dataset = new DefaultDataset(datapoints);
    // Learning
    sickSunColdModel = (TableBayesianModel) sickSunColdModel.learnModelParametersFromCompleteData(dataset);
    List<? extends TableBayesianNode> learnedNodes = sickSunColdModel.getNodes();
    // Testing
    // For the sickNode first:
    // Expected parameters (2 datapoints1): {(0, [0, 0])=0.5, (1, [0, 0])=0.5, (1, [1, 0])=0.5, (1, [1, 1])=0.5, (0, [1, 1])=0.5, (0, [1, 0])=0.5, (0, [0, 1])=0.25, (1, [0, 1])=0.75}
    TableBayesianNode learnedSickNode = learnedNodes.get(0);
    LinkedHashMap<TableVariable, Integer> variablesAndTheirValues = map();
    variablesAndTheirValues.put(sickVariable, 0);
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 0);
    // Parameter for (0, [0, 0]):
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints2) / (2 + numberOfDatapoints2)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [0, 0]):
    variablesAndTheirValues.put(sickVariable, 1);
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints2)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [1, 0]):
    variablesAndTheirValues.put(sunVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [1, 1]):
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [1, 1]):
    variablesAndTheirValues.put(sickVariable, 0);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [1, 0]):
    variablesAndTheirValues.put(coldVariable, 0);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [0, 1]):
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints3) / (2 + numberOfDatapoints1 + numberOfDatapoints3)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [0, 1]):
    variablesAndTheirValues.put(sickVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints1) / (2 + numberOfDatapoints1 + numberOfDatapoints3)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // For the sunNode:
    // Expected parameters (2 datapoints): {(0, [])=0.75, (1, [])=0.25}
    TableBayesianNode learnedSunNode = learnedNodes.get(1);
    variablesAndTheirValues = map();
    variablesAndTheirValues.put(sunVariable, 0);
    // Parameter for (0, []):
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3) / (2 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3)), learnedSunNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, []):
    variablesAndTheirValues.put(sunVariable, 1);
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3)), learnedSunNode.getEntryFor(variablesAndTheirValues));
    // For the coldNode:
    // Expected parameters (2 datapoints): {(0, [])=0.25, (1, [])=0.75}
    TableBayesianNode learnedColdNode = learnedNodes.get(2);
    variablesAndTheirValues = map();
    variablesAndTheirValues.put(coldVariable, 0);
    // Parameter for (0, []):
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints2) / (2 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3)), learnedColdNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, []):
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints1 + numberOfDatapoints3) / (2 + numberOfDatapoints1 + numberOfDatapoints2 + numberOfDatapoints3)), learnedColdNode.getEntryFor(variablesAndTheirValues));
}
Also used : TableBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) Test(org.junit.Test)

Example 29 with TableVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.

the class TableBayesianModelTest method testSickSunColdModel.

@Test
public void testSickSunColdModel() {
    // Dataset
    List<TableVariable> variables = list(sickVariable, sunVariable, coldVariable);
    List<Integer> variableValues = list(1, 0, 1);
    DefaultDatapoint datapoint = new DefaultDatapoint(variables, variableValues);
    List<DefaultDatapoint> datapoints = list();
    int numberOfDatapoints = 2;
    for (int i = 1; i <= numberOfDatapoints; i++) {
        datapoints.add(datapoint);
    }
    DefaultDataset dataset = new DefaultDataset(datapoints);
    // Learning
    sickSunColdModel = (TableBayesianModel) sickSunColdModel.learnModelParametersFromCompleteData(dataset);
    List<? extends TableBayesianNode> learnedNodes = sickSunColdModel.getNodes();
    // Testing
    // For the sickNode first:
    // Expected parameters (2 datapoints): {(0, [0, 0])=0.5, (1, [0, 0])=0.5, (1, [1, 0])=0.5, (1, [1, 1])=0.5, (0, [1, 1])=0.5, (0, [1, 0])=0.5, (0, [0, 1])=0.25, (1, [0, 1])=0.75}
    TableBayesianNode learnedSickNode = learnedNodes.get(0);
    LinkedHashMap<TableVariable, Integer> variablesAndTheirValues = map();
    variablesAndTheirValues.put(sickVariable, 0);
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 0);
    // Parameter for (0, [0, 0]):
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [0, 0]):
    variablesAndTheirValues.put(sickVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [1, 0]):
    variablesAndTheirValues.put(sunVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [1, 1]):
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [1, 1]):
    variablesAndTheirValues.put(sickVariable, 0);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [1, 0]):
    variablesAndTheirValues.put(coldVariable, 0);
    Assert.assertEquals(Double.valueOf(0.5), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (0, [0, 1]):
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, [0, 1]):
    variablesAndTheirValues.put(sickVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints) / (2 + numberOfDatapoints)), learnedSickNode.getEntryFor(variablesAndTheirValues));
    // For the sunNode:
    // Expected parameters (2 datapoints): {(0, [])=0.75, (1, [])=0.25}
    TableBayesianNode learnedSunNode = learnedNodes.get(1);
    variablesAndTheirValues = map();
    variablesAndTheirValues.put(sunVariable, 0);
    // Parameter for (0, []):
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints) / (2 + numberOfDatapoints)), learnedSunNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, []):
    variablesAndTheirValues.put(sunVariable, 1);
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints)), learnedSunNode.getEntryFor(variablesAndTheirValues));
    // For the coldNode:
    // Expected parameters (2 datapoints): {(0, [])=0.25, (1, [])=0.75}
    TableBayesianNode learnedColdNode = learnedNodes.get(2);
    variablesAndTheirValues = map();
    variablesAndTheirValues.put(coldVariable, 0);
    // Parameter for (0, []):
    Assert.assertEquals(Double.valueOf(1.0 / (2 + numberOfDatapoints)), learnedColdNode.getEntryFor(variablesAndTheirValues));
    // Parameter for (1, []):
    variablesAndTheirValues.put(coldVariable, 1);
    Assert.assertEquals(Double.valueOf((1.0 + numberOfDatapoints) / (2 + numberOfDatapoints)), learnedColdNode.getEntryFor(variablesAndTheirValues));
}
Also used : TableBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) Test(org.junit.Test)

Example 30 with TableVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable in project aic-praise by aic-sri-international.

the class TableBayesianModelTest method printSickSunColdModelTest.

public static void printSickSunColdModelTest() {
    // Dataset
    List<TableVariable> variables = list(sickVariable, sunVariable, coldVariable);
    List<Integer> variableValues = list(1, 0, 1);
    DefaultDatapoint datapoint = new DefaultDatapoint(variables, variableValues);
    List<DefaultDatapoint> datapoints = list();
    int numberOfDatapoints = 2;
    for (int i = 1; i <= numberOfDatapoints; i++) {
        datapoints.add(datapoint);
    }
    DefaultDataset dataset = new DefaultDataset(datapoints);
    // Learning
    long startTime = System.currentTimeMillis();
    sickSunColdModel = (TableBayesianModel) sickSunColdModel.learnModelParametersFromCompleteData(dataset);
    long stopTime = System.currentTimeMillis();
    long elapsedTime = stopTime - startTime;
    System.out.println("Elapsed time for learning with " + numberOfDatapoints + " datapoints: " + elapsedTime + " miliseconds \n");
    List<? extends TableBayesianNode> learnedNodes = sickSunColdModel.getNodes();
    // Testing
    String expectedParametersForSick = "{(0, [0, 0])=0.5, (1, [0, 0])=0.5, (0, [0, 1])=0.25, (1, [0, 1])=0.75, (0, [1, 0])=0.5, (1, [1, 0])=0.5, (0, [1, 1])=0.5, (1, [1, 1])=0.5}";
    System.out.println("Expected parameters for sick (with 2 datapoints):\n" + expectedParametersForSick + "\n");
    TableBayesianNode learnedSickNode = learnedNodes.get(0);
    LinkedHashMap<TableVariable, Integer> variablesAndTheirValues = new LinkedHashMap<TableVariable, Integer>();
    variablesAndTheirValues.put(sickVariable, 1);
    variablesAndTheirValues.put(sunVariable, 0);
    variablesAndTheirValues.put(coldVariable, 1);
    System.out.println("Actual entries for sick:");
    System.out.println("entryFor(" + variablesAndTheirValues.get(sickVariable) + ", [" + variablesAndTheirValues.get(sunVariable) + ", " + variablesAndTheirValues.get(coldVariable) + "]) = " + learnedSickNode.getEntryFor(variablesAndTheirValues));
    variablesAndTheirValues.put(sickVariable, 0);
    System.out.println("entryFor(" + variablesAndTheirValues.get(sickVariable) + ", [" + variablesAndTheirValues.get(sunVariable) + ", " + variablesAndTheirValues.get(coldVariable) + "]) = " + learnedSickNode.getEntryFor(variablesAndTheirValues));
    variablesAndTheirValues.put(coldVariable, 0);
    System.out.println("entryFor(" + variablesAndTheirValues.get(sickVariable) + ", [" + variablesAndTheirValues.get(sunVariable) + ", " + variablesAndTheirValues.get(coldVariable) + "]) = " + learnedSickNode.getEntryFor(variablesAndTheirValues));
}
Also used : TableBayesianNode(com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) LinkedHashMap(java.util.LinkedHashMap) DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Aggregations

TableVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)49 TableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor)34 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)13 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)10 Variable (com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable)8 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 TableFactor.copyToSubTableFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactor.copyToSubTableFactor)7 TableFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableFactorNetwork)6 Pair (com.sri.ai.util.base.Pair)6 Expression (com.sri.ai.expresso.api.Expression)4 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)3 DefaultDatapoint (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)3 DefaultDataset (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset)3 TableBayesianNode (com.sri.ai.praise.learning.parameterlearning.representation.table.TableBayesianNode)3 Util (com.sri.ai.util.Util)3 Util.arrayList (com.sri.ai.util.Util.arrayList)3 File (java.io.File)3 LinkedList (java.util.LinkedList)3