List of usage examples for org.apache.commons.math3.exception DimensionMismatchException DimensionMismatchException
public DimensionMismatchException(int wrong, int expected)
From source file:put.ci.cevo.framework.algorithms.ApacheCMAES.java
/** * Checks dimensions and values of boundaries and inputSigma if defined. *//*from w w w. j a v a 2 s .c om*/ private void checkParameters() { final double[] init = getStartPoint(); final double[] lB = getLowerBound(); final double[] uB = getUpperBound(); if (inputSigma != null) { if (inputSigma.length != init.length) { throw new DimensionMismatchException(inputSigma.length, init.length); } for (int i = 0; i < init.length; i++) { if (inputSigma[i] > uB[i] - lB[i]) { throw new OutOfRangeException(inputSigma[i], 0, uB[i] - lB[i]); } } } }
From source file:ro.hasna.ts.math.filter.MovingAverageFilter.java
/** * Creates a new instance of this class. * * @param observations the number of observations * @param symmetric smooth from both sides * @param weights the weights to be used for smoothing; it can be null * @throws DimensionMismatchException if the length of the weights is not equal to the number of observations * or the size of the window (2 * observations + 1) if the smoothing is symmetric */// w w w . ja va2 s. com public MovingAverageFilter(int observations, boolean symmetric, double[] weights) { this.observations = observations; this.symmetric = symmetric; if (weights != null) { if (!symmetric && weights.length != observations) { throw new DimensionMismatchException(weights.length, observations); } if (symmetric && weights.length != 2 * observations + 1) { throw new DimensionMismatchException(weights.length, 2 * observations + 1); } } this.weights = weights; }
From source file:ro.hasna.ts.math.representation.IndexableSymbolicAggregateApproximation.java
/** * Creates a new instance of this class with a given PAA and normalizer algorithm and * the breakpoints used to divide the distribution of the values. * * @param paa the Piecewise Aggregate Approximation algorithm * @param normalizer the normalizer (it can be null if the values were normalized) * @param alphabetSizes the size of the alphabet used to discretise the values for every segment * @param distributionDivider the divider that gets the list of breakpoints (values that divide a * distribution in equal areas of probability) * @throws DimensionMismatchException if the number of segments is different than the length of alphabetSizes */// w ww. j av a2 s . co m public IndexableSymbolicAggregateApproximation(PiecewiseAggregateApproximation paa, Normalizer normalizer, int[] alphabetSizes, DistributionDivider distributionDivider) { if (paa.getSegments() != alphabetSizes.length) { throw new DimensionMismatchException(alphabetSizes.length, paa.getSegments()); } this.paa = paa; this.normalizer = normalizer; this.alphabetSizes = alphabetSizes; this.distributionDivider = distributionDivider; }
From source file:stats.KendallsCorrelation.java
/** * Computes the Kendall's Tau rank correlation coefficient between the two * arrays./*w w w. j a va2s . c o m*/ * * @param xArray first data array * @param yArray second data array * @return Returns Kendall's Tau rank correlation coefficient for the two * arrays * @throws DimensionMismatchException if the arrays lengths do not match */ public Pair<Double, Double> correlation(final double[] xArray, final double[] yArray) throws DimensionMismatchException { if (xArray.length != yArray.length) { throw new DimensionMismatchException(xArray.length, yArray.length); } final int n = xArray.length; final double numPairs = ((double) n) * (n - 1) / 2; // to avoid overflow @SuppressWarnings("unchecked") Pair<Double, Double>[] pairs = new Pair[n]; for (int i = 0; i < n; i++) { pairs[i] = new Pair<Double, Double>(xArray[i], yArray[i]); } Arrays.sort(pairs, new Comparator<Pair<Double, Double>>() { @Override public int compare(Pair<Double, Double> pair1, Pair<Double, Double> pair2) { int compareFirst = pair1.getFirst().compareTo(pair2.getFirst()); return compareFirst != 0 ? compareFirst : pair1.getSecond().compareTo(pair2.getSecond()); } }); long tiedXPairs = 0; long tiedXYPairs = 0; long vt = 0; long consecutiveXTies = 1; long consecutiveXTiesSecondOrder = 1; long consecutiveXYTies = 1; Pair<Double, Double> prev = pairs[0]; for (int i = 1; i < n; i++) { final Pair<Double, Double> curr = pairs[i]; if (curr.getFirst().equals(prev.getFirst())) { consecutiveXTies++; if (curr.getSecond().equals(prev.getSecond())) { consecutiveXYTies++; } else { tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2; consecutiveXYTies = 1; } } else { tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2; consecutiveXTiesSecondOrder += consecutiveXTies * (consecutiveXTies - 1) * (consecutiveXTies - 2); vt += consecutiveXTies * (consecutiveXTies - 1) * (2 * consecutiveXTies + 5); consecutiveXTies = 1; tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2; consecutiveXYTies = 1; } prev = curr; } tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2; vt += consecutiveXTies * (consecutiveXTies - 1) * (2 * consecutiveXTies + 5); consecutiveXTiesSecondOrder += consecutiveXTies * (consecutiveXTies - 1) * (consecutiveXTies - 2); tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2; double swaps = 0; @SuppressWarnings("unchecked") Pair<Double, Double>[] pairsDestination = new Pair[n]; for (int segmentSize = 1; segmentSize < n; segmentSize <<= 1) { for (int offset = 0; offset < n; offset += 2 * segmentSize) { int i = offset; final int iEnd = FastMath.min(i + segmentSize, n); int j = iEnd; final int jEnd = FastMath.min(j + segmentSize, n); int copyLocation = offset; while (i < iEnd || j < jEnd) { if (i < iEnd) { if (j < jEnd) { if (pairs[i].getSecond().compareTo(pairs[j].getSecond()) <= 0) { pairsDestination[copyLocation] = pairs[i]; i++; } else { pairsDestination[copyLocation] = pairs[j]; j++; swaps += iEnd - i; } } else { pairsDestination[copyLocation] = pairs[i]; i++; } } else { pairsDestination[copyLocation] = pairs[j]; j++; } copyLocation++; } } final Pair<Double, Double>[] pairsTemp = pairs; pairs = pairsDestination; pairsDestination = pairsTemp; } long tiedYPairs = 0; long vu = 0; long consecutiveYTies = 1; long consecutiveYTiesSecondOrder = 0; prev = pairs[0]; for (int i = 1; i < n; i++) { final Pair<Double, Double> curr = pairs[i]; if (curr.getSecond().equals(prev.getSecond())) { consecutiveYTies++; } else { tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2; consecutiveYTiesSecondOrder += consecutiveYTies * (consecutiveYTies - 1) * (consecutiveYTies - 2); vu += consecutiveYTies * (consecutiveYTies - 1) * (2 * consecutiveYTies + 5); consecutiveYTies = 1; } prev = curr; } tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2; consecutiveYTiesSecondOrder += consecutiveYTies * (consecutiveYTies - 1) * (consecutiveYTies - 2); vu += consecutiveYTies * (consecutiveYTies - 1) * (2 * consecutiveYTies + 5); double concordantMinusDiscordant = numPairs - tiedXPairs - tiedYPairs + tiedXYPairs - 2 * swaps; double denom = FastMath.sqrt((numPairs - tiedXPairs) * (numPairs - tiedYPairs)); double v0 = n * (n - 1) * (2 * n + 5); double v1 = (double) tiedXPairs * tiedYPairs / numPairs; double v2 = n < 3 ? 0 : consecutiveXTiesSecondOrder * consecutiveYTiesSecondOrder / (9.0 * n * (n - 1) * (n - 2)); double v = ((v0 - vt - vu) / 18) + v1 + v2; double zB = concordantMinusDiscordant / FastMath.sqrt(v); return new Pair(denom != 0 ? concordantMinusDiscordant / denom : 0., 2 * new NormalDistribution().cumulativeProbability(-1 * Math.abs(zB))); }
From source file:stats.SpearmansCorrelation.java
/** * Computes the Spearman's rank correlation coefficient between the two * arrays./* w w w.j a v a2s. com*/ * * @param xArray * first data array * @param yArray * second data array * @return Returns Spearman's rank correlation coefficient for the two arrays * @throws DimensionMismatchException * if the arrays lengths do not match * @throws MathIllegalArgumentException * if the array length is less than 2 */ public Pair<Double, Double> correlation(final double[] xArray, final double[] yArray) { if (xArray.length != yArray.length) { throw new DimensionMismatchException(xArray.length, yArray.length); } else if (xArray.length < 2) { throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2); } else { double[] x = xArray; double[] y = yArray; if (rankingAlgorithm instanceof NaturalRanking && NaNStrategy.REMOVED == ((NaturalRanking) rankingAlgorithm).getNanStrategy()) { final Set<Integer> nanPositions = new HashSet<Integer>(); nanPositions.addAll(getNaNPositions(xArray)); nanPositions.addAll(getNaNPositions(yArray)); x = removeValues(xArray, nanPositions); y = removeValues(yArray, nanPositions); } double $ = new PearsonsCorrelation().correlation(rankingAlgorithm.rank(x), rankingAlgorithm.rank(y)); int degreesOfFreedom = x.length - 2; double t = $ * Math.sqrt(degreesOfFreedom / (1 - ($ * $))); double pValue = 2 * new TDistribution(degreesOfFreedom).cumulativeProbability(-1 * Math.abs(t)); return new Pair<>($, pValue); } }
From source file:xyz.lejon.sampling.FastMultivariateNormalDistribution.java
/** * Creates a multivariate normal distribution with the given mean vector and * covariance matrix.//from w w w . j a v a 2 s .c o m * <br/> * The number of dimensions is equal to the length of the mean vector * and to the number of rows and columns of the covariance matrix. * It is frequently written as "p" in formulae. * * @param rng Random Number Generator. * @param means Vector of means. * @param covariances Covariance matrix. * @throws DimensionMismatchException if the arrays length are * inconsistent. * @throws SingularMatrixException if the eigenvalue decomposition cannot * be performed on the provided covariance matrix. * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is * negative. */ public FastMultivariateNormalDistribution(RandomGenerator rng, final double[] means, final double[][] covariances) throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException { super(rng, means.length); final int dim = means.length; if (covariances.length != dim) { throw new DimensionMismatchException(covariances.length, dim); } for (int i = 0; i < dim; i++) { if (dim != covariances[i].length) { throw new DimensionMismatchException(covariances[i].length, dim); } } this.means = MathArrays.copyOf(means); covarianceMatrix = new Array2DRowRealMatrix(covariances); // Covariance matrix eigen decomposition. final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix); // Compute and store the inverse. covarianceMatrixInverse = covMatDec.getSolver().getInverse(); // Compute and store the determinant. covarianceMatrixDeterminant = covMatDec.getDeterminant(); // Eigenvalues of the covariance matrix. final double[] covMatEigenvalues = covMatDec.getRealEigenvalues(); for (int i = 0; i < covMatEigenvalues.length; i++) { if (covMatEigenvalues[i] < 0) { throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0); } } // Matrix where each column is an eigenvector of the covariance matrix. final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim); final Array2DRowRealMatrix tmpMatrix = new Array2DRowRealMatrix(dim, dim); for (int v = 0; v < dim; v++) { final double factor = FastMath.sqrt(covMatEigenvalues[v]); final double[] evec = covMatDec.getEigenvector(v).toArray(); covMatEigenvectors.setColumn(v, evec); tmpMatrix.setRow(v, evec); for (int col = 0; col < dim; col++) { tmpMatrix.multiplyEntry(v, col, factor); } } samplingMatrix = covMatEigenvectors.multiply(tmpMatrix); }