Search in sources :

Example 21 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class SemOptimizerEm method expectation.

private void expectation() {
    TetradMatrix bYZModel = yCovModel.inverse().times(yzCovModel);
    TetradMatrix yzCovPred = yCov.times(bYZModel);
    TetradMatrix zCovModel = yzCovModel.transpose().times(bYZModel);
    TetradMatrix zCovDiff = this.zCovModel.minus(zCovModel);
    TetradMatrix CzPred = yzCovPred.transpose().times(bYZModel);
    TetradMatrix newCz = CzPred.plus(zCovDiff);
    for (int i = 0; i < numLatent; i++) {
        for (int j = i; j < numLatent; j++) {
            expectedCov.set(idxLatent[i], idxLatent[j], newCz.get(i, j));
            expectedCov.set(idxLatent[j], idxLatent[i], newCz.get(j, i));
        }
    }
    for (int i = 0; i < numLatent; i++) {
        for (int j = 0; j < numObserved; j++) {
            double v = yzCovPred.get(j, i);
            expectedCov.set(idxLatent[i], idxObserved[j], v);
            expectedCov.set(idxObserved[j], idxLatent[i], v);
        }
    }
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 22 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class SemOptimizerScattershot 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) {
    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.");
    }
    if (numRestarts < 1)
        numRestarts = 1;
    TetradLogger.getInstance().log("info", "Trying EM...");
    TetradLogger.getInstance().log("info", "Trying scattershot...");
    double min = Double.POSITIVE_INFINITY;
    SemIm _sem = null;
    // doesn't seem necessary.
    for (int i = 0; i < numRestarts + 1; i++) {
        TetradLogger.getInstance().log("details", "Trial " + (i + 1));
        // System.out.println("Trial " + (i + 1));
        SemIm _sem2 = new SemIm(semIm);
        optimize2(_sem2);
        double chisq = _sem2.getChiSquare();
        if (Math.abs(chisq) < min) {
            min = Math.abs(chisq);
            _sem = _sem2;
        }
    }
    if (_sem == null) {
        throw new NullPointerException("Minimal score SEM could not be found.");
    }
    for (Parameter param : semIm.getFreeParameters()) {
        Node nodeA = param.getNodeA();
        Node nodeB = param.getNodeB();
        Node _nodeA = _sem.getVariableNode(nodeA.getName());
        Node _nodeB = _sem.getVariableNode(nodeB.getName());
        double value = _sem.getParamValue(_nodeA, _nodeB);
        semIm.setParamValue(param, value);
    }
// optimize2(semIm);
}
Also used : Node(edu.cmu.tetrad.graph.Node) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Example 23 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class TimeSeriesUtils method sumOfArCoefficients.

public static double sumOfArCoefficients(DataSet timeSeries, int numLags) {
    DataSet timeLags = createLagData(timeSeries, numLags);
    List<Node> regressors = new ArrayList<>();
    for (int i = timeSeries.getNumColumns(); i < timeLags.getNumColumns(); i++) {
        regressors.add(timeLags.getVariable(i));
    }
    Regression regression = new RegressionDataset(timeLags);
    TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
    double sum = 0.0;
    int n = 0;
    for (int i = 0; i < timeSeries.getNumColumns(); i++) {
        Node target = timeLags.getVariable(i);
        RegressionResult result = regression.regress(target, regressors);
        double[] coef = result.getCoef();
        for (int k = 0; k < coef.length; k++) {
            sum += coef[k] * coef[k];
            n++;
        }
        TetradVector residualsColumn = result.getResiduals();
        // residuals.viewColumn(i).assign(residualsColumn);
        residuals.assignColumn(i, residualsColumn);
    }
    return sum / n;
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) TetradVector(edu.cmu.tetrad.util.TetradVector) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 24 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class TimeSeriesUtils method ar2.

