Search in sources :

Example 41 with Dag

use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.

the class TestDiscreteProbs method testCreateUsingBayesIm.

public void testCreateUsingBayesIm() {
    Graph graph = GraphConverter.convert("X1-->X2,X1-->X3,X2-->X4,X3-->X4");
    Dag dag = new Dag(graph);
    BayesPm bayesPm = new BayesPm(dag);
    BayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
    StoredCellProbs cellProbs = StoredCellProbs.createCellTable(bayesIm);
    Proposition assertion = Proposition.tautology(bayesIm);
    assertion.setCategory(0, 1);
    assertion.removeCategory(2, 0);
    Proposition condition = Proposition.tautology(bayesIm);
    condition.setCategory(0, 1);
    cellProbs.getConditionalProb(assertion, condition);
}
Also used : Graph(edu.cmu.tetrad.graph.Graph) Dag(edu.cmu.tetrad.graph.Dag)

Example 42 with Dag

use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.

the class TestHistogram method testHistogram.

@Test
public void testHistogram() {
    RandomUtil.getInstance().setSeed(4829384L);
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        nodes.add(new ContinuousVariable("X" + (i + 1)));
    }
    Dag trueGraph = new Dag(GraphUtils.randomGraph(nodes, 0, 5, 30, 15, 15, false));
    int sampleSize = 1000;
    // Continuous
    SemPm semPm = new SemPm(trueGraph);
    SemIm semIm = new SemIm(semPm);
    DataSet data = semIm.simulateData(sampleSize, false);
    Histogram histogram = new Histogram(data);
    histogram.setTarget("X1");
    histogram.setNumBins(20);
    assertEquals(3.76, histogram.getMax(), 0.01);
    assertEquals(-3.83, histogram.getMin(), 0.01);
    assertEquals(1000, histogram.getN());
    histogram.setTarget("X1");
    histogram.setNumBins(10);
    histogram.addConditioningVariable("X3", 0, 1);
    histogram.addConditioningVariable("X4", 0, 1);
    histogram.removeConditioningVariable("X3");
    assertEquals(3.76, histogram.getMax(), 0.01);
    assertEquals(-3.83, histogram.getMin(), 0.01);
    assertEquals(188, histogram.getN());
    double[] arr = histogram.getContinuousData("X2");
    histogram.addConditioningVariable("X2", StatUtils.min(arr), StatUtils.mean(arr));
    // Discrete
    BayesPm bayesPm = new BayesPm(trueGraph);
    BayesIm bayesIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM);
    DataSet data2 = bayesIm.simulateData(sampleSize, false);
    // For some reason these are giving different
    // values when all of the unit tests are run are
    // once. TODO They produce stable values when
    // this particular test is run repeatedly.
    Histogram histogram2 = new Histogram(data2);
    histogram2.setTarget("X1");
    int[] frequencies1 = histogram2.getFrequencies();
    // assertEquals(928, frequencies1[0]);
    // assertEquals(72, frequencies1[1]);
    histogram2.setTarget("X1");
    histogram2.addConditioningVariable("X2", 0);
    histogram2.addConditioningVariable("X3", 1);
    int[] frequencies = histogram2.getFrequencies();
// assertEquals(377, frequencies[0]);
// assertEquals(28, frequencies[1]);
}
Also used : Histogram(edu.cmu.tetrad.data.Histogram) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) DataSet(edu.cmu.tetrad.data.DataSet) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Dag(edu.cmu.tetrad.graph.Dag) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm) Test(org.junit.Test)

Example 43 with Dag

use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.

the class DirichletBayesIm method constructSample.

