List of usage examples for org.apache.commons.math3.linear RealMatrix setEntry
void setEntry(int row, int column, double value) throws OutOfRangeException;
From source file:ch.unil.genescore.vegas.LinkageDisequilibrium.java
/** Compute correlation (LD) r values for the given snps (use snpCorrelation_ to avoid recomputing previous values) */ static public RealMatrix computeCorrelationMatrix(ArrayList<Snp> geneSnps) { // The correlation matrix for the given set of snps RealMatrix corr = MatrixUtils.createRealMatrix(geneSnps.size(), geneSnps.size()); // For each snp pair for (int i = 0; i < geneSnps.size(); i++) { for (int j = i + 1; j < geneSnps.size(); j++) { Snp snp_i = geneSnps.get(i); Snp snp_j = geneSnps.get(j); double r = computeCorrelation(snp_i, snp_j); corr.setEntry(i, j, r); corr.setEntry(j, i, r);//from w w w. jav a 2s . c o m } } // Set diagonal to 1 for (int i = 0; i < geneSnps.size(); i++) corr.setEntry(i, i, 1); return corr; }
From source file:edu.macalester.tagrelatedness.KendallsCorrelation.java
/** * This code is taken from a patch submitted for the Apache Commons 3.3 * library by The Apache Software Foundation * at: https://issues.apache.org/jira/browse/MATH-814 * //ww w. j a va 2 s. co m * * Computes the Kendall's Tau rank correlation matrix for the columns of * the input matrix. * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix * @author Matt Adereth */ static public RealMatrix computeCorrelationMatrix(final RealMatrix matrix) { int nVars = matrix.getColumnDimension(); RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars); for (int i = 0; i < nVars; i++) { for (int j = 0; j < i; j++) { double corr = correlation(matrix.getColumn(i), matrix.getColumn(j)); outMatrix.setEntry(i, j, corr); outMatrix.setEntry(j, i, corr); } outMatrix.setEntry(i, i, 1d); } return outMatrix; }
From source file:edu.oregonstate.eecs.mcplan.ml.WekaGlue.java
public static SequentialProjectionHashLearner createSequentialProjectionHashLearner(final RandomGenerator rng, final Instances labeled, final Instances unlabeled, final int K, final double eta, final double alpha) { assert (labeled.classIndex() >= 0); final int Nfeatures = labeled.numAttributes() - 1; final RealMatrix X = new Array2DRowRealMatrix(Nfeatures, labeled.size() + unlabeled.size()); final RealMatrix XL = new Array2DRowRealMatrix(Nfeatures, labeled.size() * 2); final RealMatrix S = new Array2DRowRealMatrix(XL.getColumnDimension(), XL.getColumnDimension()); for (int j = 0; j < labeled.size(); ++j) { final Instance inst = labeled.get(j); for (int i = 0; i < XL.getRowDimension(); ++i) { X.setEntry(i, j, inst.value(i)); XL.setEntry(i, j, inst.value(i)); }/*from www .ja v a 2 s . c om*/ int sj = -1; Instance s = null; do { sj = rng.nextInt(labeled.size()); s = labeled.get(sj); } while (s == inst || s.classValue() != inst.classValue()); S.setEntry(j, sj, 1); int dj = -1; Instance d = null; do { dj = rng.nextInt(labeled.size()); d = labeled.get(dj); } while (d == inst || d.classValue() == inst.classValue()); S.setEntry(j, dj, -1); } for (int j = 0; j < unlabeled.size(); ++j) { final Instance inst = unlabeled.get(j); for (int i = 0; i < X.getRowDimension(); ++i) { X.setEntry(i, labeled.size() + j, inst.value(i)); } } return new SequentialProjectionHashLearner(X, XL, S, K, eta, alpha); }
From source file:cooccurrence.emf.java
/** * Method to populate the apache matrix from cooccur hashmap * * @param matrixR/* w w w. ja va 2s .com*/ * @param cooccur * @param rowStrings * @param colStrings */ private static void populateMatrixR(RealMatrix matrixR, HashMap<String, HashMap<String, Double>> cooccur, ArrayList<String> rowStrings, ArrayList<String> colStrings) { Iterator iter = cooccur.keySet().iterator(); while (iter.hasNext()) { String row = iter.next().toString(); int i = rowStrings.indexOf(row); HashMap<String, Double> inner = cooccur.get(row); for (String col : inner.keySet()) { int j = colStrings.indexOf(col); double val = inner.get(col); matrixR.setEntry(j, i, val); // each column in D represents the vector w-> d_w } iter.remove(); } }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * This method will return vech(matrix). * matrix must be symmetric in order to perform vech operation. * @param RealMatrix matrix//from ww w .java 2 s . c o m * @return vech(matrix) */ public static RealMatrix getVechMatrix(RealMatrix matrix) throws IllegalArgumentException { if (matrix == null || !isSymmetric(matrix)) { throw new IllegalArgumentException("Matrix must be non-null and " + "symmetrical."); } int newRow = 0; int numRows = matrix.getRowDimension(); RealMatrix vech = new Array2DRowRealMatrix(numRows * (numRows + 1) / 2, 1); for (int c = 0; c < matrix.getColumnDimension(); c++) { for (int r = c; r < matrix.getRowDimension(); r++, newRow++) { vech.setEntry(newRow, 0, matrix.getEntry(r, c)); } } return vech; }
From source file:cooccurrence.Omer_Levy.java
/** * Method to populate the apache matrix from cooccur hashmap * @param matrixR//from w w w .j a va2 s .com * @param cooccur * @param rowStrings * @param colStrings */ private static void populateMatrixR(RealMatrix matrixR, HashMap<String, HashMap<String, Double>> cooccur, ArrayList<String> rowStrings, ArrayList<String> colStrings) { Iterator iter = cooccur.keySet().iterator(); while (iter.hasNext()) { String row = iter.next().toString(); int i = rowStrings.indexOf(row); HashMap<String, Double> inner = cooccur.get(row); for (String col : inner.keySet()) { int j = colStrings.indexOf(col); double val = inner.get(col); matrixR.setEntry(i, j, val); } iter.remove(); } }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Returns the vec(matrix)./*from w ww. j a v a 2 s . c om*/ * @param RealMatrix matrix * @return RealMatrix representing vec(matrix). */ public static RealMatrix getVecMatrix(RealMatrix matrix) { if (matrix == null) { throw new IllegalArgumentException("Null matrix not allowed for " + "getVecMatrix()"); } int numRows = matrix.getRowDimension(); int numCols = matrix.getColumnDimension(); RealMatrix vec = new Array2DRowRealMatrix(numRows * numCols, 1); int newRowNum = 0; //loop through each column for (int c = 0; c < numCols; c++) { //insert column values into new r x 1 matrix for (int r = 0; r < numRows; r++, newRowNum++) { vec.setEntry(newRowNum, 0, matrix.getEntry(r, c)); } } return vec; }
From source file:com.yahoo.egads.utilities.SpectralMethods.java
public static RealMatrix averageHankelMatrix(RealMatrix hankelMat, int windowSize) { int k = hankelMat.getRowDimension(); int m = hankelMat.getColumnDimension() / windowSize; int n = k + windowSize - 1; RealMatrix result = MatrixUtils.createRealMatrix(n, m); for (int t = 0; t < n; ++t) { int i = (t < windowSize) ? 0 : (t - windowSize + 1); int j = (t < windowSize) ? t : (windowSize - 1); int counter = 0; for (; i < k && j >= 0; ++i, --j, ++counter) { for (int h = 0; h < m; ++h) { result.addToEntry(t, h, hankelMat.getEntry(i, j * m + h)); }/*from ww w . j ava 2 s . c o m*/ } for (int h = 0; h < m; ++h) { result.setEntry(t, h, result.getEntry(t, h) / counter); } } return result; }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * This method will return the element-wise product of matrixA * and matrixB.//from www . jav a2 s . c o m * In order to perform element-wise multiplication, the incoming * matrices must have the same dimensions. * @param matrixA * @param matrixB * @return RealMatrix which is the element-wise product of A and B. */ public static RealMatrix getElementWiseProduct(RealMatrix matrixA, RealMatrix matrixB) throws IllegalArgumentException { if (matrixA == null || matrixB == null || !areDimensionsEqual(matrixA, matrixB)) { throw new IllegalArgumentException("Both matrices must be non-null " + "and matrix dimensions must be equal" + " for element-wise multiplication."); } int numRows = matrixA.getRowDimension(); int numCols = matrixA.getColumnDimension(); RealMatrix product = new Array2DRowRealMatrix(numRows, numCols); double aVal, bVal; //loop through each row for (int r = 0; r < numRows; r++) { //multiply each element of A by same element of B for (int c = 0; c < numCols; c++) { aVal = matrixA.getEntry(r, c); bVal = matrixB.getEntry(r, c); product.setEntry(r, c, aVal * bVal); } } return product; }
From source file:edu.cudenver.bios.matrix.OrthogonalPolynomials.java
/** * Computes orthogonal polynomial contrasts for the specified data values. Currently only * supports fitting (not prediction contrasts). * /* ww w. j a va2 s. c o m*/ * @param x the points at which the polynomials will be evaluated * @param maxDegree contrasts will be computed for degrees 1 to maxDegree * @return matrix containing 0th,1st, 2nd,...maxDegree-th degree contrasts in each column * @throws IllegalArgumentException */ public static RealMatrix orthogonalPolynomialCoefficients(double[] x, int maxDegree) throws IllegalArgumentException { if (x == null) throw new IllegalArgumentException("no data specified"); if (maxDegree < 1) throw new IllegalArgumentException("max polynomial degree must be greater than 1"); // count number of unique values HashSet<Double> s = new HashSet<Double>(); for (double i : x) s.add(i); int uniqueCount = s.size(); if (maxDegree >= uniqueCount) throw new IllegalArgumentException( "max polynomial degree must be less than the number of unique points"); // center the data double xBar = StatUtils.mean(x); double[] xCentered = new double[x.length]; for (int i = 0; i < x.length; i++) xCentered[i] = x[i] - xBar; // compute an "outer product" of the centered x vector and a vector // containing the sequence 0 to maxDegree-1, but raise the x values // to the power in the sequence array double[][] xOuter = new double[x.length][maxDegree + 1]; int row = 0; for (double xValue : xCentered) { for (int col = 0; col <= maxDegree; col++) { xOuter[row][col] = Math.pow(xValue, col); } row++; } // do some mysterious QR decomposition stuff. See Emerson (1968) RealMatrix outerVector = new Array2DRowRealMatrix(xOuter); QRDecomposition qrDecomp = new QRDecomposition(outerVector); RealMatrix z = MatrixUtils.getDiagonalMatrix(qrDecomp.getR()); RealMatrix raw = qrDecomp.getQ().multiply(z); // column sum of squared elements in raw double[] normalizingConstants = new double[raw.getColumnDimension()]; for (int col = 0; col < raw.getColumnDimension(); col++) { normalizingConstants[col] = 0; for (row = 0; row < raw.getRowDimension(); row++) { double value = raw.getEntry(row, col); normalizingConstants[col] += value * value; } } // now normalize the raw values for (int col = 0; col < raw.getColumnDimension(); col++) { double normalConstantSqrt = Math.sqrt(normalizingConstants[col]); for (row = 0; row < raw.getRowDimension(); row++) { raw.setEntry(row, col, raw.getEntry(row, col) / normalConstantSqrt); } } return raw; }