use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class IndTestMultinomialLogisticRegression method expandVariable.
private List<Node> expandVariable(DataSet dataSet, Node node) {
if (node instanceof ContinuousVariable) {
return Collections.singletonList(node);
}
if (node instanceof DiscreteVariable && ((DiscreteVariable) node).getNumCategories() < 3) {
return Collections.singletonList(node);
}
if (!(node instanceof DiscreteVariable)) {
throw new IllegalArgumentException();
}
List<String> varCats = new ArrayList<>(((DiscreteVariable) node).getCategories());
varCats.remove(0);
List<Node> variables = new ArrayList<>();
for (String cat : varCats) {
Node newVar;
do {
String newVarName = node.getName() + "MULTINOM" + "." + cat;
newVar = new DiscreteVariable(newVarName, 2);
} while (dataSet.getVariable(newVar.getName()) != null);
variables.add(newVar);
dataSet.addVariable(newVar);
int newVarIndex = dataSet.getColumn(newVar);
int numCases = dataSet.getNumRows();
for (int l = 0; l < numCases; l++) {
Object dataCell = dataSet.getObject(l, dataSet.getColumn(node));
int dataCellIndex = ((DiscreteVariable) node).getIndex(dataCell.toString());
if (dataCellIndex == ((DiscreteVariable) node).getIndex(cat))
dataSet.setInt(l, newVarIndex, 1);
else
dataSet.setInt(l, newVarIndex, 0);
}
}
return variables;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class IndTestRegressionAD method determines.
// ==========================PRIVATE METHODS============================//
// /**
// * Return the p-value of the last calculated independence fact.
// *
// * @return this p-value. When accessed through the IndependenceChecker
// * interface, this p-value should only be considered to be a
// * relative strength.
// */
// private double getRelativeStrength() {
//
// // precondition: pdf is the most recently used partial
// // correlation distribution function, and storedR is the most
// // recently calculated partial correlation.
// return 2.0 * Integrator.getArea(npdf, Math.abs(storedR), 9.0, 100);
// }
// /**
// * Computes that value x such that P(abs(N(0,1) > x) < alpha. Note that
// * this is a two sided test of the null hypothesis that the Fisher's Z
// * value, which is distributed as N(0,1) is not equal to 0.0.
// */
// private double cutoffGaussian() {
// npdf = new NormalPdf();
// final double upperBound = 9.0;
// final double delta = 0.001;
// // double alpha = this.alpha/2.0; //Two sided test
// return CutoffFinder.getCutoff(npdf, upperBound, alpha, delta);
// }
// private int sampleSize() {
// return data.rows();
// }
public boolean determines(List<Node> zList, Node xVar) {
if (zList == null) {
throw new NullPointerException();
}
for (Node node : zList) {
if (node == null) {
throw new NullPointerException();
}
}
int size = zList.size();
int[] zCols = new int[size];
int xIndex = getVariables().indexOf(xVar);
for (int i = 0; i < zList.size(); i++) {
zCols[i] = getVariables().indexOf(zList.get(i));
}
int[] zRows = new int[data.rows()];
for (int i = 0; i < data.rows(); i++) {
zRows[i] = i;
}
DoubleMatrix2D Z = data.viewSelection(zRows, zCols);
DoubleMatrix1D x = data.viewColumn(xIndex);
DoubleMatrix2D Zt = new Algebra().transpose(Z);
DoubleMatrix2D ZtZ = new Algebra().mult(Zt, Z);
DoubleMatrix2D G = new DenseDoubleMatrix2D(new TetradMatrix(ZtZ.toArray()).inverse().toArray());
// Bug in Colt? Need to make a copy before multiplying to avoid
// a ClassCastException.
DoubleMatrix2D Zt2 = Zt.like();
Zt2.assign(Zt);
DoubleMatrix2D GZt = new Algebra().mult(G, Zt2);
DoubleMatrix1D b_x = new Algebra().mult(GZt, x);
DoubleMatrix1D xPred = new Algebra().mult(Z, b_x);
DoubleMatrix1D xRes = xPred.copy().assign(x, Functions.minus);
double SSE = xRes.aggregate(Functions.plus, Functions.square);
boolean determined = SSE < 0.0001;
if (determined) {
StringBuilder sb = new StringBuilder();
sb.append("Determination found: ").append(xVar).append(" is determined by {");
for (int i = 0; i < zList.size(); i++) {
sb.append(zList.get(i));
if (i < zList.size() - 1) {
sb.append(", ");
}
}
sb.append("}");
TetradLogger.getInstance().log("independencies", sb.toString());
}
return determined;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class IndTestSepset method getVariableNames.
/**
* @return the list of variable varNames.
*/
public List<String> getVariableNames() {
List<Node> nodes = getVariables();
List<String> nodeNames = new ArrayList<>();
for (Node var : nodes) {
nodeNames.add(var.getName());
}
return nodeNames;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class FciOrientT method rulesR1R2cycle.
// Does all 3 of these rules at once instead of going through all
// triples multiple times per iteration of doFinalOrientation.
public void rulesR1R2cycle(Graph graph) {
List<Node> nodes = graph.getNodes();
for (Node B : nodes) {
List<Node> adj = graph.getAdjacentNodes(B);
if (adj.size() < 2) {
continue;
}
ChoiceGenerator cg = new ChoiceGenerator(adj.size(), 2);
int[] combination;
while ((combination = cg.next()) != null) {
Node A = adj.get(combination[0]);
Node C = adj.get(combination[1]);
// choice gen doesnt do diff orders, so must switch A & C around.
ruleR1(A, B, C, graph);
ruleR1(C, B, A, graph);
ruleR2(A, B, C, graph);
ruleR2(C, B, A, graph);
}
}
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class FciOrientT method ruleR6R7.
/**
* Implements Zhang's rules R6 and R7, applies them over the graph once. Orient single tails. R6: If A---Bo-*C then
* A---B--*C. R7: If A--oBo-*C and A,C nonadjacent, then A--oB--*C
*/
public void ruleR6R7(Graph graph) {
List<Node> nodes = graph.getNodes();
for (Node b : nodes) {
List<Node> adjacents = graph.getAdjacentNodes(b);
if (adjacents.size() < 2)
continue;
ChoiceGenerator cg = new ChoiceGenerator(adjacents.size(), 2);
for (int[] choice = cg.next(); choice != null; choice = cg.next()) {
Node a = adjacents.get(choice[0]);
Node c = adjacents.get(choice[1]);
if (graph.isAdjacentTo(a, c))
continue;
if (!(graph.getEndpoint(b, a) == Endpoint.TAIL))
continue;
if (!(graph.getEndpoint(c, b) == Endpoint.CIRCLE))
continue;
if (graph.getEndpoint(a, b) == Endpoint.TAIL) {
// We know A---Bo-*C: R6 applies!
graph.setEndpoint(c, b, Endpoint.TAIL);
logger.log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Single tails (tail)", graph.getEdge(c, b)));
changeFlag = true;
}
if (graph.getEndpoint(a, b) == Endpoint.CIRCLE) {
// if (graph.isAdjacentTo(a, c)) continue;
logger.log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Single tails (tail)", graph.getEdge(c, b)));
// We know A--oBo-*C and A,C nonadjacent: R7 applies!
graph.setEndpoint(c, b, Endpoint.TAIL);
changeFlag = true;
}
}
}
}
Aggregations