Example usage for org.apache.commons.math3.linear EigenDecomposition EigenDecomposition

List of usage examples for org.apache.commons.math3.linear EigenDecomposition EigenDecomposition

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear EigenDecomposition EigenDecomposition.

Prototype

public EigenDecomposition(final RealMatrix matrix) throws MathArithmeticException 

Source Link

Document

Calculates the eigen decomposition of the given real matrix.

Usage

From source file:de.biomedical_imaging.traj.math.MomentsCalculator.java

public double calculateNthMoment(int n) {
    Array2DRowRealMatrix gyr = RadiusGyrationTensor2D.getRadiusOfGyrationTensor(t);
    EigenDecomposition eigdec = new EigenDecomposition(gyr);

    Vector2d eigv = new Vector2d(eigdec.getEigenvector(0).getEntry(0), eigdec.getEigenvector(0).getEntry(1));

    double[] projected = new double[t.size()];
    for (int i = 0; i < t.size(); i++) {
        Vector2d pos = new Vector2d(t.get(i).x, t.get(i).y);
        double v = eigv.dot(pos);
        projected[i] = v;//ww  w . j  a va2s. co m
    }

    Mean m = new Mean();
    StandardDeviation s = new StandardDeviation();
    double mean = m.evaluate(projected);
    double sd = s.evaluate(projected);
    double sumPowN = 0;

    for (int i = 0; i < projected.length; i++) {
        sumPowN += Math.pow((projected[i] - mean) / sd, n);
    }

    double nThMoment = sumPowN / projected.length;

    return nThMoment;
}

From source file:edu.cmu.tetrad.search.EstimateRank.java

public double[] CanCor(int[] iA, int[] iB, double[][] cov) {
    this.iA = iA;
    this.iB = iB;
    this.cov = cov;
    RealMatrix covA = new BlockRealMatrix(cov).getSubMatrix(iA, iA);
    RealMatrix covB = new BlockRealMatrix(cov).getSubMatrix(iB, iB);
    RealMatrix covAB = new BlockRealMatrix(cov).getSubMatrix(iA, iB);
    RealMatrix covBA = new BlockRealMatrix(cov).getSubMatrix(iB, iA);
    RealMatrix S = getInverse(covA).multiply(covAB).multiply(getInverse(covB)).multiply(covBA);
    double[] rtCors = new EigenDecomposition(S).getRealEigenvalues();
    Arrays.sort(rtCors);//from w w w . j a va2  s .c o  m
    double[] Cors = new double[rtCors.length];
    for (int i = rtCors.length; i > 0; i--) {
        Cors[rtCors.length - i] = Math.pow(rtCors[i - 1], .5);
    }
    return Cors;
}

From source file:com.itemanalysis.psychometrics.factoranalysis.GeneralizedLeastSquaresMethod.java

private void computeFactorLoadings(double[] x) {
    uniqueness = x;// www . ja  va  2s  .c  o  m
    communality = new double[nVariables];

    for (int i = 0; i < nVariables; i++) {
        R.setEntry(i, i, 1.0 - x[i]);
    }

    EigenDecomposition E = new EigenDecomposition(R);
    RealMatrix L = E.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1);
    double[] ev = new double[nFactors];
    for (int i = 0; i < nFactors; i++) {
        ev[i] = Math.sqrt(E.getRealEigenvalue(i));
    }
    DiagonalMatrix M = new DiagonalMatrix(ev);
    RealMatrix LOAD = L.multiply(M);

    //rotate factor loadings
    if (rotationMethod != RotationMethod.NONE) {
        GPArotation gpa = new GPArotation();
        RotationResults results = gpa.rotate(LOAD, rotationMethod);
        LOAD = results.getFactorLoadings();
    }

    Sum[] colSums = new Sum[nFactors];
    Sum[] colSumsSquares = new Sum[nFactors];

    for (int j = 0; j < nFactors; j++) {
        colSums[j] = new Sum();
        colSumsSquares[j] = new Sum();
    }

    factorLoading = new double[nVariables][nFactors];

    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            factorLoading[i][j] = LOAD.getEntry(i, j);
            colSums[j].increment(factorLoading[i][j]);
            colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2));
            communality[i] += Math.pow(factorLoading[i][j], 2);
        }
    }

    //check sign of factor
    double sign = 1.0;
    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            if (colSums[j].getResult() < 0) {
                sign = -1.0;
            } else {
                sign = 1.0;
            }
            factorLoading[i][j] = factorLoading[i][j] * sign;
        }
    }

    double totSumOfSquares = 0.0;
    sumsOfSquares = new double[nFactors];
    proportionOfExplainedVariance = new double[nFactors];
    proportionOfVariance = new double[nFactors];
    for (int j = 0; j < nFactors; j++) {
        sumsOfSquares[j] = colSumsSquares[j].getResult();
        totSumOfSquares += sumsOfSquares[j];
    }
    for (int j = 0; j < nFactors; j++) {
        proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares;
        proportionOfVariance[j] = sumsOfSquares[j] / nVariables;
    }

}

