use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class FactorAnalysisJoe method successiveResidualHelper.
/*
* Helper method for the basic structure successive factor method above.
* Takes a residual matrix and a approximation vector, and finds both
* the factor loading vector and the "d value" which is used to determine
* the amount of total variance accounted for so far.
*/
private void successiveResidualHelper(TetradMatrix residual, TetradMatrix approximationVector) {
TetradMatrix uVector = matrixMult(residual, approximationVector);
TetradMatrix lVector = matrixMult(transpose(approximationVector), uVector);
double dScalar = Math.sqrt(lVector.get(0, 0));
TetradMatrix aVector = matrixDiv(dScalar, uVector);
Vector aVectors = new Vector();
Vector uVectors = new Vector();
Vector dScalars = new Vector();
aVectors.add(aVector);
uVectors.add(uVector);
dScalars.add(dScalar);
for (int i = 0; i < 100; i++) {
approximationVector = (TetradMatrix) aVectors.lastElement();
uVector = matrixMult(residual, approximationVector);
lVector = matrixMult(transpose(approximationVector), uVector);
dScalar = Math.sqrt(lVector.get(0, 0));
aVector = matrixDiv(dScalar, uVector);
aVectors.add(aVector);
uVectors.add(uVector);
dScalars.add(dScalar);
//
// TetradMatrix oldFactorLoading = (TetradMatrix)factorLoadings.lastElement();
// // TetradMatrix newUVector = matrixMult((TetradMatrix)correlationMatrix.getMatrix(),
// // (TetradMatrix)factorLoadings.lastElement());
// TetradMatrix newUVector = matrixMult((TetradMatrix)uVectors.lastElement(),
// (TetradMatrix)factorLoadings.lastElement());
// uVectors.add(newUVector);
// TetradMatrix newLScalar = matrixMult(transpose(oldFactorLoading), newUVector);
// if (lVeco.get(0, 0) < 0) throw new IllegalArgumentException();
// double newDScalar = Math.sqrt(lVector.get(0, 0));
// dScalars.add(newDScalar);
// TetradMatrix _aVector = matrixDiv(newDScalar, uVector);
// aVectors.add(_aVector);
System.out.println("New D Scalar: " + dScalar);
if (Math.sqrt((dScalar / (Double) dScalars.get(dScalars.size() - 2)) - 1) < .00001) {
System.out.println("Stopped on the " + i + "th iteration.");
break;
}
}
System.out.println("Resultant factor loading matrix: ");
System.out.println(aVectors.lastElement());
this.dValues.add((Double) dScalars.lastElement());
this.factorLoadingVectors.add((TetradMatrix) aVectors.lastElement());
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class FactorAnalysisJoe method matrixMult.
/*
* Calculates (a * b)
*/
private static TetradMatrix matrixMult(TetradMatrix a, TetradMatrix b) {
if (a.columns() != b.rows()) {
throw new IllegalArgumentException();
}
TetradMatrix result = new TetradMatrix(a.rows(), b.columns());
for (int i = 0; i < a.rows(); i++) {
for (int j = 0; j < b.columns(); j++) {
double value = 0;
for (int k = 0; k < b.rows(); k++) {
value += a.get(i, k) * b.get(k, j);
}
result.set(i, j, value);
}
}
// System.out.println(result);
return result;
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class FactorAnalysisJoe method largestNonDiagonalMagnitude.
/*
* Largest nondiagonal magnitude method.
*
* Estimates the communality (diagonal of the residual correlation
* matrix) as the largest nondiagonal absolute value present in each column.
*
* Tends to produce smaller numbers of factors than the unity method.
*/
public void largestNonDiagonalMagnitude(CorrelationMatrix r) {
TetradMatrix residual = r.getMatrix();
// System.out.println(residual.toString());
for (int i = 0; i < residual.columns(); i++) {
double max = 0;
for (int j = 0; j < residual.columns(); j++) {
if (i == j)
continue;
double temp = Math.abs(residual.get(j, i));
if (temp > max)
max = temp;
}
residual.set(i, i, max);
}
// System.out.println(residual.toString());
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class SemOptimizerRicf method optimize.
// ==============================PUBLIC METHODS========================//
/**
* Optimizes the fitting function of the given Sem using the Powell method
* from Numerical Recipes by adjusting the freeParameters of the Sem.
*/
public void optimize(SemIm semIm) {
if (numRestarts < 1)
numRestarts = 1;
if (numRestarts != 1) {
throw new IllegalArgumentException("Number of restarts must be 1 for this method.");
}
TetradMatrix sampleCovar = semIm.getSampleCovar();
if (sampleCovar == null) {
throw new NullPointerException("Sample covar has not been set.");
}
if (DataUtils.containsMissingValue(sampleCovar)) {
throw new IllegalArgumentException("Please remove or impute missing values.");
}
if (DataUtils.containsMissingValue(sampleCovar)) {
throw new IllegalArgumentException("Please remove or impute missing values.");
}
TetradLogger.getInstance().log("info", "Trying EM...");
// new SemOptimizerEm().optimize(semIm);
CovarianceMatrix cov = new CovarianceMatrix(semIm.getMeasuredNodes(), sampleCovar, semIm.getSampleSize());
SemGraph graph = semIm.getSemPm().getGraph();
Ricf.RicfResult result = new Ricf().ricf(graph, cov, 0.001);
// Ricf.RicfResult result = null;
//
// for (int t = 0; t < 10; t++) {
// Graph graph = semIm.getSemPm().getGraph();
// result = new Ricf().ricf(graph, cov, 0.001);
//
// TetradMatrix bHat = result.getBhat();
// TetradMatrix lHat = result.getLhat();
// TetradMatrix oHat = result.getOhat();
// TetradMatrix sHat = result.getShat();
//
// for (Parameter param : semIm.getFreeParameters()) {
// if (param.getType() == ParamType.COEF) {
// int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
// int j = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeB());
// semIm.setEdgeCoef(param.getNodeA(), param.getNodeB(), -bHat.get(j, i));
// }
//
// if (param.getType() == ParamType.VAR) {
// int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
// if (lHat.get(i, i) != 0) {
// semIm.setErrVar(param.getNodeA(), lHat.get(i, i));
// } else if (oHat.get(i, i) != 0) {
// semIm.setErrVar(param.getNodeA(), oHat.get(i, i));
// }
// }
// }
//
// if (t < 9) {
// for (Parameter param : semIm.getFreeParameters()) {
// double value = semIm.getParamValue(param);
// double max = Double.NEGATIVE_INFINITY;
// double d;
//
// for (d = value - .5; d <= value + 0.5; d += 0.001) {
// semIm.setParamValue(param, d);
// double fml = semIm.getFml();
// if (fml > max) max = fml;
// }
//
// semIm.setParamValue(param, d);
// }
// }
// }
TetradMatrix bHat = new TetradMatrix(result.getBhat().toArray());
TetradMatrix lHat = new TetradMatrix(result.getLhat().toArray());
TetradMatrix oHat = new TetradMatrix(result.getOhat().toArray());
for (Parameter param : semIm.getFreeParameters()) {
if (param.getType() == ParamType.COEF) {
int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
int j = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeB());
semIm.setEdgeCoef(param.getNodeA(), param.getNodeB(), -bHat.get(j, i));
}
if (param.getType() == ParamType.VAR) {
int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
if (lHat.get(i, i) != 0) {
semIm.setErrVar(param.getNodeA(), lHat.get(i, i));
} else if (oHat.get(i, i) != 0) {
semIm.setErrVar(param.getNodeA(), oHat.get(i, i));
}
}
if (param.getType() == ParamType.COVAR) {
int i = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeA());
int j = semIm.getSemPm().getVariableNodes().indexOf(param.getNodeB());
if (lHat.get(i, i) != 0) {
semIm.setErrCovar(param.getNodeA(), param.getNodeB(), lHat.get(j, i));
} else if (oHat.get(i, i) != 0) {
semIm.setErrCovar(param.getNodeA(), param.getNodeB(), oHat.get(j, i));
}
}
}
System.out.println(result);
System.out.println(semIm);
}
use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.
the class CovMatrixDifferenceWrapper method calcDifference.
// public CovMatrixDifferenceWrapper(SemEstimatorWrapper wrapper1, DataWrapper wrapper2, Parameters params) {
// if (wrapper1 == null || wrapper2 == null) {
// throw new NullPointerException("The data must not be null");
// }
//
// DataModel model2 = wrapper2.getSelectedDataModel();
//
// if (!(model2 instanceof ICovarianceMatrix)) {
// throw new IllegalArgumentException("Expecting corrariance matrices.");
// }
//
// TetradMatrix corr1 = wrapper1.getEstimatedSemIm().getImplCovarMeas();
// TetradMatrix corr2 = ((ICovarianceMatrix) model2).getMatrix();
//
// TetradMatrix corr3 = calcDifference(corr1, corr2);
//
// ICovarianceMatrix corrWrapper = new CovarianceMatrix(model2.getVariable(), corr3,
// ((ICovarianceMatrix) model2).getSampleSize());
//
// setDataModel(corrWrapper);
// setSourceGraph(wrapper2.getSourceGraph());
// LogDataUtils.logDataModelList("Difference of matrices.", getDataModelList());
//
// }
// public CovMatrixDifferenceWrapper(SemImWrapper wrapper1, DataWrapper wrapper2, Parameters params) {
// try {
// if (wrapper1 == null || wrapper2 == null) {
// throw new NullPointerException("The data must not be null");
// }
//
// DataModel model2 = wrapper2.getSelectedDataModel();
//
// if (!(model2 instanceof ICovarianceMatrix)) {
// throw new IllegalArgumentException("Expecting corrariance matrices.");
// }
//
// TetradMatrix corr1 = wrapper1.getSemIm().getImplCovarMeas();
// TetradMatrix corr2 = ((ICovarianceMatrix) model2).getMatrix();
//
// TetradMatrix corr3 = calcDifference(corr1, corr2);
//
// ICovarianceMatrix corrWrapper = new CovarianceMatrix(model2.getVariable(), corr3,
// ((ICovarianceMatrix) model2).getSampleSize());
//
// setDataModel(corrWrapper);
// setSourceGraph(wrapper2.getSourceGraph());
// LogDataUtils.logDataModelList("Difference of matrices.", getDataModelList());
// } catch (Exception e) {
// e.printStackTrace();
// throw new RuntimeException(e);
// }
//
// }
private TetradMatrix calcDifference(TetradMatrix corr1, TetradMatrix corr2) {
if (corr1.rows() != corr2.rows()) {
throw new IllegalArgumentException("Covariance matrices must be the same size.");
}
TetradMatrix corr3 = new TetradMatrix(corr2.rows(), corr2.rows());
for (int i = 0; i < corr3.rows(); i++) {
for (int j = 0; j < corr3.rows(); j++) {
double v = corr1.get(i, j) - corr2.get(i, j);
corr3.set(i, j, v);
// corr3.set(j, i, v);
}
}
for (int i = 0; i < corr3.rows(); i++) {
corr3.set(i, i, Math.abs(corr3.get(i, i)));
}
return corr3;
}
Aggregations