use of edu.cmu.tetrad.util.TetradVector in project tetrad by cmu-phil.
the class Ling method simulateReducedForm.
// check against model in which: A = ..... / (1 - xyzw)
private static TetradVector simulateReducedForm(TetradMatrix reducedForm, TetradVector errorCoefficients, Distribution distr) {
int n = reducedForm.rows();
TetradVector vector = new TetradVector(n);
TetradVector samples = new TetradVector(n);
for (int j = 0; j < n; j++) {
// sample from each noise term
double sample = distr.nextRandom();
double errorCoefficient = errorCoefficients.get(j);
samples.set(j, sample * errorCoefficient);
}
for (int i = 0; i < n; i++) {
// for each observed variable, i.e. dimension
double sum = 0;
for (int j = 0; j < n; j++) {
double coefficient = reducedForm.get(i, j);
double sample = samples.get(j);
sum += coefficient * sample;
}
vector.set(i, sum);
}
return vector;
}
use of edu.cmu.tetrad.util.TetradVector 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.TetradVector 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.TetradVector in project tetrad by cmu-phil.
the class MixedBicScore method getBicLinear.
private double getBicLinear(int i, int[] parents) {
double residualVariance = getCovariances().getValue(i, i);
int n = getSampleSize();
int p = parents.length;
TetradMatrix covxx = getSelection1(getCovariances(), parents);
try {
TetradMatrix covxxInv = covxx.inverse();
TetradVector covxy = getSelection2(getCovariances(), 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().getValue(i, i)));
}
return Double.NaN;
}
double c = getPenaltyDiscount();
return -n * Math.log(residualVariance) - c * (p + 1) * logn;
} 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());
}
return Double.NaN;
}
}
use of edu.cmu.tetrad.util.TetradVector 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;
}
Aggregations