Example usage for java.lang Double POSITIVE_INFINITY

List of usage examples for java.lang Double POSITIVE_INFINITY

Introduction

In this page you can find the example usage for java.lang Double POSITIVE_INFINITY.

Prototype

double POSITIVE_INFINITY

To view the source code for java.lang Double POSITIVE_INFINITY.

Click Source Link

Document

A constant holding the positive infinity of type double .

Usage

From source file:handlers.actionshift.OnActionShift.java

/**
 * Method addResist./* www.j av a  2  s.  c  om*/
 * @param dialog StringBuilder
 * @param name String
 * @param val double
 * @return boolean
 */
private boolean addResist(StringBuilder dialog, String name, double val) {
    if (val == 0) {
        return false;
    }

    dialog.append("<tr><td>").append(name).append("</td><td>");

    if (val == Double.POSITIVE_INFINITY) {
        dialog.append("MAX");
    } else if (val == Double.NEGATIVE_INFINITY) {
        dialog.append("MIN");
    } else {
        dialog.append(String.valueOf((int) val));
        dialog.append("</td></tr>");
        return true;
    }

    dialog.append("</td></tr>");
    return true;
}

From source file:jp.furplag.util.commons.NumberUtilsTest.java

/**
 * {@link jp.furplag.util.commons.NumberUtils#compareTo(java.lang.Number, java.lang.Number)}.
 *//*from w  w w  . ja  v  a2s .  c om*/
@SuppressWarnings("unchecked")
@Test
public void testCompareToNumberNumber() {
    assertEquals("null", (Object) 0, compareTo(null, null));
    assertEquals("null", (Object) 1, compareTo(1, null));
    assertEquals("null", (Object) 1, compareTo(-1, null));
    assertEquals("null", (Object) 1, compareTo(Float.NEGATIVE_INFINITY, null));
    assertEquals("null", (Object) (-1), compareTo(null, 1));
    assertEquals("null", (Object) (-1), compareTo(null, -1));
    assertEquals("null", (Object) (-1), compareTo(null, Double.NEGATIVE_INFINITY));
    assertEquals("Infinity", (Object) 0, compareTo(Float.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY));
    assertEquals("Infinity", (Object) 0, compareTo(Double.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
    assertEquals("NaN", (Object) 1, compareTo(Float.NaN, null));
    assertEquals("NaN", (Object) 1, compareTo(Float.NaN, 1));
    assertEquals("NaN", (Object) 1, compareTo(Float.NaN, -1));
    assertEquals("NaN", (Object) 1, compareTo(Float.NaN, Double.POSITIVE_INFINITY));
    assertEquals("NaN", (Object) 0, compareTo(Float.NaN, Double.NaN));
    assertEquals("NaN", (Object) (-1), compareTo(null, Double.NaN));
    assertEquals("NaN", (Object) (-1), compareTo(1, Double.NaN));
    assertEquals("NaN", (Object) (-1), compareTo(-1, Double.NaN));
    assertEquals("NaN", (Object) (-1), compareTo(Float.NEGATIVE_INFINITY, Double.NaN));
    assertEquals("NaN", (Object) 0, compareTo(Double.NaN, Float.NaN));
    for (Class<?> type : NUMBERS) {
        Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type);
        try {
            Number n = null;
            if (ClassUtils.isPrimitiveWrapper(type)) {
                n = (Number) wrapper.getField("MAX_VALUE").get(null);
            } else {
                n = INFINITY_DOUBLE.pow(2);
                if (BigInteger.class.equals(type))
                    n = ((BigDecimal) n).toBigInteger();
            }
            assertEquals("equals: " + type.getSimpleName(), 0, compareTo(n, new BigDecimal(n.toString())));
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace()));
        }
    }
}

From source file:net.droidsolutions.droidcharts.core.renderer.xy.AbstractXYItemRenderer.java

/**
 * Returns the range of values the renderer requires to display all the
 * items from the specified dataset./*from   w w  w.  j  a va 2s  .co  m*/
 *
 * @param dataset  the dataset (<code>null</code> permitted).
 *
 * @return The range (<code>null</code> if the dataset is <code>null</code>
 *         or empty).
 *
 * @since 1.0.13
 */
