use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class MNLRLikelihood method multipleRegression.
private double multipleRegression(TetradVector Y, TetradMatrix X) {
int n = X.rows();
TetradVector r;
try {
TetradMatrix Xt = X.transpose();
TetradMatrix XtX = Xt.times(X);
r = X.times(XtX.inverse().times(Xt.times(Y))).minus(Y);
} catch (Exception e) {
TetradVector ones = new TetradVector(n);
for (int i = 0; i < n; i++) ones.set(i, 1);
r = ones.scalarMult(ones.dotProduct(Y) / (double) n).minus(Y);
}
double sigma2 = r.dotProduct(r) / n;
if (sigma2 <= 0) {
TetradVector ones = new TetradVector(n);
for (int i = 0; i < n; i++) ones.set(i, 1);
r = ones.scalarMult(ones.dotProduct(Y) / (double) Math.max(n, 2)).minus(Y);
sigma2 = r.dotProduct(r) / n;
}
double lik = -(n / 2) * (Math.log(2 * Math.PI) + Math.log(sigma2) + 1);
if (Double.isInfinite(lik) || Double.isNaN(lik)) {
System.out.println(lik);
}
return lik;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemBicScore 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;
try {
double s2 = getCovariances().getValue(i, i);
int p = parents.length;
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 <= 0) {
if (isVerbose()) {
out.println("Nonpositive residual varianceY: resVar / varianceY = " + (s2 / getCovariances().getValue(i, i)));
}
return Double.NaN;
}
int n = getSampleSize();
return -(n) * log(s2) - getPenaltyDiscount() * log(n);
// + getStructurePrior(parents.length);// - getStructurePrior(parents.length + 1);
} catch (Exception e) {
boolean removedOne = true;
while (removedOne) {
List<Integer> _parents = new ArrayList<>();
for (int parent : parents) _parents.add(parent);
_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());
}
return Double.NaN;
}
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemBicScore method partialCorrelation.
private double partialCorrelation(Node x, Node y, List<Node> z) throws SingularMatrixException {
int[] indices = new int[z.size() + 2];
indices[0] = indexMap.get(x.getName());
indices[1] = indexMap.get(y.getName());
for (int i = 0; i < z.size(); i++) indices[i + 2] = indexMap.get(z.get(i).getName());
TetradMatrix submatrix = covariances.getSubmatrix(indices).getMatrix();
return StatUtils.partialCorrelation(submatrix);
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemBicScore 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 SemBicScoreImages3 method cov.
private TetradMatrix cov(DataSet x) {
TetradMatrix M = x.getDoubleData();
RealMatrix covarianceMatrix = new Covariance(M.getRealMatrix(), true).getCovarianceMatrix();
return new TetradMatrix(covarianceMatrix, covarianceMatrix.getRowDimension(), covarianceMatrix.getColumnDimension());
}
Aggregations