List of usage examples for org.apache.commons.math3.analysis UnivariateFunction value
double value(double x);
From source file:eu.crisis_economics.abm.bank.strategies.TimeSeriesCatalogue.java
static public void main(String[] args) { System.out.println("testing TimeSeriesCatalogue type.."); {/*from www. jav a2 s . c o m*/ UnivariateFunction suspendedSinusoid = TimeSeriesCatalogue.sinusoidalFunction(10., 1., 10.); Double[] expectedValues = { 5.500, 8.145, 9.779, 9.779, 8.145, 5.500, 2.854, 1.220, 1.220, 2.854 }; for (int i = 0; i < 10; ++i) { final double expectedValue = expectedValues[i], gainedValue = suspendedSinusoid.value(i); Assert.assertEquals(expectedValue, gainedValue, 1.e-2); } } { UnivariateFunction constantReturnFunction = TimeSeriesCatalogue.constantFunction(1.); Double[] expectedValues = { 1., 1., 1., 1., 1., 1., 1., 1., 1., 1. }; for (int i = 0; i < 10; ++i) { final double expectedValue = expectedValues[i], gainedValue = constantReturnFunction.value(i); Assert.assertEquals(expectedValue, gainedValue, 1.e-5); } } { UnivariateFunction squareWaveFunction = TimeSeriesCatalogue.squareWaveFunction(10., 1., 10.); Double[] expectedValues = { 1., 1., 1., 1., 1., 10., 10., 10., 10., 10. }; for (int i = 0; i < 10; ++i) { final double expectedValue = expectedValues[i], gainedValue = squareWaveFunction.value(i); Assert.assertEquals(expectedValue, gainedValue, 1.e-5); } } System.out.println("TimeSeriesCatalogue tests ok."); }
From source file:com.sciaps.utils.Util.java
public static Spectrum createAverage(Collection<? extends Spectrum> shots, double sampleRate) { Min minWL = new Min(); Max maxWL = new Max(); for (Spectrum shot : shots) { minWL.increment(shot.getValidRange().getMinimumDouble()); maxWL.increment(shot.getValidRange().getMaximumDouble()); }/*from w ww . j a v a2 s .c om*/ double range = maxWL.getResult() - minWL.getResult(); int numSamples = (int) Math.floor(range * sampleRate); double[][] data = new double[2][numSamples]; Mean avgy = new Mean(); for (int i = 0; i < numSamples; i++) { double x = minWL.getResult() + i * (1 / sampleRate); avgy.clear(); for (Spectrum shot : shots) { if (shot.getValidRange().containsDouble(x)) { UnivariateFunction iv = shot.getIntensityFunction(); double y = iv.value(x); avgy.increment(y); } } data[0][i] = x; data[1][i] = avgy.getResult(); } RawDataSpectrum newSpectrum = new RawDataSpectrum(data); return newSpectrum; }
From source file:com.google.cloud.genomics.dataflow.functions.verifybamid.Solver.java
/** * Runs a grid search for the maximum value of a univariate function. * * @param fn the likelihood function to minimize * @param start lower bound of the interval to search * @param end upper bound of the interval to search * @param step grid step size/* w w w. j a v a 2s . c o m*/ * @return an Interval bracketing the minimum */ static Interval gridSearch(UnivariateFunction fn, double start, double end, double step) { double lowMax = start; // lower bound on interval surrounding alphaMax double alphaMax = start - step; double likMax = 0.0; double lastAlpha = start; double alpha = start; while (alpha < end) { double likelihood = fn.value(alpha); if (alphaMax < start || likelihood > likMax) { lowMax = lastAlpha; alphaMax = alpha; likMax = likelihood; } lastAlpha = alpha; alpha += step; } // make sure we've checked the rightmost endpoint (won't happen if // end - start is not an integer multiple of step, because of roundoff // errors, etc) double likelihood = fn.value(end); if (likelihood > likMax) { lowMax = lastAlpha; alphaMax = end; likMax = likelihood; } return new Interval(lowMax, Math.min(end, alphaMax + step)); }
From source file:com.google.cloud.genomics.dataflow.utils.Solver.java
/** * Runs a grid search for the maximum value of a univariate function. * /*from w w w . j a va2s.c o m*/ * @param fn the likelihood function to minimize * @param start lower bound of the interval to search * @param end upper bound of the interval to search * @param step grid step size * @return an Interval bracketing the minimum */ static Interval gridSearch(UnivariateFunction fn, double start, double end, double step) { double lowMax = start; // lower bound on interval surrounding alphaMax double alphaMax = start - step; double likMax = 0.0; double lastAlpha = start; double alpha = start; while (alpha < end) { double likelihood = fn.value(alpha); if (alphaMax < start || likelihood > likMax) { lowMax = lastAlpha; alphaMax = alpha; likMax = likelihood; } lastAlpha = alpha; alpha += step; } // make sure we've checked the rightmost endpoint (won't happen if // end - start is not an integer multiple of step, because of roundoff // errors, etc) double likelihood = fn.value(end); if (likelihood > likMax) { lowMax = lastAlpha; alphaMax = end; likMax = likelihood; } return new Interval(lowMax, Math.min(end, alphaMax + step)); }
From source file:com.sciaps.utils.Util.java
public static void populateXYSeriesData(SpectrumShotItem shotItem) { double[] x = shotItem.getShot().getPixelLocations(); double[] y = new double[x.length]; UnivariateFunction yfun = shotItem.getShot().getIntensityFunction(); for (int i = 0; i < x.length; i++) { y[i] = yfun.value(x[i]); }//from www .j av a 2 s . c om for (int i = 0; i < x.length; i++) { shotItem.getXYSeries().add(x[i], y[i]); } }
From source file:gedi.util.math.stat.distributions.GeneralizedExtremeValueDistribution.java
public static GeneralizedExtremeValueDistribution fitDataByMoments(double[] data, int start, int end) { int n = end - start; if (n < 2) throw new RuntimeException("Too few data!"); Arrays.sort(data, start, end); // compute moments final double[] b = new double[3]; for (int j = 1; j <= n; j++) { int index = j - 1 - start; b[0] += data[index];//from www. j a v a 2s . c o m b[1] += data[index] * ((j - 1.0) / (n - 1.0)); b[2] += data[index] * ((j - 1.0) / (n - 1.0)) * ((j - 2.0) / (n - 2.0)); } b[0] /= n; b[1] /= n; b[2] /= n; // solve parameters UnivariateFunction f = new UnivariateFunction() { public double value(double k) { return (3 * b[2] - b[0]) / (2 * b[1] - b[0]) - (1 - Math.pow(3, -k)) / (1 - Math.pow(2, -k)); } }; double k; if (Math.signum(f.value(-0.5)) != Math.signum(f.value(0.5))) k = UnivariateSolverUtils.solve(f, -0.5, 0.5); else { double c = (2 * b[1] - b[0]) / (3 * b[2] - b[0]) - Math.log(2) / Math.log(3); k = 7.859 * c + 2.9554 * c * c; } double g = Gamma.gamma(1 + k); double alpha = ((2 * b[1] - b[0]) * k) / (g * (1 - Math.pow(2, -k))); double xi = b[0] + alpha * (g - 1) / k; double location = xi; double scale = alpha; double shape = -k; return new GeneralizedExtremeValueDistribution(location, scale, shape); }
From source file:area.math.nm.lib.Interpolation.java
public String splineInterpolation(double x[], double y[], double input) { UnivariateInterpolator interpolator = new SplineInterpolator(); UnivariateFunction function = interpolator.interpolate(x, y); double interpolationX = input; double interpolatedY = function.value(input); System.out.println("f(" + interpolationX + ") = " + interpolatedY); String result = new String(Double.toString(interpolatedY)); return (result); }
From source file:io.yields.math.concepts.operator.CurveLength.java
private UnivariateFunction getLengthFunction(Function<Double, Double> function) { double epsilon = Math.min((max - min) / MAX_EVAL, DELTA); UnivariateFunction dFunction = x -> (function.apply(x + epsilon) - function.apply(x - epsilon)) / (2 * epsilon);/*from w w w . j a v a2 s . c om*/ return x -> Math.sqrt(1 + Math.pow(dFunction.value(x), 2)); }
From source file:io.yields.math.concepts.operator.CurveLength.java
protected Double simpsonIntegration(Function<Double, Double> originalFunction) { UnivariateFunction function = getLengthFunction(originalFunction); double delta = (max - min) / MAX_EVAL; double integral = IntStream.rangeClosed(0, MAX_EVAL).mapToDouble(index -> function.value(index * delta)) .sum() * delta;//from w w w .jav a 2 s.co m return integral - .5 * delta * (function.value(min) + function.value(max)); }
From source file:eu.crisis_economics.abm.algorithms.statistics.TestFloorInterpolator.java
/** * Test whether an instance of {@link FloorInterpolator} interpolates * a simple discrete input series as expected. This unit test operates * as follows:<br><br>// w w w . j a v a 2s .c om * * {@code (a)} * A short discrete subsequence of the function {@code f(T) = T**2} * is generated;<br> * {@code (b)} * An instance of {@link FloorInterpolator} is used to create a * {@link UnivariateFunction}, {@code U}, from this sequence;<br> * {@code (c)} * {@code U} is sampled repeatedly for a number of points in the domain * of the input sequence. The results of this operation are compared * to correct, expected outputs. */ @Test public void testFloorInterpolatorOutput() { final double[] x = new double[] { 2., 3., 4., 5., 6., }, y = new double[] { 4., 9., 16., 25., 36., }; final double[] expectedResults = { -Double.MAX_VALUE, 4.000000000, Double.MIN_VALUE, 4.000000000, 1.000000000, 4.000000000, 1.111111111, 4.000000000, 1.222222222, 4.000000000, 1.333333333, 4.000000000, 1.444444444, 4.000000000, 1.555555556, 4.000000000, 1.666666667, 4.000000000, 1.777777778, 4.000000000, 1.888888889, 4.000000000, 2.000000000, 4.000000000, 2.111111111, 4.000000000, 2.222222222, 4.000000000, 2.333333333, 4.000000000, 2.444444444, 4.000000000, 2.555555556, 4.000000000, 2.666666667, 4.000000000, 2.777777778, 4.000000000, 2.888888889, 4.000000000, 3.000000000, 9.000000000, 3.111111111, 9.000000000, 3.222222222, 9.000000000, 3.333333333, 9.000000000, 3.444444444, 9.000000000, 3.555555556, 9.000000000, 3.666666667, 9.000000000, 3.777777778, 9.000000000, 3.888888889, 9.000000000, 4.000000000, 16.00000000, 4.111111111, 16.00000000, 4.222222222, 16.00000000, 4.333333333, 16.00000000, 4.444444444, 16.00000000, 4.555555556, 16.00000000, 4.666666667, 16.00000000, 4.777777778, 16.00000000, 4.888888889, 16.00000000, 5.000000000, 25.00000000, 5.111111111, 25.00000000, 5.222222222, 25.00000000, 5.333333333, 25.00000000, 5.444444444, 25.00000000, 5.555555556, 25.00000000, 5.666666667, 25.00000000, 5.777777778, 25.00000000, 5.888888889, 25.00000000, 6.000000000, 36.00000000, 6.111111111, 36.00000000, 6.222222222, 36.00000000, 6.333333333, 36.00000000, 6.444444444, 36.00000000, 6.555555556, 36.00000000, 6.666666667, 36.00000000, 6.777777778, 36.00000000, 6.888888889, 36.00000000, 7.000000000, 36.00000000, 7.111111111, 36.00000000, 7.222222222, 36.00000000, 7.333333333, 36.00000000, 7.444444444, 36.00000000, 7.555555556, 36.00000000, 7.666666667, 36.00000000, 7.777777778, 36.00000000, 7.888888889, 36.00000000, 8.000000000, 36.00000000, 8.111111111, 36.00000000, 8.222222222, 36.00000000, 8.333333333, 36.00000000, 8.444444444, 36.00000000, 8.555555556, 36.00000000, 8.666666667, 36.00000000, 8.777777778, 36.00000000, 8.888888889, 36.00000000, 9.000000000, 36.00000000, 9.111111111, 36.00000000, 9.222222222, 36.00000000, 9.333333333, 36.00000000, 9.444444444, 36.00000000, 9.555555556, 36.00000000, 9.666666667, 36.00000000, 9.777777778, 36.00000000, 9.888888889, 36.00000000, 10.00000000, 36.00000000, 10.11111111, 36.00000000, 10.22222222, 36.00000000, 10.33333333, 36.00000000, 10.44444444, 36.00000000, 10.55555556, 36.00000000, 10.66666667, 36.00000000, 10.77777778, 36.00000000, 10.88888889, 36.00000000, 11.00000000, 36.00000000, 11.11111111, 36.00000000, 11.22222222, 36.00000000, 11.33333333, 36.00000000, 11.44444444, 36.00000000, 11.55555556, 36.00000000, 11.66666667, 36.00000000, 11.77777778, 36.00000000, 11.88888889, 36.00000000, 12.00000000, 36.00000000, Double.MAX_VALUE, 36.00000000 }; final UnivariateInterpolator interpolator = new FloorInterpolator(); final UnivariateFunction f = interpolator.interpolate(x, y); for (int i = 0; i < expectedResults.length; i += 2) { final double t = expectedResults[i], f_t_Observed = f.value(t), f_t_Expected = expectedResults[i + 1]; System.out.printf("t: %16.10g observed: %16.10g expected: %16.10g\n", t, f_t_Observed, f_t_Expected); Assert.assertEquals(f_t_Observed, f_t_Expected, 1.e-12); } }