protected Range findRangeBounds(XYDataset dataset, boolean includeInterval) {
    if (dataset == null) {
        return null;
    }
    if (getDataBoundsIncludesVisibleSeriesOnly()) {
        List visibleSeriesKeys = new ArrayList();
        int seriesCount = dataset.getSeriesCount();
        for (int s = 0; s < seriesCount; s++) {
            if (isSeriesVisible(s)) {
                visibleSeriesKeys.add(dataset.getSeriesKey(s));
            }
        }
        // the bounds should be calculated using just the items within
        // the current range of the x-axis...if there is one
        Range xRange = null;
        XYPlot p = getPlot();
        if (p != null) {
            ValueAxis xAxis = null;
            int index = p.getIndexOf(this);
            if (index >= 0) {
                xAxis = plot.getDomainAxisForDataset(index);
            }
            if (xAxis != null) {
                xRange = xAxis.getRange();
            }
        }
        if (xRange == null) {
            xRange = new Range(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
        }
        return DatasetUtilities.findRangeBounds(dataset, visibleSeriesKeys, xRange, includeInterval);
    } else {
        return DatasetUtilities.findRangeBounds(dataset, includeInterval);
    }
}

From source file:knop.psfj.BeadFrame.java

/**
 * Filter centroids.//from www.j  a  va  2  s.  co  m
 * 
 * @param centroids
 *           list of centroids found by the 3D Counter Object
 * @return the float[] a 3 items array reprensenting x,y, and z coordinates of the center
 */
private float[] filterCentroids(float[][] centroids) {

    int indexMax = 0;
    int intensityMax = 0;
    double closest = Double.POSITIVE_INFINITY;
    int indexClosest = 0;

    float[] center = new float[3];

    // getting the boundaries of the center
    if (boundaries == null) {
        boundaries = new Rectangle(0, 0, getSubstack().getWidth(), getSubstack().getHeight());
    }
    center[0] = getWidth() / 2;
    center[1] = getHeight() / 2;
    center[2] = source.getFocusPlane() * (float) ip.getCalibration().pixelDepth;

    if (centroids.length == 1) {
        indexMax = 0;
    } else {
        float[] point;
        int intensity;
        double distanceToCenter;
        for (int i = 0; i != centroids.length; i++) {
            point = centroids[i];
            intensity = getIntensity(point);
            distanceToCenter = getDistance(center, point);
            if (intensity > intensityMax) {
                indexMax = i;
                intensityMax = intensity;
            }

            if (distanceToCenter < closest) {
                closest = distanceToCenter;
                indexClosest = i;
            }

        }

        object3d = counter.getObject(indexMax);
        maxIntensity = object3d.max;

    }

    return centroids[indexClosest];

}

From source file:de.bund.bfr.knime.pmm.common.chart.ChartCreator.java

private void plotFunction(XYPlot plot, Plotable plotable, String id, Color defaultColor, Shape defaultShape,
        double minX, double maxX) throws ConvertException {
    double[][] points = plotable.getFunctionPoints(paramX, paramY, unitX, unitY, transformX, transformY, minX,
            maxX, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
    double[][] functionErrors = null;
    String legend = shortLegend.get(id);
    Color color = colors.get(id);
    Shape shape = shapes.get(id);

    if (showConfidenceInterval) {
        functionErrors = plotable.getFunctionErrors(paramX, paramY, unitX, unitY, transformX, transformY, minX,
                maxX, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
    }/*  w w  w.j  a v  a2 s .co  m*/

    if (addInfoInLegend) {
        legend = longLegend.get(id);
    }

    if (color == null) {
        color = defaultColor;
    }

    if (shape == null) {
        shape = defaultShape;
    }

    if (points != null) {
        int i;

        if (plot.getDataset(0) == null) {
            i = 0;
        } else {
            i = plot.getDatasetCount();
        }

        if (functionErrors != null) {
            YIntervalSeriesCollection functionDataset = new YIntervalSeriesCollection();
            DeviationRenderer functionRenderer = new DeviationRenderer(true, false);
            YIntervalSeries series = new YIntervalSeries(legend);

            for (int j = 0; j < points[0].length; j++) {
                double error = Double.isNaN(functionErrors[1][j]) ? 0.0 : functionErrors[1][j];

                series.add(points[0][j], points[1][j], points[1][j] - error, points[1][j] + error);
            }

            functionDataset.addSeries(series);
            functionRenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
            functionRenderer.setSeriesPaint(0, color);
            functionRenderer.setSeriesFillPaint(0, color);
            functionRenderer.setSeriesShape(0, shape);

            plot.setDataset(i, functionDataset);
            plot.setRenderer(i, functionRenderer);
        } else {
            DefaultXYDataset functionDataset = new DefaultXYDataset();
            XYLineAndShapeRenderer functionRenderer = new XYLineAndShapeRenderer(true, false);

            functionDataset.addSeries(legend, points);
            functionRenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
            functionRenderer.setSeriesPaint(0, color);
            functionRenderer.setSeriesShape(0, shape);

            plot.setDataset(i, functionDataset);
            plot.setRenderer(i, functionRenderer);
        }
    }
}

From source file:gdsc.smlm.fitting.nonlinear.MaximumLikelihoodFitter.java

public FitStatus fit(int n, double[] y, double[] y_fit, double[] a, double[] a_dev, double[] error,
        double noise) {
    numberOfFittedPoints = n;/*from  w  ww .  j  a va  2  s  .  c o m*/

    LikelihoodWrapper maximumLikelihoodFunction;

    // We can use different likelihood wrapper functions:
    switch (likelihoodFunction) {
    case POISSON_GAMMA_GAUSSIAN:
        // Poisson-Gamma-Gaussian - EM-CCD data
        if (alpha > 0 && sigma > 0) {
            maximumLikelihoodFunction = new PoissonGammaGaussianLikelihoodWrapper(f, a, y, n, alpha, sigma);
            break;
        }

    case POISSON_GAUSSIAN:
        // Poisson-Gaussian - CCD data
        if (sigma > 0) {
            maximumLikelihoodFunction = new PoissonGaussianLikelihoodWrapper(f, a, y, n, sigma);
            break;
        }

    case POISSON:
    default:
        // Poisson - most counting data
        maximumLikelihoodFunction = new PoissonLikelihoodWrapper(f, a, y, n);
    }

    // Check if the method requires the gradient but it cannot be computed
    if (searchMethod.usesGradient && !maximumLikelihoodFunction.canComputeGradient()) {
        maximumLikelihoodFunction = new PoissonLikelihoodWrapper(f, a, y, n);
    }

    try {
        double[] startPoint = getInitialSolution(a);

        PointValuePair optimum = null;
        if (searchMethod == SearchMethod.POWELL || searchMethod == SearchMethod.POWELL_BOUNDED) {
            // Non-differentiable version using Powell Optimiser

            // This is as per the method in Numerical Recipes 10.5 (Direction Set (Powell's) method)
            // I could extend the optimiser and implement bounds on the directions moved. However the mapping
            // adapter seems to work OK.

            final boolean basisConvergence = false;

            // Perhaps these thresholds should be tighter?
            // The default is to use the sqrt() of the overall tolerance
            //final double lineRel = FastMath.sqrt(relativeThreshold);
            //final double lineAbs = FastMath.sqrt(absoluteThreshold);
            //final double lineRel = relativeThreshold * 1e2;
            //final double lineAbs = absoluteThreshold * 1e2;

            // Since we are fitting only a small number of parameters then just use the same tolerance 
            // for each search direction
            final double lineRel = relativeThreshold;
            final double lineAbs = absoluteThreshold;

            CustomPowellOptimizer o = new CustomPowellOptimizer(relativeThreshold, absoluteThreshold, lineRel,
                    lineAbs, null, basisConvergence);

            OptimizationData maxIterationData = null;
            if (getMaxIterations() > 0)
                maxIterationData = new MaxIter(getMaxIterations());

            if (searchMethod == SearchMethod.POWELL) {
                if (powellFunction == null) {
                    // We must map all the parameters into the same range. This is done in the Mortensen MLE 
                    // Python code by using the sqrt of the number of photons and background.
                    if (mapGaussian) {
                        Gaussian2DFunction gf = (Gaussian2DFunction) f;
                        // Re-map signal and background using the sqrt
                        int[] indices = gf.gradientIndices();
                        int[] map = new int[indices.length];
                        int count = 0;
                        // Background is always first
                        if (indices[0] == Gaussian2DFunction.BACKGROUND) {
                            map[count++] = 0;
                        }
                        // Look for the Signal in multiple peak 2D Gaussians
                        for (int i = 1; i < indices.length; i++)
                            if (indices[i] % 6 == Gaussian2DFunction.SIGNAL) {
                                map[count++] = i;
                            }
                        if (count > 0) {
                            powellFunction = new MappedMultivariateLikelihood(maximumLikelihoodFunction,
                                    Arrays.copyOf(map, count));
                        }
                    }
                    if (powellFunction == null) {
                        powellFunction = new MultivariateLikelihood(maximumLikelihoodFunction);
                    }
                }

                // Update the maximum likelihood function in the Powell function wrapper
                powellFunction.fun = maximumLikelihoodFunction;

                OptimizationData positionChecker = null;
                // new org.apache.commons.math3.optim.PositionChecker(relativeThreshold, absoluteThreshold);
                if (powellFunction.isMapped()) {
                    MappedMultivariateLikelihood adapter = (MappedMultivariateLikelihood) powellFunction;
                    optimum = o.optimize(maxIterationData, new MaxEval(getMaxEvaluations()),
                            new ObjectiveFunction(powellFunction), GoalType.MINIMIZE,
                            new InitialGuess(adapter.map(startPoint)), positionChecker);
                    double[] solution = adapter.unmap(optimum.getPointRef());
                    optimum = new PointValuePair(solution, optimum.getValue());
                } else {
                    optimum = o.optimize(maxIterationData, new MaxEval(getMaxEvaluations()),
                            new ObjectiveFunction(powellFunction), GoalType.MINIMIZE,
                            new InitialGuess(startPoint), positionChecker);
                }
            } else {
                // Try using the mapping adapter for a bounded Powell search
                MultivariateFunctionMappingAdapter adapter = new MultivariateFunctionMappingAdapter(
                        new MultivariateLikelihood(maximumLikelihoodFunction), lower, upper);
                optimum = o.optimize(maxIterationData, new MaxEval(getMaxEvaluations()),
                        new ObjectiveFunction(adapter), GoalType.MINIMIZE,
                        new InitialGuess(adapter.boundedToUnbounded(startPoint)));
                double[] solution = adapter.unboundedToBounded(optimum.getPointRef());
                optimum = new PointValuePair(solution, optimum.getValue());
            }
            iterations = o.getIterations();
            evaluations = o.getEvaluations();
        } else if (searchMethod == SearchMethod.BOBYQA) {
            // Differentiable approximation using Powell's BOBYQA algorithm.
            // This is slower than the Powell optimiser and requires a high number of evaluations.
            int numberOfInterpolationPoints = this.getNumberOfFittedParameters() + 2;

            BOBYQAOptimizer o = new BOBYQAOptimizer(numberOfInterpolationPoints);
            optimum = o.optimize(new MaxEval(getMaxEvaluations()),
                    new ObjectiveFunction(new MultivariateLikelihood(maximumLikelihoodFunction)),
                    GoalType.MINIMIZE, new InitialGuess(startPoint), new SimpleBounds(lower, upper));
            iterations = o.getIterations();
            evaluations = o.getEvaluations();
        } else if (searchMethod == SearchMethod.CMAES) {
            // TODO - Understand why the CMAES optimiser does not fit very well on test data. It appears 
            // to converge too early and the likelihood scores are not as low as the other optimisers.

            // CMAESOptimiser based on Matlab code:
            // https://www.lri.fr/~hansen/cmaes.m
            // Take the defaults from the Matlab documentation
            double stopFitness = 0; //Double.NEGATIVE_INFINITY;
            boolean isActiveCMA = true;
            int diagonalOnly = 0;
            int checkFeasableCount = 1;
            RandomGenerator random = new Well19937c();
            boolean generateStatistics = false;
            // The sigma determines the search range for the variables. It should be 1/3 of the initial search region.
            double[] sigma = new double[lower.length];
            for (int i = 0; i < sigma.length; i++)
                sigma[i] = (upper[i] - lower[i]) / 3;
            int popSize = (int) (4 + Math.floor(3 * Math.log(sigma.length)));

            // The CMAES optimiser is random and restarting can overcome problems with quick convergence.
            // The Apache commons documentations states that convergence should occur between 30N and 300N^2
            // function evaluations
            final int n30 = FastMath.min(sigma.length * sigma.length * 30, getMaxEvaluations() / 2);
            evaluations = 0;
            OptimizationData[] data = new OptimizationData[] { new InitialGuess(startPoint),
                    new CMAESOptimizer.PopulationSize(popSize), new MaxEval(getMaxEvaluations()),
                    new CMAESOptimizer.Sigma(sigma),
                    new ObjectiveFunction(new MultivariateLikelihood(maximumLikelihoodFunction)),
                    GoalType.MINIMIZE, new SimpleBounds(lower, upper) };
            // Iterate to prevent early convergence
            int repeat = 0;
            while (evaluations < n30) {
                if (repeat++ > 1) {
                    // Update the start point and population size
                    data[0] = new InitialGuess(optimum.getPointRef());
                    popSize *= 2;
                    data[1] = new CMAESOptimizer.PopulationSize(popSize);
                }
                CMAESOptimizer o = new CMAESOptimizer(getMaxIterations(), stopFitness, isActiveCMA,
                        diagonalOnly, checkFeasableCount, random, generateStatistics,
                        new SimpleValueChecker(relativeThreshold, absoluteThreshold));
                PointValuePair result = o.optimize(data);
                iterations += o.getIterations();
                evaluations += o.getEvaluations();
                //System.out.printf("CMAES [%d] i=%d [%d], e=%d [%d]\n", repeat, o.getIterations(), iterations,
                //      o.getEvaluations(), totalEvaluations);
                if (optimum == null || result.getValue() < optimum.getValue()) {
                    optimum = result;
                }
            }
        } else if (searchMethod == SearchMethod.BFGS) {
            // BFGS can use an approximate line search minimisation where as Powell and conjugate gradient
            // methods require a more accurate line minimisation. The BFGS search does not do a full 
            // minimisation but takes appropriate steps in the direction of the current gradient.

            // Do not use the convergence checker on the value of the function. Use the convergence on the 
            // point coordinate and gradient
            //BFGSOptimizer o = new BFGSOptimizer(new SimpleValueChecker(rel, abs));
            BFGSOptimizer o = new BFGSOptimizer();

            // Configure maximum step length for each dimension using the bounds
            double[] stepLength = new double[lower.length];
            for (int i = 0; i < stepLength.length; i++) {
                stepLength[i] = (upper[i] - lower[i]) * 0.3333333;
                if (stepLength[i] <= 0)
                    stepLength[i] = Double.POSITIVE_INFINITY;
            }

            // The GoalType is always minimise so no need to pass this in
            OptimizationData positionChecker = null;
            //new org.apache.commons.math3.optim.PositionChecker(relativeThreshold, absoluteThreshold);
            optimum = o.optimize(new MaxEval(getMaxEvaluations()),
                    new ObjectiveFunctionGradient(new MultivariateVectorLikelihood(maximumLikelihoodFunction)),
                    new ObjectiveFunction(new MultivariateLikelihood(maximumLikelihoodFunction)),
                    new InitialGuess(startPoint), new SimpleBounds(lowerConstraint, upperConstraint),
                    new BFGSOptimizer.GradientTolerance(relativeThreshold), positionChecker,
                    new BFGSOptimizer.StepLength(stepLength));
            iterations = o.getIterations();
            evaluations = o.getEvaluations();
        } else {
            // The line search algorithm often fails. This is due to searching into a region where the 
            // function evaluates to a negative so has been clipped. This means the upper bound of the line
            // cannot be found.
            // Note that running it on an easy problem (200 photons with fixed fitting (no background)) the algorithm
            // does sometimes produces results better than the Powell algorithm but it is slower.

            BoundedNonLinearConjugateGradientOptimizer o = new BoundedNonLinearConjugateGradientOptimizer(
                    (searchMethod == SearchMethod.CONJUGATE_GRADIENT_FR) ? Formula.FLETCHER_REEVES
                            : Formula.POLAK_RIBIERE,
                    new SimpleValueChecker(relativeThreshold, absoluteThreshold));

            // Note: The gradients may become unstable at the edge of the bounds. Or they will not change 
            // direction if the true solution is on the bounds since the gradient will always continue 
            // towards the bounds. This is key to the conjugate gradient method. It searches along a vector 
            // until the direction of the gradient is in the opposite direction (using dot products, i.e. 
            // cosine of angle between them)

            // NR 10.7 states there is no advantage of the variable metric DFP or BFGS methods over
            // conjugate gradient methods. So I will try these first.

            // Try this:
            // Adapt the conjugate gradient optimiser to use the gradient to pick the search direction
            // and then for the line minimisation. However if the function is out of bounds then clip the 
            // variables at the bounds and continue. 
            // If the current point is at the bounds and the gradient is to continue out of bounds then 
            // clip the gradient too.

            // Or: just use the gradient for the search direction then use the line minimisation/rest
            // as per the Powell optimiser. The bounds should limit the search.

            // I tried a Bounded conjugate gradient optimiser with clipped variables:
            // This sometimes works. However when the variables go a long way out of the expected range the gradients
            // can have vastly different magnitudes. This results in the algorithm stalling since the gradients
            // can be close to zero and the some of the parameters are no longer adjusted.
            // Perhaps this can be looked for and the algorithm then gives up and resorts to a Powell optimiser from 
            // the current point.

            // Changed the bracketing step to very small (default is 1, changed to 0.001). This improves the 
            // performance. The gradient direction is very sensitive to small changes in the coordinates so a 
            // tighter bracketing of the line search helps.

            // Tried using a non-gradient method for the line search copied from the Powell optimiser:
            // This also works when the bracketing step is small but the number of iterations is higher.

            // 24.10.2014: I have tried to get conjugate gradient to work but the gradient function 
            // must not behave suitably for the optimiser. In the current state both methods of using a 
            // Bounded Conjugate Gradient Optimiser perform poorly relative to other optimisers:
            // Simulated : n=1000, signal=200, x=0.53, y=0.47
            // LVM : n=1000, signal=171, x=0.537, y=0.471 (1.003s)
            // Powell : n=1000, signal=187, x=0.537, y=0.48 (1.238s)
            // Gradient based PR (constrained): n=858, signal=161, x=0.533, y=0.474 (2.54s)
            // Gradient based PR (bounded): n=948, signal=161, x=0.533, y=0.473 (2.67s)
            // Non-gradient based : n=1000, signal=151.47, x=0.535, y=0.474 (1.626s)
            // The conjugate optimisers are slower, under predict the signal by the most and in the case of 
            // the gradient based optimiser, fail to converge on some problems. This is worse when constrained
            // fitting is used and not tightly bounded fitting.
            // I will leave the code in as an option but would not recommend using it. I may remove it in the 
            // future.

            // Note: It is strange that the non-gradient based line minimisation is more successful.
            // It may be that the gradient function is not accurate (due to round off error) or that it is
            // simply wrong when far from the optimum. My JUnit tests only evaluate the function within the 
            // expected range of the answer.

            // Note the default step size on the Powell optimiser is 1 but the initial directions are unit vectors.
            // So our bracketing step should be a minimum of 1 / average length of the first gradient vector to prevent
            // the first step being too large when bracketing.
            final double gradient[] = new double[startPoint.length];
            maximumLikelihoodFunction.likelihood(startPoint, gradient);
            double l = 0;
            for (double d : gradient)
                l += d * d;
            final double bracketingStep = FastMath.min(0.001, ((l > 1) ? 1.0 / l : 1));
            //System.out.printf("Bracketing step = %f (length=%f)\n", bracketingStep, l);

            o.setUseGradientLineSearch(gradientLineMinimisation);

            optimum = o.optimize(new MaxEval(getMaxEvaluations()),
                    new ObjectiveFunctionGradient(new MultivariateVectorLikelihood(maximumLikelihoodFunction)),
                    new ObjectiveFunction(new MultivariateLikelihood(maximumLikelihoodFunction)),
                    GoalType.MINIMIZE, new InitialGuess(startPoint),
                    new SimpleBounds(lowerConstraint, upperConstraint),
                    new BoundedNonLinearConjugateGradientOptimizer.BracketingStep(bracketingStep));
            iterations = o.getIterations();
            evaluations = o.getEvaluations();

            //maximumLikelihoodFunction.value(solution, gradient);
            //System.out.printf("Iter = %d, %g @ %s : %s\n", iterations, ll, Arrays.toString(solution),
            //      Arrays.toString(gradient));
        }

        final double[] solution = optimum.getPointRef();

        setSolution(a, solution);

        //System.out.printf("Iter = %d, Eval = %d, %g @ %s\n", iterations, evaluations, optimum.getValue(), 
        //   java.util.Arrays.toString(solution));

        // Compute residuals for the FunctionSolver interface
        if (y_fit == null || y_fit.length < n)
            y_fit = new double[n];
        f.initialise(a);
        residualSumOfSquares = 0;
        for (int i = 0; i < n; i++) {
            y_fit[i] = f.eval(i);
            final double residual = y[i] - y_fit[i];
            residualSumOfSquares += residual * residual;
        }

        if (a_dev != null) {
            // Assume the Maximum Likelihood estimator returns the optimum fit (achieves the Cramer Roa
            // lower bounds) and so the covariance can be obtained from the Fisher Information Matrix. 
            final int[] gradientIndices = f.gradientIndices();
            final int nparams = gradientIndices.length;
            GradientCalculator calculator = GradientCalculatorFactory.newCalculator(nparams);
            final double[] I = calculator.fisherInformationDiagonal(n, a, f);
            for (int i = 0; i < gradientIndices.length; i++)
                a_dev[gradientIndices[i]] = 1.0 / Math.sqrt(I[i]);
        }

        error[0] = NonLinearFit.getError(residualSumOfSquares, noise, n, f.gradientIndices().length);
        totalSumOfSquares = getSumOfSquares(n, y);
    } catch (TooManyIterationsException e) {
        //System.out.printf("Too many iterations = %d\n", e.getMax());
        //e.printStackTrace();
        return FitStatus.FAILED_TO_CONVERGE;
    } catch (TooManyEvaluationsException e) {
        //System.out.printf("Too many evaluations = %d\n", e.getMax());
        //e.printStackTrace();
        return FitStatus.FAILED_TO_CONVERGE;
    } catch (ConvergenceException e) {
        // Occurs when QR decomposition fails - mark as a singular non-linear model (no solution)
        //System.out.printf("Singular non linear model = %s\n", e.getMessage());
        return FitStatus.SINGULAR_NON_LINEAR_MODEL;
    } catch (BFGSOptimizer.LineSearchRoundoffException e) {
        //System.out.println("BFGS error: " + e.getMessage());
        //e.printStackTrace();
        return FitStatus.FAILED_TO_CONVERGE;
    } catch (Exception e) {
        //System.out.printf("Unknown error = %s\n", e.getMessage());
        e.printStackTrace();
        return FitStatus.UNKNOWN;
    }

    return FitStatus.OK;
}

From source file:eu.brokeratcloud.rest.gui.AdminFacingComponent.java

@GET
@Path("/category-attribute-mappings/{cat_id}")
@Produces("application/json;charset=UTF-8")
@RolesAllowed("admin")
public String getServiceCategoryAttributes(@PathParam("cat_id") String catId) throws IOException {
    long startTm = System.currentTimeMillis();
    logger.info("-------------- getServiceCategoryAttributes: INPUT: service-category={}", catId);

    // Call REST service in order to get service category attributes
    ResteasyClient client = new ResteasyClientBuilder().build();
    ResteasyWebTarget target = client.target(baseUrl + "/opt/service-category/" + catId + "/attributes");
    long callStartTm = System.currentTimeMillis();
    Response response = target.request().get();
    ServiceCategoryAttribute[] scaList = response.readEntity(ServiceCategoryAttribute[].class);
    long callEndTm = System.currentTimeMillis();
    logger.debug("Response: {}, Duration: {}", response.getStatus(), callEndTm - callStartTm);
    response.close();/* w  ww .j av a 2  s .c om*/

    // order SCA list by name
    Arrays.sort(scaList, new Comparator<ServiceCategoryAttribute>() {
        public int compare(ServiceCategoryAttribute sca1, ServiceCategoryAttribute sca2) {
            return sca1.getName().compareToIgnoreCase(sca2.getName());
        }
    });

    // Prepare JSON to retrurn to page
    StringBuilder sb = new StringBuilder("[");
    boolean first = true;
    String[] types = { "NUMERIC_INC", "NUMERIC_DEC", "NUMERIC_RANGE", "BOOLEAN", "UNORDERED_SET", "FUZZY_INC",
            "FUZZY_DEC", "FUZZY_RANGE", "LINGUISTIC" };
    String fmtCommon = "{ \"rownum\" : \"%d\", \"id\" : \"%s\", \"bppName\" : \"%s\", \"aid\" : \"%s\", \"name\" : \"%s\", \"type\" : \"%s\", \"unit\" : \"%s\", \"mandatory\" : %s, \"labelEn\" : \"%s\", \"labelDe\" : \"%s\", \"comment\" : \"%s\", \"measuredBy\" : \"%s\", ";
    String fmtNum = " \"from\" : \"%s\", \"to\" : \"%s\" }";
    String fmtFuzzy = " \"fromL\" : \"%s\", \"from\" : \"%s\", \"fromU\" : \"%s\", \"toL\" : \"%s\", \"to\" : \"%s\", \"toU\" : \"%s\" }";
    String fmtText = " \"from\" : \"%s\" }";
    int typesLen = types.length;
    int row = 1;
    for (ServiceCategoryAttribute sca : scaList) {
        logger.debug("'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''");
        logger.debug("SCA={}", sca);
        logger.debug(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
        if (first)
            first = false;
        else
            sb.append(", \n");
        String typ = sca.getType();

        String id = sca.getId();
        String bppName = sca.getBppName();
        String attrId = sca.getAttribute();
        String attrName = sca.getName(); // Note: As a convenience we use 'ServiceCategoryAttribute.name' to convey the 'OptimisationAtttibute.name' of the referenced opt. attribute
        String unit = sca.getUnit();
        unit = (unit != null && !unit.trim().isEmpty()) ? unit.trim() : "";
        boolean mandatory = sca.getMandatory();

        // RootObject properties
        String labelEn = sca.getLabelEn();
        String labelDe = sca.getLabelDe();
        String comment = sca.getComment();
        String measuredBy = sca.getMeasuredBy();
        if (labelEn == null)
            labelEn = "";
        if (labelDe == null)
            labelDe = "";
        if (comment == null)
            comment = "";
        if (measuredBy == null)
            measuredBy = "";

        sb.append(String.format(fmtCommon, row++,
                java.net.URLEncoder.encode(_jsonVal(id), java.nio.charset.StandardCharsets.UTF_8.toString()),
                _jsonVal(bppName), _jsonVal(attrId), _jsonVal(attrName), _jsonVal(typ), _jsonVal(unit),
                (!mandatory ? "false" : "true"), _jsonVal(labelEn), _jsonVal(labelDe), _jsonVal(comment),
                _jsonVal(measuredBy)));
        if (ServiceCategoryAttribute.isNumericType(typ)) {
            double m = sca.getMin();
            double M = sca.getMax();
            sb.append(String.format(fmtNum, _jsonVal(m), _jsonVal(M)));
        } else if (ServiceCategoryAttribute.isFuzzyType(typ)) {
            double ml = Double.NEGATIVE_INFINITY;
            double mm = Double.NEGATIVE_INFINITY;
            double mu = Double.NEGATIVE_INFINITY;
            double Ml = Double.POSITIVE_INFINITY;
            double Mm = Double.POSITIVE_INFINITY;
            double Mu = Double.POSITIVE_INFINITY;
            TFN m = sca.getFmin();
            TFN M = sca.getFmax();
            if (m != null) {
                ml = m.getLowerBound();
                mm = m.getMeanValue();
                mu = m.getUpperBound();
            }
            if (M != null) {
                Ml = M.getLowerBound();
                Mm = M.getMeanValue();
                Mu = M.getUpperBound();
            }
            sb.append(String.format(fmtFuzzy, _jsonVal(ml), _jsonVal(mm), _jsonVal(mu), _jsonVal(Ml),
                    _jsonVal(Mm), _jsonVal(Mu)));
        } else if (ServiceCategoryAttribute.isBooleanType(typ)) {
            String[] terms = sca.getTerms();
            String labels = "";
            if (terms != null) {
                boolean _first = true;
                for (int k = 0, n = terms.length; k < n; k++) {
                    if (_first)
                        _first = false;
                    else
                        labels += ",";
                    labels += _jsonVal(terms[k]);
                }
            }
            if (labels.trim().isEmpty())
                labels = "No, Yes";
            sb.append(String.format(fmtText, labels));
        } else if (ServiceCategoryAttribute.isUnorderedSetType(typ)) {
            String[] members = sca.getMembers();
            String labels = "";
            if (members != null) {
                boolean frst = true;
                for (int k = 0, n = members.length; k < n; k++) {
                    if (frst)
                        frst = false;
                    else
                        labels += ",";
                    labels += _jsonVal(members[k]);
                }
            }
            sb.append(String.format(fmtText, labels));
        } else if (ServiceCategoryAttribute.isLinguisticType(typ)) {
            String[] terms = sca.getTerms();
            String labels = "";
            if (terms != null) {
                boolean frst = true;
                for (int k = 0, n = terms.length; k < n; k++) {
                    if (frst)
                        frst = false;
                    else
                        labels += ",";
                    labels += _jsonVal(terms[k]);
                }
            }
            sb.append(String.format(fmtText, labels));
        } else {
            throw new RuntimeException("UNKNOWN TYPE: " + typ);
        }
    }

    sb.append(" ]");
    String str = sb.toString();

    logger.info("-------------- getServiceCategoryAttributes: OUTPUT: {}", str);
    long endTm = System.currentTimeMillis();
    logger.debug("duration={}ms", endTm - startTm);

    return str;
}

From source file:com.addthis.hydra.data.tree.prop.DataReservoir.java

private List<DataTreeNode> sigmaAnomalyDetection(long targetEpoch, int numObservations,
        boolean doubleToLongBits, boolean raw, double sigma, int minMeasurement) {

    int measurement;
    if (targetEpoch < 0) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (sigma == Double.POSITIVE_INFINITY) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (numObservations <= 0) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (reservoir == null) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (targetEpoch < minEpoch) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (targetEpoch >= minEpoch + reservoir.length) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (numObservations > (reservoir.length - 1)) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    }//from   w  w w  . j a v a2s  .  co m

    int count = 0;
    double mean = 0.0;
    double m2 = 0.0;
    double stddev;

    int index = reservoir.length - 1;
    long currentEpoch = minEpoch + index;

    while (currentEpoch != targetEpoch) {
        index--;
        currentEpoch--;
    }

    measurement = reservoir[index--];

    while (count < numObservations && index >= 0) {
        int value = reservoir[index--];
        count++;
        double delta = value - mean;
        mean += delta / count;
        m2 += delta * (value - mean);
    }

    while (count < numObservations) {
        int value = 0;
        count++;
        double delta = value - mean;
        mean += delta / count;
        m2 += delta * (value - mean);
    }

    if (count < 2) {
        stddev = 0.0;
    } else {
        stddev = Math.sqrt(m2 / count);
    }

    double delta = (measurement - (sigma * stddev + mean));

    VirtualTreeNode vchild, vparent;
    if (delta >= 0 && measurement >= minMeasurement) {
        List<DataTreeNode> result = new ArrayList<>();
        vchild = new VirtualTreeNode("threshold", doubleToLong(sigma * stddev + mean, doubleToLongBits));
        vparent = new VirtualTreeNode("stddev", doubleToLong(stddev, doubleToLongBits),
                generateSingletonArray(vchild));
        vchild = vparent;
        vparent = new VirtualTreeNode("mean", doubleToLong(mean, doubleToLongBits),
                generateSingletonArray(vchild));
        vchild = vparent;
        vparent = new VirtualTreeNode("measurement", measurement, generateSingletonArray(vchild));
        vchild = vparent;
        vparent = new VirtualTreeNode("delta", doubleToLong(delta, doubleToLongBits),
                generateSingletonArray(vchild));
        result.add(vparent);
        if (raw) {
            addRawObservations(result, targetEpoch, numObservations);
        }
        return result;
    } else {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    }
}

From source file:edu.cuny.cat.stat.HistoricalReport.java

public double getLowestAcceptedBidPrice() {
    final Iterator<Shout> i = bids.iterator();
    double lowestAcceptedBidPrice = Double.POSITIVE_INFINITY;
    while (i.hasNext()) {
        final Shout s = i.next();
        if (isMatched(s)) {
            if (s.getPrice() < lowestAcceptedBidPrice) {
                lowestAcceptedBidPrice = s.getPrice();
            }/*  w ww .  j  av a 2s.  c om*/
        }
    }
    return lowestAcceptedBidPrice;
}

From source file:com.opera.core.systems.scope.stp.services.ScopeEcmascriptService.java

private Object parseValue(Type type, Value value, Set<Integer> visitedIDs) {
    switch (type) {
    case TRUE:/*from w ww  .j a  v a 2 s  .c  o m*/
        return true;
    case FALSE:
        return false;
    case PLUS_INFINITY:
        return Double.POSITIVE_INFINITY;
    case MINUS_INFINITY:
        return Double.NEGATIVE_INFINITY;
    case NUMBER:
        return value.getNumber();
    case STRING:
        return value.getStr();
    case OBJECT:
        return examineScriptResult(value.getObject().getObjectID(), visitedIDs);
    case UNDEFINED:
    case NULL:
    case NAN:
    default:
        return null;
    }
}