From source file:lanchester.MultiArena.java

public void step() {
    int numFoes = forces.size();
    if (isMyTurn == null) {
        isMyTurn = new boolean[numFoes][numFoes];
        stanceArray = new int[numFoes][numFoes];
        currentFloor = new double[numFoes][numFoes];
        for (int i1 = 0; i1 < numFoes; i1++) {
            int ind1 = forceMap.get(forces.get(i1));
            for (int i2 = 0; i2 < numFoes; i2++) {
                int ind2 = forceMap.get(forces.get(i2));
                isMyTurn[i1][i2] = true;
                if (i1 == i2) {
                    stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                    currentFloor[i1][i2] = 0.;
                } else {
                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                    setFloor(i1, i2);//from w  w w  . j av  a 2 s  .  co m
                }
            }
        }
    }
    Array2DRowRealMatrix mat = getMat();
    EigenDecomposition eigen = new EigenDecomposition(mat);
    double det = eigen.getDeterminant();
    double[] eVals = eigen.getRealEigenvalues();
    //        for(int i1=0;i1<eVals.length;i1++){
    //            System.out.println("eVals["+i1+"] = "+eVals[i1]);
    //        }
    if (eigen.hasComplexEigenvalues()) {
        System.out.println("Complex eigenvalues");
        for (int i1 = 0; i1 < forces.size(); i1++) {
            AthenaForce f = forces.get(i1);
            System.out.println(f.getName() + " has " + f.getForceSize() + " forces remaining");
        }
    }
    double[] initialNums = getInitialNumbers(forces);
    Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV();
    LUDecomposition lu = new LUDecomposition(eVectors);
    double det2 = lu.getDeterminant();
    double[] coeffs = new double[numFoes];
    for (int i1 = 0; i1 < numFoes; i1++) {
        Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy();
        tmpMat.setColumn(i1, initialNums);
        LUDecomposition tmpLU = new LUDecomposition(tmpMat);
        double tmpDet = tmpLU.getDeterminant();
        coeffs[i1] = tmpDet / det2;
    }
    MultiTimeStep currentStep = new MultiTimeStep(numFoes);
    currentTime += timeStep;
    currentStep.setTime(currentTime);
    for (int i1 = 0; i1 < numFoes; i1++) {
        double updatedForce = 0.;
        for (int i2 = 0; i2 < numFoes; i2++) {
            updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * timeStep);
            //                updatedForce+=coeffs[i2]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i2]*timeStep);
            //                updatedForce+=coeffs[i1]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i1]*timeStep);
        }
        forces.get(i1).updateForce(updatedForce);
        currentStep.setForceNumber(updatedForce, i1);
    }
    history.add(currentStep);
    //        eVectors.
    //        this.currentTime++;
    //                Truncator truncator = new Truncator();
    if (true) {
        //            System.out.println("time = " + time);
    }
}

From source file:lanchester.MultiArena2.java

