use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class Ling method makeDataSet.
private void makeDataSet(GraphWithParameters graphWP) {
// define the "Gaussian-squared" distribution
Distribution gp2 = new GaussianPower(2);
// the coefficients of the error terms (here, all 1s)
TetradVector errorCoefficients = getErrorCoeffsIdentity(graphWP.getGraph().getNumNodes());
// generate data from the SEM
TetradMatrix inVectors = simulateCyclic(graphWP, errorCoefficients, numSamples, gp2);
// reformat it
dataSet = ColtDataSet.makeContinuousData(graphWP.getGraph().getNodes(), new TetradMatrix(inVectors.transpose().toArray()));
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class Ling method findCandidateModels.
// given the W matrix, outputs the list of SEMs consistent with the observed distribution.
private StoredGraphs findCandidateModels(List<Node> variables, TetradMatrix matrixW, boolean approximateZeros) {
TetradMatrix normalizedZldW;
List<PermutationMatrixPair> zldPerms;
StoredGraphs gs = new StoredGraphs();
System.out.println("Calculating zeroless diagonal permutations...");
TetradLogger.getInstance().log("lingDetails", "Calculating zeroless diagonal permutations.");
zldPerms = zerolessDiagonalPermutations(matrixW, approximateZeros, variables, dataSet);
System.out.println("Calculated zeroless diagonal permutations.");
// for each W~, compute a candidate B, and score it
for (PermutationMatrixPair zldPerm : zldPerms) {
TetradLogger.getInstance().log("lingDetails", "" + zldPerm);
System.out.println(zldPerm);
normalizedZldW = LingUtils.normalizeDiagonal(zldPerm.getMatrixW());
// Note: add method to deal with this data
// B~ = I - W~
zldPerm.setMatrixBhat(computeBhatTetradMatrix(normalizedZldW, variables));
TetradMatrix doubleData = zldPerm.getMatrixBhat().getDoubleData();
boolean isStableTetradMatrix = allEigenvaluesAreSmallerThanOneInModulus(new TetradMatrix(doubleData.toArray()));
GraphWithParameters graph = new GraphWithParameters(zldPerm.getMatrixBhat());
gs.addGraph(graph.getGraph());
gs.addStable(isStableTetradMatrix);
gs.addData(zldPerm.getMatrixBhat());
}
TetradLogger.getInstance().log("stableGraphs", "Stable Graphs:");
for (int d = 0; d < gs.getNumGraphs(); d++) {
if (!gs.isStable(d)) {
continue;
}
TetradLogger.getInstance().log("stableGraphs", "" + gs.getGraph(d));
if (TetradLogger.getInstance().getLoggerConfig() != null && TetradLogger.getInstance().getLoggerConfig().isEventActive("stableGraphs")) {
TetradLogger.getInstance().log("wMatrices", "" + gs.getData(d));
}
}
TetradLogger.getInstance().log("unstableGraphs", "Unstable Graphs:");
for (int d = 0; d < gs.getNumGraphs(); d++) {
if (gs.isStable(d)) {
continue;
}
TetradLogger.getInstance().log("unstableGraphs", "" + gs.getGraph(d));
if (TetradLogger.getInstance().getLoggerConfig() != null && TetradLogger.getInstance().getLoggerConfig().isEventActive("unstableGraphs")) {
TetradLogger.getInstance().log("wMatrices", "" + gs.getData(d));
}
}
return gs;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class Ling method computeBhatTetradMatrix.
// B^ = I - W~'
private static DataSet computeBhatTetradMatrix(TetradMatrix normalizedZldW, List<Node> nodes) {
// , List<Integer> perm) {
int size = normalizedZldW.rows();
TetradMatrix mat = TetradMatrix.identity(size).minus(normalizedZldW);
return ColtDataSet.makeContinuousData(nodes, new TetradMatrix(mat.toArray()));
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class Ling method getWFastIca.
private TetradMatrix getWFastIca() {
TetradMatrix A;
// Using this Fast ICA to get the logging.
TetradMatrix W;
TetradMatrix data = new TetradMatrix(dataSet.getDoubleData().toArray());
FastIca fastIca = new FastIca(data, data.columns());
fastIca.setVerbose(false);
fastIca.setAlgorithmType(FastIca.DEFLATION);
fastIca.setFunction(FastIca.LOGCOSH);
fastIca.setTolerance(1e-20);
fastIca.setMaxIterations(500);
fastIca.setAlpha(1.0);
FastIca.IcaResult result = fastIca.findComponents();
A = new TetradMatrix(result.getA().transpose().toArray());
W = A.inverse();
return W;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class Ling method zerolessDiagonalPermutation.
private List<PermutationMatrixPair> zerolessDiagonalPermutation(TetradMatrix ica_W, boolean approximateZeros, List<Node> vars, DataSet dataSet) {
List<PermutationMatrixPair> permutations = new Vector<>();
if (approximateZeros) {
// setInsignificantEntriesToZero(ica_W);
ica_W = pruneEdgesByResampling(ica_W);
ica_W = removeZeroRowsAndCols(ica_W, vars);
}
// List<PermutationMatrixPair > zldPerms = new ArrayList<PermutationMatrixPair >();
List<Integer> perm = new ArrayList<>();
for (int i = 0; i < vars.size(); i++) perm.add(i);
TetradMatrix matrixW = ica_W.transpose();
PermutationMatrixPair pair = new PermutationMatrixPair(perm, matrixW);
permutations.add(pair);
return permutations;
}
Aggregations