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:de.bund.bfr.math.MathUtils.java

public static List<StartValues> createStartValuesList(List<ParamRange> ranges, int n,
        ToDoubleFunction<List<Double>> errorFunction, DoubleConsumer progessListener, ExecutionContext exec)
        throws CanceledExecutionException {
    List<StartValues> valuesList = new ArrayList<>();

    for (int i = 0; i < n; i++) {
        valuesList.add(new StartValues(Collections.nCopies(ranges.size(), i + 1.0), Double.POSITIVE_INFINITY));
    }//from w  w w. ja  va  2 s  .c o  m

    List<Integer> paramStepIndex = new ArrayList<>(Collections.nCopies(ranges.size(), 0));
    boolean done = false;
    int allStepSize = 1;
    int count = 0;

    for (ParamRange range : ranges) {
        allStepSize *= range.getStepCount();
    }

    while (!done) {
        List<Double> values = new ArrayList<>();

        for (int i = 0; i < ranges.size(); i++) {
            values.add(ranges.get(i).getMin() + paramStepIndex.get(i) * ranges.get(i).getStepSize());
        }

        double error = errorFunction.applyAsDouble(values);

        if (Double.isFinite(error) || error < valuesList.get(n - 1).getError()) {
            for (int i = 0; i < n; i++) {
                if (error < valuesList.get(i).getError()) {
                    valuesList.add(i, new StartValues(values, error));
                    valuesList.remove(n);
                    break;
                }
            }
        }

        for (int i = 0;; i++) {
            if (i >= ranges.size()) {
                done = true;
                break;
            }

            paramStepIndex.set(i, paramStepIndex.get(i) + 1);

            if (paramStepIndex.get(i) >= ranges.get(i).getStepCount()) {
                paramStepIndex.set(i, 0);
            } else {
                break;
            }
        }

        if (exec != null) {
            exec.checkCanceled();
        }

        progessListener.accept((double) ++count / (double) allStepSize);
    }

    return valuesList;
}

From source file:ch.epfl.leb.sass.models.fluorophores.commands.internal.FluorophoreReceiverIT.java

/**
 * Test of generateFluorophoresGrid2D method, of class FluorophoreReceiver.
 *//*ww  w . j a  v  a  2  s . c om*/
@Test
public void testGenerateFluorophoresGrid2D() {
    GenerateFluorophoresGrid2D.Builder fluorBuilder = new GenerateFluorophoresGrid2D.Builder();
    fluorBuilder.spacing(4); // spacing in pixels

    // Create the set of fluorophores.
    fluorBuilder.camera(camera).psfBuilder(psfBuilder).fluorDynamics(fluorDynamics).illumination(illumination);
    FluorophoreCommand fluorCommand = fluorBuilder.build();
    List<Fluorophore> fluorophores = fluorCommand.generateFluorophores();
    assertEquals(49, fluorophores.size());

    double minX = Double.POSITIVE_INFINITY;
    double maxX = Double.NEGATIVE_INFINITY;
    double minY = Double.POSITIVE_INFINITY;
    double maxY = Double.NEGATIVE_INFINITY;
    for (Fluorophore f : fluorophores) {
        if (f.getX() < minX)
            minX = f.getX();

        if (f.getX() > maxX)
            maxX = f.getX();

        if (f.getY() < minY)
            minY = f.getY();

        if (f.getY() > maxY)
            maxY = f.getY();
    }

    assertEquals(28.0, maxX, 0.0);
    assertEquals(4.0, minX, 0.0);
    assertEquals(28.0, maxY, 0.0);
    assertEquals(4.0, minY, 0.0);
}

From source file:com.opengamma.analytics.financial.model.volatility.BlackScholesFormulaRepository.java

