use of edu.cmu.tetrad.data.GeneralAndersonDarlingTest 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;
}
use of edu.cmu.tetrad.data.GeneralAndersonDarlingTest in project tetrad by cmu-phil.
the class IndTestRegressionAD method isIndependent.
/**
* Determines whether variable x is independent of variable y given a list of conditioning variables z.
*
* @param xVar the one variable being compared.
* @param yVar the second variable being compared.
* @param zList the list of conditioning variables.
* @return true iff x _||_ y | z.
* @throws RuntimeException if a matrix singularity is encountered.
*/
public boolean isIndependent(Node xVar, Node yVar, List<Node> zList) {
if (zList == null) {
throw new NullPointerException();
}
for (Node node : zList) {
if (node == null) {
throw new NullPointerException();
}
}
TetradVector v1, v2;
try {
List<Node> regressors = new ArrayList<>();
regressors.add(dataSet.getVariable(yVar.getName()));
for (Node zVar : zList) {
regressors.add(dataSet.getVariable(zVar.getName()));
}
RegressionDataset regression = new RegressionDataset(dataSet);
RegressionResult result = regression.regress(xVar, regressors);
v1 = result.getResiduals();
v2 = regression.getResidualsWithoutFirstRegressor();
// regressors.remove(dataSet.getVariable(yVar.getNode()));
// regression = new RegressionDataset(dataSet);
// result = regression.regress(xVar, regressors);
// v2 = result.getResiduals();
} catch (Exception e) {
throw e;
}
List<Double> d1 = new ArrayList<>();
for (int i = 0; i < v1.size(); i++) d1.add(v1.get(i));
List<Double> d2 = new ArrayList<>();
double[] f2 = new double[v2.size()];
for (int i = 0; i < v2.size(); i++) {
d2.add(v2.get(i));
f2[i] = v2.get(i);
}
double sd = StatUtils.sd(f2);
// RealDistribution c2 = new EmpiricalCdf(d2);
RealDistribution c2 = new NormalDistribution(0, sd);
GeneralAndersonDarlingTest test = new GeneralAndersonDarlingTest(d1, c2);
double aSquaredStar = test.getASquaredStar();
System.out.println("A squared star = " + aSquaredStar + " p = " + test.getP());
double p = test.getP();
double aa2 = 1 - tanh(aSquaredStar);
boolean independent = p > alpha;
this.pvalue = aa2;
if (verbose) {
if (independent) {
TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFactMsg(xVar, yVar, zList, 0.));
} else {
TetradLogger.getInstance().log("dependencies", SearchLogUtils.dependenceFactMsg(xVar, yVar, zList, 0.));
}
}
return independent;
}
Aggregations