use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class TestMatrixUtils method testImpiedCovar.
@Test
public void testImpiedCovar() {
List<Node> nodes = new ArrayList<>();
for (int i = 0; i < 10; i++) {
nodes.add(new ContinuousVariable("X" + (i + 1)));
}
Graph graph = new Dag(GraphUtils.randomGraph(nodes, 0, 10, 30, 15, 15, false));
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
TetradMatrix err = im.getErrCovar();
TetradMatrix coef = im.getEdgeCoef();
TetradMatrix implied = MatrixUtils.impliedCovar(coef, err);
assertTrue(MatrixUtils.isPositiveDefinite(implied));
TetradMatrix corr = MatrixUtils.convertCovToCorr(new TetradMatrix(implied));
assertTrue(MatrixUtils.isPositiveDefinite(corr));
}
use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class TestMimbuild2 method test1.
@Test
public void test1() {
RandomUtil.getInstance().setSeed(49283494L);
for (int r = 0; r < 1; r++) {
Graph mim = DataGraphUtils.randomSingleFactorModel(5, 5, 6, 0, 0, 0);
Graph mimStructure = structure(mim);
Parameters params = new Parameters();
params.set("coefLow", .5);
params.set("coefHigh", 1.5);
SemPm pm = new SemPm(mim);
SemIm im = new SemIm(pm, params);
DataSet data = im.simulateData(300, false);
String algorithm = "FOFC";
Graph searchGraph;
List<List<Node>> partition;
if (algorithm.equals("FOFC")) {
FindOneFactorClusters fofc = new FindOneFactorClusters(data, TestType.TETRAD_WISHART, FindOneFactorClusters.Algorithm.GAP, 0.001);
searchGraph = fofc.search();
partition = fofc.getClusters();
} else if (algorithm.equals("BPC")) {
TestType testType = TestType.TETRAD_WISHART;
TestType purifyType = TestType.TETRAD_BASED;
BuildPureClusters bpc = new BuildPureClusters(data, 0.001, testType, purifyType);
searchGraph = bpc.search();
partition = MimUtils.convertToClusters2(searchGraph);
} else {
throw new IllegalStateException();
}
List<String> latentVarList = reidentifyVariables(mim, data, partition, 2);
// System.out.println(partition);
// System.out.println(latentVarList);
//
// System.out.println("True\n" + mimStructure);
Graph mimbuildStructure;
for (int mimbuildMethod : new int[] { 2 }) {
if (mimbuildMethod == 2) {
Mimbuild2 mimbuild = new Mimbuild2();
mimbuild.setAlpha(0.001);
mimbuild.setMinClusterSize(3);
mimbuildStructure = mimbuild.search(partition, latentVarList, new CovarianceMatrix(data));
int shd = SearchGraphUtils.structuralHammingDistance(mimStructure, mimbuildStructure);
assertEquals(7, shd);
} else if (mimbuildMethod == 3) {
// System.out.println("Mimbuild Trek\n");
MimbuildTrek mimbuild = new MimbuildTrek();
mimbuild.setAlpha(0.1);
mimbuild.setMinClusterSize(3);
mimbuildStructure = mimbuild.search(partition, latentVarList, new CovarianceMatrix(data));
// ICovarianceMatrix latentcov = mimbuild.getLatentsCov();
// System.out.println("\nCovariance over the latents");
// System.out.println(latentcov);
// System.out.println("Estimated\n" + mimbuildStructure);
int shd = SearchGraphUtils.structuralHammingDistance(mimStructure, mimbuildStructure);
// System.out.println("SHD = " + shd);
// System.out.println();
assertEquals(3, shd);
} else {
throw new IllegalStateException();
}
}
}
}
use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class SemPmWrapper method setSemPm.
private void setSemPm(SemPm oldSemPm) {
try {
SemPm pm = (SemPm) new MarshalledObject(oldSemPm).get();
setSemPm(pm);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class PurifyScoreBased method gaussianEM.
private double gaussianEM(SemGraph semdag, SemIm initialSemIm) {
double score, newScore = -Double.MAX_VALUE, bestScore = -Double.MAX_VALUE;
SemPm semPm = new SemPm(semdag);
semdag.setShowErrorTerms(true);
for (int p = 0; p < numObserved; p++) {
for (int q = 0; q < numObserved; q++) {
this.bestCyy[p][q] = this.Cyy[p][q];
}
if (this.Cyz != null) {
for (int q = 0; q < numLatent; q++) {
this.bestCyz[p][q] = this.Cyz[p][q];
}
}
}
if (this.Czz != null) {
for (int p = 0; p < numLatent; p++) {
for (int q = 0; q < numLatent; q++) {
this.bestCzz[p][q] = this.Czz[p][q];
}
}
}
initializeGaussianEM(semdag);
for (int i = 0; i < 3; i++) {
System.out.println("--Trial " + i);
SemIm semIm;
if (i == 0 && initialSemIm != null) {
semIm = initialSemIm;
} else {
semIm = new SemIm(semPm);
semIm.setCovMatrix(this.covarianceMatrix);
}
do {
score = newScore;
gaussianExpectation(semIm);
newScore = gaussianMaximization(semIm);
if (newScore == -Double.MAX_VALUE) {
break;
}
} while (Math.abs(score - newScore) > 1.E-3);
System.out.println(newScore);
if (newScore > bestScore && !Double.isInfinite(newScore)) {
bestScore = newScore;
for (int p = 0; p < numObserved; p++) {
for (int q = 0; q < numObserved; q++) {
this.bestCyy[p][q] = this.Cyy[p][q];
}
for (int q = 0; q < numLatent; q++) {
this.bestCyz[p][q] = this.Cyz[p][q];
}
}
for (int p = 0; p < numLatent; p++) {
for (int q = 0; q < numLatent; q++) {
this.bestCzz[p][q] = this.Czz[p][q];
}
}
}
}
for (int p = 0; p < numObserved; p++) {
for (int q = 0; q < numObserved; q++) {
this.Cyy[p][q] = this.bestCyy[p][q];
}
for (int q = 0; q < numLatent; q++) {
this.Cyz[p][q] = this.bestCyz[p][q];
}
}
for (int p = 0; p < numLatent; p++) {
for (int q = 0; q < numLatent; q++) {
this.Czz[p][q] = this.bestCzz[p][q];
}
}
if (Double.isInfinite(bestScore)) {
System.out.println("* * Warning: Heywood case in this step");
return -Double.MAX_VALUE;
}
// System.exit(0);
return bestScore;
}
use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class PurifyScoreBased method scoreCandidate.
private double scoreCandidate() {
SemGraph graph = updatedGraph();
initializeGaussianEM(graph);
SemPm semPm = new SemPm(graph);
SemIm semIm = new SemIm(semPm, covarianceMatrix);
gaussianMaximization(semIm);
try {
System.out.println("trunk ll = " + semIm.getTruncLL());
return -semIm.getTruncLL() - 0.5 * semIm.getNumFreeParams() * Math.log(covarianceMatrix.getSampleSize());
} catch (IllegalArgumentException e) {
return -Double.MAX_VALUE;
}
}
Aggregations