use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemLikelihood2 method likelihoodJoint.
// The likelihood of the joint over all of these variables, assuming conditional Gaussian,
// continuous and discrete.
private Ret likelihoodJoint(List<ContinuousVariable> X) {
X = new ArrayList<>(X);
int k = X.size();
int[] cols = new int[k];
for (int j = 0; j < k; j++) cols[j] = nodesHash.get(X.get(j));
int N = covMatrix.getSampleSize();
TetradMatrix cov = covMatrix.getSelection(cols, cols);
double lnL = N * gaussianLikelihood(k, cov);
final int dof = h(X);
return new Ret(lnL, dof);
}
use of edu.cmu.tetrad.util.TetradMatrix 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.TetradMatrix in project tetrad by cmu-phil.
the class MixedBicScore 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 Mimbuild2 method search.
// =================================== PUBLIC METHODS =========================================//
public Graph search(List<List<Node>> clustering, List<String> latentNames, ICovarianceMatrix measuresCov) {
List<String> _latentNames = new ArrayList<>(latentNames);
List<String> allVarNames = new ArrayList<>();
for (List<Node> cluster : clustering) {
for (Node node : cluster) allVarNames.add(node.getName());
}
measuresCov = measuresCov.getSubmatrix(allVarNames);
List<List<Node>> _clustering = new ArrayList<>();
for (List<Node> cluster : clustering) {
List<Node> _cluster = new ArrayList<>();
for (Node node : cluster) {
_cluster.add(measuresCov.getVariable(node.getName()));
}
_clustering.add(_cluster);
}
List<Node> latents = defineLatents(_latentNames);
this.latents = latents;
// This removes the small clusters and their names.
removeSmallClusters(latents, _clustering, getMinClusterSize());
this.clustering = _clustering;
Node[][] indicators = new Node[latents.size()][];
for (int i = 0; i < latents.size(); i++) {
indicators[i] = new Node[_clustering.get(i).size()];
for (int j = 0; j < _clustering.get(i).size(); j++) {
indicators[i][j] = _clustering.get(i).get(j);
}
}
TetradMatrix cov = getCov(measuresCov, latents, indicators);
CovarianceMatrix latentscov = new CovarianceMatrix(latents, cov, measuresCov.getSampleSize());
this.latentsCov = latentscov;
Graph graph;
Cpc search = new Cpc(new IndTestFisherZ(latentscov, alpha));
search.setKnowledge(knowledge);
graph = search.search();
// try {
// Ges search = new Ges(latentscov);
// search.setDepErrorsAlpha(penaltyDiscount);
// search.setKnowledge(knowledge);
// graph = search.search();
// } catch (Exception e) {
// // e.printStackTrace();
// CPC search = new CPC(new IndTestFisherZ(latentscov, alpha));
// search.setKnowledge(knowledge);
// graph = search.search();
// }
this.structureGraph = new EdgeListGraph(graph);
GraphUtils.fruchtermanReingoldLayout(this.structureGraph);
return this.structureGraph;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class MimbuildTrek method search.
// =================================== PUBLIC METHODS =========================================//
public Graph search(List<List<Node>> clustering, List<String> latentNames, ICovarianceMatrix measuresCov) {
List<String> _latentNames = new ArrayList<>(latentNames);
List<String> allVarNames = new ArrayList<>();
for (List<Node> cluster : clustering) {
for (Node node : cluster) allVarNames.add(node.getName());
}
measuresCov = measuresCov.getSubmatrix(allVarNames);
List<List<Node>> _clustering = new ArrayList<>();
for (List<Node> cluster : clustering) {
List<Node> _cluster = new ArrayList<>();
for (Node node : cluster) {
_cluster.add(measuresCov.getVariable(node.getName()));
}
_clustering.add(_cluster);
}
List<Node> latents = defineLatents(_latentNames);
this.latents = latents;
// This removes the small clusters and their names.
removeSmallClusters(latents, _clustering, getMinClusterSize());
this.clustering = _clustering;
Node[][] indicators = new Node[latents.size()][];
for (int i = 0; i < latents.size(); i++) {
indicators[i] = new Node[_clustering.get(i).size()];
for (int j = 0; j < _clustering.get(i).size(); j++) {
indicators[i][j] = _clustering.get(i).get(j);
}
}
TetradMatrix cov = getCov(measuresCov, latents, indicators);
CovarianceMatrix latentscov = new CovarianceMatrix(latents, cov, measuresCov.getSampleSize());
this.latentsCov = latentscov;
Graph graph;
Cpc search = new Cpc(new IndTestTrekSep(measuresCov, alpha, clustering, latents));
search.setKnowledge(knowledge);
graph = search.search();
// try {
// Ges search = new Ges(latentscov);
// search.setCorrErrorsAlpha(penaltyDiscount);
// search.setKnowledge(knowledge);
// graph = search.search();
// } catch (Exception e) {
// // e.printStackTrace();
// CPC search = new CPC(new IndTestFisherZ(latentscov, alpha));
// search.setKnowledge(knowledge);
// graph = search.search();
// }
this.structureGraph = new EdgeListGraph(graph);
GraphUtils.fruchtermanReingoldLayout(this.structureGraph);
return this.structureGraph;
}
Aggregations