Example usage for org.apache.commons.math3.linear MatrixUtils createColumnRealMatrix

List of usage examples for org.apache.commons.math3.linear MatrixUtils createColumnRealMatrix

Introduction

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

Prototype

public static RealMatrix createColumnRealMatrix(double[] columnData)
        throws NoDataException, NullArgumentException 

Source Link

Document

Creates a column RealMatrix using the data from the input array.

Usage

From source file:hulo.localization.models.obs.GaussianProcessLDPLMean.java

@Override
public double looPredLogLikelihood() {

    double[][] Y = getY();
    double[][] dY = getdY();
    double[][] mask = getMask();

    int ns = Y.length;
    int ny = Y[0].length;

    RealMatrix Ky = MatrixUtils.createRealMatrix(this.Ky);
    RealMatrix invK = new LUDecomposition(Ky).getSolver().getInverse();

    RealMatrix dYmat = MatrixUtils.createRealMatrix(dY);

    double[] LOOPredLL = new double[ny];
    for (int j = 0; j < ny; j++) {
        RealMatrix dy = MatrixUtils.createColumnRealMatrix(dYmat.getColumn(j));
        RealMatrix invKdy = invK.multiply(dy);
        double sum = 0;
        for (int i = 0; i < ns; i++) {
            double mu_i = dYmat.getEntry(i, j) - invKdy.getEntry(i, 0) / invK.getEntry(i, i);
            double sigma_i2 = 1 / invK.getEntry(i, i);
            double logLL = StatUtils.logProbaNormal(dYmat.getEntry(i, j), mu_i, Math.sqrt(sigma_i2));
            sum += logLL * mask[i][j];//w  w w.  j  ava 2  s.c  om
        }
        LOOPredLL[j] = sum;
    }

    double sumLOOPredLL = 0;
    for (int j = 0; j < ny; j++) {
        sumLOOPredLL += LOOPredLL[j];
    }

    return sumLOOPredLL;
}

From source file:hulo.localization.models.obs.GaussianProcess.java

public double looPredLogLikelihood() {

    double[][] Y = getY();
    double[][] dY = getdY();

    int ns = X.length;
    int ny = Y[0].length;

    RealMatrix Ky = MatrixUtils.createRealMatrix(this.Ky);
    RealMatrix invKy = new LUDecomposition(Ky).getSolver().getInverse();

    RealMatrix dYmat = MatrixUtils.createRealMatrix(dY);

    double[] LOOPredLL = new double[ny];
    for (int j = 0; j < ny; j++) {
        RealMatrix dy = MatrixUtils.createColumnRealMatrix(dYmat.getColumn(j));
        RealMatrix invKdy = invKy.multiply(dy);
        double sum = 0;
        for (int i = 0; i < ns; i++) {
            double mu_i = dYmat.getEntry(i, j) - invKdy.getEntry(i, 0) / invKy.getEntry(i, i);
            double sigma_i2 = 1 / invKy.getEntry(i, i);
            double logLL = StatUtils.logProbaNormal(dYmat.getEntry(i, j), mu_i, Math.sqrt(sigma_i2));
            sum += logLL;//ww  w.ja va 2s .c  o m
        }
        LOOPredLL[j] = sum;
    }

    double sumLOOPredLL = 0;
    for (int j = 0; j < ny; j++) {
        sumLOOPredLL += LOOPredLL[j];
    }

    return sumLOOPredLL;
}

From source file:citation_prediction.CitationCore.java

