use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class IndTestFisherZD method indTestSubset.
// ==========================PUBLIC METHODS=============================//
/**
* Creates a new independence test instance for a subset of the variables.
*/
public IndependenceTest indTestSubset(List<Node> vars) {
if (vars.isEmpty()) {
throw new IllegalArgumentException("Subset may not be empty.");
}
for (Node var : vars) {
if (!variables.contains(var)) {
throw new IllegalArgumentException("All vars must be original vars");
}
}
int[] indices = new int[vars.size()];
for (int i = 0; i < indices.length; i++) {
indices[i] = indexMap.get(vars.get(i));
}
ICovarianceMatrix newCovMatrix = covMatrix.getSubmatrix(indices);
double alphaNew = getAlpha();
return new IndTestFisherZD(newCovMatrix, alphaNew);
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class IndTestFisherZD method getVariableNames.
/**
* @return the list of variable varNames.
*/
public List<String> getVariableNames() {
List<Node> variables = getVariables();
List<String> variableNames = new ArrayList<>();
for (Node variable1 : variables) {
variableNames.add(variable1.getName());
}
return variableNames;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class IndTestFisherZGeneralizedInverse method determines.
public boolean determines(List<Node> zList, Node xVar) {
if (zList == null) {
throw new NullPointerException();
}
if (zList.isEmpty()) {
return false;
}
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);
TetradMatrix _ZtZ = new TetradMatrix(ZtZ.toArray());
TetradMatrix ginverse = _ZtZ.inverse();
DoubleMatrix2D G = new DenseDoubleMatrix2D(ginverse.toArray());
// DoubleMatrix2D G = MatrixUtils.ginverse(ZtZ);
DoubleMatrix2D Zt2 = Zt.copy();
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);
double variance = SSE / (data.rows() - (zList.size() + 1));
// ChiSquare chiSquare = new ChiSquare(data.rows(),
// PersistentRandomUtil.getInstance().getEngine());
//
// double p = chiSquare.cdf(sum);
// boolean determined = p < 1 - getAlternativePenalty();
//
boolean determined = variance < getAlpha();
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("}");
// sb.append(" p = ").append(nf.format(p));
sb.append(" SSE = ").append(nf.format(SSE));
TetradLogger.getInstance().log("independencies", sb.toString());
System.out.println(sb);
}
return determined;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class GrowShrink method findMb.
/**
* Finds the Markov blanket of the given target.
*
* @param targetName the name of the target
* @return the list of node in the Markov blanket.
*/
public List<Node> findMb(String targetName) {
Node target = getVariableForName(targetName);
List<Node> blanket = new LinkedList<>();
boolean changed = true;
while (changed) {
changed = false;
List<Node> remaining = new LinkedList<>(variables);
remaining.removeAll(blanket);
remaining.remove(target);
for (Node node : remaining) {
if (!independenceTest.isIndependent(node, target, blanket)) {
blanket.add(node);
changed = true;
}
}
}
changed = true;
while (changed) {
changed = false;
for (Node node : new LinkedList<>(blanket)) {
blanket.remove(node);
if (independenceTest.isIndependent(node, target, blanket)) {
changed = true;
continue;
}
blanket.add(node);
}
}
return blanket;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class IndTestRegression method isIndependent.
/**
* Determines whether variable x is independent of variable y given a list of conditioning variables z.
*
* @param xVar the one variable being compared.
* @param yVar the second variable being compared.
* @param zList the list of conditioning variables.
* @return true iff x _||_ y | z.
* @throws RuntimeException if a matrix singularity is encountered.
*/
public boolean isIndependent(Node xVar, Node yVar, List<Node> zList) {
if (zList == null) {
throw new NullPointerException();
}
for (Node node : zList) {
if (node == null) {
throw new NullPointerException();
}
}
List<Node> regressors = new ArrayList<>();
regressors.add(dataSet.getVariable(yVar.getName()));
for (Node zVar : zList) {
regressors.add(dataSet.getVariable(zVar.getName()));
}
Regression regression = new RegressionDataset(dataSet);
RegressionResult result = null;
try {
result = regression.regress(xVar, regressors);
} catch (Exception e) {
return false;
}
double p = result.getP()[1];
boolean independent = p > alpha;
if (verbose) {
if (independent) {
TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(xVar, yVar, zList, p));
} else {
TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(xVar, yVar, zList, p));
}
}
return independent;
}
Aggregations