Search in sources :

Example 46 with TetradVector

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

the class FactorAnalysis method successiveFactorVarimax.

public TetradMatrix successiveFactorVarimax(TetradMatrix factorLoadingMatrix) {
    if (factorLoadingMatrix.columns() == 1)
        return factorLoadingMatrix;
    Vector<TetradMatrix> residuals = new Vector<>();
    Vector<TetradMatrix> rotatedFactorVectors = new Vector<>();
    TetradMatrix normalizedFactorLoadings = normalizeRows(factorLoadingMatrix);
    residuals.add(normalizedFactorLoadings);
    TetradMatrix unitColumn = new TetradMatrix(factorLoadingMatrix.rows(), 1);
    for (int i = 0; i < factorLoadingMatrix.rows(); i++) {
        unitColumn.set(i, 0, 1);
    }
    TetradMatrix r = residuals.lastElement();
    TetradMatrix sumCols = r.transpose().times(unitColumn);
    TetradMatrix wVector = sumCols.scalarMult(1.0 / Math.sqrt(unitColumn.transpose().times(r).times(sumCols).get(0, 0)));
    TetradMatrix vVector = r.times(wVector);
    for (int k = 0; k < normalizedFactorLoadings.columns(); k++) {
        // time to find the minimum value in the v vector
        int lIndex = 0;
        double minValue = Double.POSITIVE_INFINITY;
        for (int i = 0; i < vVector.rows(); i++) {
            if (vVector.get(i, 0) < minValue) {
                minValue = vVector.get(i, 0);
                lIndex = i;
            }
        }
        Vector<TetradMatrix> hVectors = new Vector<>();
        Vector<TetradMatrix> bVectors = new Vector<>();
        double alpha1 = Double.NaN;
        r = residuals.lastElement();
        hVectors.add(new TetradMatrix(r.columns(), 1));
        TetradVector rowFromFactorLoading = r.getRow(lIndex);
        for (int j = 0; j < hVectors.lastElement().rows(); j++) {
            hVectors.lastElement().set(j, 0, rowFromFactorLoading.get(j));
        }
        for (int i = 0; i < 200; i++) {
            TetradMatrix bVector = r.times(hVectors.get(i));
            double averageSumSquaresBVector = unitColumn.transpose().times(matrixExp(bVector, 2)).scalarMult(1.0 / (double) bVector.rows()).get(0, 0);
            TetradMatrix betaVector = matrixExp(bVector, 3).minus(bVector.scalarMult(averageSumSquaresBVector));
            TetradMatrix uVector = r.transpose().times(betaVector);
            double alpha2 = (Math.sqrt(uVector.transpose().times(uVector).get(0, 0)));
            bVectors.add(bVector);
            hVectors.add(uVector.scalarMult(1.0 / alpha2));
            if (!Double.isNaN(alpha1) && abs((alpha2 - alpha1)) < getThreshold()) {
                break;
            }
            alpha1 = alpha2;
        }
        TetradMatrix b = bVectors.lastElement();
        rotatedFactorVectors.add(b);
        residuals.add(r.minus(b.times(hVectors.lastElement().transpose())));
    }
    TetradMatrix result = factorLoadingMatrix.like();
    if (!rotatedFactorVectors.isEmpty()) {
        for (int i = 0; i < rotatedFactorVectors.get(0).rows(); i++) {
            for (int j = 0; j < rotatedFactorVectors.size(); j++) {
                result.set(i, j, rotatedFactorVectors.get(j).get(i, 0));
            }
        }
    }
    return result;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) Vector(java.util.Vector) TetradVector(edu.cmu.tetrad.util.TetradVector)

Example 47 with TetradVector

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

the class FactorAnalysis method normalizeRows.