// /**
// * Constructs a random sample using the given already allocated data set, to
// * avoid allocating more memory.
// */
// private DataSet simulateDataHelper(DataSet dataSet,
// RandomUtil randomUtil,
// boolean latentDataSaved) {
// if (dataSet.getNumColumns() != nodes.length) {
// throw new IllegalArgumentException("When rewriting the old data set, " +
// "number of variables in data set must equal number of variables " +
// "in Bayes net.");
// }
// 
// int sampleSize = dataSet.getNumRows();
// 
// int numMeasured = 0;
// int[] map = new int[nodes.length];
// List<Node> variables = new LinkedList<>();
// 
// for (int j = 0; j < nodes.length; j++) {
// if (!latentDataSaved && nodes[j].getNodeType() != NodeType.MEASURED) {
// continue;
// }
// 
// int numCategories = bayesPm.getNumCategories(nodes[j]);
// List<String> categories = new LinkedList<>();
// 
// for (int k = 0; k < numCategories; k++) {
// categories.add(bayesPm.getCategory(nodes[j], k));
// }
// 
// DiscreteVariable var =
// new DiscreteVariable(nodes[j].getNode(), categories);
// variables.add(var);
// int index = ++numMeasured - 1;
// map[index] = j;
// }
// 
// for (int i = 0; i < variables.size(); i++) {
// Node node = dataSet.getVariable(i);
// Node _node = variables.get(i);
// dataSet.changeVariable(node, _node);
// }
// 
// constructSample(sampleSize, randomUtil, numMeasured, dataSet, map);
// return dataSet;
// }
private void constructSample(int sampleSize, RandomUtil randomUtil, int numMeasured, DataSet dataSet, int[] map) {
    // Get a tier ordering and convert it to an int array.
    Graph graph = getBayesPm().getDag();
    Dag dag = new Dag(graph);
    List<Node> tierOrdering = dag.getCausalOrdering();
    int[] tiers = new int[tierOrdering.size()];
    for (int i = 0; i < tierOrdering.size(); i++) {
        tiers[i] = getNodeIndex(tierOrdering.get(i));
    }
    // Construct the sample.
    int[] combination = new int[nodes.length];
    for (int i = 0; i < sampleSize; i++) {
        int[] point = new int[nodes.length];
        for (int nodeIndex : tiers) {
            double cutoff = randomUtil.nextDouble();
            for (int k = 0; k < getNumParents(nodeIndex); k++) {
                combination[k] = point[getParent(nodeIndex, k)];
            }
            int rowIndex = getRowIndex(nodeIndex, combination);
            double sum = 0.0;
            for (int k = 0; k < getNumColumns(nodeIndex); k++) {
                double probability = getProbability(nodeIndex, rowIndex, k);
                if (Double.isNaN(probability)) {
                    throw new IllegalStateException("Some probability " + "values in the BayesIm are not filled in; " + "cannot simulate data.");
                }
                sum += probability;
                if (sum >= cutoff) {
                    point[nodeIndex] = k;
                    break;
                }
            }
        }
        for (int j = 0; j < numMeasured; j++) {
            dataSet.setInt(i, j, point[map[j]]);
        }
    }
}
Also used : Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) Dag(edu.cmu.tetrad.graph.Dag)

Example 44 with Dag

use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.

the class Identifiability method createManipulatedGraph.

private Dag createManipulatedGraph(Graph graph) {
    Dag updatedGraph = new Dag(graph);
    // alters graph for manipulated evidenceItems
    for (int i = 0; i < evidence.getNumNodes(); ++i) {
        if (evidence.isManipulated(i)) {
            Node node = updatedGraph.getNode(evidence.getNode(i).getName());
            List<Node> parents = updatedGraph.getParents(node);
            for (Object parent1 : parents) {
                Node parent = (Node) parent1;
                updatedGraph.removeEdge(node, parent);
            }
        }
    }
    return updatedGraph;
}
Also used : Node(edu.cmu.tetrad.graph.Node) Dag(edu.cmu.tetrad.graph.Dag)

Example 45 with Dag

use of edu.cmu.tetrad.graph.Dag in project tetrad by cmu-phil.

the class Identifiability method setEvidence.

// ///////////////////////////////////////////////////////////////
public final void setEvidence(Evidence evidence) {
    if (evidence == null) {
        throw new NullPointerException();
    }
    if (evidence.isIncompatibleWith(bayesIm)) {
        throw new IllegalArgumentException("The variable list for the " + "given bayesIm must be compatible with the variable list " + "for this evidence.");
    }
    this.evidence = evidence;
    Graph graph = bayesIm.getBayesPm().getDag();
    Dag manipulatedGraph = createManipulatedGraph(graph);
    BayesPm manipulatedPm = createUpdatedBayesPm(manipulatedGraph);
    this.manipulatedBayesIm = createdUpdatedBayesIm(manipulatedPm);
/*
        this.manipulatedBayesIm = bayesIm;
		this.bayesImProbs = new BayesImProbs(manipulatedBayesIm);
		 */
/*
      The BayesIm after update, if this was calculated.

      */
}
Also used : Graph(edu.cmu.tetrad.graph.Graph) Dag(edu.cmu.tetrad.graph.Dag)

Aggregations

Dag (edu.cmu.tetrad.graph.Dag)53 Node (edu.cmu.tetrad.graph.Node)41 Graph (edu.cmu.tetrad.graph.Graph)21 GraphNode (edu.cmu.tetrad.graph.GraphNode)21 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)12 SemIm (edu.cmu.tetrad.sem.SemIm)10 SemPm (edu.cmu.tetrad.sem.SemPm)10 DataSet (edu.cmu.tetrad.data.DataSet)7 BayesPm (edu.cmu.tetrad.bayes.BayesPm)6 MlBayesIm (edu.cmu.tetrad.bayes.MlBayesIm)6 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)6 BayesIm (edu.cmu.tetrad.bayes.BayesIm)5 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)3 FruchtermanReingoldLayout (edu.cmu.tetrad.graph.FruchtermanReingoldLayout)2 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)2 LinkedList (java.util.LinkedList)2 StoredCellProbs (edu.cmu.tetrad.bayes.StoredCellProbs)1 ColtDataSet (edu.cmu.tetrad.data.ColtDataSet)1 CovarianceMatrixOnTheFly (edu.cmu.tetrad.data.CovarianceMatrixOnTheFly)1