Search in sources :

Example 1 with MultiGeneralAndersonDarlingTest

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;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) MultiGeneralAndersonDarlingTest(edu.cmu.tetrad.data.MultiGeneralAndersonDarlingTest) RealDistribution(org.apache.commons.math3.distribution.RealDistribution) Expression(edu.cmu.tetrad.calculator.expression.Expression) GeneralAndersonDarlingTest(edu.cmu.tetrad.data.GeneralAndersonDarlingTest) MultiGeneralAndersonDarlingTest(edu.cmu.tetrad.data.MultiGeneralAndersonDarlingTest) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Expression (edu.cmu.tetrad.calculator.expression.Expression)1 GeneralAndersonDarlingTest (edu.cmu.tetrad.data.GeneralAndersonDarlingTest)1 MultiGeneralAndersonDarlingTest (edu.cmu.tetrad.data.MultiGeneralAndersonDarlingTest)1 Node (edu.cmu.tetrad.graph.Node)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RealDistribution (org.apache.commons.math3.distribution.RealDistribution)1