List of usage examples for org.apache.commons.math.analysis UnivariateRealFunction UnivariateRealFunction
UnivariateRealFunction
From source file:dr.math.distributions.ReflectedNormalDistribution.java
public static void main(String[] args) { // final ReflectedNormalDistribution rnd = new ReflectedNormalDistribution(2, 2, 0.5, 2, 1e-6); final ReflectedNormalDistribution rnd = new ReflectedNormalDistribution(2, 2, 1, 2, 1e-6); rnd.pdf(1);// w w w.j av a 2s . c o m UnivariateRealFunction f = new UnivariateRealFunction() { public double value(double v) throws FunctionEvaluationException { return rnd.pdf(v); } }; final UnivariateRealIntegrator integrator = new RombergIntegrator(); integrator.setAbsoluteAccuracy(1e-14); integrator.setMaximalIterationCount(16); // fail if it takes too much time double x; try { x = integrator.integrate(f, rnd.lower, rnd.upper); // ptotErr += cdf != 0.0 ? Math.abs(x-cdf)/cdf : x; // np += 1; //assertTrue("" + shape + "," + scale + "," + value + " " + Math.abs(x-cdf)/x + "> 1e-6", Math.abs(1-cdf/x) < 1e-6); System.out.println("Integrated pdf = " + x); //System.out.println(shape + "," + scale + " " + value); } catch (ConvergenceException e) { // can't integrate , skip test // System.out.println(shape + "," + scale + " skipped"); } catch (FunctionEvaluationException e) { throw new RuntimeException("I have no idea why I am here."); } }
From source file:com.opengamma.analytics.math.util.wrapper.CommonsMathWrapper.java
/** * @param f An OG 1-D function mapping doubles onto doubles, not null * @return A Commons univariate real function *//*from w w w .j a v a 2 s . c o m*/ public static UnivariateRealFunction wrapUnivariate(final Function1D<Double, Double> f) { Validate.notNull(f); return new UnivariateRealFunction() { @Override public double value(final double x) { return f.evaluate(x); } }; }
From source file:OughtaFocus.java
private double runAutofocusAlgorithm() throws Exception { UnivariateRealFunction scoreFun = new UnivariateRealFunction() { public double value(double d) throws FunctionEvaluationException { try { return measureFocusScore(d); } catch (Exception e) { throw new FunctionEvaluationException(e, d); }/*from ww w.j av a2s .co m*/ } }; BrentOptimizer brentOptimizer = new BrentOptimizer(); brentOptimizer.setAbsoluteAccuracy(tolerance); imageCount_ = 0; CMMCore core = app_.getMMCore(); double z = core.getPosition(core.getFocusDevice()); startZUm_ = z; // getCurrentFocusScore(); double zResult = brentOptimizer.optimize(scoreFun, GoalType.MAXIMIZE, z - searchRange / 2, z + searchRange / 2); ReportingUtils.logMessage("OughtaFocus Iterations: " + brentOptimizer.getIterationCount() + ", z=" + TextUtils.FMT2.format(zResult) + ", dz=" + TextUtils.FMT2.format(zResult - startZUm_) + ", t=" + (System.currentTimeMillis() - startTimeMs_)); return zResult; }
From source file:org.jcurl.math.analysis.PieceWiseRealFunctionTest.java
public void test010() { final UnivariateRealFunction sine = new UnivariateRealFunction() { public double value(double t) throws FunctionEvaluationException { return Math.sin(t); }/*from w w w . j ava2 s . com*/ }; final PieceWiseRealFunction f = new PieceWiseRealFunction(); f.add(Double.NEGATIVE_INFINITY, false, 1.0, false, sine); f.add(-1, false, 1.0, false, null); f.add(-3, false, -2, false, sine); }
From source file:pltag.parser.params.SparseVec.java
public Vec set(final Random random, final double noise, final TypeAdd type) { try {/* w w w . ja v a2 s . c o m*/ counts.mapToSelf(new UnivariateRealFunction() { @Override public double value(double d) throws FunctionEvaluationException { return (type == TypeAdd.RANDOM) ? Math.pow(1 + random.nextDouble(), noise) : random.nextDouble() * noise; } }); } catch (FunctionEvaluationException ex) { LogInfo.error(ex); } return computeSum(); }
From source file:pltag.parser.params.SparseVec.java
@Override public Vec set(final Random random, final double min, final double max, final double noise) { try {/* w w w. ja v a2 s.c o m*/ counts.mapToSelf(new UnivariateRealFunction() { @Override public double value(double d) throws FunctionEvaluationException { return Math.pow(min + (random.nextDouble() * ((max - min) + 1)), noise); } }); } catch (FunctionEvaluationException ex) { LogInfo.error(ex); } return computeSum(); }
From source file:sphericalGeo.SphericalDiffusionModel.java
/** anglerange[0] = lower bound, anglerange[1] = upper bound of range for angle2 * **//* w w w . j a va 2 s . c o m*/ public double[] sample(double[] start, double time, double precision, double[] angleRange) throws ConvergenceException, FunctionEvaluationException, IllegalArgumentException { // first, sample an angle from the spherical diffusion density final double inverseVariance = precision / time; UnivariateRealFunction function = new UnivariateRealFunction() { @Override public double value(double x) throws FunctionEvaluationException { double logR = -x * x * inverseVariance / 2.0 + 0.5 * Math.log(x * Math.sin(x) * inverseVariance); return Math.exp(logR); } }; UnivariateRealIntegrator integrator = new TrapezoidIntegrator(); integrator.setAbsoluteAccuracy(1.0e-10); integrator.setRelativeAccuracy(1.0e-14); integrator.setMinimalIterationCount(2); integrator.setMaximalIterationCount(25); double[] cumulative = new double[NR_OF_STEPS]; for (int i = 1; i < NR_OF_STEPS; i++) { cumulative[i] = cumulative[i - 1] + integrator.integrate(function, (i - 1) * Math.PI / NR_OF_STEPS, i * Math.PI / NR_OF_STEPS); } // normalise double sum = cumulative[NR_OF_STEPS - 1]; for (int i = 0; i < NR_OF_STEPS; i++) { cumulative[i] /= sum; } int i = Randomizer.randomChoice(cumulative); double angle = i * Math.PI / NR_OF_STEPS; // now we have an angle, use this to rotate the point [0,0] over // this angle in a random direction angle2 double angle2 = angleRange[0] + Randomizer.nextDouble() * (angleRange[1] - angleRange[0]); //angleRange[0] = angle2; double[] xC = new double[] { Math.cos(angle), Math.sin(angle) * Math.cos(angle2), Math.sin(angle) * Math.sin(angle2) }; // convert back to latitude, longitude relative to (lat=0, long=0) double[] xL = cartesian2Sperical(xC, true); //double [] sC = spherical2Cartesian(start[0], start[1]); double[] position = reverseMap(xL[0], xL[1], start[0], start[1]); return position; }
From source file:test.dr.math.GeneralizedIntegerGammaTest.java
public void testPdf() { for (int i = 0; i < shape1s.length; ++i) { final GeneralizedIntegerGammaDistribution gig = new GeneralizedIntegerGammaDistribution(shape1s[i], shape2s[i], rate1s[i], rate2s[i]); TrapezoidIntegrator integrator = new TrapezoidIntegrator(); double m0 = 0.0; double m1 = 0.0; double m2 = 0.0; try {//from w ww . j av a 2 s . com m0 = integrator.integrate(new UnivariateRealFunction() { @Override public double value(double x) throws FunctionEvaluationException { final double pdf = gig.pdf(x); return pdf; } }, 0.0, 1000.0); m1 = integrator.integrate(new UnivariateRealFunction() { @Override public double value(double x) throws FunctionEvaluationException { final double pdf = gig.pdf(x); return x * pdf; } }, 0.0, 1000.0); m2 = integrator.integrate(new UnivariateRealFunction() { @Override public double value(double x) throws FunctionEvaluationException { final double pdf = gig.pdf(x); return x * x * pdf; } }, 0.0, 1000.0); } catch (MaxIterationsExceededException e) { e.printStackTrace(); } catch (FunctionEvaluationException e) { e.printStackTrace(); } // Check normalization assertEquals(1.0, m0, tolerance); // Check mean double mean = shape1s[i] / rate1s[i] + shape2s[i] / rate2s[i]; assertEquals(mean, m1, tolerance); // Check variance m2 -= m1 * m1; double variance = shape1s[i] / rate1s[i] / rate1s[i] + shape2s[i] / rate2s[i] / rate2s[i]; assertEquals(variance, m2, tolerance * 10); // Harder to approximate } }