/**
* The spot delta//from  w ww.  j av  a  2  s  .com
* @param spot The spot value of the underlying
* @param strike The Strike
* @param timeToExpiry The time-to-expiry
* @param lognormalVol The log-normal volatility
* @param interestRate The interest rate 
* @param costOfCarry The cost-of-carry rate
* @param isCall true for call
* @return The spot delta
*/
@ExternalFunction
public static double delta(final double spot, final double strike, final double timeToExpiry,
        final double lognormalVol, final double interestRate, final double costOfCarry, final boolean isCall) {
    ArgumentChecker.isTrue(spot >= 0.0, "negative/NaN spot; have {}", spot);
    ArgumentChecker.isTrue(strike >= 0.0, "negative/NaN strike; have {}", strike);
    ArgumentChecker.isTrue(timeToExpiry >= 0.0, "negative/NaN timeToExpiry; have {}", timeToExpiry);
    ArgumentChecker.isTrue(lognormalVol >= 0.0, "negative/NaN lognormalVol; have {}", lognormalVol);
    ArgumentChecker.isFalse(Double.isNaN(interestRate), "interestRate is NaN");
    ArgumentChecker.isFalse(Double.isNaN(costOfCarry), "costOfCarry is NaN");

    double coef = 0.;
    if ((interestRate > LARGE && costOfCarry > LARGE) || (-interestRate > LARGE && -costOfCarry > LARGE)
            || Math.abs(costOfCarry - interestRate) < SMALL) {
        coef = 1.; //ref value is returned
    } else {
        final double rate = costOfCarry - interestRate;
        if (rate > LARGE) {
            return isCall ? Double.POSITIVE_INFINITY : (costOfCarry > LARGE ? 0. : Double.NEGATIVE_INFINITY);
        }
        if (-rate > LARGE) {
            return 0.;
        }
        coef = Math.exp(rate * timeToExpiry);
    }

    if (spot > LARGE * strike) {
        return isCall ? coef : 0.;
    }
    if (spot < SMALL * strike) {
        return isCall ? 0. : -coef;
    }

    final int sign = isCall ? 1 : -1;
    final double rootT = Math.sqrt(timeToExpiry);
    double sigmaRootT = lognormalVol * rootT;
    if (Double.isNaN(sigmaRootT)) {
        sigmaRootT = 1.; //ref value is returned
    }

    double factor = Math.exp(costOfCarry * timeToExpiry);
    if (Double.isNaN(factor)) {
        factor = 1.; //ref value is returned
    }
    double rescaledSpot = spot * factor;

    double d1 = 0.;
    if (Math.abs(spot - strike) < SMALL || sigmaRootT > LARGE || (spot > LARGE && strike > LARGE)) {
        final double coefD1 = (costOfCarry / lognormalVol + 0.5 * lognormalVol);
        final double tmp = coefD1 * rootT;
        d1 = Double.isNaN(tmp) ? 0. : tmp;
    } else {
        if (sigmaRootT < SMALL) {
            return isCall ? (rescaledSpot > strike ? coef : 0.) : (rescaledSpot < strike ? -coef : 0.);
        }
        final double tmp = costOfCarry * rootT / lognormalVol;
        final double sig = (costOfCarry >= 0.) ? 1. : -1.;
        final double scnd = Double.isNaN(tmp)
                ? ((lognormalVol < LARGE && lognormalVol > SMALL) ? sig / lognormalVol : sig * rootT)
                : tmp;
        d1 = Math.log(spot / strike) / sigmaRootT + scnd + 0.5 * sigmaRootT;
    }
    //    if (Double.isNaN(d1)) {
    //      throw new IllegalArgumentException("NaN found");
    //    }
    final double norm = NORMAL.getCDF(sign * d1);

    return norm < SMALL ? 0. : sign * coef * norm;
}

From source file:com.clust4j.metrics.pairwise.TestDistanceEnums.java

@Test
public void testInfP() {
    assertTrue(Distance.CHEBYSHEV.getP() == Double.POSITIVE_INFINITY);
    assertTrue(Double.isInfinite(Distance.CHEBYSHEV.getP()));

    for (Distance d : Distance.values()) {
        if (d.equals(Distance.CHEBYSHEV))
            continue;

        assertFalse(d.getP() == Double.POSITIVE_INFINITY);
        assertFalse(Double.isInfinite(d.getP()));
    }//from  w  ww  .  ja v a  2s.c  o m
}