public void step() {
    boolean aboveFloor = true;
    double currentCycle = 0.;
    int numFoes = forces.size();
    if (isMyTurn == null) {
        isMyTurn = new boolean[numFoes][numFoes];
        stanceArray = new int[numFoes][numFoes];
        currentFloor = new double[numFoes][numFoes];
        for (int i1 = 0; i1 < numFoes; i1++) {
            int ind1 = forceMap.get(forces.get(i1));
            for (int i2 = 0; i2 < numFoes; i2++) {
                int ind2 = forceMap.get(forces.get(i2));
                isMyTurn[i1][i2] = true;
                if (i1 == i2) {
                    stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                    currentFloor[i1][i2] = 0.;
                } else {
                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                    setFloor(i1, i2);/*from  w ww  . j a  v a2s  . c o  m*/
                }
            }
        }
    }
    Array2DRowRealMatrix mat = getMat();
    EigenDecomposition eigen = new EigenDecomposition(mat);
    double det = eigen.getDeterminant();
    double[] eVals = eigen.getRealEigenvalues();
    //        for(int i1=0;i1<eVals.length;i1++){
    //            System.out.println("eVals["+i1+"] = "+eVals[i1]);
    //        }
    if (eigen.hasComplexEigenvalues()) {
        System.out.println("Complex eigenvalues");
        for (int i1 = 0; i1 < forces.size(); i1++) {
            AthenaForce f = forces.get(i1);
            System.out.println(f.getName() + " has " + f.getForceSize() + " forces remaining");
        }
    }
    double[] initialNums = getInitialNumbers(forces);
    Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV();
    LUDecomposition lu = new LUDecomposition(eVectors);
    double det2 = lu.getDeterminant();
    double[] coeffs = new double[numFoes];
    for (int i1 = 0; i1 < numFoes; i1++) {
        Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy();
        tmpMat.setColumn(i1, initialNums);
        LUDecomposition tmpLU = new LUDecomposition(tmpMat);
        double tmpDet = tmpLU.getDeterminant();
        coeffs[i1] = tmpDet / det2;
    }
    aboveFloor = true;
    int cntr = 0;
    int numGone;
    do {
        MultiTimeStep currentStep = new MultiTimeStep(numFoes);
        currentTime += timeStep;
        currentCycle += timeStep;
        currentStep.setTime(currentTime);
        numGone = 0;
        for (int i1 = 0; i1 < numFoes; i1++) {
            double updatedForce = 0.;
            if (forces.get(i1).getForceSize() > lb) {
                for (int i2 = 0; i2 < numFoes; i2++) {
                    //                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * timeStep);
                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * currentCycle);
                    if (updatedForce < 1.) {
                        updatedForce = 0.;
                        numGone++;
                    }
                    //                updatedForce+=coeffs[i2]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i2]*timeStep);
                    //                updatedForce+=coeffs[i1]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i1]*timeStep);
                }
            } else {
                updatedForce = lb / 2.;
                numGone++;
            }
            forces.get(i1).updateForce(updatedForce);
            currentStep.setForceNumber(updatedForce, i1);
        }
        history.add(currentStep);
        aboveFloor = checkAboveFloors();
        cntr++;
    } while (aboveFloor && cntr < 2000 && (numFoes - numGone) > 1);
    for (int i1 = 0; i1 < numFoes; i1++) {
        for (int i2 = 0; i2 < numFoes; i2++) {
            if (i1 == i2) {
                stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                currentFloor[i1][i2] = 0.;
            } else {
                stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                setFloor(i1, i2);
            }
        }
    }

    //        eVectors.
    //        this.currentTime++;
    //                Truncator truncator = new Truncator();
    if (numFoes - numGone == 1) {
        loneSurvivor = true;
        //            System.out.println("time = " + time);
    }
}

From source file:com.itemanalysis.psychometrics.factoranalysis.MINRESmethod.java

