use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class PcStableLocalSearchEditor method reportIfContinuous.
private String reportIfContinuous(Graph dag, DataSet dataSet) {
SemPm semPm = new SemPm(dag);
SemEstimator estimator = new SemEstimator(dataSet, semPm);
estimator.estimate();
SemIm semIm = estimator.getEstimatedSem();
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(4);
StringBuilder buf = new StringBuilder();
buf.append("\nDegrees of Freedom = ").append(semPm.getDof()).append("Chi-Square = ").append(nf.format(semIm.getChiSquare())).append("\nP Value = ").append(nf.format(semIm.getPValue())).append("\nBIC Score = ").append(nf.format(semIm.getBicScore()));
buf.append("\n\nThe above chi square test assumes that the maximum " + "likelihood function over the measured variables has been " + "maximized. Under that assumption, the null hypothesis for " + "the test is that the population covariance matrix over all " + "of the measured variables is equal to the estimated covariance " + "matrix over all of the measured variables written as a function " + "of the free model parameters--that is, the unfixed parameters " + "for each directed edge (the linear coefficient for that edge), " + "each exogenous variable (the variance for the error term for " + "that variable), and each bidirected edge (the covariance for " + "the exogenous variables it connects). The model is explained " + "in Bollen, Structural Equations with Latent Variable, 110. ");
return buf.toString();
}
use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class PcGesSearchEditor method reportIfCovMatrix.
private String reportIfCovMatrix(Graph dag, ICovarianceMatrix dataSet) {
SemPm semPm = new SemPm(dag);
SemEstimator estimator = new SemEstimator(dataSet, semPm);
estimator.estimate();
SemIm semIm = estimator.getEstimatedSem();
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(4);
StringBuilder buf = new StringBuilder();
buf.append("\nDegrees of Freedom = ").append(semPm.getDof()).append(" Chi-Square = ").append(nf.format(semIm.getChiSquare())).append("\nP Value = ").append(nf.format(semIm.getPValue())).append("\nBIC Score = ").append(nf.format(semIm.getBicScore()));
buf.append("\n\nThe above chi square test assumes that the maximum " + "likelihood function over the measured variables has been " + "maximized. Under that assumption, the null hypothesis for " + "the test is that the population covariance matrix over all " + "of the measured variables is equal to the estimated covariance " + "matrix over all of the measured variables written as a function " + "of the free model parameters--that is, the unfixed parameters " + "for each directed edge (the linear coefficient for that edge), " + "each exogenous variable (the variance for the error term for " + "that variable), and each bidirected edge (the covariance for " + "the exogenous variables it connects). The model is explained " + "in Bollen, Structural Equations with Latent Variable, 110. ");
return buf.toString();
}
use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class PcGesSearchEditor method reportIfContinuous.
private String reportIfContinuous(Graph dag, DataSet dataSet) {
SemPm semPm = new SemPm(dag);
SemEstimator estimator = new SemEstimator(dataSet, semPm);
estimator.estimate();
SemIm semIm = estimator.getEstimatedSem();
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(4);
StringBuilder buf = new StringBuilder();
buf.append("\nDegrees of Freedom = ").append(semPm.getDof()).append(" Chi-Square = ").append(nf.format(semIm.getChiSquare())).append("\nP Value = ").append(nf.format(semIm.getPValue())).append("\nBIC Score = ").append(nf.format(semIm.getBicScore()));
buf.append("\n\nThe above chi square test assumes that the maximum " + "likelihood function over the measured variables has been " + "maximized. Under that assumption, the null hypothesis for " + "the test is that the population covariance matrix over all " + "of the measured variables is equal to the estimated covariance " + "matrix over all of the measured variables written as a function " + "of the free model parameters--that is, the unfixed parameters " + "for each directed edge (the linear coefficient for that edge), " + "each exogenous variable (the variance for the error term for " + "that variable), and each bidirected edge (the covariance for " + "the exogenous variables it connects). The model is explained " + "in Bollen, Structural Equations with Latent Variable, 110. ");
return buf.toString();
}
use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class SemThenDiscretize method simulate.
private DataSet simulate(Graph graph, Parameters parameters) {
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm);
DataSet continuousData = im.simulateData(parameters.getInt("sampleSize"), false);
if (this.shuffledOrder == null) {
List<Node> shuffledNodes = new ArrayList<>(continuousData.getVariables());
Collections.shuffle(shuffledNodes);
this.shuffledOrder = shuffledNodes;
}
Discretizer discretizer = new Discretizer(continuousData);
for (int i = 0; i < shuffledOrder.size() * parameters.getDouble("percentDiscrete") * 0.01; i++) {
discretizer.equalIntervals(continuousData.getVariable(shuffledOrder.get(i).getName()), parameters.getInt("numCategories"));
}
return discretizer.discretize();
}
use of edu.cmu.tetrad.sem.SemPm in project tetrad by cmu-phil.
the class TimeSeriesSemSimulation method createData.
@Override
public void createData(Parameters parameters) {
dataSets = new ArrayList<>();
graphs = new ArrayList<>();
Graph graph = randomGraph.createGraph(parameters);
graph = TimeSeriesUtils.graphToLagGraph(graph, parameters.getInt("numLags"));
topToBottomLayout((TimeLagGraph) graph);
this.knowledge = TimeSeriesUtils.getKnowledge(graph);
for (int i = 0; i < parameters.getInt("numRuns"); i++) {
if (parameters.getBoolean("differentGraphs") && i > 0) {
graph = randomGraph.createGraph(parameters);
graph = TimeSeriesUtils.graphToLagGraph(graph, 2);
topToBottomLayout((TimeLagGraph) graph);
}
graphs.add(graph);
SemPm pm = new SemPm(graph);
SemIm im = new SemIm(pm, parameters);
final int sampleSize = parameters.getInt("sampleSize");
boolean saveLatentVars = parameters.getBoolean("saveLatentVars");
DataSet dataSet = im.simulateData(sampleSize, saveLatentVars);
int numLags = ((TimeLagGraph) graph).getMaxLag();
dataSet = TimeSeriesUtils.createLagData(dataSet, numLags);
dataSet.setName("" + (i + 1));
dataSet.setKnowledge(knowledge.copy());
dataSets.add(dataSet);
// LargeScaleSimulation sim = new LargeScaleSimulation(graph);
// if (parameters.getDouble("coefHigh") > 0.80) {
// System.out.println("Coefficients have been set (perhaps by default) too " +
// "high for stationary time series.");
// System.out.println("Setting coefficient range to [0.20,0.60].");
// sim.setCoefRange(0.20, 0.60);
// } else sim.setCoefRange(parameters.getDouble("coefLow"), parameters.getDouble("coefHigh"));
// boolean isStableTetradMatrix;
// int attempt = 1;
// int tierSize = parameters.getInt("numMeasures") + parameters.getInt("numLatents"); //params.getNumVars();
// int[] sub = new int[tierSize];
// int[] sub2 = new int[tierSize];
// for (int j = 0; j < tierSize; j++) {
// sub[j] = j;
// sub2[j] = tierSize + j;
// }
// DataSet dataSet;
// do {
// dataSet = sim.simulateDataFisher(parameters.getInt("sampleSize")); //params.getSampleSize());
//
// TetradMatrix coefMat = new TetradMatrix(sim.getCoefficientMatrix());
// TetradMatrix B = coefMat.getSelection(sub, sub);
// TetradMatrix Gamma1 = coefMat.getSelection(sub2, sub);
// TetradMatrix Gamma0 = TetradMatrix.identity(tierSize).minus(B);
// TetradMatrix A1 = Gamma0.inverse().times(Gamma1);
//
// isStableTetradMatrix = TimeSeriesUtils.allEigenvaluesAreSmallerThanOneInModulus(A1);
// attempt++;
// } while ((!isStableTetradMatrix) && attempt <= 5);
// if (!isStableTetradMatrix) {
// System.out.println("%%%%%%%%%% WARNING %%%%%%%% not a stable coefficient matrix, forcing coefs to [0.15,0.3]");
// System.out.println("Made " + (attempt - 1) + " attempts to get stable matrix.");
// sim.setCoefRange(0.15, 0.3);
// dataSet = sim.simulateDataFisher(parameters.getInt("sampleSize"));//params.getSampleSize());
// } //else System.out.println("Coefficient matrix is stable.");
// dataSet.setName("" + (i + 1));
// dataSet.setKnowledge(knowledge.copy());
// dataSets.add(dataSet);
}
}
Aggregations