/**
 * This function implements the algorithm designed by Josiah Neuberger and William Etcho used to solve for a WSB solution.
 * The general math for the Newton-Raphson method was provided by Dr. Allen Parks and can be found in the function 'getPartialsData'.
 * <br><br>/*  w ww.  j a v  a  2 s. c o m*/
 * This algorithm was created under the direction of Supervising University of Mary Washington faculty, Dr. Melody Denhere along with 
 * consultation from Dr. Jeff Solka and Computer Scientists Kristin Ash with the Dahlgren Naval Surface Warfare Center.
 * <br><br>
 * If your interested in more information about the research behind this algorithm please visit:<br>
 * http://josiahneuberger.github.io/citation_prediction/
 * <br><br>
 * 
 * @param data The citation data in days.
 * @param mu The initial mu guess to use in the Newton-Raphson method.
 * @param sigma The initial sigma guess to use in the Newton-Raphson method.
 * @param m The constant value, which is determined by the average number of references in each new paper for a journal.
 * @param t The last time value in the paper's citation history.
 * @param n The total number of citations for this paper.
 * @param l A list structure to store values for each iteration (should be null to start).
 * @param iteration The iteration (should be zero to start)
 * @param max_iteration The maximum number of iterations to try before stopping.
 * @param tolerance The tolerance level, which determines that a solution has been converged on.
 * @return A list containing the WSB solution of (lambda, mu, sigma, iterations).
 */
private LinkedHashMap<String, Double> newtonRaphson(double[][] data, double mu, double sigma, double m,
        double t, double n, LinkedHashMap<String, Double> l, int iteration, int max_iteration,
        double tolerance) {

    double lambda;
    LinkedHashMap<String, Double> r = new LinkedHashMap<String, Double>();

    if (iteration > max_iteration) {
        System.out.println("Does not converge.");

        r.put("lambda", null);
        r.put("mu", null);
        r.put("sigma", null);
        r.put("iterations", null);

        return r;
    } else if (tolerance < 0.00000001) {
        System.out.println("Stopped due to tolerance.");

        r.put("lambda", getLambda(data, mu, sigma, m, t, n));
        r.put("mu", mu);
        r.put("sigma", sigma);
        r.put("iterations", (double) iteration);

        return r;
    } else {

        l = getPartialsData(getIterationData(data, mu, sigma, m));

        double[] array_xn = { mu, sigma };
        double[] array_yn = { l.get("fn"), l.get("gn") };

        RealMatrix xn = MatrixUtils.createColumnRealMatrix(array_xn);
        RealMatrix yn = MatrixUtils.createColumnRealMatrix(array_yn);

        //http://commons.apache.org/proper/commons-math/userguide/linear.html

        double[][] array_jacobian = { { l.get("df_dmu"), l.get("df_dsigma") },
                { l.get("dg_dmu"), l.get("dg_dsigma") } };
        RealMatrix jacobian = MatrixUtils.createRealMatrix(array_jacobian);
        LUDecomposition lud = new LUDecomposition(jacobian);
        DecompositionSolver decS = lud.getSolver();

        l.put("iteration", (double) (iteration + 1));
        //DEBUG: printList(l);

        if (!decS.isNonSingular()) {
            l.put("iteration", (double) (max_iteration + 1));
            System.err.println("ERROR: Jacobian matrix was singular.");
        } else {

            RealMatrix solution = xn.subtract(decS.getInverse().multiply(yn));

            RealMatrix xndiff = solution.subtract(xn);
            tolerance = Math.sqrt(Math.pow(xndiff.getEntry(0, 0), 2) + Math.pow(xndiff.getEntry(1, 0), 2));

            //update values
            l.put("mu", solution.getEntry(0, 0));
            l.put("sigma", solution.getEntry(1, 0));

            //DEBUG: System.out.printf("\"%-20s=%25f\"\n", "NEW MU", l.get("mu"));
            //DEBUG: System.out.printf("\"%-20s=%25f\"\n",  "NEW SIGMA", l.get("sigma"));
        }
        //DEBUG: System.out.println("****************************************");

        return newtonRaphson(data, l.get("mu"), l.get("sigma"), m, l, (l.get("iteration").intValue()),
                tolerance);
    }
}

From source file:org.knime.al.util.noveltydetection.kernel.KernelCalculator.java

