List of usage examples for org.apache.commons.math3.linear RealMatrix getColumn
double[] getColumn(int column) throws OutOfRangeException;
From source file:com.rapidminer.tools.math.LinearRegression.java
/** Calculates the coefficients of linear ridge regression. */ public static double[] performRegression(Matrix a, Matrix b, double ridge) { RealMatrix x = MatrixUtils.createRealMatrix(a.getArray()); RealMatrix y = MatrixUtils.createRealMatrix(b.getArray()); int numberOfColumns = x.getColumnDimension(); double[] coefficients = new double[numberOfColumns]; RealMatrix xTransposed = x.transpose(); Matrix result;// w ww . j av a2 s. c o m boolean finished = false; while (!finished) { RealMatrix xTx = xTransposed.multiply(x); for (int i = 0; i < numberOfColumns; i++) { xTx.addToEntry(i, i, ridge); } RealMatrix xTy = xTransposed.multiply(y); coefficients = xTy.getColumn(0); try { // do not use Apache LUDecomposition for solve instead because it creates different // results result = new Matrix(xTx.getData()).solve(new Matrix(coefficients, coefficients.length)); for (int i = 0; i < numberOfColumns; i++) { coefficients[i] = result.get(i, 0); } finished = true; } catch (Exception ex) { double ridgeOld = ridge; if (ridge > 0) { ridge *= 10; } else { ridge = 0.0000001; } finished = false; logger.warning("Error during calculation: " + ex.getMessage() + ": Increasing ridge factor from " + ridgeOld + " to " + ridge); } } return coefficients; }
From source file:com.vsthost.rnd.commons.math.ext.linear.EMatrixUtils.java
/** * Returns the standard deviations of columns. * * @param matrix The matrix of which the standard deviations of columns to be computed * @return A double array of column standard deviations. *//* w ww. j av a 2 s . c om*/ public static double[] columnStdDevs(RealMatrix matrix) { double[] retval = new double[matrix.getColumnDimension()]; for (int i = 0; i < retval.length; i++) { retval[i] = new DescriptiveStatistics(matrix.getColumn(i)).getStandardDeviation(); } return retval; }
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 * //from w w w . jav a 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:br.unicamp.ic.recod.gpsi.measures.gpsiDistanceOfMediansScore.java
@Override public double score(double[][][] input) { Median median = new Median(); RealMatrix matrix0 = MatrixUtils.createRealMatrix(input[0]); RealMatrix matrix1 = MatrixUtils.createRealMatrix(input[1]); return Math.abs(median.evaluate(matrix0.getColumn(0)) - median.evaluate(matrix1.getColumn(0))); }
From source file:br.unicamp.ic.recod.gpsi.measures.gpsiWilcoxonRankSumTestScore.java
@Override public double score(double[][][] samples) { double p_value; RealMatrix m0 = MatrixUtils.createRealMatrix(samples[0]); RealMatrix m1 = MatrixUtils.createRealMatrix(samples[1]); MannWhitneyUTest t = new MannWhitneyUTest(); p_value = t.mannWhitneyUTest(m0.getColumn(0), m1.getColumn(0)); return p_value; }
From source file:io.yields.math.concepts.operator.Smoothness.java
@Override public DescriptiveStatistics apply(Collection<Tuple> tuples) { Validate.isTrue(tuples.size() > order); //first we normalize the tuples so data fits in the unit square List<Tuple> normalizedData = normalize(tuples); //calculate error (i.e. distance between tuple y and fitted polynomial RealMatrix error = computeDistance(normalizedData); //group in stats object DescriptiveStatistics stats = new DescriptiveStatistics(); for (double value : error.getColumn(0)) { stats.addValue(Math.abs(value)); }/*w w w .j a v a2 s .co m*/ return stats; }
From source file:com.itemanalysis.psychometrics.mixture.MvNormalComponentDistribution.java
public void generateStartValues(RealMatrix x, RealMatrix mean, RealMatrix cov) { JDKRandomGenerator jg = new JDKRandomGenerator(); NormalizedRandomGenerator rg = new GaussianRandomGenerator(jg); CorrelatedRandomVectorGenerator sg = null; sg = new CorrelatedRandomVectorGenerator(mean.getColumn(0), cov, 0.00001, rg); mu = new Array2DRowRealMatrix(sg.nextVector()); }
From source file:gr.iti.mklab.reveal.forensics.maps.dwnoisevar.DWNoiseVarExtractor.java
public void getNoiseMap() throws IOException { BufferedImage img = inputImage; int imWidth, imHeight; if (img.getWidth() % 2 == 0) { imWidth = img.getWidth();//from w w w . j a v a2 s . co m } else { imWidth = (img.getWidth() - 1); } if (img.getHeight() % 2 == 0) { imHeight = img.getHeight(); } else { imHeight = (img.getHeight() - 1); } int columnFilterScale = (int) (Math.log(imHeight) / Math.log(2)) - 1; int rowFilterScale = (int) (Math.log(imWidth) / Math.log(2)) - 1; double[][] imgYAsArray = new double[imWidth][imHeight]; double[][] filteredImgYAsArray = new double[imWidth][imHeight / 2]; double[][] doubleFilteredImgYAsArray = new double[imWidth / 2][imHeight / 2]; double[] imgColumn, imgRow; Color tmpcolor; double R, G, B; for (int ii = 0; ii < imWidth; ii++) { for (int jj = 0; jj < imHeight; jj++) { tmpcolor = new Color(img.getRGB(ii, jj)); R = tmpcolor.getRed(); G = tmpcolor.getGreen(); B = tmpcolor.getBlue(); imgYAsArray[ii][jj] = 0.2989 * R + 0.5870 * G + 0.1140 * B; } } double[] waveletColumn; RealMatrix rm = new Array2DRowRealMatrix(imgYAsArray); for (int ii = 0; ii < imWidth; ii++) { try { imgColumn = new double[imHeight]; imgColumn = rm.getRow(ii); //Long startTime1 = System.currentTimeMillis(); waveletColumn = DWT.transform(imgColumn, Wavelet.Daubechies, 8, columnFilterScale, DWT.Direction.forward); System.arraycopy(waveletColumn, imHeight / 2, filteredImgYAsArray[ii], 0, imHeight / 2); } catch (Exception ex) { Logger.getLogger(DWNoiseVarExtractor.class.getName()).log(Level.SEVERE, null, ex); } } double[] waveletRow; RealMatrix rm2 = new Array2DRowRealMatrix(filteredImgYAsArray); for (int jj = 0; jj < imHeight / 2; jj++) { try { imgRow = new double[imWidth]; imgRow = rm2.getColumn(jj); waveletRow = DWT.transform(imgRow, Wavelet.Daubechies, 8, rowFilterScale, DWT.Direction.forward); for (int ii = 0; ii < imWidth / 2; ii++) { doubleFilteredImgYAsArray[ii][jj] = waveletRow[ii + imWidth / 2]; } } catch (Exception ex) { Logger.getLogger(DWNoiseVarExtractor.class.getName()).log(Level.SEVERE, null, ex); } } int blockSize = 8; double[][] blockNoiseVar = Util.blockNoiseVar(doubleFilteredImgYAsArray, blockSize); int medianFilterSize = 7; if (medianFilterSize > blockNoiseVar.length) { medianFilterSize = blockNoiseVar.length - 1; } if (medianFilterSize > blockNoiseVar[0].length) { medianFilterSize = blockNoiseVar[0].length - 1; } if (medianFilterSize < 5) { minNoiseValue = 0; maxNoiseValue = 0; noiseMap = new float[1][1]; noiseMap[0][0] = 0; double[][] artificialOutput = new double[1][1]; artificialOutput[0][0] = 0; BufferedImage outputImage = Util.visualizeWithJet(artificialOutput); displaySurface = outputImage; return; } float[][] outBlockMap = Util.medianFilterSingleChannelImage(blockNoiseVar, medianFilterSize); minNoiseValue = Util.minDouble2DArray(outBlockMap); maxNoiseValue = Util.maxDouble2DArray(outBlockMap); noiseMap = outBlockMap; double[][] normalizedMap = Util.normalizeIm(outBlockMap); BufferedImage outputImage = Util.visualizeWithJet(normalizedMap); // output displaySurface = outputImage; }
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];/*from ww w . j av a2s .c o m*/ } LOOPredLL[j] = sum; } double sumLOOPredLL = 0; for (int j = 0; j < ny; j++) { sumLOOPredLL += LOOPredLL[j]; } return sumLOOPredLL; }
From source file:br.unicamp.ic.recod.gpsi.ml.gpsi1NNToMomentScalarClassificationAlgorithm.java
@Override public void fit(HashMap<Byte, ArrayList<double[]>> x) { int i;//from www .jav a 2 s . c om double[] centroid; RealMatrix entities; this.centroids = new HashMap<>(); this.nClasses = x.size(); this.dimensionality = 0; for (byte label : x.keySet()) { if (this.dimensionality <= 0) this.dimensionality = x.get(label).get(0).length; entities = MatrixUtils.createRealMatrix(x.get(label).toArray(new double[0][])); centroid = new double[this.dimensionality]; for (i = 0; i < this.dimensionality; i++) centroid[i] = this.moment.evaluate(entities.getColumn(i)); this.centroids.put(label, centroid); } }