use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class TestIndTestFisherZ method test2.
@Test
public void test2() {
for (int p = 0; p < 50; p++) {
Graph graph = new EdgeListGraph();
Node x = new ContinuousVariable("X");
Node y = new ContinuousVariable("Y");
Node w1 = new ContinuousVariable("W1");
Node w2 = new ContinuousVariable("W2");
Node w3 = new ContinuousVariable("W3");
Node r = new ContinuousVariable("R");
graph.addNode(x);
graph.addNode(y);
graph.addNode(w1);
graph.addNode(w2);
graph.addNode(w3);
graph.addNode(r);
graph.addDirectedEdge(x, w1);
graph.addDirectedEdge(w1, w2);
graph.addDirectedEdge(w2, y);
graph.addDirectedEdge(w3, y);
// graph.addDirectedEdge(x, r);
// graph.addDirectedEdge(r, y);
graph.addDirectedEdge(y, r);
//
SemPm pm = new SemPm(graph);
Parameters parameters = new Parameters();
parameters.set("coefLow", .3);
parameters.set("coefHigh", .8);
parameters.set("coefSymmetric", false);
SemIm im = new SemIm(pm, parameters);
final int N = 1000;
DataSet data = im.simulateData(N, false);
ICovarianceMatrix _cov = new CovarianceMatrix(data);
TetradMatrix cov = _cov.getMatrix();
List<Node> nodes = _cov.getVariables();
final int xi = nodes.indexOf(x);
final int yi = nodes.indexOf(y);
final int ri = nodes.indexOf(r);
double xy = StatUtils.partialCorrelation(cov, xi, yi);
double xyr = StatUtils.partialCorrelation(cov, xi, yi, ri);
double f1 = 0.5 * sqrt(N - 3) * log(1. + xy) - log(1. - xy);
double f2 = 0.5 * sqrt(N - 3 - 1) * log(1. + xyr) - log(1. - xyr);
System.out.println(abs(f1) > abs(f2));
}
}
use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class SpecialExampleSimulationClark method main.
public static void main(String... args) {
Parameters parameters = new Parameters();
parameters.set("numRuns", 20);
parameters.set("sampleSize", 1000);
parameters.set("twoCycleAlpha", 1);
Statistics statistics = new Statistics();
statistics.add(new AdjacencyPrecision());
statistics.add(new AdjacencyRecall());
statistics.add(new ArrowheadPrecision());
statistics.add(new ArrowheadRecall());
// statistics.add(new TwoCycleTruePositive());
// statistics.add(new TwoCycleFalseNegative());
// statistics.add(new TwoCycleFalsePositive());
// For randomm forward graph
parameters.set("numMeasures", 10);
parameters.set("numLatents", 0);
parameters.set("avgDegree", 2);
parameters.set("maxDegree", 100);
parameters.set("maxIndegree", 100);
parameters.set("maxOutdegree", 100);
parameters.set("connected", false);
//
// statistics.setWeight("AP", 1.0);
// statistics.setWeight("AR", 0.5);
Algorithms algorithms = new Algorithms();
algorithms.add(new Fask());
Simulations simulations = new Simulations();
// simulations.add(new SpecialDataClark(new SpecialGraphClark()));
simulations.add(new SpecialDataClark(new RandomForward()));
Comparison comparison = new Comparison();
comparison.setShowAlgorithmIndices(true);
comparison.setShowSimulationIndices(true);
comparison.setSortByUtility(false);
comparison.setShowUtilities(false);
comparison.setParallelized(true);
comparison.setSaveGraphs(true);
comparison.setSavePatterns(true);
comparison.setSavePags(true);
// comparison.saveToFiles("comparison", new SpecialDataClark(new SpecialGraphClark()), parameters);
comparison.compareFromSimulations("comparison", simulations, algorithms, statistics, parameters);
}
use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class FirstInflection method search.
@Override
public Graph search(DataModel dataSet, Parameters parameters) {
Parameters _parameters = new Parameters(parameters);
Graph _previous = null;
int _prevDiff = Integer.MAX_VALUE;
double _value = 0.0;
if (increment > 0) {
for (double value = low - increment; value <= high + 0.0000001; value += increment) {
double value0 = getValue(value, parameters);
_parameters.set(parameter, value0);
intialGraph = algorithm.search(dataSet, _parameters);
if (_previous == null) {
_previous = intialGraph;
continue;
}
intialGraph = GraphUtils.replaceNodes(intialGraph, _previous.getNodes());
Set<Edge> edges1 = intialGraph.getEdges();
int numEdges = edges1.size();
Set<Edge> edges2 = _previous.getEdges();
edges2.removeAll(edges1);
int diff = edges2.size();
System.out.println(parameter + " = " + _parameters.getDouble(parameter) + " # edges = " + numEdges + " # additional = " + diff);
if (diff >= _prevDiff) {
break;
}
_previous = intialGraph;
_value = _parameters.getDouble(parameter);
_prevDiff = diff;
}
if (_value == Math.round((low + increment) * 1000000000.0) / 1000000000.0) {
for (double value = low; value >= Double.NEGATIVE_INFINITY; value -= increment) {
value = getValue(value, parameters);
_parameters.set(parameter, value);
intialGraph = algorithm.search(dataSet, _parameters);
intialGraph = GraphUtils.replaceNodes(intialGraph, _previous.getNodes());
Set<Edge> edges1 = intialGraph.getEdges();
int numEdges = edges1.size();
Set<Edge> edges2 = intialGraph.getEdges();
edges2.removeAll(_previous.getEdges());
int diff = edges2.size();
System.out.println(parameter + " = " + _parameters.getDouble(parameter) + " # edges = " + numEdges + " # additional = " + diff);
if (diff >= _prevDiff) {
break;
}
_previous = intialGraph;
_value = _parameters.getDouble(parameter);
_prevDiff = diff;
}
}
} else {
for (double value = high; value >= low - 0.0000001; value += increment) {
double value0 = getValue(value, parameters);
_parameters.set(parameter, value0);
intialGraph = algorithm.search(dataSet, _parameters);
if (_previous == null) {
_previous = intialGraph;
continue;
}
intialGraph = GraphUtils.replaceNodes(intialGraph, _previous.getNodes());
Set<Edge> edges1 = intialGraph.getEdges();
int numEdges = edges1.size();
Set<Edge> edges2 = _previous.getEdges();
edges2.removeAll(edges1);
int diff = edges2.size();
System.out.println(parameter + " = " + _parameters.getDouble(parameter) + " # edges = " + numEdges + " # additional = " + diff);
if (diff >= _prevDiff) {
break;
}
_previous = intialGraph;
_value = _parameters.getDouble(parameter);
_prevDiff = diff;
}
if (_value == Math.round((high - increment) * 1000000000.0) / 1000000000.0) {
for (double value = low; value >= Double.NEGATIVE_INFINITY; value -= increment) {
value = getValue(value, parameters);
_parameters.set(parameter, value);
intialGraph = algorithm.search(dataSet, _parameters);
intialGraph = GraphUtils.replaceNodes(intialGraph, _previous.getNodes());
Set<Edge> edges1 = intialGraph.getEdges();
int numEdges = edges1.size();
Set<Edge> edges2 = intialGraph.getEdges();
edges2.removeAll(_previous.getEdges());
int diff = edges2.size();
System.out.println(parameter + " = " + _parameters.getDouble(parameter) + " # edges = " + numEdges + " # additional = " + diff);
if (diff >= _prevDiff) {
break;
}
_previous = intialGraph;
_value = _parameters.getDouble(parameter);
_prevDiff = diff;
}
}
}
System.out.println(parameter + " = " + _value);
return _previous;
// double tolerance = parameters.getDouble("StARS.tolerance");
//
// MultivariateOptimizer search = new PowellOptimizer(tolerance, tolerance);
// FittingFunction f = new FittingFunction(_parameters, algorithm, low, high, parameter, (DataSet) dataSet);
// PointValuePair p = search.optimize(
// new InitialGuess(new double[]{increment, increment}),
// new ObjectiveFunction(f),
// GoalType.MINIMIZE,
// new MaxEval(100000)
// );
//
//
// double[] point = p.getPoint();
//
// double p1 = point[0];
// double p2 = point[1];
//
// p1 = Math.round(p1 * 10.0) / 10.0;
// p2 = Math.round(p2 * 10.0) / 10.0;
//
// double value = Math.max(p1, p2);
//
// // double value = (p.getPoint()[0] + p.getPoint()[1]) / 2;
// System.out.println(parameter + " = " + getValue(value, parameters));
// _parameters.set(parameter, getValue(value, parameters));
//
// return algorithm.search(dataSet, _parameters);
}
use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class StARS method search.
@Override
public Graph search(DataModel dataSet, Parameters parameters) {
this._dataSet = (DataSet) dataSet;
// int numVars = Math.min(50, ((DataSet) dataSet).getNumColumns());
// int[] cols = new int[numVars];
// for (int i = 0; i < numVars; i++) cols[i] = i;
// .subsetColumns(cols);
_dataSet = (DataSet) dataSet;
double percentageB = parameters.getDouble("percentSubsampleSize");
double tolerance = parameters.getDouble("StARS.tolerance");
double beta = parameters.getDouble("StARS.cutoff");
int numSubsamples = parameters.getInt("numSubsamples");
Parameters _parameters = new Parameters(parameters);
List<DataSet> samples = new ArrayList<>();
for (int i = 0; i < numSubsamples; i++) {
BootstrapSampler sampler = new BootstrapSampler();
sampler.setWithoutReplacements(true);
samples.add(sampler.sample(_dataSet, (int) (percentageB * _dataSet.getNumRows())));
}
// double pFrom = low;
// double pTo = high;
double maxD = Double.NEGATIVE_INFINITY;
double _lambda = Double.NaN;
for (double lambda = low; lambda <= high; lambda += 0.5) {
double D = getD(parameters, parameter, lambda, samples, algorithm);
System.out.println("lambda = " + lambda + " D = " + D);
if (D > maxD && D < beta) {
maxD = D;
_lambda = lambda;
}
}
// double D1 = getD(parameters, parameter, low, samples, algorithm);
// System.out.println("D1 (low) = " + D1);
// double D2 = getD(parameters, parameter, high, samples, algorithm);
// System.out.println("D2 (high) = " + D2);
//
// double lastD;
// double pMid;
// double pBest;
// double bestD;
//
// if (D1 > D2 && D1 < beta) {
// lastD = D1;
// pBest = low;
// bestD = D1;
// } else if (D2 > D1 && D2 < beta) {
// lastD = D2;
// pBest = high;
// bestD = D2;
// } else {
// lastD = Double.NEGATIVE_INFINITY;
// pBest = (low + high) / 2.0;
// bestD = Double.NEGATIVE_INFINITY;
// }
//
// System.out.println("lastD = " + lastD);
//
// while (abs(pFrom - pTo) > tolerance) {
// pMid = (pFrom + pTo) / 2.0;
// double D = getD(parameters, parameter, pMid, samples, algorithm);
// System.out.println("pFrom = " + pFrom + " pTo = " + pTo + " pMid = " + pMid + " D = " + D);
//
// if (D1 > D2) {
// if (D > bestD && D < beta) {
// pTo = pMid;
// pBest = pMid;
// bestD = D;
// } else {
// pFrom = pMid;
// }
// } else {
// if (D > bestD && D < beta) {
// pFrom = pMid;
// pBest = pMid;
// bestD = D;
// } else {
// pTo = pMid;
// }
// }
//
// lastD = D;
// System.out.println("lastD = " + lastD + " pBest = " + pBest);
// }
//
// if (D1 > bestD) {
// pBest = low;
// }
//
// if (D2 > bestD) {
// pBest = high;
// }
System.out.println("FINAL: lambda = " + _lambda + " D = " + maxD);
System.out.println(parameter + " = " + getValue(_lambda, parameters));
_parameters.set(parameter, getValue(_lambda, parameters));
return algorithm.search(dataSet, _parameters);
}
use of edu.cmu.tetrad.util.Parameters in project tetrad by cmu-phil.
the class ExampleCompareSimulation method main.
public static void main(String... args) {
Parameters parameters = new Parameters();
https: parameters.set("numRuns", 10);
parameters.set("numMeasures", 100);
parameters.set("avgDegree", 4, 6);
parameters.set("sampleSize", 500);
parameters.set("alpha", 1e-4, 1e-3, 1e-2);
Statistics statistics = new Statistics();
statistics.add(new AdjacencyPrecision());
statistics.add(new AdjacencyRecall());
statistics.add(new ArrowheadPrecision());
statistics.add(new ArrowheadRecall());
statistics.add(new MathewsCorrAdj());
statistics.add(new MathewsCorrArrow());
statistics.add(new F1Adj());
statistics.add(new F1Arrow());
statistics.add(new SHD());
statistics.add(new ElapsedTime());
statistics.setWeight("AP", 1.0);
statistics.setWeight("AR", 0.5);
Algorithms algorithms = new Algorithms();
algorithms.add(new Pc(new FisherZ()));
algorithms.add(new Cpc(new FisherZ(), new Fges(new SemBicScore(), false)));
algorithms.add(new PcStable(new FisherZ()));
algorithms.add(new CpcStable(new FisherZ()));
Simulations simulations = new Simulations();
simulations.add(new SemSimulation(new RandomForward()));
Comparison comparison = new Comparison();
comparison.setShowAlgorithmIndices(true);
comparison.setShowSimulationIndices(true);
comparison.setSortByUtility(true);
comparison.setShowUtilities(true);
comparison.setParallelized(true);
comparison.compareFromSimulations("comparison", simulations, algorithms, statistics, parameters);
}
Aggregations