use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class GraphScore method aBetterScore.
private double aBetterScore(int x, int y, int[] z) {
Node _y = variables.get(y);
Node _x = variables.get(x);
List<Node> _z = getVariableList(z);
boolean dsep = dag.isDSeparatedFrom(_x, _y, _z);
int count = 0;
if (!dsep)
count++;
for (Node z0 : _z) {
if (dag.isDSeparatedFrom(_x, z0, _z)) {
count += 1;
}
}
double score = dsep ? -1 - count : 1 + count;
// if (score == 1) score -= Math.tanh(z.length);
return score;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class IndTestMNLRLRT 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 IndTestMixedMultipleTTest method multiLL.
private double multiLL(DoubleMatrix2D coeffs, Node dep, List<Node> indep) {
DoubleMatrix2D indepData = factory2D.make(internalData.subsetColumns(indep).getDoubleData().toArray());
List<Node> depList = new ArrayList<>();
depList.add(dep);
DoubleMatrix2D depData = factory2D.make(internalData.subsetColumns(depList).getDoubleData().toArray());
int N = indepData.rows();
DoubleMatrix2D probs = Algebra.DEFAULT.mult(factory2D.appendColumns(factory2D.make(N, 1, 1.0), indepData), coeffs);
probs = factory2D.appendColumns(factory2D.make(indepData.rows(), 1, 1.0), probs).assign(Functions.exp);
double ll = 0;
for (int i = 0; i < N; i++) {
DoubleMatrix1D curRow = probs.viewRow(i);
curRow.assign(Functions.div(curRow.zSum()));
ll += Math.log(curRow.get((int) depData.get(i, 0)));
}
return ll;
}
use of edu.cmu.tetrad.graph.Node in project tetrad by cmu-phil.
the class IndTestMultinomialLogisticRegression 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 IndTestMultinomialLogisticRegression method isIndependentMultinomialLogisticRegression.
private boolean isIndependentMultinomialLogisticRegression(Node x, Node y, List<Node> z) {
if (!variablesPerNode.containsKey(x)) {
throw new IllegalArgumentException("Unrecogized node: " + x);
}
if (!variablesPerNode.containsKey(y)) {
throw new IllegalArgumentException("Unrecogized node: " + y);
}
for (Node node : z) {
if (!variablesPerNode.containsKey(x)) {
throw new IllegalArgumentException("Unrecogized node: " + node);
}
}
List<Double> pValues = new ArrayList<>();
int[] _rows = getNonMissingRows(x, y, z);
logisticRegression.setRows(_rows);
for (Node _x : variablesPerNode.get(x)) {
// Without y
List<Node> regressors0 = new ArrayList<>();
for (Node _z : z) {
regressors0.addAll(variablesPerNode.get(_z));
}
LogisticRegression.Result result0 = logisticRegression.regress((DiscreteVariable) _x, regressors0);
// With y.
List<Node> regressors1 = new ArrayList<>();
regressors1.addAll(variablesPerNode.get(y));
for (Node _z : z) {
regressors1.addAll(variablesPerNode.get(_z));
}
LogisticRegression.Result result1 = logisticRegression.regress((DiscreteVariable) _x, regressors1);
// Returns -2 LL
double ll0 = result0.getLogLikelihood();
double ll1 = result1.getLogLikelihood();
double chisq = (ll0 - ll1);
int df = variablesPerNode.get(y).size();
double p = 1.0 - new ChiSquaredDistribution(df).cumulativeProbability(chisq);
pValues.add(p);
}
double p = 1.0;
// This is only one method that can be used, this requires every coefficient to be significant
for (double val : pValues) {
if (val < p)
p = val;
}
boolean indep = p > alpha;
this.lastP = p;
if (verbose) {
if (indep) {
TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(x, y, z, p));
} else {
TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(x, y, z, p));
}
}
return indep;
}
Aggregations