private void computeFactorLoadings(double[] x) {
    uniqueness = x;//from  ww w . j  a v  a  2s  . co  m
    communality = new double[nVariables];

    double[] sqrtPsi = new double[nVariables];
    double[] invSqrtPsi = new double[nVariables];
    for (int i = 0; i < nVariables; i++) {
        sqrtPsi[i] = Math.sqrt(x[i]);
        invSqrtPsi[i] = 1.0 / Math.sqrt(x[i]);
    }
    DiagonalMatrix diagPsi = new DiagonalMatrix(x);
    DiagonalMatrix diagSqtPsi = new DiagonalMatrix(sqrtPsi);
    DiagonalMatrix diagInvSqrtPsi = new DiagonalMatrix(invSqrtPsi);

    RealMatrix Sstar = diagInvSqrtPsi.multiply(R2).multiply(diagInvSqrtPsi);
    EigenDecomposition E = new EigenDecomposition(Sstar);
    RealMatrix L = E.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1);
    double[] ev = new double[nFactors];
    for (int i = 0; i < nFactors; i++) {
        ev[i] = Math.sqrt(Math.max(E.getRealEigenvalue(i) - 1, 0));
    }
    DiagonalMatrix M = new DiagonalMatrix(ev);
    RealMatrix LOAD2 = L.multiply(M);
    RealMatrix LOAD = diagSqtPsi.multiply(LOAD2);

    //rotate factor loadings
    if (rotationMethod != RotationMethod.NONE) {
        GPArotation gpa = new GPArotation();
        RotationResults results = gpa.rotate(LOAD, rotationMethod);
        LOAD = results.getFactorLoadings();
    }

    Sum[] colSums = new Sum[nFactors];
    Sum[] colSumsSquares = new Sum[nFactors];

    for (int j = 0; j < nFactors; j++) {
        colSums[j] = new Sum();
        colSumsSquares[j] = new Sum();
    }

    factorLoading = new double[nVariables][nFactors];

    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            factorLoading[i][j] = LOAD.getEntry(i, j);
            colSums[j].increment(factorLoading[i][j]);
            colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2));
            communality[i] += Math.pow(factorLoading[i][j], 2);
        }
    }

    //check sign of factor
    double sign = 1.0;
    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            if (colSums[j].getResult() < 0) {
                sign = -1.0;
            } else {
                sign = 1.0;
            }
            factorLoading[i][j] = factorLoading[i][j] * sign;
        }
    }

    double totSumOfSquares = 0.0;
    sumsOfSquares = new double[nFactors];
    proportionOfExplainedVariance = new double[nFactors];
    proportionOfVariance = new double[nFactors];
    for (int j = 0; j < nFactors; j++) {
        sumsOfSquares[j] = colSumsSquares[j].getResult();
        totSumOfSquares += sumsOfSquares[j];
    }
    for (int j = 0; j < nFactors; j++) {
        proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares;
        proportionOfVariance[j] = sumsOfSquares[j] / nVariables;
    }

}

From source file:lanchester.MultiArena3.java

public void step() {
    boolean aboveFloor = true;
    double currentCycle = 0.;
    int numFoes = forces.size();
    if (isMyTurn == null) {
        isMyTurn = new boolean[numFoes][numFoes];
        stanceArray = new int[numFoes][numFoes];
        currentFloor = new double[numFoes][numFoes];
        currentCeiling = new double[numFoes][numFoes];
        for (int i1 = 0; i1 < numFoes; i1++) {
            int ind1 = forceMap.get(forces.get(i1));
            for (int i2 = 0; i2 < numFoes; i2++) {
                int ind2 = forceMap.get(forces.get(i2));
                isMyTurn[i1][i2] = true;
                if (i1 == i2) {
                    stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                    currentFloor[i1][i2] = 0.;
                    currentCeiling[i1][i2] = 100.;
                } else {
                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                    setFloor(i1, i2);/*from   www . ja v a  2  s.co  m*/
                    setCeiling(i1, i2);
                }
            }
        }
    }
    Array2DRowRealMatrix mat = getMat();
    EigenDecomposition eigen = new EigenDecomposition(mat);
    double det = eigen.getDeterminant();
    double[] eVals = eigen.getRealEigenvalues();
    if (eigen.hasComplexEigenvalues()) {
        System.out.println("Complex eigenvalues");
        for (int i1 = 0; i1 < forces.size(); i1++) {
            MultiForce f = forces.get(i1);
            System.out.println(f.getName() + " has " + f.getNumber() + " forces remaining");
        }
    }
    double[] initialNums = getInitialNumbers(forces);
    Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV();
    LUDecomposition lu = new LUDecomposition(eVectors);
    double det2 = lu.getDeterminant();
    double[] coeffs = new double[numFoes];
    for (int i1 = 0; i1 < numFoes; i1++) {
        Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy();
        tmpMat.setColumn(i1, initialNums);
        LUDecomposition tmpLU = new LUDecomposition(tmpMat);
        double tmpDet = tmpLU.getDeterminant();
        coeffs[i1] = tmpDet / det2;
    }
    aboveFloor = true;
    boolean belowCeiling = true;
    int cntr = 0;
    int numGone;
    do {
        timeStep = determineTimeStep();
        MultiTimeStep currentStep = new MultiTimeStep(numFoes);
        currentTime += timeStep;
        currentCycle += timeStep;
        currentStep.setTime(currentTime);
        numGone = 0;
        for (int i1 = 0; i1 < numFoes; i1++) {
            double updatedForce = 0.;
            if (stillAlive[i1]) {
                for (int i2 = 0; i2 < numFoes; i2++) {
                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * currentCycle);
                }
                if (updatedForce < 1.) {
                    updatedForce = lb;
                    stillAlive[i1] = false;
                    numGone++;
                }
            } else {
                numGone++;
                updatedForce = lb;
            }
            forces.get(i1).updateForce(updatedForce);
            currentStep.setForceNumber(updatedForce, i1);
        }
        history.add(currentStep);
        aboveFloor = checkAboveFloors();
        belowCeiling = checkBelowCeilings();
        cntr++;
    } while (aboveFloor && belowCeiling && cntr < 2000 && (numFoes - numGone) > 1);
    for (int i1 = 0; i1 < numFoes; i1++) {
        for (int i2 = 0; i2 < numFoes; i2++) {
            if (i1 == i2) {
                stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                currentFloor[i1][i2] = 0.;
            } else {
                stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                setFloor(i1, i2);
            }
        }
    }

    //        eVectors.
    //        this.currentTime++;
    //                Truncator truncator = new Truncator();
    if (numFoes - numGone == 1) {
        loneSurvivor = true;
        //            System.out.println("time = " + time);
    }
}

