use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemBicScoreDeterministic method printMinimalLinearlyDependentSet.
// Prints a smallest subset of parents that causes a singular matrix exception.
private boolean printMinimalLinearlyDependentSet(int[] parents, ICovarianceMatrix cov) {
List<Node> _parents = new ArrayList<>();
for (int p : parents) _parents.add(variables.get(p));
DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
int[] choice;
while ((choice = gen.next()) != null) {
int[] sel = new int[choice.length];
List<Node> _sel = new ArrayList<>();
for (int m = 0; m < choice.length; m++) {
sel[m] = parents[m];
_sel.add(variables.get(sel[m]));
}
TetradMatrix m = cov.getSelection(sel, sel);
try {
m.inverse();
} catch (Exception e2) {
forbidden.add(sel[0]);
out.println("### Linear dependence among variables: " + _sel);
out.println("### Removing " + _sel.get(0));
return true;
}
}
return false;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemBicScoreDeterministic method determines.
@Override
public boolean determines(List<Node> z, Node y) {
int i = variables.indexOf(y);
int[] parents = new int[z.size()];
for (int t = 0; t < z.size(); t++) {
parents[t] = variables.indexOf(z.get(t));
}
double small = getDeterminismThreshold();
try {
double s2 = getCovariances().getValue(i, i);
TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
if (s2 <= small) {
printDeterminism(i, parents);
return true;
}
} catch (Exception e) {
printDeterminism(i, parents);
}
return false;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemBicScoreDeterministic method getMaximalLinearlyDependentSet.
private int[] getMaximalLinearlyDependentSet(int i, int[] parents, ICovarianceMatrix cov) {
double small = getDeterminismThreshold();
List<Node> _parents = new ArrayList<>();
for (int p : parents) _parents.add(variables.get(p));
DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
int[] choice;
while ((choice = gen.next()) != null) {
int[] sel0 = new int[choice.length];
List<Integer> all = new ArrayList<>();
for (int w = 0; w < parents.length; w++) all.add(parents[w]);
for (int w = 0; w < sel0.length; w++) all.remove(sel0[w]);
int[] sel = new int[all.size()];
for (int w = 0; w < all.size(); w++) sel[w] = all.get(w);
List<Node> _sel = new ArrayList<>();
for (int m = 0; m < choice.length; m++) {
sel[m] = parents[m];
_sel.add(variables.get(sel[m]));
}
TetradMatrix m = cov.getSelection(sel, sel);
double s2 = getCovariances().getValue(i, i);
TetradMatrix covxx = getSelection(getCovariances(), parents, parents);
TetradVector covxy = getSelection(getCovariances(), parents, new int[] { i }).getColumn(0);
s2 -= covxx.inverse().times(covxy).dotProduct(covxy);
if (s2 <= small) {
out.println("### Linear dependence among variables: " + _sel);
out.println("### Removing " + _sel.get(0));
return sel;
}
try {
m.inverse();
} catch (Exception e2) {
// forbidden.add(sel[0]);
out.println("### Linear dependence among variables: " + _sel);
out.println("### Removing " + _sel.get(0));
return sel;
}
}
return new int[0];
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemBicScoreImages2 method printMinimalLinearlyDependentSet.
// Prints a smallest subset of parents that causes a singular matrix exception.
private boolean printMinimalLinearlyDependentSet(int[] parents, ICovarianceMatrix cov) {
List<Node> _parents = new ArrayList<>();
for (int p : parents) _parents.add(variables.get(p));
DepthChoiceGenerator gen = new DepthChoiceGenerator(_parents.size(), _parents.size());
int[] choice;
while ((choice = gen.next()) != null) {
int[] sel = new int[choice.length];
List<Node> _sel = new ArrayList<>();
for (int m = 0; m < choice.length; m++) {
sel[m] = parents[m];
_sel.add(variables.get(sel[m]));
}
TetradMatrix m = cov.getSelection(sel, sel);
try {
m.inverse();
} catch (Exception e2) {
forbidden.add(sel[0]);
out.println("### Linear dependence among variables: " + _sel);
out.println("### Removing " + _sel.get(0));
return true;
}
}
return false;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemBicScoreImages2 method localScore.
/**
* Calculates the sample likelihood and BIC score for i given its parents in a simple SEM model
*/
public double localScore(int i, int... parents) {
for (int p : parents) if (forbidden.contains(p))
return Double.NaN;
double lik = 0.0;
for (int k = 0; k < covariances.size(); k++) {
double residualVariance = getCovariances(k).getValue(i, i);
TetradMatrix covxx = getSelection1(getCovariances(k), parents);
try {
TetradMatrix covxxInv = covxx.inverse();
TetradVector covxy = getSelection2(getCovariances(k), parents, i);
TetradVector b = covxxInv.times(covxy);
residualVariance -= covxy.dotProduct(b);
if (residualVariance <= 0) {
if (isVerbose()) {
out.println("Nonpositive residual varianceY: resVar / varianceY = " + (residualVariance / getCovariances(k).getValue(i, i)));
}
return Double.NaN;
}
int cols = getCovariances(0).getDimension();
double q = 2 / (double) cols;
lik += -sampleSize * Math.log(residualVariance);
} catch (Exception e) {
boolean removedOne = true;
while (removedOne) {
List<Integer> _parents = new ArrayList<>();
for (int y = 0; y < parents.length; y++) _parents.add(parents[y]);
_parents.removeAll(forbidden);
parents = new int[_parents.size()];
for (int y = 0; y < _parents.size(); y++) parents[y] = _parents.get(y);
removedOne = printMinimalLinearlyDependentSet(parents, getCovariances(k));
}
return Double.NaN;
}
}
int p = parents.length;
double c = getPenaltyDiscount();
return 2 * lik - c * (p + 1) * Math.log(covariances.size() * sampleSize);
}
Aggregations