Java Code Examples for org.apache.commons.math3.util.MathArrays#scaleInPlace()
The following examples show how to use
org.apache.commons.math3.util.MathArrays#scaleInPlace() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FuzzyKMeansClusterer.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Update the cluster centers. */ private void updateClusterCenters() { int j = 0; final List<CentroidCluster<T>> newClusters = new ArrayList<CentroidCluster<T>>(k); for (final CentroidCluster<T> cluster : clusters) { final Clusterable center = cluster.getCenter(); int i = 0; double[] arr = new double[center.getPoint().length]; double sum = 0.0; for (final T point : points) { final double u = FastMath.pow(membershipMatrix[i][j], fuzziness); final double[] pointArr = point.getPoint(); for (int idx = 0; idx < arr.length; idx++) { arr[idx] += u * pointArr[idx]; } sum += u; i++; } MathArrays.scaleInPlace(1.0 / sum, arr); newClusters.add(new CentroidCluster<T>(new DoublePoint(arr))); j++; } clusters.clear(); clusters = newClusters; }
Example 2
Source File: FuzzyKMeansClusterer.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Update the cluster centers. */ private void updateClusterCenters() { int j = 0; final List<CentroidCluster<T>> newClusters = new ArrayList<CentroidCluster<T>>(k); for (final CentroidCluster<T> cluster : clusters) { final Clusterable center = cluster.getCenter(); int i = 0; double[] arr = new double[center.getPoint().length]; double sum = 0.0; for (final T point : points) { final double u = FastMath.pow(membershipMatrix[i][j], fuzziness); final double[] pointArr = point.getPoint(); for (int idx = 0; idx < arr.length; idx++) { arr[idx] += u * pointArr[idx]; } sum += u; i++; } MathArrays.scaleInPlace(1.0 / sum, arr); newClusters.add(new CentroidCluster<T>(new DoublePoint(arr))); j++; } clusters.clear(); clusters = newClusters; }
Example 3
Source File: FuzzyKMeansClusterer.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Update the cluster centers. */ private void updateClusterCenters() { int j = 0; final List<CentroidCluster<T>> newClusters = new ArrayList<CentroidCluster<T>>(k); for (final CentroidCluster<T> cluster : clusters) { final Clusterable center = cluster.getCenter(); int i = 0; double[] arr = new double[center.getPoint().length]; double sum = 0.0; for (final T point : points) { final double u = FastMath.pow(membershipMatrix[i][j], fuzziness); final double[] pointArr = point.getPoint(); for (int idx = 0; idx < arr.length; idx++) { arr[idx] += u * pointArr[idx]; } sum += u; i++; } MathArrays.scaleInPlace(1.0 / sum, arr); newClusters.add(new CentroidCluster<T>(new DoublePoint(arr))); j++; } clusters.clear(); clusters = newClusters; }
Example 4
Source File: FuzzyKMeansClusterer.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Update the cluster centers. */ private void updateClusterCenters() { int j = 0; final List<CentroidCluster<T>> newClusters = new ArrayList<CentroidCluster<T>>(k); for (final CentroidCluster<T> cluster : clusters) { final Clusterable center = cluster.getCenter(); int i = 0; double[] arr = new double[center.getPoint().length]; double sum = 0.0; for (final T point : points) { final double u = FastMath.pow(membershipMatrix[i][j], fuzziness); final double[] pointArr = point.getPoint(); for (int idx = 0; idx < arr.length; idx++) { arr[idx] += u * pointArr[idx]; } sum += u; i++; } MathArrays.scaleInPlace(1.0 / sum, arr); newClusters.add(new CentroidCluster<T>(new DoublePoint(arr))); j++; } clusters.clear(); clusters = newClusters; }
Example 5
Source File: Mutect2FilteringEngine.java From gatk with BSD 3-Clause "New" or "Revised" License | 5 votes |
public double[] weightedAverageOfTumorAFs(final VariantContext vc) { final MutableDouble totalWeight = new MutableDouble(0); final double[] AFs = new double[vc.getNAlleles() - 1]; vc.getGenotypes().stream().filter(this::isTumor).forEach(g -> { final double weight = MathUtils.sum(g.getAD()); totalWeight.add(weight); final double[] sampleAFs = VariantContextGetters.getAttributeAsDoubleArray(g, VCFConstants.ALLELE_FREQUENCY_KEY, () -> new double[] {0.0}, 0.0); MathArrays.scaleInPlace(weight, sampleAFs); MathUtils.addToArrayInPlace(AFs, sampleAFs); }); MathArrays.scaleInPlace(1/totalWeight.getValue(), AFs); return AFs; }
Example 6
Source File: GmmSemi.java From orbit-image-analysis with GNU General Public License v3.0 | 4 votes |
/** * Helper method to create a multivariate normal mixture model which can be * used to initialize {@link #fit(MixtureMultivariateNormalDistribution)}. * * This method uses the data supplied to the constructor to try to determine * a good mixture model at which to start the fit, but it is not guaranteed * to supply a model which will find the optimal solution or even converge. * * @param data Data to estimate distribution * @param numComponents Number of components for estimated mixture * @return Multivariate normal mixture model estimated from the data * @throws NumberIsTooLargeException if {@code numComponents} is greater * than the number of data rows. * @throws NumberIsTooSmallException if {@code numComponents < 2}. * @throws NotStrictlyPositiveException if data has less than 2 rows * @throws DimensionMismatchException if rows of data have different numbers * of columns */ public static MixtureMultivariateNormalDistribution estimate(final double[][] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException { if (data.length < 2) { throw new NotStrictlyPositiveException(data.length); } if (numComponents < 2) { throw new NumberIsTooSmallException(numComponents, 2, true); } if (numComponents > data.length) { throw new NumberIsTooLargeException(numComponents, data.length, true); } final int numRows = data.length; final int numCols = data[0].length; // sort the data final DataRow[] sortedData = new DataRow[numRows]; for (int i = 0; i < numRows; i++) { sortedData[i] = new DataRow(data[i]); } Arrays.sort(sortedData); // uniform weight for each bin final double weight = 1d / numComponents; // components of mixture model to be created final List<Pair<Double, MultivariateNormalDistribution>> components = new ArrayList<Pair<Double, MultivariateNormalDistribution>>(numComponents); // create a component based on data in each bin for (int binIndex = 0; binIndex < numComponents; binIndex++) { // minimum index (inclusive) from sorted data for this bin final int minIndex = (binIndex * numRows) / numComponents; // maximum index (exclusive) from sorted data for this bin final int maxIndex = ((binIndex + 1) * numRows) / numComponents; // number of data records that will be in this bin final int numBinRows = maxIndex - minIndex; // data for this bin final double[][] binData = new double[numBinRows][numCols]; // mean of each column for the data in the this bin final double[] columnMeans = new double[numCols]; // populate bin and create component for (int i = minIndex, iBin = 0; i < maxIndex; i++, iBin++) { for (int j = 0; j < numCols; j++) { final double val = sortedData[i].getRow()[j]; columnMeans[j] += val; binData[iBin][j] = val; } } MathArrays.scaleInPlace(1d / numBinRows, columnMeans); // covariance matrix for this bin final double[][] covMat = new Covariance(binData).getCovarianceMatrix().getData(); final MultivariateNormalDistribution mvn = new MultivariateNormalDistribution(columnMeans, covMat); components.add(new Pair<Double, MultivariateNormalDistribution>(weight, mvn)); } return new MixtureMultivariateNormalDistribution(components); }
Example 7
Source File: Spectrum.java From cineast with MIT License | 4 votes |
/** * */ public void normalize() { MathArrays.scaleInPlace(1.0/this.getMaximum().second, this.spectrum); }
Example 8
Source File: MultivariateNormalMixtureExpectationMaximization.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Helper method to create a multivariate normal mixture model which can be * used to initialize {@link #fit(MixtureMultivariateNormalDistribution)}. * * This method uses the data supplied to the constructor to try to determine * a good mixture model at which to start the fit, but it is not guaranteed * to supply a model which will find the optimal solution or even converge. * * @param data Data to estimate distribution * @param numComponents Number of components for estimated mixture * @return Multivariate normal mixture model estimated from the data * @throws NumberIsTooLargeException if {@code numComponents} is greater * than the number of data rows. * @throws NumberIsTooSmallException if {@code numComponents < 2}. * @throws NotStrictlyPositiveException if data has less than 2 rows * @throws DimensionMismatchException if rows of data have different numbers * of columns */ public static MixtureMultivariateNormalDistribution estimate(final double[][] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException { if (data.length < 2) { throw new NotStrictlyPositiveException(data.length); } if (numComponents < 2) { throw new NumberIsTooSmallException(numComponents, 2, true); } if (numComponents > data.length) { throw new NumberIsTooLargeException(numComponents, data.length, true); } final int numRows = data.length; final int numCols = data[0].length; // sort the data final DataRow[] sortedData = new DataRow[numRows]; for (int i = 0; i < numRows; i++) { sortedData[i] = new DataRow(data[i]); } Arrays.sort(sortedData); // uniform weight for each bin final double weight = 1d / numComponents; // components of mixture model to be created final List<Pair<Double, MultivariateNormalDistribution>> components = new ArrayList<Pair<Double, MultivariateNormalDistribution>>(numComponents); // create a component based on data in each bin for (int binIndex = 0; binIndex < numComponents; binIndex++) { // minimum index (inclusive) from sorted data for this bin final int minIndex = (binIndex * numRows) / numComponents; // maximum index (exclusive) from sorted data for this bin final int maxIndex = ((binIndex + 1) * numRows) / numComponents; // number of data records that will be in this bin final int numBinRows = maxIndex - minIndex; // data for this bin final double[][] binData = new double[numBinRows][numCols]; // mean of each column for the data in the this bin final double[] columnMeans = new double[numCols]; // populate bin and create component for (int i = minIndex, iBin = 0; i < maxIndex; i++, iBin++) { for (int j = 0; j < numCols; j++) { final double val = sortedData[i].getRow()[j]; columnMeans[j] += val; binData[iBin][j] = val; } } MathArrays.scaleInPlace(1d / numBinRows, columnMeans); // covariance matrix for this bin final double[][] covMat = new Covariance(binData).getCovarianceMatrix().getData(); final MultivariateNormalDistribution mvn = new MultivariateNormalDistribution(columnMeans, covMat); components.add(new Pair<Double, MultivariateNormalDistribution>(weight, mvn)); } return new MixtureMultivariateNormalDistribution(components); }
Example 9
Source File: MultivariateNormalMixtureExpectationMaximization.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Helper method to create a multivariate normal mixture model which can be * used to initialize {@link #fit(MixtureMultivariateNormalDistribution)}. * * This method uses the data supplied to the constructor to try to determine * a good mixture model at which to start the fit, but it is not guaranteed * to supply a model which will find the optimal solution or even converge. * * @param data Data to estimate distribution * @param numComponents Number of components for estimated mixture * @return Multivariate normal mixture model estimated from the data * @throws NumberIsTooLargeException if {@code numComponents} is greater * than the number of data rows. * @throws NumberIsTooSmallException if {@code numComponents < 2}. * @throws NotStrictlyPositiveException if data has less than 2 rows * @throws DimensionMismatchException if rows of data have different numbers * of columns */ public static MixtureMultivariateNormalDistribution estimate(final double[][] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException { if (data.length < 2) { throw new NotStrictlyPositiveException(data.length); } if (numComponents < 2) { throw new NumberIsTooSmallException(numComponents, 2, true); } if (numComponents > data.length) { throw new NumberIsTooLargeException(numComponents, data.length, true); } final int numRows = data.length; final int numCols = data[0].length; // sort the data final DataRow[] sortedData = new DataRow[numRows]; for (int i = 0; i < numRows; i++) { sortedData[i] = new DataRow(data[i]); } Arrays.sort(sortedData); // uniform weight for each bin final double weight = 1d / numComponents; // components of mixture model to be created final List<Pair<Double, MultivariateNormalDistribution>> components = new ArrayList<Pair<Double, MultivariateNormalDistribution>>(numComponents); // create a component based on data in each bin for (int binIndex = 0; binIndex < numComponents; binIndex++) { // minimum index (inclusive) from sorted data for this bin final int minIndex = (binIndex * numRows) / numComponents; // maximum index (exclusive) from sorted data for this bin final int maxIndex = ((binIndex + 1) * numRows) / numComponents; // number of data records that will be in this bin final int numBinRows = maxIndex - minIndex; // data for this bin final double[][] binData = new double[numBinRows][numCols]; // mean of each column for the data in the this bin final double[] columnMeans = new double[numCols]; // populate bin and create component for (int i = minIndex, iBin = 0; i < maxIndex; i++, iBin++) { for (int j = 0; j < numCols; j++) { final double val = sortedData[i].getRow()[j]; columnMeans[j] += val; binData[iBin][j] = val; } } MathArrays.scaleInPlace(1d / numBinRows, columnMeans); // covariance matrix for this bin final double[][] covMat = new Covariance(binData).getCovarianceMatrix().getData(); final MultivariateNormalDistribution mvn = new MultivariateNormalDistribution(columnMeans, covMat); components.add(new Pair<Double, MultivariateNormalDistribution>(weight, mvn)); } return new MixtureMultivariateNormalDistribution(components); }
Example 10
Source File: MultivariateNormalMixtureExpectationMaximization.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Helper method to create a multivariate normal mixture model which can be * used to initialize {@link #fit(MixtureMultivariateRealDistribution)}. * * This method uses the data supplied to the constructor to try to determine * a good mixture model at which to start the fit, but it is not guaranteed * to supply a model which will find the optimal solution or even converge. * * @param data Data to estimate distribution * @param numComponents Number of components for estimated mixture * @return Multivariate normal mixture model estimated from the data * @throws NumberIsTooLargeException if {@code numComponents\ is greater * than the number of data rows. * @throws NumberIsTooSmallException if {@code numComponents < 2}. * @throws NotStrictlyPositiveException if data has less than 2 rows * @throws DimensionMismatchException if rows of data have different numbers * of columns * @see #fit */ public static MixtureMultivariateNormalDistribution estimate(final double[][] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException { if (data.length < 2) { throw new NotStrictlyPositiveException(data.length); } if (numComponents < 2) { throw new NumberIsTooSmallException(numComponents, 2, true); } if (numComponents > data.length) { throw new NumberIsTooLargeException(numComponents, data.length, true); } final int numRows = data.length; final int numCols = data[0].length; // sort the data final DataRow[] sortedData = new DataRow[numRows]; for (int i = 0; i < numRows; i++) { sortedData[i] = new DataRow(data[i]); } Arrays.sort(sortedData); final int totalBins = numComponents; // uniform weight for each bin final double weight = 1d / totalBins; // components of mixture model to be created final List<Pair<Double, MultivariateNormalDistribution>> components = new ArrayList<Pair<Double, MultivariateNormalDistribution>>(); // create a component based on data in each bin for (int binNumber = 1; binNumber <= totalBins; binNumber++) { // minimum index from sorted data for this bin final int minIndex = (int) FastMath.max(0, FastMath.floor((binNumber - 1) * numRows / totalBins)); // maximum index from sorted data for this bin final int maxIndex = (int) FastMath.ceil(binNumber * numRows / numComponents) - 1; // number of data records that will be in this bin final int numBinRows = maxIndex - minIndex + 1; // data for this bin final double[][] binData = new double[numBinRows][numCols]; // mean of each column for the data in the this bin final double[] columnMeans = new double[numCols]; // populate bin and create component for (int i = minIndex, iBin = 0; i <= maxIndex; i++, iBin++) { for (int j = 0; j < numCols; j++) { final double val = sortedData[i].getRow()[j]; columnMeans[j] += val; binData[iBin][j] = val; } } MathArrays.scaleInPlace(1d / numBinRows, columnMeans); // covariance matrix for this bin final double[][] covMat = new Covariance(binData).getCovarianceMatrix().getData(); final MultivariateNormalDistribution mvn = new MultivariateNormalDistribution(columnMeans, covMat); components.add(new Pair<Double, MultivariateNormalDistribution>(weight, mvn)); } return new MixtureMultivariateNormalDistribution(components); }
Example 11
Source File: MultivariateNormalMixtureExpectationMaximization.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Helper method to create a multivariate normal mixture model which can be * used to initialize {@link #fit(MixtureMultivariateNormalDistribution)}. * * This method uses the data supplied to the constructor to try to determine * a good mixture model at which to start the fit, but it is not guaranteed * to supply a model which will find the optimal solution or even converge. * * @param data Data to estimate distribution * @param numComponents Number of components for estimated mixture * @return Multivariate normal mixture model estimated from the data * @throws NumberIsTooLargeException if {@code numComponents} is greater * than the number of data rows. * @throws NumberIsTooSmallException if {@code numComponents < 2}. * @throws NotStrictlyPositiveException if data has less than 2 rows * @throws DimensionMismatchException if rows of data have different numbers * of columns */ public static MixtureMultivariateNormalDistribution estimate(final double[][] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException { if (data.length < 2) { throw new NotStrictlyPositiveException(data.length); } if (numComponents < 2) { throw new NumberIsTooSmallException(numComponents, 2, true); } if (numComponents > data.length) { throw new NumberIsTooLargeException(numComponents, data.length, true); } final int numRows = data.length; final int numCols = data[0].length; // sort the data final DataRow[] sortedData = new DataRow[numRows]; for (int i = 0; i < numRows; i++) { sortedData[i] = new DataRow(data[i]); } Arrays.sort(sortedData); // uniform weight for each bin final double weight = 1d / numComponents; // components of mixture model to be created final List<Pair<Double, MultivariateNormalDistribution>> components = new ArrayList<Pair<Double, MultivariateNormalDistribution>>(); // create a component based on data in each bin for (int binIndex = 0; binIndex < numComponents; binIndex++) { // minimum index (inclusive) from sorted data for this bin final int minIndex = (binIndex * numRows) / numComponents; // maximum index (exclusive) from sorted data for this bin final int maxIndex = ((binIndex + 1) * numRows) / numComponents; // number of data records that will be in this bin final int numBinRows = maxIndex - minIndex; // data for this bin final double[][] binData = new double[numBinRows][numCols]; // mean of each column for the data in the this bin final double[] columnMeans = new double[numCols]; // populate bin and create component for (int i = minIndex, iBin = 0; i < maxIndex; i++, iBin++) { for (int j = 0; j < numCols; j++) { final double val = sortedData[i].getRow()[j]; columnMeans[j] += val; binData[iBin][j] = val; } } MathArrays.scaleInPlace(1d / numBinRows, columnMeans); // covariance matrix for this bin final double[][] covMat = new Covariance(binData).getCovarianceMatrix().getData(); final MultivariateNormalDistribution mvn = new MultivariateNormalDistribution(columnMeans, covMat); components.add(new Pair<Double, MultivariateNormalDistribution>(weight, mvn)); } return new MixtureMultivariateNormalDistribution(components); }
Example 12
Source File: MultivariateNormalMixtureExpectationMaximization.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Helper method to create a multivariate normal mixture model which can be * used to initialize {@link #fit(MixtureMultivariateNormalDistribution)}. * * This method uses the data supplied to the constructor to try to determine * a good mixture model at which to start the fit, but it is not guaranteed * to supply a model which will find the optimal solution or even converge. * * @param data Data to estimate distribution * @param numComponents Number of components for estimated mixture * @return Multivariate normal mixture model estimated from the data * @throws NumberIsTooLargeException if {@code numComponents} is greater * than the number of data rows. * @throws NumberIsTooSmallException if {@code numComponents < 2}. * @throws NotStrictlyPositiveException if data has less than 2 rows * @throws DimensionMismatchException if rows of data have different numbers * of columns */ public static MixtureMultivariateNormalDistribution estimate(final double[][] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException { if (data.length < 2) { throw new NotStrictlyPositiveException(data.length); } if (numComponents < 2) { throw new NumberIsTooSmallException(numComponents, 2, true); } if (numComponents > data.length) { throw new NumberIsTooLargeException(numComponents, data.length, true); } final int numRows = data.length; final int numCols = data[0].length; // sort the data final DataRow[] sortedData = new DataRow[numRows]; for (int i = 0; i < numRows; i++) { sortedData[i] = new DataRow(data[i]); } Arrays.sort(sortedData); // uniform weight for each bin final double weight = 1d / numComponents; // components of mixture model to be created final List<Pair<Double, MultivariateNormalDistribution>> components = new ArrayList<Pair<Double, MultivariateNormalDistribution>>(numComponents); // create a component based on data in each bin for (int binIndex = 0; binIndex < numComponents; binIndex++) { // minimum index (inclusive) from sorted data for this bin final int minIndex = (binIndex * numRows) / numComponents; // maximum index (exclusive) from sorted data for this bin final int maxIndex = ((binIndex + 1) * numRows) / numComponents; // number of data records that will be in this bin final int numBinRows = maxIndex - minIndex; // data for this bin final double[][] binData = new double[numBinRows][numCols]; // mean of each column for the data in the this bin final double[] columnMeans = new double[numCols]; // populate bin and create component for (int i = minIndex, iBin = 0; i < maxIndex; i++, iBin++) { for (int j = 0; j < numCols; j++) { final double val = sortedData[i].getRow()[j]; columnMeans[j] += val; binData[iBin][j] = val; } } MathArrays.scaleInPlace(1d / numBinRows, columnMeans); // covariance matrix for this bin final double[][] covMat = new Covariance(binData).getCovarianceMatrix().getData(); final MultivariateNormalDistribution mvn = new MultivariateNormalDistribution(columnMeans, covMat); components.add(new Pair<Double, MultivariateNormalDistribution>(weight, mvn)); } return new MixtureMultivariateNormalDistribution(components); }