From source file:lanchester.MultiArena4.java

public void step() {
    boolean aboveFloor = true;
    double currentCycle = 0.;
    int numFoes = forces.size();
    if (isMyTurn == null) {
        isMyTurn = new boolean[numFoes][numFoes];
        stanceArray = new int[numFoes][numFoes];
        currentFloor = new double[numFoes][numFoes];
        currentCeiling = new double[numFoes][numFoes];
        for (int i1 = 0; i1 < numFoes; i1++) {
            //                int ind1 = forceMap.get(forces.get(i1));
            for (int i2 = 0; i2 < numFoes; i2++) {
                //                    int ind2 = forceMap.get(forces.get(i2));
                isMyTurn[i1][i2] = true;
                if (i1 == i2) {
                    stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                    currentFloor[i1][i2] = 0.;
                    currentCeiling[i1][i2] = 1000000.;
                } else {
                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                    //                        setStances(i1,i2);
                    setFloor(i1, i2);/*from   w w w.  j  a  v a  2 s. co  m*/
                    setCeiling(i1, i2);
                }
            }
        }
    }
    Array2DRowRealMatrix mat = getMat();
    EigenDecomposition eigen = new EigenDecomposition(mat);
    double det = eigen.getDeterminant();
    double[] eVals = eigen.getRealEigenvalues();
    if (eigen.hasComplexEigenvalues()) {
        System.out.println("Complex eigenvalues");
        for (int i1 = 0; i1 < forces.size(); i1++) {
            MultiForce f = forces.get(i1);
            System.out.println(f.getName() + " has " + f.getNumber() + " forces remaining");
        }
    }
    double[] initialNums = getInitialNumbers(forces);
    Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV();
    LUDecomposition lu = new LUDecomposition(eVectors);
    double det2 = lu.getDeterminant();
    double[] coeffs = new double[numFoes];
    for (int i1 = 0; i1 < numFoes; i1++) {
        Array2DRowRealMatrix tmpMat = (Array2DRowRealMatrix) eVectors.copy();
        tmpMat.setColumn(i1, initialNums);
        LUDecomposition tmpLU = new LUDecomposition(tmpMat);
        double tmpDet = tmpLU.getDeterminant();
        coeffs[i1] = tmpDet / det2;
    }
    aboveFloor = true;
    boolean belowCeiling = true;
    int cntr = 0;
    int numGone;
    do {
        timeStep = determineTimeStep();
        MultiTimeStep currentStep = new MultiTimeStep(numFoes);
        currentTime += timeStep;
        currentCycle += timeStep;
        currentStep.setTime(currentTime);
        numGone = 0;
        for (int i1 = 0; i1 < numFoes; i1++) {
            double updatedForce = 0.;
            //                if (forces.get(i1).getForceSize() > lb) {
            if (stillAlive[i1]) {
                for (int i2 = 0; i2 < numFoes; i2++) {
                    //                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * timeStep);
                    updatedForce += coeffs[i2] * eVectors.getEntry(i1, i2) * Math.exp(eVals[i2] * currentCycle);

                    //                updatedForce+=coeffs[i2]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i2]*timeStep);
                    //                updatedForce+=coeffs[i1]*eVectors.getEntry(i2, i1)*Math.exp(eVals[i1]*timeStep);
                }
                if (updatedForce < 1.) {
                    updatedForce = lb;
                    stillAlive[i1] = false;
                    numGone++;
                }
            } else {
                updatedForce = lb;
                numGone++;
            }
            forces.get(i1).updateForce(updatedForce);
            currentStep.setForceNumber(updatedForce, i1);
            //                for (int i2 = 0; i2 < numFoes; i1++) {
            //                    if (i1 != i2) {
            //
            //                    }
            //                }
        }
        history.add(currentStep);
        aboveFloor = checkAboveFloors();
        belowCeiling = checkBelowCeilings();
        cntr++;
    } while (aboveFloor && belowCeiling && cntr < 2000 && (numFoes - numGone) > 1);
    for (int i1 = 0; i1 < numFoes; i1++) {
        for (int i2 = 0; i2 < numFoes; i2++) {
            if (i1 == i2) {
                stanceArray[i1][i2] = AthenaConstants.ALLIED_POSTURE;
                currentFloor[i1][i2] = 0.;
            } else {
                //                    stanceArray[i1][i2] = initializeStance(forces.get(i1), forces.get(i2));
                setStances(i1, i2);
                setFloor(i1, i2);
                setCeiling(i1, i2);
            }
        }
    }

    //        eVectors.
    //        this.currentTime++;
    //                Truncator truncator = new Truncator();
    if (numFoes - numGone == 1) {
        loneSurvivor = true;
        //            System.out.println("time = " + time);
    }
}