public static DataSet ar2(DataSet timeSeries, int numLags) {
    List<Node> missingVariables = new ArrayList<>();
    for (Node node : timeSeries.getVariables()) {
        int index = timeSeries.getVariables().indexOf(node);
        boolean missing = true;
        for (int i = 0; i < timeSeries.getNumRows(); i++) {
            if (!Double.isNaN(timeSeries.getDouble(i, index))) {
                missing = false;
                break;
            }
        }
        if (missing) {
            missingVariables.add(node);
        }
    }
    DataSet timeLags = createLagData(timeSeries, numLags);
    Regression regression = new RegressionDataset(timeLags);
    TetradMatrix residuals = new TetradMatrix(timeLags.getNumRows(), timeSeries.getNumColumns());
    for (int i = 0; i < timeSeries.getNumColumns(); i++) {
        Node target = timeLags.getVariable(i);
        int index = timeSeries.getVariables().indexOf(target);
        if (missingVariables.contains(target)) {
            for (int i2 = 0; i2 < residuals.rows(); i2++) {
                residuals.set(i2, index, Double.NaN);
            }
            continue;
        }
        List<Node> regressors = new ArrayList<>();
        for (int i2 = timeSeries.getNumColumns(); i2 < timeLags.getNumColumns(); i2++) {
            int varIndex = i2 % timeSeries.getNumColumns();
            Node var = timeSeries.getVariable(varIndex);
            if (missingVariables.contains(var)) {
                continue;
            }
            regressors.add(timeLags.getVariable(i2));
        }
        RegressionResult result = regression.regress(target, regressors);
        TetradVector residualsColumn = result.getResiduals();
        residuals.assignColumn(i, residualsColumn);
    }
    return ColtDataSet.makeContinuousData(timeSeries.getVariables(), residuals);
}
Also used : RegressionDataset(edu.cmu.tetrad.regression.RegressionDataset) TetradVector(edu.cmu.tetrad.util.TetradVector) ArrayList(java.util.ArrayList) Regression(edu.cmu.tetrad.regression.Regression) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) RegressionResult(edu.cmu.tetrad.regression.RegressionResult)

Example 25 with TetradMatrix

use of edu.cmu.tetrad.util.TetradMatrix in project tetrad by cmu-phil.

the class TimeSeriesUtils method difference.

/**
 * Calculates the dth difference of the given data. If d = 0, the original
 * data is returned. If d = 1, the data (with one fewer rows) is returned,
 * with each row subtracted from its successor. If d = 1, the same operation
 * is applied to the result of d = 1. And so on.
 *
 * @param data the data to be differenced.
 * @param d the number of differences to take, >= 0.
 * @return the differenced data.
 */
public static DataSet difference(DataSet data, int d) {
    if (d == 0) {
        return data;
    }
    TetradMatrix _data = data.getDoubleData();
    for (int k = 1; k <= d; k++) {
        TetradMatrix _data2 = new TetradMatrix(_data.rows() - 1, _data.columns());
        for (int i = 1; i < _data.rows(); i++) {
            for (int j = 0; j < _data.columns(); j++) {
                _data2.set(i - 1, j, _data.get(i, j) - _data.get(i - 1, j));
            }
        }
        _data = _data2;
    }
    return ColtDataSet.makeContinuousData(data.getVariables(), _data);
}
Also used : TetradMatrix(edu.cmu.tetrad.util.TetradMatrix)

Aggregations

TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)161 TetradVector (edu.cmu.tetrad.util.TetradVector)46 ArrayList (java.util.ArrayList)43 Node (edu.cmu.tetrad.graph.Node)41 List (java.util.List)12 CovarianceMatrix (edu.cmu.tetrad.data.CovarianceMatrix)10 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)9 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)9 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)8 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)8 Test (org.junit.Test)8 Regression (edu.cmu.tetrad.regression.Regression)7 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)7 SemIm (edu.cmu.tetrad.sem.SemIm)7 Graph (edu.cmu.tetrad.graph.Graph)6 SemPm (edu.cmu.tetrad.sem.SemPm)6 Vector (java.util.Vector)6 DoubleArrayList (cern.colt.list.DoubleArrayList)5 DataSet (edu.cmu.tetrad.data.DataSet)5 ICovarianceMatrix (edu.cmu.tetrad.data.ICovarianceMatrix)5