// designed for normalizing a vector.
// as usual, vectors are treated as matrices to simplify operations elsewhere
private static TetradMatrix normalizeRows(TetradMatrix matrix) {
    Vector<TetradMatrix> normalizedRows = new Vector<>();
    for (int i = 0; i < matrix.rows(); i++) {
        TetradVector vector = matrix.getRow(i);
        TetradMatrix colVector = new TetradMatrix(matrix.columns(), 1);
        for (int j = 0; j < matrix.columns(); j++) colVector.set(j, 0, vector.get(j));
        normalizedRows.add(normalizeVector(colVector));
    }
    TetradMatrix result = new TetradMatrix(matrix.rows(), matrix.columns());
    for (int i = 0; i < matrix.rows(); i++) {
        TetradMatrix normalizedRow = normalizedRows.get(i);
        for (int j = 0; j < matrix.columns(); j++) {
            result.set(i, j, normalizedRow.get(j, 0));
        }
    }
    return result;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) Vector(java.util.Vector) TetradVector(edu.cmu.tetrad.util.TetradVector)

Example 48 with TetradVector

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

the class FastIca method scale.

private TetradMatrix scale(TetradMatrix x) {
    for (int j = 0; j < x.columns(); j++) {
        TetradVector u = x.getColumn(j);
        double rms = rms(u);
        for (int i = 0; i < x.rows(); i++) {
            x.set(i, j, x.get(i, j) / rms);
        }
    }
    return x;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector)

Example 49 with TetradVector

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

the class FastIca method center.

private TetradMatrix center(TetradMatrix x) {
    for (int j = 0; j < x.columns(); j++) {
        TetradVector u = x.getColumn(j);
        double mean = mean(u);
        for (int i = 0; i < x.rows(); i++) {
            x.set(i, j, x.get(i, j) - mean);
        }
    }
    return x;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector)

Example 50 with TetradVector

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

the class FastIca method icaParallel.