private RealMatrix calculateKernelVector(final double[][] training, final double[] test,
        final KernelFunction kernelFunction) {
    final double[] result = new double[training.length];

    for (int r = 0; r < training.length; r++) {
        result[r] = kernelFunction.calculate(training[r], test);
    }/* w  ww.j ava 2  s  . co m*/
    return MatrixUtils.createColumnRealMatrix(result);
}

From source file:org.knime.al.util.noveltydetection.knfst.KNFST.java

public static RealMatrix projection(final RealMatrix kernelMatrix, final String[] labels)
        throws KNFSTException {

    final ClassWrapper[] classes = ClassWrapper.classes(labels);

    // check labels
    if (classes.length == 1) {
        throw new IllegalArgumentException(
                "not able to calculate a nullspace from data of a single class using KNFST (input variable \"labels\" only contains a single value)");
    }/*w w w.  j  a  v  a 2s. c  o m*/

    // check kernel matrix
    if (!kernelMatrix.isSquare()) {
        throw new IllegalArgumentException("The KernelMatrix must be quadratic!");
    }

    // calculate weights of orthonormal basis in kernel space
    final RealMatrix centeredK = centerKernelMatrix(kernelMatrix);

    final EigenDecomposition eig = new EigenDecomposition(centeredK);
    final double[] eigVals = eig.getRealEigenvalues();
    final ArrayList<Integer> nonZeroEigValIndices = new ArrayList<Integer>();
    for (int i = 0; i < eigVals.length; i++) {
        if (eigVals[i] > 1e-12) {
            nonZeroEigValIndices.add(i);
        }
    }

    int eigIterator = 0;
    final RealMatrix eigVecs = eig.getV();
    RealMatrix basisvecs = null;
    try {
        basisvecs = MatrixUtils.createRealMatrix(eigVecs.getRowDimension(), nonZeroEigValIndices.size());
    } catch (final Exception e) {
        throw new KNFSTException("Something went wrong. Try different parameters or a different kernel.");
    }

    for (final Integer index : nonZeroEigValIndices) {
        final double normalizer = 1 / Math.sqrt(eigVals[index]);
        final RealVector basisVec = eigVecs.getColumnVector(eigIterator).mapMultiply(normalizer);
        basisvecs.setColumnVector(eigIterator++, basisVec);
    }

    // calculate transformation T of within class scatter Sw:
    // T= B'*K*(I-L) and L a block matrix
    final RealMatrix L = kernelMatrix.createMatrix(kernelMatrix.getRowDimension(),
            kernelMatrix.getColumnDimension());
    int start = 0;
    for (final ClassWrapper cl : classes) {
        final int count = cl.getCount();
        L.setSubMatrix(MatrixFunctions.ones(count, count).scalarMultiply(1.0 / count).getData(), start, start);
        start += count;
    }

    // need Matrix M with all entries 1/m to modify basisvecs which allows
    // usage of
    // uncentered kernel values (eye(size(M)).M)*basisvecs
    final RealMatrix M = MatrixFunctions
            .ones(kernelMatrix.getColumnDimension(), kernelMatrix.getColumnDimension())
            .scalarMultiply(1.0 / kernelMatrix.getColumnDimension());
    final RealMatrix I = MatrixUtils.createRealIdentityMatrix(M.getColumnDimension());

    // compute helper matrix H
    final RealMatrix H = ((I.subtract(M)).multiply(basisvecs)).transpose().multiply(kernelMatrix)
            .multiply(I.subtract(L));

    // T = H*H' = B'*Sw*B with B=basisvecs
    final RealMatrix T = H.multiply(H.transpose());

    // calculate weights for null space
    RealMatrix eigenvecs = MatrixFunctions.nullspace(T);

    if (eigenvecs == null) {
        final EigenDecomposition eigenComp = new EigenDecomposition(T);
        final double[] eigenvals = eigenComp.getRealEigenvalues();
        eigenvecs = eigenComp.getV();
        final int minId = MatrixFunctions.argmin(MatrixFunctions.abs(eigenvals));
        final double[] eigenvecsData = eigenvecs.getColumn(minId);
        eigenvecs = MatrixUtils.createColumnRealMatrix(eigenvecsData);
    }

    // System.out.println("eigenvecs:");
    // test.printMatrix(eigenvecs);

    // calculate null space projection
    final RealMatrix proj = ((I.subtract(M)).multiply(basisvecs)).multiply(eigenvecs);

    return proj;
}

