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);
}
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]);
}
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]]);
}
}
}
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;
}
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.
*/
}
Aggregations