use of edu.cmu.tetrad.data.MultiGeneralAndersonDarlingTest in project tetrad by cmu-phil.
the class GeneralizedSemEstimator method estimate.
/**
* Maximizes likelihood equation by equation. Assumes the equations are recursive and that
* each has exactly one error term.
*/
public GeneralizedSemIm estimate(GeneralizedSemPm pm, DataSet data) {
StringBuilder builder = new StringBuilder();
GeneralizedSemIm estIm = new GeneralizedSemIm(pm);
List<Node> nodes = pm.getGraph().getNodes();
nodes.removeAll(pm.getErrorNodes());
MyContext context = new MyContext();
List<List<Double>> allResiduals = new ArrayList<>();
List<RealDistribution> allDistributions = new ArrayList<>();
for (int index = 0; index < nodes.size(); index++) {
Node node = nodes.get(index);
List<String> parameters = new ArrayList<>(pm.getReferencedParameters(node));
Node error = pm.getErrorNode(node);
parameters.addAll(pm.getReferencedParameters(error));
LikelihoodFittingFunction2 likelihoodFittingfunction = new LikelihoodFittingFunction2(index, pm, parameters, nodes, data, context);
double[] values = new double[parameters.size()];
for (int j = 0; j < parameters.size(); j++) {
String parameter = parameters.get(j);
Expression parameterEstimationInitializationExpression = pm.getParameterEstimationInitializationExpression(parameter);
values[j] = parameterEstimationInitializationExpression.evaluate(new MyContext());
}
double[] point = optimize(likelihoodFittingfunction, values, 1);
for (int j = 0; j < parameters.size(); j++) {
estIm.setParameterValue(parameters.get(j), point[j]);
}
List<Double> residuals = likelihoodFittingfunction.getResiduals();
allResiduals.add(residuals);
RealDistribution distribution = likelihoodFittingfunction.getDistribution();
allDistributions.add(distribution);
GeneralAndersonDarlingTest test = new GeneralAndersonDarlingTest(residuals, distribution);
builder.append("\nEquation: ").append(node).append(" := ").append(estIm.getNodeSubstitutedString(node));
builder.append("\n\twhere ").append(pm.getErrorNode(node)).append(" ~ ").append(estIm.getNodeSubstitutedString(pm.getErrorNode(node)));
builder.append("\nAnderson Darling A^2* for this equation = ").append(test.getASquaredStar()).append("\n");
}
List<String> parameters = new ArrayList<>();
double[] values = new double[parameters.size()];
for (int i = 0; i < parameters.size(); i++) {
values[i] = estIm.getParameterValue(parameters.get(i));
}
LikelihoodFittingFunction likelihoodFittingFunction = new LikelihoodFittingFunction(pm, parameters, nodes, data, context);
optimize(likelihoodFittingFunction, values, 1);
MultiGeneralAndersonDarlingTest test = new MultiGeneralAndersonDarlingTest(allResiduals, allDistributions);
double aSquaredStar = test.getASquaredStar();
this.aSquaredStar = aSquaredStar;
String builder2 = "Report:\n" + "\nModel A^2* (Anderson Darling) = " + aSquaredStar + "\n" + builder;
this.report = builder2;
return estIm;
}
Aggregations