From source file:de.bund.bfr.knime.pmm.common.math.ParameterOptimizer.java

public void optimize(AtomicInteger progress, int nParameterSpace, int nLevenberg, boolean stopWhenSuccessful) {
    List<Double> paramMin = new ArrayList<>();
    List<Integer> paramStepCount = new ArrayList<>();
    List<Double> paramStepSize = new ArrayList<>();
    int maxCounter = 1;
    int paramsWithRange = 0;
    int maxStepCount = 10;

    for (int i = 0; i < parameters.size(); i++) {
        Double min = minParameterValues.get(i);
        Double max = maxParameterValues.get(i);

        if (min != null && max != null) {
            paramsWithRange++;//www .j ava  2s  .  c  o  m
        }
    }

    if (paramsWithRange != 0) {
        maxStepCount = (int) Math.pow(nParameterSpace, 1.0 / paramsWithRange);
        maxStepCount = Math.max(maxStepCount, 2);
        maxStepCount = Math.min(maxStepCount, 10);
    }

    for (int i = 0; i < parameters.size(); i++) {
        Double min = minParameterValues.get(i);
        Double max = maxParameterValues.get(i);

        if (min != null && max != null) {
            paramMin.add(min);
            paramStepCount.add(maxStepCount);
            maxCounter *= maxStepCount;

            if (max > min) {
                paramStepSize.add((max - min) / (maxStepCount - 1));
            } else {
                paramStepSize.add(1.0);
            }
        } else if (min != null) {
            if (min != 0.0) {
                paramMin.add(min);
            } else {
                paramMin.add(MathUtilities.EPSILON);
            }

            paramStepCount.add(1);
            paramStepSize.add(1.0);
        } else if (max != null) {
            if (max != 0.0) {
                paramMin.add(max);
            } else {
                paramMin.add(-MathUtilities.EPSILON);
            }

            paramStepCount.add(1);
            paramStepSize.add(1.0);
        } else {
            paramMin.add(MathUtilities.EPSILON);
            paramStepCount.add(1);
            paramStepSize.add(1.0);
        }
    }

    List<List<Double>> bestValues = new ArrayList<>();
    List<Double> bestError = new ArrayList<>();

    for (int i = 0; i < nLevenberg; i++) {
        bestValues.add(new ArrayList<>(Collections.nCopies(parameters.size(), i + 1.0)));
        bestError.add(Double.POSITIVE_INFINITY);
    }

    List<Integer> paramStepIndex = new ArrayList<>(Collections.nCopies(parameters.size(), 0));
    boolean done = false;
    int counter = 0;

    while (!done) {
        progress.set(Float.floatToIntBits((float) counter / (float) maxCounter));
        counter++;

        List<Double> values = new ArrayList<>();
        double error = 0.0;

        for (int i = 0; i < parameters.size(); i++) {
            double value = paramMin.get(i) + paramStepIndex.get(i) * paramStepSize.get(i);

            values.add(value);
            parser.setVarValue(parameters.get(i), value);
        }

        for (int i = 0; i < targetValues.size(); i++) {
            for (int j = 0; j < arguments.size(); j++) {
                parser.setVarValue(arguments.get(j), argumentValues.get(j).get(i));
            }

            try {
                double value = (Double) parser.evaluate(function);
                double diff = targetValues.get(i) - value;

                error += diff * diff;
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (ClassCastException e) {
                error = Double.POSITIVE_INFINITY;
                break;
            }
        }

        for (int i = nLevenberg; i >= 0; i--) {
            if (i == 0 || !(error < bestError.get(i - 1))) {
                if (i != nLevenberg) {
                    bestError.add(i, error);
                    bestValues.add(i, values);
                    bestError.remove(nLevenberg);
                    bestValues.remove(nLevenberg);
                }

                break;
            }
        }

        for (int i = 0;; i++) {
            if (i >= parameters.size()) {
                done = true;
                break;
            }

            paramStepIndex.set(i, paramStepIndex.get(i) + 1);

            if (paramStepIndex.get(i) >= paramStepCount.get(i)) {
                paramStepIndex.set(i, 0);
            } else {
                break;
            }
        }
    }

    successful = false;

    for (List<Double> startValues : bestValues) {
        try {
            optimize(startValues);

            double cost = optimizerValues.getCost();

            if (!successful || cost * cost < sse) {
                useCurrentResults(startValues);

                if (rSquare != 0.0) {
                    successful = true;

                    if (stopWhenSuccessful) {
                        break;
                    }
                }
            }
        } catch (TooManyEvaluationsException e) {
            break;
        } catch (ConvergenceException e) {
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:ec.ui.view.res.ResidualsView.java

private Range calcRange(double[] values) {
    double min = Double.NEGATIVE_INFINITY, max = -Double.POSITIVE_INFINITY;

    DescriptiveStatistics stats = new DescriptiveStatistics(new DataBlock(values));
    double smin = stats.getMin(), smax = stats.getMax();
    if (Double.isInfinite(min) || smin < min) {
        min = smin;//from w  w  w .ja v  a  2  s.  c  o  m
    }
    if (Double.isInfinite(max) || smax > max) {
        max = smax;
    }

    if (Double.isInfinite(max) || Double.isInfinite(min)) {
        return new Range(0, 1);
    }
    double length = max - min;
    if (length == 0) {
        return new Range(0, 1);
    } else {
        //double eps = length * .05;
        //return new Range(min - eps, max + eps);
        return new Range(min, max);
    }
}

From source file:com.moilioncircle.redis.replicator.rdb.AbstractRdbParser.java

protected double rdbLoadDoubleValue() throws IOException {
    int len = in.read();
    switch (len) {
    case 255://from   w w  w  . jav a  2  s  .co  m
        return Double.NEGATIVE_INFINITY;
    case 254:
        return Double.POSITIVE_INFINITY;
    case 253:
        return Double.NaN;
    default:
        byte[] bytes = in.readBytes(len);
        return Double.valueOf(new String(bytes));
    }
}

From source file:net.iponweb.hadoop.streaming.avro.IOWJsonDecoder.java

@Override
public double readDouble() throws IOException {
    advance(Symbol.DOUBLE);//from   ww  w  .  j  ava2s .  co m
    if (in.getCurrentToken().isNumeric()) {
        double result = in.getDoubleValue();
        in.nextToken();
        return result;
    } else {
        try {
            String s = in.getText();
            in.nextToken();
            if (s.equals("NaN")) {
                return Double.NaN;
            } else if (s.equals("-Inf")) {
                return Double.NEGATIVE_INFINITY;
            } else if (s.equals("+Inf")) {
                return Double.POSITIVE_INFINITY;
            } else {
                return Double.parseDouble(s);
            }
        } catch (Exception e) {
            throw error("double (" + e.getMessage() + ")");
        }
    }
}

From source file:com.rapidminer.operator.preprocessing.discretization.FrequencyDiscretization.java

@Override
public PreprocessingModel createPreprocessingModel(ExampleSet exampleSet) throws OperatorException {
    HashMap<Attribute, double[]> ranges = new HashMap<Attribute, double[]>();
    // Get and check parametervalues
    boolean useSqrt = getParameterAsBoolean(PARAMETER_USE_SQRT_OF_EXAMPLES);
    int numberOfBins = 0;
    if (!useSqrt) {
        // if not automatic sizing of bins, use parametervalue
        numberOfBins = getParameterAsInt(PARAMETER_NUMBER_OF_BINS);
        if (numberOfBins >= (exampleSet.size() - 1)) {
            throw new UserError(this, 116, PARAMETER_NUMBER_OF_BINS,
                    "number of bins must be smaller than number of examples (here: " + exampleSet.size() + ")");
        }/*from  w  w  w  .j  ava  2  s . c  o  m*/
    } else {
        exampleSet.recalculateAllAttributeStatistics();
    }

    for (Attribute currentAttribute : exampleSet.getAttributes()) {
        if (useSqrt) {
            numberOfBins = (int) Math.round(Math.sqrt(
                    exampleSet.size() - (int) exampleSet.getStatistics(currentAttribute, Statistics.UNKNOWN)));
        }
        double[] attributeRanges = new double[numberOfBins];
        ExampleSet sortedSet = new SortedExampleSet(exampleSet, currentAttribute, SortedExampleSet.INCREASING);

        // finding ranges
        double examplesPerBin = exampleSet.size() / (double) numberOfBins;
        double currentBinSpace = examplesPerBin;
        double lastValue = Double.NaN;
        int currentBin = 0;

        for (Example example : sortedSet) {
            double value = example.getValue(currentAttribute);
            if (!Double.isNaN(value)) {
                // change bin if full and not last
                if (currentBinSpace < 1 && currentBin < numberOfBins && value != lastValue) {
                    if (!Double.isNaN(lastValue)) {
                        attributeRanges[currentBin] = (lastValue + value) / 2;
                        currentBin++;
                        currentBinSpace += examplesPerBin; // adding because same values might
                        // cause binspace to be negative
                        if (currentBinSpace < 1) {
                            throw new UserError(this, 944, currentAttribute.getName());
                        }
                    }
                }
                currentBinSpace--;
                lastValue = value;
            }
        }
        attributeRanges[numberOfBins - 1] = Double.POSITIVE_INFINITY;
        ranges.put(currentAttribute, attributeRanges);
    }
    DiscretizationModel model = new DiscretizationModel(exampleSet);

    // determine number of digits
    int numberOfDigits = -1;
    if (getParameterAsBoolean(PARAMETER_AUTOMATIC_NUMBER_OF_DIGITS) == false) {
        numberOfDigits = getParameterAsInt(PARAMETER_NUMBER_OF_DIGITS);
    }

    model.setRanges(ranges, "range", getParameterAsInt(PARAMETER_RANGE_NAME_TYPE), numberOfDigits);
    return model;
}

From source file:es.udc.gii.common.eaf.algorithm.operator.reproduction.CMASamplePopulationOperator.java

private void eidgendecomposition(CMAEvolutionaryAlgorithm algorithm, int N, int flgforce) {

    if (countCUpdatesSinceEigenupdate == 0 && flgforce < 2) {
        return;//w w w.ja  v a2  s. co  m
    }

    if (!algorithm.getFlgDiag() && flgforce <= 0
            && countCUpdatesSinceEigenupdate < 1.0 / algorithm.getCcov() / N / 5.0) {
        return;
    }

    if (algorithm.getFlgDiag()) {
        for (int i = 0; i < N; i++) {
            algorithm.getDiag()[i] = Math.sqrt(algorithm.getC()[i][i]);
        }
        algorithm.setCountCupdatesSinceEigenupdate(0);

    } else {
        // set B <- C
        for (int i = 0; i < N; i++) {
            for (int j = 0; j <= i; j++) {
                algorithm.getB()[i][j] = algorithm.getB()[j][i] = algorithm.getC()[i][j];
            }
        }

        //eigendecomposition:
        double[] offdiag = new double[N];
        tred2(N, algorithm.getB(), algorithm.getDiag(), offdiag);
        tql2(N, algorithm.getDiag(), offdiag, algorithm.getB());
        checkEigenSystem(N, algorithm.getC(), algorithm.getDiag(), algorithm.getB());

        for (int i = 0; i < N; i++) {
            if (algorithm.getDiag()[i] < 0.0) {
                if (algorithm.isDebug()) {
                    System.err.println("ERROR - An eigenvalue has become negative.");
                }
            }
            algorithm.getDiag()[i] = Math.sqrt(algorithm.getDiag()[i]);
        }
        algorithm.setCountCupdatesSinceEigenupdate(0);
    }

    /*
     * TODO: Check if necessary
     */
    if (StatUtils.min(algorithm.getDiag()) == 0.0) {
        algorithm.setAxisRatio(Double.POSITIVE_INFINITY);
    } else {
        algorithm.setAxisRatio(StatUtils.max(algorithm.getDiag()) / StatUtils.min(algorithm.getDiag()));
    }

}