private TetradMatrix icaParallel(TetradMatrix X, int numComponents, double tolerance, int function, final double alpha, int maxIterations, boolean verbose, TetradMatrix wInit) {
    int p = X.columns();
    TetradMatrix W = wInit;
    SingularValueDecomposition sW = new SingularValueDecomposition(W.getRealMatrix());
    TetradMatrix D = new TetradMatrix(sW.getS());
    for (int i = 0; i < D.rows(); i++) D.set(i, i, 1.0 / D.get(i, i));
    TetradMatrix WTemp = new TetradMatrix(sW.getU()).times(D);
    WTemp = WTemp.times(new TetradMatrix(sW.getU()).transpose());
    WTemp = WTemp.times(W);
    W = WTemp;
    TetradMatrix W1;
    double _tolerance = Double.POSITIVE_INFINITY;
    int it = 0;
    if (function == LOGCOSH) {
        if (verbose) {
            TetradLogger.getInstance().log("info", "Symmetric FastICA using logcosh approx. to neg-entropy function");
        }
        while (_tolerance > tolerance && it < maxIterations) {
            TetradMatrix wx = W.times(X);
            TetradMatrix gwx = new TetradMatrix(numComponents, p);
            for (int i = 0; i < numComponents; i++) {
                for (int j = 0; j < p; j++) {
                    gwx.set(i, j, Math.tanh(alpha * wx.get(i, j)));
                }
            }
            TetradMatrix v1 = gwx.times(X.transpose().copy().scalarMult(1.0 / p));
            TetradMatrix g_wx = gwx.copy();
            for (int i = 0; i < g_wx.rows(); i++) {
                for (int j = 0; j < g_wx.columns(); j++) {
                    double v = g_wx.get(i, j);
                    double w = alpha * (1.0 - v * v);
                    g_wx.set(i, j, w);
                }
            }
            TetradVector V20 = new TetradVector(numComponents);
            for (int k = 0; k < numComponents; k++) {
                V20.set(k, mean(g_wx.getRow(k)));
            }
            TetradMatrix v2 = V20.diag();
            v2 = v2.times(W);
            W1 = v1.minus(v2);
            SingularValueDecomposition sW1 = new SingularValueDecomposition(W1.getRealMatrix());
            TetradMatrix U = new TetradMatrix(sW1.getU());
            TetradMatrix sD = new TetradMatrix(sW1.getS());
            for (int i = 0; i < sD.rows(); i++) sD.set(i, i, 1.0 / sD.get(i, i));
            TetradMatrix W1Temp = U.times(sD);
            W1Temp = W1Temp.times(U.transpose());
            W1Temp = W1Temp.times(W1);
            W1 = W1Temp;
            TetradMatrix d1 = W1.times(W.transpose());
            TetradVector d = d1.diag();
            _tolerance = Double.NEGATIVE_INFINITY;
            for (int i = 0; i < d.size(); i++) {
                double m = Math.abs(Math.abs(d.get(i)) - 1);
                if (m > _tolerance)
                    _tolerance = m;
            }
            W = W1;
            if (verbose) {
                TetradLogger.getInstance().log("fastIcaDetails", "Iteration " + (it + 1) + " tol = " + _tolerance);
            }
            it++;
        }
    } else if (function == EXP) {
        if (verbose) {
            TetradLogger.getInstance().log("info", "Symmetric FastICA using exponential approx. to neg-entropy function");
        }
        while (_tolerance > tolerance && it < maxIterations) {
            TetradMatrix wx = W.times(X);
            TetradMatrix gwx = new TetradMatrix(numComponents, p);
            for (int i = 0; i < numComponents; i++) {
                for (int j = 0; j < p; j++) {
                    double v = wx.get(i, j);
                    gwx.set(i, j, v * Math.exp(-(v * v) / 2.0));
                }
            }
            TetradMatrix v1 = gwx.times(X.transpose().scalarMult(p));
            TetradMatrix g_wx = wx.copy();
            for (int i = 0; i < g_wx.rows(); i++) {
                for (int j = 0; j < g_wx.columns(); j++) {
                    double v = g_wx.get(i, j);
                    double w = (1.0 - v * v) * Math.exp(-(v * v) / 2.0);
                    g_wx.set(i, j, w);
                }
            }
            TetradVector V20 = new TetradVector(numComponents);
            for (int k = 0; k < numComponents; k++) {
                V20.set(k, mean(g_wx.getRow(k)));
            }
            TetradMatrix v2 = V20.diag();
            v2 = v2.times(W);
            W1 = v1.minus(v2);
            SingularValueDecomposition sW1 = new SingularValueDecomposition(W1.getRealMatrix());
            TetradMatrix U = new TetradMatrix(sW1.getU());
            TetradMatrix sD = new TetradMatrix(sW1.getS());
            for (int i = 0; i < sD.rows(); i++) sD.set(i, i, 1.0 / sD.get(i, i));
            TetradMatrix W1Temp = U.times(sD);
            W1Temp = W1Temp.times(U.transpose());
            W1Temp = W1Temp.times(W1);
            W1 = W1Temp;
            TetradMatrix d1 = W1.times(W.transpose());
            TetradVector d = d1.diag();
            _tolerance = Double.NEGATIVE_INFINITY;
            for (int i = 0; i < d.size(); i++) {
                double m = Math.abs(Math.abs(d.get(i)) - 1);
                if (m > _tolerance)
                    _tolerance = m;
            }
            W.assign(W1);
            if (verbose) {
                TetradLogger.getInstance().log("fastIcaDetails", "Iteration " + (it + 1) + " tol = " + _tolerance);
            }
            it++;
        }
    }
    return W;
}
Also used : TetradVector(edu.cmu.tetrad.util.TetradVector) TetradMatrix(edu.cmu.tetrad.util.TetradMatrix) SingularValueDecomposition(org.apache.commons.math3.linear.SingularValueDecomposition)

Aggregations

TetradVector (edu.cmu.tetrad.util.TetradVector)54 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)44 ArrayList (java.util.ArrayList)19 Node (edu.cmu.tetrad.graph.Node)11 RegressionResult (edu.cmu.tetrad.regression.RegressionResult)9 Regression (edu.cmu.tetrad.regression.Regression)7 RegressionDataset (edu.cmu.tetrad.regression.RegressionDataset)7 DoubleArrayList (cern.colt.list.DoubleArrayList)5 AndersonDarlingTest (edu.cmu.tetrad.data.AndersonDarlingTest)5 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)5 List (java.util.List)4 Test (org.junit.Test)4 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)3 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)2 RandomUtil (edu.cmu.tetrad.util.RandomUtil)2 Vector (java.util.Vector)2 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)1 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)1 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)1 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)1