Example usage for org.apache.commons.math3.linear RealMatrix getTrace

List of usage examples for org.apache.commons.math3.linear RealMatrix getTrace

Introduction

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

Prototype

double getTrace() throws NonSquareMatrixException;

Source Link

Document

Returns the <a href="http://mathworld.wolfram.com/MatrixTrace.html"> trace</a> of the matrix (the sum of the elements on the main diagonal).

Usage

From source file:io.warp10.script.functions.TR.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object o = stack.pop();/*from   ww w.  j  av a  2s .c  om*/

    if (!(o instanceof RealMatrix)) {
        throw new WarpScriptException(getName() + " expects a matrix on top of the stack.");
    }

    RealMatrix matrix = (RealMatrix) o;

    stack.push(matrix.getTrace());

    return stack;
}

From source file:com.itemanalysis.psychometrics.cfa.UnweightedLeastSquares.java

/**
* Empty method - artifact of interface/*ww w .jav  a2  s. c om*/
*
* @param argument
* @return
*/
public double value(double[] argument) {
    SIGMA = model.getImpliedCovariance(argument);
    RealMatrix D = varcov.subtract(SIGMA);
    RealMatrix D2 = D.multiply(D);
    F = 0.5 * D2.getTrace();
    return F;
}

From source file:com.itemanalysis.psychometrics.cfa.GeneralizedLeastSquares.java

/**
 * /* www .  j  a va2 s  .  com*/
 *
 * @param argument
 * @return
 */
public double value(double[] argument) {
    SIGMA = model.getImpliedCovariance(argument);
    RealMatrix D = (varcov.subtract(SIGMA)).multiply(VCinv);
    RealMatrix D2 = D.multiply(D);
    F = 0.5 * D2.getTrace();
    return F;
}

From source file:com.itemanalysis.psychometrics.cfa.GeneralizedLeastSquares.java

public double gfi() {
    double fit = 0.0;
    double q = Double.valueOf(model.getNumberOfItems()).doubleValue();
    RealMatrix I = new IdentityMatrix(nItems);
    RealMatrix P = SIGMA.multiply(VCinv);
    RealMatrix D = I.subtract(P);//from   w  w w  .jav  a  2  s . c om
    RealMatrix D2 = D.multiply(D);
    fit = 1.0 - D2.getTrace() / q;
    return fit;
}

From source file:com.itemanalysis.psychometrics.cfa.UnweightedLeastSquares.java

public double gfi() {
    double fit = 0.0;
    double q = Double.valueOf(model.getNumberOfItems()).doubleValue();
    RealMatrix D = varcov.subtract(SIGMA);
    RealMatrix D2 = D.multiply(D);
    RealMatrix V2 = varcov.multiply(varcov);
    fit = 1.0 - D2.getTrace() / V2.getTrace();
    return fit;//from   ww  w .  j av  a 2  s .  c om
}

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

/**
 * Computes the function value for varimax rotation.
 *
 * @param L matrix of factor loadings.//from w w w  . j a v  a  2  s.c  om
 */
public void computeValues(RealMatrix L) {
    //initialize dimensions and column mean array
    int nrow = L.getRowDimension();
    int ncol = L.getColumnDimension();
    Mean[] colMean = new Mean[ncol];
    for (int i = 0; i < ncol; i++) {
        colMean[i] = new Mean();
    }

    //square each element in matrix
    RealMatrix L2 = L.copy();
    double value = 0.0;
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            value = L.getEntry(i, j);
            value *= value;
            L2.setEntry(i, j, value);
            colMean[j].increment(value);
        }
    }

    double dif = 0.0;
    RealMatrix QL = new Array2DRowRealMatrix(nrow, ncol);
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            dif = L2.getEntry(i, j) - colMean[j].getResult();
            QL.setEntry(i, j, dif);
        }
    }

    //compute gradientAt
    gradient = new Array2DRowRealMatrix(nrow, ncol);
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            value = -L.getEntry(i, j) * QL.getEntry(i, j);
            gradient.setEntry(i, j, value);
        }
    }

    //compute function value
    RealMatrix B = QL.transpose().multiply(QL);
    double sum = B.getTrace();
    functionValue = -sum / 4.0;

}

From source file:com.itemanalysis.psychometrics.cfa.MaximumLikelihoodEstimation.java

public double value(double[] argument) {
    model.setParameters(argument);/*w ww . j av a2  s .c o m*/

    //            Linesearch method in QNMinimizer is causing NaN values after repeated calls to here
    //            Next libe is for monitoring values when called from line search
    //            No problem occurs with CGMinimizer
    //            System.out.println("valueAt: " + argument[0] + " " + argument[1]);
    SIGMA = model.getImpliedCovariance(argument);

    //compute determinant of SIGMA
    LUDecomposition SLUD = new LUDecomposition(SIGMA);
    double detSig = SLUD.getDeterminant();

    //compute inverse of SIGMA
    RealMatrix SIGMAinv = SLUD.getSolver().getInverse();
    RealMatrix VC_SIGMA_INV = varcov.multiply(SIGMAinv);
    double trace = VC_SIGMA_INV.getTrace();

    //convert number of items to double
    double p = Double.valueOf(model.getNumberOfItems()).doubleValue();

    //compute objective function
    F = Math.log(detSig) + trace - Math.log(detVc) - p;
    return F;
}

From source file:edu.cudenver.bios.power.glmm.GLMMTestHotellingLawley.java

/**
 * Compute a Hotelling-Lawley Trace statistic
 *
 * @param H hypothesis sum of squares matrix
 * @param E error sum of squares matrix//from   w ww.  j ava  2s . co  m
 * @returns F statistic
 */
private double getHotellingLawleyTrace(RealMatrix H, RealMatrix E) throws IllegalArgumentException {
    if (!H.isSquare() || !E.isSquare() || H.getColumnDimension() != E.getRowDimension())
        throw new IllegalArgumentException(
                "Failed to compute Hotelling-Lawley Trace: hypothesis and error matrices must be square and same dimensions");

    RealMatrix inverseE = new LUDecomposition(E).getSolver().getInverse();
    RealMatrix HinverseE = H.multiply(inverseE);

    return HinverseE.getTrace();
}

From source file:edu.ucdenver.bios.statisticaltest.HotellingLawleyTraceTest.java

/**
 * Compute a Hotelling-Lawley Trace statistic
 * // ww w . java 2 s . com
 * @param H hypothesis sum of squares matrix
 * @param E error sum of squares matrix
 * @returns F statistic
 */
private double getHotellingLawleyTrace() throws IllegalArgumentException {
    RealMatrix inverseE = new LUDecomposition(this.errorSumOfSquares).getSolver().getInverse();
    RealMatrix HinverseE = this.hypothesisSumOfSquares.multiply(inverseE);

    return HinverseE.getTrace();
}

From source file:com.opengamma.strata.math.impl.matrix.CommonsMatrixAlgebra.java

@Override
public double getTrace(Matrix m) {
    ArgChecker.notNull(m, "m");
    if (m instanceof DoubleMatrix) {
        RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix) m);
        return temp.getTrace();
    }//from   w  ww .ja va 2  s . co  m
    throw new IllegalArgumentException("Can only find trace of DoubleMatrix; have " + m.getClass());
}