From source file:org.mcisb.util.math.MathUtils.java

/**
 * /*w  ww  . j  a v  a2 s. co  m*/
 * @param array1
 * @param array2
 * @return double[]
 */
public static double[] add(final double[] array1, final double[] array2) {
    final int FIRST = 0;
    return MatrixUtils.createColumnRealMatrix(array1).add(MatrixUtils.createColumnRealMatrix(array2))
            .getColumn(FIRST);
}

From source file:org.mcisb.util.math.MathUtils.java

/**
 * /*from  w  ww .jav  a 2  s.  c o m*/
 * @param array1
 * @param array2
 * @return double[]
 */
public static double[] multiply(final double[][] array1, final double[] array2) {
    final int FIRST = 0;
    return MatrixUtils.createRealMatrix(array1).multiply(MatrixUtils.createColumnRealMatrix(array2))
            .getColumn(FIRST);
}

From source file:org.mcisb.util.math.MathUtils.java

/**
 * /*from  w ww .  java 2  s. co m*/
 * @param array1
 * @param factor
 * @return double[][]
 */
public static double[] scalarMultiply(final double[] array1, final double factor) {
    final int FIRST = 0;
    return MatrixUtils.createColumnRealMatrix(array1).scalarMultiply(factor).getColumn(FIRST);
}

From source file:org.mcisb.util.math.MathUtils.java

/**
 * /*w w w  . ja  v a  2 s . c  o m*/
 * @param array1
 * @param array2
 * @return double[]
 */
public static double[] subtract(final double[] array1, final double[] array2) {
    final int FIRST = 0;
    return MatrixUtils.createColumnRealMatrix(array1).subtract(MatrixUtils.createColumnRealMatrix(array2))
            .getColumn(FIRST);
}

From source file:org.meteoinfo.math.fitting.OLSTrendLine.java

@Override
public void setValues(Array y, Array x) {
    if (x.getSize() != y.getSize()) {
        throw new IllegalArgumentException(String
                .format("The numbers of y and x values must be equal (%d != %d)", y.getSize(), x.getSize()));
    }//  w  w  w .  j  a v  a 2 s .  c  o m
    double[][] xData = new double[(int) x.getSize()][];
    for (int i = 0; i < x.getSize(); i++) {
        // the implementation determines how to produce a vector of predictors from a single x
        xData[i] = xVector(x.getDouble(i));
    }
    double[] yy = new double[(int) y.getSize()];
    if (logY()) { // in some models we are predicting ln y, so we replace each y with ln y
        for (int i = 0; i < yy.length; i++) {
            if (i < x.getSize())
                yy[i] = Math.log(y.getDouble(i));
            else
                yy[i] = y.getDouble(i);
        }
    } else {
        for (int i = 0; i < yy.length; i++) {
            yy[i] = y.getDouble(i);
        }
    }
    //        double[] yy = (double[])y.copyTo1DJavaArray();
    //        if(logY()) { // in some models we are predicting ln y, so we replace each y with ln y
    //            yy = Arrays.copyOf(yy, yy.length); // user might not be finished with the array we were given
    //            for (int i = 0; i < x.getSize(); i++) {
    //                yy[i] = Math.log(yy[i]);
    //            }
    //        }
    OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
    ols.setNoIntercept(true); // let the implementation include a constant in xVector if desired
    ols.newSampleData(yy, xData); // provide the data to the model
    coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); // get our coefs
    rs = ols.calculateRSquared();
}