List of usage examples for org.apache.commons.math3.exception NotStrictlyPositiveException NotStrictlyPositiveException
public NotStrictlyPositiveException(Number value)
From source file:gdsc.core.utils.TurboRandomGenerator.java
@Override public int nextInt(int n) { if (n <= 0) { throw new NotStrictlyPositiveException(n); }// w ww . j av a 2s .c om return nextIntFast(n); }
From source file:jasima.core.random.continuous.DblNormal.java
/** * Sets the standard deviation of this normally distributed number stream. * This defaults to {@code 1.0}.//from ww w. j av a2 s.c o m * * @param stdev * The standard deviation. * @throws NotPositiveException * Raised if stdev was negative. */ public void setStdev(double stdev) throws NotPositiveException { if (stdev >= 0.0) { this.stdev = stdev; } else { throw new NotStrictlyPositiveException(stdev); } }
From source file:eu.tsp.sal.NPointCrossover.java
/** * Creates a new {@link NPointCrossover} policy using the given number of points. * <p>//from w w w . j av a 2s .com * <b>Note</b>: the number of crossover points must be < <code>chromosome length - 1</code>. * This condition can only be checked at runtime, as the chromosome length is not known in advance. * * @param crossoverPoints the number of crossover points * @throws NotStrictlyPositiveException if the number of {@code crossoverPoints} is not strictly positive */ public NPointCrossover(final int crossoverPoints) throws NotStrictlyPositiveException { if (crossoverPoints <= 0) { throw new NotStrictlyPositiveException(crossoverPoints); } this.crossoverPoints = crossoverPoints; }
From source file:com.clust4j.algo.preprocess.BoxCoxTransformer.java
private static double estimateLambdaSingle(double[] x, BoxCoxTransformer transformer, double lmin, double lmax) { BCOptimizer optimizer = new BCOptimizer(x, transformer); double best_lambda = new BrentDownhillOptimizer(optimizer, lmin, lmax).optimize(); if (Double.isNaN(best_lambda)) { throw new NotStrictlyPositiveException(best_lambda); }/* ww w . ja va 2s . c om*/ return best_lambda; }
From source file:com.github.joulupunikki.math.random.BitsStreamGenerator64.java
/** * Returns a pseudorandom, uniformly distributed {@code long} value between * 0 (inclusive) and the specified value (exclusive), drawn from this random * number generator's sequence. Identical with super, except with long * primitives we need just one call to nextL(int bits). * * @param n the bound on the random number to be returned. Must be positive. * @return a pseudorandom, uniformly distributed {@code long} value between * 0 (inclusive) and n (exclusive)./*from w w w . j a v a 2 s. co m*/ * @throws IllegalArgumentException if n is not positive. */ @Override public long nextLong(long n) throws IllegalArgumentException { if (n > 0) { long bits; long val; do { // the only modification, 2 * next() => 1 * nextL() bits = nextL(63); val = bits % n; } while (bits - val + (n - 1) < 0); return val; } throw new NotStrictlyPositiveException(n); }
From source file:gedi.util.math.stat.distributions.NormalMixtureDistribution.java
public static NormalMixtureDistribution fit(NormalMixtureDistribution initialMixture, double[] data, final int maxIterations, final double threshold) { if (maxIterations < 1) { throw new NotStrictlyPositiveException(maxIterations); }/* ww w .j a v a2 s . co m*/ if (threshold < Double.MIN_VALUE) { throw new NotStrictlyPositiveException(threshold); } final int n = data.length; final int k = initialMixture.getNumComponents(); if (k == 1) return new NormalMixtureDistribution(new NormalDistribution[] { new NormalDistribution(new Mean().evaluate(data), new StandardDeviation().evaluate(data)) }, new double[] { 1 }); int numIterations = 0; double previousLogLikelihood = 0d; double logLikelihood = Double.NEGATIVE_INFINITY; // Initialize model to fit to initial mixture. NormalMixtureDistribution fittedModel = new NormalMixtureDistribution(initialMixture.components, initialMixture.mixing); while (numIterations++ <= maxIterations && FastMath.abs(previousLogLikelihood - logLikelihood) > threshold) { previousLogLikelihood = logLikelihood; logLikelihood = 0d; // E-step: compute the data dependent parameters of the expectation // function. // The percentage of row's total density between a row and a // component final double[][] gamma = new double[n][k]; // Sum of gamma for each component final double[] gammaSums = new double[k]; for (int i = 0; i < n; i++) { final double rowDensity = fittedModel.density(data[i]); logLikelihood += FastMath.log(rowDensity); for (int j = 0; j < k; j++) { gamma[i][j] = fittedModel.mixing[j] * fittedModel.components[j].density(data[i]) / rowDensity; gammaSums[j] += gamma[i][j]; } } logLikelihood /= n; // System.out.println(logLikelihood); // M-step: compute the new parameters based on the expectation // function. final double[] newWeights = gammaSums.clone(); ArrayUtils.mult(newWeights, 1.0 / n); NormalDistribution[] comp = new NormalDistribution[k]; for (int j = 0; j < k; j++) { double m = 0; for (int i = 0; i < n; i++) { m += gamma[i][j] * data[i]; } m /= gammaSums[j]; double var = 0; for (int i = 0; i < n; i++) { double d = m - data[i]; var += gamma[i][j] * d * d; } var /= gammaSums[j]; comp[j] = new NormalDistribution(m, Math.sqrt(var)); } // Update current model fittedModel = new NormalMixtureDistribution(comp, newWeights); } if (FastMath.abs(previousLogLikelihood - logLikelihood) > threshold) { // Did not converge before the maximum number of iterations throw new ConvergenceException(); } return fittedModel; }
From source file:gedi.util.math.stat.distributions.NormalMixtureDistribution.java
public static NormalMixtureDistribution init(final double[] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException { if (numComponents == 1) return new NormalMixtureDistribution(new NormalDistribution[] { new NormalDistribution(new Mean().evaluate(data), new StandardDeviation().evaluate(data)) }, new double[] { 1 }); if (data.length < 2) { throw new NotStrictlyPositiveException(data.length); }//w w w .j a v a 2 s . co m if (numComponents < 1) { throw new NumberIsTooSmallException(numComponents, 2, true); } if (numComponents > data.length) { throw new NumberIsTooLargeException(numComponents, data.length, true); } final int numRows = data.length; double[] sortedData = data.clone(); Arrays.sort(sortedData); // components of mixture model to be created double[] mixing = new double[numComponents]; NormalDistribution[] comp = new NormalDistribution[numComponents]; // create a component based on data in each bin for (int k = 0; k < numComponents; k++) { // minimum index (inclusive) from sorted data for this bin final int minIndex = (k * numRows) / numComponents; // maximum index (exclusive) from sorted data for this bin final int maxIndex = Math.min(numRows, ((k + 1) * numRows) / numComponents); double m = new Mean().evaluate(sortedData, minIndex, maxIndex - minIndex); double sd = new StandardDeviation().evaluate(sortedData, minIndex, maxIndex - minIndex); mixing[k] = 1d / numComponents; comp[k] = new NormalDistribution(m, sd); } return new NormalMixtureDistribution(comp, mixing); }
From source file:com.github.joulupunikki.math.random.BitsStreamGenerator64.java
/** * Seed with strictly positive long seed. The high and low 32 bits of seed * are converted to two doubles/* w w w . j a v a 2 s . com*/ * <code>cos_idx = 1 + (seed >> 32) * dim;</code> and * <code>sin_idx = 1 + ((seed & LSB32_MASK_LONG) - 1) * dim;</code> where * <code>dim = 2 * STATE_WORDS</code>. These doubles are used as indexes to * create an int array of length <code>dim<\code> like so * <pre>array[i] = * (int) (Double.doubleToRawLongBits(Math.sin(sin_idx + i)) ^ * Double.doubleToRawLongBits(Math.cos(cos_idx + i)));</pre> so the 32 LSBs * of (sin(sin_idx + i) XOR cos(cos_idx + i)) are used. Since the period of * sin and cos is Π, in theory these values should never repeat for * integer arguments. In practice this may not hold due to double * approximation of numbers in ℝ * * @param seed * @return */ public int[] sineSeed(long seed) { if (seed < 1) { throw new NotStrictlyPositiveException(seed); } int dim = STATE_WORDS * 2 + 1; int[] seed_array = new int[dim]; double cos_idx = 1 + (seed >> 32) * dim; double sin_idx = 1 + ((seed & LSB32_MASK_LONG) - 1) * dim; for (int i = 0; i < seed_array.length; i++) { sin_idx += 1; cos_idx += 1; int a = (int) Double.doubleToRawLongBits(FastMath.sin(sin_idx)); int b = (int) Double.doubleToRawLongBits(FastMath.cos(cos_idx)); seed_array[i] = a ^ b; } for (int i = 0; i < seed_array.length - 1; i++) { seed_array[i] ^= seed_array[i + 1]; } return seed_array; }
From source file:org.briljantframework.array.Arrays.java
/** * Construct an array by repeating the given array the given number of times (per dimension). * * <p/>// w w w.ja va 2s . c om * The constructed array has the same dimension as {@code Math.max(reps.length, x.dims())}. * * <p/> * If {@code x.dims() < reps.length}, {@code x} is promoted to a {@code reps.length}-dimensional * array by prepending dimensions of size {@code 1}. For example, if {@code x} has shape 5 and the * replication is {@code new int[2]} the resulting array is promoted to 2d. * * <p/> * If {@code reps.length < x.dims()}, the replication array is prepended with ones. * * <p/> * Example * * <pre> * IntArray a = Range.of(3); * Arrays.tile(a, 2); * </pre> * * produces * * <pre> * array([0, 1, 2, 0, 1, 2]) * </pre> * * and * * <pre> * Arrays.tile(a, 2, 2); * </pre> * * produces * * <pre> * array([[0, 1, 2, 0, 1, 2], * [0, 1, 2, 0, 1, 2]]) * </pre> * * and * * <pre> * DoubleArray b = Arrays.newDoubleMatrix(new double[][] { {1, 2}, {3, 4}}); * Arrays.tile(b, 2); * </pre> * * produces * * <pre> * array([[1.000, 2.000, 1.000, 2.000], * [3.000, 4.000, 3.000, 4.000]]) * </pre> * * and * * <pre> * Arrays.tile(b, 2, 1); * </pre> * * produces * * <pre> * array([[1.000, 2.000], * [3.000, 4.000], * [1.000, 2.000], * [3.000, 4.000]]) * </pre> * * @param x the array * @param reps the number of replications per dimension * @param <S> the array type * @return a new array */ public static <S extends BaseArray<S>> S tile(S x, int... reps) { int dims = Math.max(x.dims(), reps.length); if (x.dims() < reps.length) { x = x.reshape(prependDimension(x.getShape(), dims)); } else if (reps.length < x.dims()) { reps = prependDimension(reps, dims); } int[] shape = new int[dims]; for (int i = 0; i < shape.length; i++) { int rep = reps[i]; if (rep <= 0) { throw new NotStrictlyPositiveException(rep); } shape[i] = x.size(i) * rep; } S array = x.newEmptyArray(shape); tile(array, x, 0, reps); return array; }
From source file:org.gitools.analysis.groupcomparison.format.math33Preview.MathArrays.java
/** * Check that all entries of the input array are strictly positive. * * @param in Array to be tested//w w w .j a v a 2s . co m * @throws NotStrictlyPositiveException if any entries of the array are not * strictly positive. * @since 3.1 */ public static void checkPositive(final double[] in) throws NotStrictlyPositiveException { for (int i = 0; i < in.length; i++) { if (in[i] <= 0) { throw new NotStrictlyPositiveException(in[i]); } } }