From source file:com.itemanalysis.psychometrics.factoranalysis.MaximumLikelihoodMethod.java

private void computeFactorLoadings(double[] x) {
    uniqueness = x;/*from  w w w  .j av  a2 s  . c o  m*/
    communality = new double[nVariables];

    double[] sqrtPsi = new double[nVariables];
    double[] invSqrtPsi = new double[nVariables];
    for (int i = 0; i < nVariables; i++) {
        sqrtPsi[i] = Math.sqrt(x[i]);
        invSqrtPsi[i] = 1.0 / Math.sqrt(x[i]);
    }
    DiagonalMatrix diagPsi = new DiagonalMatrix(x);
    DiagonalMatrix diagSqtPsi = new DiagonalMatrix(sqrtPsi);
    DiagonalMatrix diagInvSqrtPsi = new DiagonalMatrix(invSqrtPsi);

    RealMatrix Sstar = diagInvSqrtPsi.multiply(R).multiply(diagInvSqrtPsi);
    EigenDecomposition E = new EigenDecomposition(Sstar);
    RealMatrix L = E.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1);
    double[] ev = new double[nFactors];
    for (int i = 0; i < nFactors; i++) {
        ev[i] = Math.sqrt(Math.max(E.getRealEigenvalue(i) - 1, 0));
    }
    DiagonalMatrix M = new DiagonalMatrix(ev);
    RealMatrix LOAD2 = L.multiply(M);
    RealMatrix LOAD = diagSqtPsi.multiply(LOAD2);

    //rotate factor loadings
    if (rotationMethod != RotationMethod.NONE) {
        GPArotation gpa = new GPArotation();
        RotationResults results = gpa.rotate(LOAD, rotationMethod);
        LOAD = results.getFactorLoadings();
    }

    Sum[] colSums = new Sum[nFactors];
    Sum[] colSumsSquares = new Sum[nFactors];

    for (int j = 0; j < nFactors; j++) {
        colSums[j] = new Sum();
        colSumsSquares[j] = new Sum();
    }

    factorLoading = new double[nVariables][nFactors];

    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            factorLoading[i][j] = LOAD.getEntry(i, j);
            colSums[j].increment(factorLoading[i][j]);
            colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2));
            communality[i] += Math.pow(factorLoading[i][j], 2);
        }
    }

    //check sign of factor
    double sign = 1.0;
    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            if (colSums[j].getResult() < 0) {
                sign = -1.0;
            } else {
                sign = 1.0;
            }
            factorLoading[i][j] = factorLoading[i][j] * sign;
        }
    }

    double totSumOfSquares = 0.0;
    sumsOfSquares = new double[nFactors];
    proportionOfExplainedVariance = new double[nFactors];
    proportionOfVariance = new double[nFactors];
    for (int j = 0; j < nFactors; j++) {
        sumsOfSquares[j] = colSumsSquares[j].getResult();
        totSumOfSquares += sumsOfSquares[j];
    }
    for (int j = 0; j < nFactors; j++) {
        proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares;
        proportionOfVariance[j] = sumsOfSquares[j] / nVariables;
    }

}

