use of edu.cmu.tetrad.search.IndependenceTest in project tetrad by cmu-phil.
the class IndependenceFactsEditor method generateResults.
private void generateResults() {
results = new ArrayList<>();
List<String> dataVars = getDataVars();
if (getVars().size() < 2) {
tableModel.fireTableDataChanged();
return;
}
// Need a choice generator over the ?'s, and a depth choice generator over the +'s. The +'s all have to come
// at the end at index >= 2.
int numQuestionMarksFirstTwo = 0;
int numQuestionMarksRest = 0;
int numPluses = 0;
int numFixed = 0;
for (int i = 0; i < vars.size(); i++) {
String var = vars.get(i);
if ("?".equals(var) && i < 2)
numQuestionMarksFirstTwo++;
else if ("?".equals(var))
numQuestionMarksRest++;
else if ("+".equals(var))
numPluses++;
else
numFixed++;
}
int[] questionMarkFirstTwoIndices = new int[numQuestionMarksFirstTwo];
int[] questionMarkRestIndices = new int[numQuestionMarksRest];
int[] plusIndices = new int[numPluses];
int[] fixedIndices = new int[numFixed];
String[] fixedVars = new String[numFixed];
int _i = -1;
int _j = -1;
int _k = -1;
int _l = -1;
for (int i = 0; i < vars.size(); i++) {
if ("?".equals(vars.get(i)) && i < 2)
questionMarkFirstTwoIndices[++_i] = i;
else if ("?".equals(vars.get(i)))
questionMarkRestIndices[++_j] = i;
else if ("+".equals(vars.get(i)))
plusIndices[++_k] = i;
else {
fixedIndices[++_l] = i;
fixedVars[_l] = vars.get(i);
}
}
List<String> vars1 = new ArrayList<>(dataVars);
vars1.removeAll(vars);
ChoiceGenerator gen1 = new ChoiceGenerator(vars1.size(), questionMarkFirstTwoIndices.length);
int[] choice1;
LOOP: while ((choice1 = gen1.next()) != null) {
List<String> s2 = asList(choice1, vars1);
List<String> vars2 = new ArrayList<>(vars1);
vars2.removeAll(s2);
ChoiceGenerator gen2 = new ChoiceGenerator(vars2.size(), questionMarkRestIndices.length);
int[] choice2;
while ((choice2 = gen2.next()) != null) {
List<String> s3 = asList(choice2, vars2);
List<String> vars3 = new ArrayList<>(vars2);
vars3.removeAll(s3);
DepthChoiceGenerator gen3 = new DepthChoiceGenerator(vars3.size(), plusIndices.length);
int[] choice3;
while ((choice3 = gen3.next()) != null) {
results.add(new ArrayList<IndependenceResult>());
for (int prod = 0; prod < indTestProducers.size(); prod++) {
String[] vars4 = new String[fixedIndices.length + questionMarkFirstTwoIndices.length + questionMarkRestIndices.length + choice3.length];
for (int i = 0; i < fixedIndices.length; i++) {
vars4[fixedIndices[i]] = fixedVars[i];
}
for (int i = 0; i < choice1.length; i++) {
vars4[questionMarkFirstTwoIndices[i]] = vars1.get(choice1[i]);
}
for (int i = 0; i < choice2.length; i++) {
vars4[questionMarkRestIndices[i]] = vars2.get(choice2[i]);
}
for (int i = 0; i < choice3.length; i++) {
vars4[plusIndices[i]] = vars3.get(choice3[i]);
}
IndependenceTest independenceTest = getIndependenceTest(prod);
Node x = independenceTest.getVariable(vars4[0]);
Node y = independenceTest.getVariable(vars4[1]);
List<Node> z = new ArrayList<>();
for (int i = 2; i < vars4.length; i++) {
z.add(independenceTest.getVariable(vars4[i]));
}
IndependenceResult.Type indep;
double pValue;
try {
indep = independenceTest.isIndependent(x, y, z) ? IndependenceResult.Type.INDEPENDENT : IndependenceResult.Type.DEPENDENT;
pValue = independenceTest.getPValue();
} catch (Exception e) {
indep = IndependenceResult.Type.UNDETERMINED;
pValue = Double.NaN;
}
results.get(results.size() - 1).add(new IndependenceResult(results.size(), factString(x, y, z), indep, pValue));
}
if (results.size() > getListLimit())
break LOOP;
}
}
}
model.setResults(results);
tableModel.fireTableDataChanged();
}
use of edu.cmu.tetrad.search.IndependenceTest in project tetrad by cmu-phil.
the class MBFS method search.
@Override
public Graph search(DataModel dataSet, Parameters parameters) {
if (parameters.getInt("bootstrapSampleSize") < 1) {
IndependenceTest test = this.test.getTest(dataSet, parameters);
edu.cmu.tetrad.search.Mbfs search = new edu.cmu.tetrad.search.Mbfs(test, parameters.getInt("depth"));
search.setDepth(parameters.getInt("depth"));
search.setKnowledge(knowledge);
this.targetName = parameters.getString("targetName");
if (targetName.isEmpty()) {
throw new IllegalArgumentException("Target variable name needs to be provided.");
}
if (test.getVariable(targetName) == null) {
throw new IllegalArgumentException("Target variable name '" + targetName + "' not found in dataset.");
}
Node target = test.getVariable(targetName);
return search.search(target.getName());
} else {
MBFS algorithm = new MBFS(test);
// algorithm.setKnowledge(knowledge);
DataSet data = (DataSet) dataSet;
GeneralBootstrapTest search = new GeneralBootstrapTest(data, algorithm, parameters.getInt("bootstrapSampleSize"));
search.setKnowledge(knowledge);
BootstrapEdgeEnsemble edgeEnsemble = BootstrapEdgeEnsemble.Highest;
switch(parameters.getInt("bootstrapEnsemble", 1)) {
case 0:
edgeEnsemble = BootstrapEdgeEnsemble.Preserved;
break;
case 1:
edgeEnsemble = BootstrapEdgeEnsemble.Highest;
break;
case 2:
edgeEnsemble = BootstrapEdgeEnsemble.Majority;
}
search.setEdgeEnsemble(edgeEnsemble);
search.setParameters(parameters);
search.setVerbose(parameters.getBoolean("verbose"));
return search.search();
}
}
use of edu.cmu.tetrad.search.IndependenceTest in project tetrad by cmu-phil.
the class ImagesPcStableMax method search.
@Override
public Graph search(List<DataModel> dataModels, Parameters parameters) {
if (parameters.getInt("bootstrapSampleSize") < 1) {
List<DataSet> dataSets = new ArrayList<>();
for (DataModel dataModel : dataModels) {
dataSets.add((DataSet) dataModel);
}
SemBicScoreImages3 score = new SemBicScoreImages3(dataSets);
score.setPenaltyDiscount(parameters.getDouble("penaltyDiscount"));
IndependenceTest test = new IndTestScore(score);
PcStableMax search = new PcStableMax(test);
search.setUseHeuristic(parameters.getBoolean("useMaxPOrientationHeuristic"));
search.setMaxPathLength(parameters.getInt("maxPOrientationMaxPathLength"));
search.setKnowledge(knowledge);
search.setDepth(parameters.getInt("depth"));
return search.search();
} else {
ImagesPcStableMax imagesPcStableMax = new ImagesPcStableMax();
// imagesPcStableMax.setKnowledge(knowledge);
List<DataSet> datasets = new ArrayList<>();
for (DataModel dataModel : dataModels) {
datasets.add((DataSet) dataModel);
}
GeneralBootstrapTest search = new GeneralBootstrapTest(datasets, imagesPcStableMax, parameters.getInt("bootstrapSampleSize"));
search.setKnowledge(knowledge);
BootstrapEdgeEnsemble edgeEnsemble = BootstrapEdgeEnsemble.Highest;
switch(parameters.getInt("bootstrapEnsemble", 1)) {
case 0:
edgeEnsemble = BootstrapEdgeEnsemble.Preserved;
break;
case 1:
edgeEnsemble = BootstrapEdgeEnsemble.Highest;
break;
case 2:
edgeEnsemble = BootstrapEdgeEnsemble.Majority;
}
search.setEdgeEnsemble(edgeEnsemble);
search.setParameters(parameters);
search.setVerbose(parameters.getBoolean("verbose"));
return search.search();
}
}
use of edu.cmu.tetrad.search.IndependenceTest in project tetrad by cmu-phil.
the class TsImages method search.
@Override
public Graph search(DataModel dataModel, Parameters parameters) {
if (parameters.getInt("bootstrapSampleSize") < 1) {
DataSet dataSet = (DataSet) dataModel;
TsGFci search;
Score score1 = score.getScore(dataSet, parameters);
IndependenceTest test = new IndTestScore(score1);
search = new TsGFci(test, score1);
search.setKnowledge(dataSet.getKnowledge());
return search.search();
} else {
TsImages algorithm = new TsImages(score);
DataSet data = (DataSet) dataModel;
GeneralBootstrapTest search = new GeneralBootstrapTest(data, algorithm, parameters.getInt("bootstrapSampleSize"));
search.setKnowledge(knowledge);
BootstrapEdgeEnsemble edgeEnsemble = BootstrapEdgeEnsemble.Highest;
switch(parameters.getInt("bootstrapEnsemble", 1)) {
case 0:
edgeEnsemble = BootstrapEdgeEnsemble.Preserved;
break;
case 1:
edgeEnsemble = BootstrapEdgeEnsemble.Highest;
break;
case 2:
edgeEnsemble = BootstrapEdgeEnsemble.Majority;
}
search.setEdgeEnsemble(edgeEnsemble);
search.setParameters(parameters);
search.setVerbose(parameters.getBoolean("verbose"));
return search.search();
}
}
use of edu.cmu.tetrad.search.IndependenceTest in project tetrad by cmu-phil.
the class TestIndTestFisherZ method testDirections.
@Test
public void testDirections() {
RandomUtil.getInstance().setSeed(48285934L);
Graph graph1 = new EdgeListGraph();
Graph graph2 = new EdgeListGraph();
Node x = new GraphNode("X");
Node y = new GraphNode("Y");
Node z = new GraphNode("Z");
graph1.addNode(x);
graph1.addNode(y);
graph1.addNode(z);
graph2.addNode(x);
graph2.addNode(y);
graph2.addNode(z);
graph1.addEdge(Edges.directedEdge(x, y));
graph1.addEdge(Edges.directedEdge(y, z));
graph2.addEdge(Edges.directedEdge(x, y));
graph2.addEdge(Edges.directedEdge(z, y));
SemPm pm1 = new SemPm(graph1);
SemPm pm2 = new SemPm(graph2);
SemIm im1 = new SemIm(pm1);
SemIm im2 = new SemIm(pm2);
im2.setEdgeCoef(x, y, im1.getEdgeCoef(x, y));
im2.setEdgeCoef(z, y, im1.getEdgeCoef(y, z));
DataSet data1 = im1.simulateData(500, false);
DataSet data2 = im2.simulateData(500, false);
IndependenceTest test1 = new IndTestFisherZ(data1, 0.05);
IndependenceTest test2 = new IndTestFisherZ(data2, 0.05);
test1.isIndependent(data1.getVariable(x.getName()), data1.getVariable(y.getName()));
double p1 = test1.getPValue();
test2.isIndependent(data2.getVariable(x.getName()), data2.getVariable(z.getName()), data2.getVariable(y.getName()));
double p2 = test2.getPValue();
test2.isIndependent(data2.getVariable(x.getName()), data2.getVariable(z.getName()));
double p3 = test2.getPValue();
assertEquals(0, p1, 0.01);
assertEquals(0, p2, 0.01);
assertEquals(0.38, p3, 0.01);
}
Aggregations