From source file:edu.oregonstate.eecs.mcplan.ml.KernelPrincipalComponentsAnalysis.java

/**
 * TODO: Things to consider:/* w w w  . ja  v  a 2s.c o  m*/
 *       * Nystrom approximation to kernel matrix
 *       * Iterative eigenvalue algorithm
 *       * Online version of KPCA
 * @param data Training data
 * @param Nbases Number of eigenvectors to retain
 * @param k Kernel function
 * @param jitter We regularize by solving ((1 - jitter)*K + jitter*I).
 */
public KernelPrincipalComponentsAnalysis(final ArrayList<T> data, final KernelFunction<T> k,
        final double jitter) {
    this.data = data;
    this.k = k;
    this.Ndata = data.size();

    // Compute kernel matrix
    System.out.println("[KPCA] Computing kernel matrix");
    final RealMatrix K = new Array2DRowRealMatrix(Ndata, Ndata);
    for (int i = 0; i < Ndata; ++i) {
        final T xi = data.get(i);
        for (int j = i; j < Ndata; ++j) {
            final T xj = data.get(j);
            final double K_ij = (1.0 - jitter) * k.apply(xi, xj);
            final double jitter_if_diag = (i == j ? jitter : 0.0);
            K.setEntry(i, j, K_ij + jitter_if_diag);
            K.setEntry(j, i, K_ij + jitter_if_diag);
        }
    }
    //      System.out.println( K );

    System.out.println("[KPCA] Centering");
    // Averages for centering
    row_avg = new double[Ndata];
    final MeanVarianceAccumulator total_mv = new MeanVarianceAccumulator();
    for (int i = 0; i < Ndata; ++i) {
        final MeanVarianceAccumulator row_mv = new MeanVarianceAccumulator();
        for (int j = 0; j < Ndata; ++j) {
            final double K_ij = K.getEntry(i, j);
            row_mv.add(K_ij);
            total_mv.add(K_ij);
        }
        row_avg[i] = row_mv.mean();
    }
    total_avg = total_mv.mean();
    // Centered version of the kernel matrix:
    // K_c(i, j) = K_ij - sum_z K_zj / m - sum_z K_iz / m + sum_{z,y} K_zy / m^2
    for (int i = 0; i < Ndata; ++i) {
        for (int j = 0; j < Ndata; ++j) {
            final double K_ij = K.getEntry(i, j);
            K.setEntry(i, j, K_ij - row_avg[i] - row_avg[j] + total_avg);
        }
    }

    System.out.println("[KPCA] Eigendecomposition");
    eigenvectors = new ArrayList<RealVector>();
    final EigenDecomposition evd = new EigenDecomposition(K);
    for (int j = 0; j < Ndata; ++j) {
        final double eigenvalue = evd.getRealEigenvalue(j);
        if (eigenvalue < eps) {
            break;
        }
        eigenvalues.add(eigenvalue);
        final double scale = 1.0 / Math.sqrt(eigenvalue);
        final RealVector eigenvector = evd.getEigenvector(j);
        eigenvectors.add(eigenvector.mapMultiply(scale));
    }
}