Example usage for java.lang Double NEGATIVE_INFINITY

List of usage examples for java.lang Double NEGATIVE_INFINITY

Introduction

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

Prototype

double NEGATIVE_INFINITY

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

Click Source Link

Document

A constant holding the negative infinity of type double .

Usage

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();/*from   w  ww  .j  ava2 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:edu.cuny.cat.stat.HistoricalReport.java

public double getHighestUnacceptedBidPrice() {
    if (highestUnmatchedBid != null) {
        return highestUnmatchedBid.getPrice();
    }/*from   w w w  .  j  ava2 s. c  om*/

    final Iterator<Shout> i = bids.iterator();
    double highestUnacceptedBidPrice = Double.NEGATIVE_INFINITY;
    while (i.hasNext()) {
        final Shout s = i.next();
        if (!isMatched(s)) {
            if (s.getPrice() > highestUnacceptedBidPrice) {
                highestUnacceptedBidPrice = s.getPrice();
                highestUnmatchedBid = s;
            }
        }
    }
    return highestUnacceptedBidPrice;
}

From source file:gdsc.smlm.ij.plugins.pcpalm.PCPALMMolecules.java

/**
 * Calculate the average precision by fitting a skewed Gaussian to the histogram of the precision distribution.
 * <p>//from ww  w  . j  av a2 s  .c  o  m
 * A simple mean and SD of the histogram is computed. If the mean of the Skewed Gaussian does not fit within 3 SDs
 * of the simple mean then the simple mean is returned.
 * 
 * @param molecules
 * @param title
 *            the plot title (null if no plot should be displayed)
 * @param histogramBins
 * @param logFitParameters
 *            Record the fit parameters to the ImageJ log
 * @param removeOutliers
 *            The distribution is created using all values within 1.5x the inter-quartile range (IQR) of the data
 * @return The average precision
 */
public double calculateAveragePrecision(ArrayList<Molecule> molecules, String title, int histogramBins,
        boolean logFitParameters, boolean removeOutliers) {
    // Plot histogram of the precision
    float[] data = new float[molecules.size()];
    DescriptiveStatistics stats = new DescriptiveStatistics();
    double yMin = Double.NEGATIVE_INFINITY, yMax = 0;
    for (int i = 0; i < data.length; i++) {
        data[i] = (float) molecules.get(i).precision;
        stats.addValue(data[i]);
    }

    // Set the min and max y-values using 1.5 x IQR 
    if (removeOutliers) {
        double lower = stats.getPercentile(25);
        double upper = stats.getPercentile(75);
        if (Double.isNaN(lower) || Double.isNaN(upper)) {
            if (logFitParameters)
                Utils.log("Error computing IQR: %f - %f", lower, upper);
        } else {
            double iqr = upper - lower;

            yMin = FastMath.max(lower - iqr, stats.getMin());
            yMax = FastMath.min(upper + iqr, stats.getMax());

            if (logFitParameters)
                Utils.log("  Data range: %f - %f. Plotting 1.5x IQR: %f - %f", stats.getMin(), stats.getMax(),
                        yMin, yMax);
        }
    }

    if (yMin == Double.NEGATIVE_INFINITY) {
        yMin = stats.getMin();
        yMax = stats.getMax();

        if (logFitParameters)
            Utils.log("  Data range: %f - %f", yMin, yMax);
    }

    float[][] hist = Utils.calcHistogram(data, yMin, yMax, histogramBins);

    Plot2 plot = null;
    if (title != null) {
        plot = new Plot2(title, "Precision", "Frequency");
        float[] xValues = hist[0];
        float[] yValues = hist[1];
        if (xValues.length > 0) {
            double xPadding = 0.05 * (xValues[xValues.length - 1] - xValues[0]);
            plot.setLimits(xValues[0] - xPadding, xValues[xValues.length - 1] + xPadding, 0,
                    Maths.max(yValues) * 1.05);
        }
        plot.addPoints(xValues, yValues, Plot2.BAR);
        Utils.display(title, plot);
    }

    // Extract non-zero data
    float[] x = Arrays.copyOf(hist[0], hist[0].length);
    float[] y = hist[1];
    int count = 0;
    float dx = (x[1] - x[0]) * 0.5f;
    for (int i = 0; i < y.length; i++)
        if (y[i] > 0) {
            x[count] = x[i] + dx;
            y[count] = y[i];
            count++;
        }
    x = Arrays.copyOf(x, count);
    y = Arrays.copyOf(y, count);

    // Sense check to fitted data. Get mean and SD of histogram
    double[] stats2 = Utils.getHistogramStatistics(x, y);
    double mean = stats2[0];
    if (logFitParameters)
        log("  Initial Statistics: %f +/- %f", stats2[0], stats2[1]);

    // Standard Gaussian fit
    double[] parameters = fitGaussian(x, y);
    if (parameters == null) {
        log("  Failed to fit initial Gaussian");
        return mean;
    }
    double newMean = parameters[1];
    double error = Math.abs(stats2[0] - newMean) / stats2[1];
    if (error > 3) {
        log("  Failed to fit Gaussian: %f standard deviations from histogram mean", error);
        return mean;
    }
    if (newMean < yMin || newMean > yMax) {
        log("  Failed to fit Gaussian: %f outside data range %f - %f", newMean, yMin, yMax);
        return mean;
    }

    mean = newMean;

    if (logFitParameters)
        log("  Initial Gaussian: %f @ %f +/- %f", parameters[0], parameters[1], parameters[2]);

    double[] initialSolution = new double[] { parameters[0], parameters[1], parameters[2], -1 };

    // Fit to a skewed Gaussian (or appropriate function)
    double[] skewParameters = fitSkewGaussian(x, y, initialSolution);
    if (skewParameters == null) {
        log("  Failed to fit Skewed Gaussian");
        return mean;
    }

    SkewNormalFunction sn = new SkewNormalFunction(skewParameters);
    if (logFitParameters)
        log("  Skewed Gaussian: %f @ %f +/- %f (a = %f) => %f +/- %f", skewParameters[0], skewParameters[1],
                skewParameters[2], skewParameters[3], sn.getMean(), Math.sqrt(sn.getVariance()));

    newMean = sn.getMean();
    error = Math.abs(stats2[0] - newMean) / stats2[1];
    if (error > 3) {
        log("  Failed to fit Skewed Gaussian: %f standard deviations from histogram mean", error);
        return mean;
    }
    if (newMean < yMin || newMean > yMax) {
        log("  Failed to fit Skewed Gaussian: %f outside data range %f - %f", newMean, yMin, yMax);
        return mean;
    }

    // Use original histogram x-axis to maintain all the bins
    if (plot != null) {
        x = hist[0];
        for (int i = 0; i < y.length; i++)
            x[i] += dx;
        plot.setColor(Color.red);
        addToPlot(plot, x, skewParameters, Plot2.LINE);

        plot.setColor(Color.black);
        Utils.display(title, plot);
    }

    // Return the average precision from the fitted curve
    return newMean;
}

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  .  ja va 2s . co  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;
    }
}

From source file:org.obiba.mica.web.model.DatasetDtos.java

private Mica.DatasetVariableAggregationDto.Builder asDto(Math.ContinuousSummaryDto summary) {
    Mica.DatasetVariableAggregationDto.Builder aggDto = Mica.DatasetVariableAggregationDto.newBuilder();
    Math.DescriptiveStatsDto stats = summary.getSummary();

    aggDto.setN(Long.valueOf(stats.getN()).intValue());

    Mica.StatisticsDto.Builder builder = Mica.StatisticsDto.newBuilder();

    if (stats.hasSum())
        builder.setSum(Double.valueOf(stats.getSum()).floatValue());
    if (stats.hasMin() && stats.getMin() != Double.POSITIVE_INFINITY)
        builder.setMin(Double.valueOf(stats.getMin()).floatValue());
    if (stats.hasMax() && stats.getMax() != Double.NEGATIVE_INFINITY)
        builder.setMax(Double.valueOf(stats.getMax()).floatValue());
    if (stats.hasMean() && !Double.isNaN(stats.getMean()))
        builder.setMean(Double.valueOf(stats.getMean()).floatValue());
    if (stats.hasSumsq() && !Double.isNaN(stats.getSumsq()))
        builder.setSumOfSquares(Double.valueOf(stats.getSumsq()).floatValue());
    if (stats.hasVariance() && !Double.isNaN(stats.getVariance()))
        builder.setVariance(Double.valueOf(stats.getVariance()).floatValue());
    if (stats.hasStdDev() && !Double.isNaN(stats.getStdDev()))
        builder.setStdDeviation(Double.valueOf(stats.getStdDev()).floatValue());

    aggDto.setStatistics(builder);//from www .j a va 2  s . c om

    if (summary.getFrequenciesCount() > 0) {
        summary.getFrequenciesList().forEach(freq -> aggDto.addFrequencies(asDto(freq)));
    }

    int total = 0;
    if (summary.getFrequenciesCount() > 0) {
        for (Math.FrequencyDto freq : summary.getFrequenciesList()) {
            total += freq.getFreq();
        }
    }
    aggDto.setTotal(total);

    return aggDto;
}

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

public double getHighestAcceptedAskPrice() {
    final Iterator<Shout> i = asks.iterator();
    double highestAcceptedAskPrice = Double.NEGATIVE_INFINITY;
    while (i.hasNext()) {
        final Shout s = i.next();
        if (isMatched(s)) {
            if (s.getPrice() > highestAcceptedAskPrice) {
                highestAcceptedAskPrice = s.getPrice();
            }//from   w ww . j  ava  2  s  .  c  o  m
        }
    }
    return highestAcceptedAskPrice;
}

From source file:ffx.xray.CrystalStats.java

/**
 * print signal to noise ratio statistics
 *///from w  w w.  j  a  v a 2s .com
public void printSNStats() {
    double res[][] = new double[n][2];
    double nhkl[] = new double[n + 1];
    double sn[][] = new double[n + 1][3];

    for (int i = 0; i < n; i++) {
        res[i][0] = Double.NEGATIVE_INFINITY;
        res[i][1] = Double.POSITIVE_INFINITY;
    }

    for (HKL ih : reflectionlist.hkllist) {
        int i = ih.index();
        int b = ih.bin();

        // ignored cases
        if (Double.isNaN(fo[i][0]) || fo[i][1] <= 0.0) {
            continue;
        }

        // determine res limits of each bin
        double rs = Crystal.res(crystal, ih);
        if (rs > res[b][0]) {
            res[b][0] = rs;
        }
        if (rs < res[b][1]) {
            res[b][1] = rs;
        }

        // running mean
        nhkl[b]++;
        nhkl[n]++;
        sn[b][0] += (fo[i][0] - sn[b][0]) / nhkl[b];
        sn[b][1] += (fo[i][1] - sn[b][1]) / nhkl[b];
        sn[b][2] += ((fo[i][0] / fo[i][1]) - sn[b][2]) / nhkl[b];

        sn[n][0] += (fo[i][0] - sn[n][0]) / nhkl[b];
        sn[n][1] += (fo[i][1] - sn[n][1]) / nhkl[b];
        sn[n][2] += ((fo[i][0] / fo[i][1]) - sn[n][2]) / nhkl[n];
    }

    StringBuilder sb = new StringBuilder(
            String.format("\n %15s | %7s | %7s | %7s \n", "Res. Range", "Signal", "Sigma", "S/N"));
    for (int i = 0; i < n; i++) {
        sb.append(String.format(" %7.3f %7.3f | ", res[i][0], res[i][1]));
        sb.append(String.format("%7.2f | %7.2f | %7.2f\n", sn[i][0], sn[i][1], sn[i][2]));
    }

    sb.append(String.format(" %7.3f %7.3f | ", res[0][0], res[n - 1][1]));
    sb.append(String.format("%7.2f | %7.2f | %7.2f", sn[n][0], sn[n][1], sn[n][2]));

    if (print) {
        logger.info(sb.toString());
    }
}

From source file:com.AandR.beans.plotting.imagePlotPanel.CanvasPanel.java

private final double[] computeMinMax(double[][] data) {
    double[] minMax = new double[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY };
    for (int j = 0; j < data[0].length; j++) {
        for (int i = 0; i < data.length; i++) {
            minMax[0] = data[i][j] < minMax[0] ? data[i][j] : minMax[0];
            minMax[1] = data[i][j] > minMax[1] ? data[i][j] : minMax[1];
        }//  ww w.  j  a  va 2  s  .  c  o  m
    }
    return minMax;
}

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

private void plotFunctionSample(XYPlot plot, Plotable plotable, String id, Color defaultColor,
        Shape defaultShape, double minX, double maxX, List<String> warnings) throws ConvertException {
    double[][] functionPoints = plotable.getFunctionPoints(paramX, paramY, unitX, unitY, transformX, transformY,
            minX, maxX, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
    double[][] samplePoints;

    if (!inverse) {
        samplePoints = plotable.getFunctionSamplePoints(paramX, paramY, unitX, unitY, transformX, transformY,
                minX, maxX, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, warnings);
    } else {// w ww  .  j  a  v a 2  s .  c o  m
        samplePoints = plotable.getInverseFunctionSamplePoints(paramX, paramY, unitX, unitY, transformX,
                transformY, minX, maxX, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, warnings);
    }

    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);
    }

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

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

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

    if (functionPoints != 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 < functionPoints[0].length; j++) {
                double error = Double.isNaN(functionErrors[1][j]) ? 0.0 : functionErrors[1][j];

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

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

            if (samplePoints != null) {
                functionRenderer.setBaseSeriesVisibleInLegend(false);
            }

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

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

            if (samplePoints != null) {
                functionRenderer.setBaseSeriesVisibleInLegend(false);
            }

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

        if (samplePoints != null) {
            DefaultXYDataset sampleDataset = new DefaultXYDataset();
            XYLineAndShapeRenderer sampleRenderer = new XYLineAndShapeRenderer(false, true);

            sampleDataset.addSeries(legend, samplePoints);
            sampleRenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
            sampleRenderer.setSeriesPaint(0, color);
            sampleRenderer.setSeriesShape(0, shape);

            plot.setDataset(i + 1, sampleDataset);
            plot.setRenderer(i + 1, sampleRenderer);
        }
    }
}

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

/**
* The theta, the sensitivity of the present value to a change in time to maturity, $\-frac{\partial V}{\partial T}$
*
* @param spot The spot value of the underlying
* @param strike The Strike/*from  w  ww .j  a va 2  s.  co  m*/
* @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, false for put
* @return theta
*/
@ExternalFunction
public static double theta(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");

    if (Math.abs(interestRate) > LARGE) {
        return 0.;
    }
    double discount = (Math.abs(interestRate) < SMALL && timeToExpiry > LARGE) ? 1.
            : Math.exp(-interestRate * timeToExpiry);

    if (costOfCarry > LARGE) {
        return isCall ? Double.NEGATIVE_INFINITY : 0.;
    }
    if (-costOfCarry > LARGE) {
        final double res = isCall ? 0. : (discount > SMALL ? strike * discount * interestRate : 0.);
        return Double.isNaN(res) ? discount : res;
    }

    if (spot > LARGE * strike) {
        final double tmp = Math.exp((costOfCarry - interestRate) * timeToExpiry);
        final double res = isCall ? (tmp > SMALL ? -(costOfCarry - interestRate) * spot * tmp : 0.) : 0.;
        return Double.isNaN(res) ? tmp : res;
    }
    if (LARGE * spot < strike) {
        final double res = isCall ? 0. : (discount > SMALL ? strike * discount * interestRate : 0.);
        return Double.isNaN(res) ? discount : res;
    }
    if (spot > LARGE && strike > LARGE) {
        return Double.POSITIVE_INFINITY;
    }

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

    final int sign = isCall ? 1 : -1;
    double d1 = 0.;
    double d2 = 0.;
    if (Math.abs(spot - strike) < SMALL || sigmaRootT > LARGE) {
        final double coefD1 = (Math.abs(costOfCarry) < SMALL && lognormalVol < SMALL)
                ? Math.signum(costOfCarry) + 0.5 * lognormalVol
                : (costOfCarry / lognormalVol + 0.5 * lognormalVol);
        final double tmpD1 = Math.abs(coefD1) < SMALL ? 0. : coefD1 * rootT;
        d1 = Double.isNaN(tmpD1) ? Math.signum(coefD1) : tmpD1;
        final double coefD2 = (Math.abs(costOfCarry) < SMALL && lognormalVol < SMALL)
                ? Math.signum(costOfCarry) - 0.5 * lognormalVol
                : (costOfCarry / lognormalVol - 0.5 * lognormalVol);
        final double tmpD2 = Math.abs(coefD2) < SMALL ? 0. : coefD2 * rootT;
        d2 = Double.isNaN(tmpD2) ? Math.signum(coefD2) : tmpD2;
    } else {
        if (sigmaRootT < SMALL) {
            d1 = (Math.log(spot / strike) / rootT + costOfCarry * rootT) / lognormalVol;
            d2 = d1;
        } else {
            final double tmp = (Math.abs(costOfCarry) < SMALL && lognormalVol < SMALL) ? rootT
                    : ((Math.abs(costOfCarry) < SMALL && rootT > LARGE) ? 1. / lognormalVol
                            : costOfCarry / lognormalVol * rootT);
            d1 = Math.log(spot / strike) / sigmaRootT + tmp + 0.5 * sigmaRootT;
            d2 = d1 - sigmaRootT;
        }
    }
    //    if (Double.isNaN(d1) || Double.isNaN(d2)) {
    //      throw new IllegalArgumentException("NaN found");
    //    }
    final double norm = NORMAL.getPDF(d1);
    final double rescaledSpot = Math.exp((costOfCarry - interestRate) * timeToExpiry) * spot;
    final double rescaledStrike = discount * strike;
    final double normForSpot = NORMAL.getCDF(sign * d1);
    final double normForStrike = NORMAL.getCDF(sign * d2);
    final double spotTerm = normForSpot < SMALL ? 0.
            : (Double.isNaN(rescaledSpot) ? -sign * Math.signum((costOfCarry - interestRate)) * rescaledSpot
                    : -sign * ((costOfCarry - interestRate) * rescaledSpot * normForSpot));
    final double strikeTerm = normForStrike < SMALL ? 0.
            : (Double.isNaN(rescaledSpot) ? sign * (-Math.signum(interestRate) * discount)
                    : sign * (-interestRate * rescaledStrike * normForStrike));

    double coef = rescaledSpot * lognormalVol / rootT;
    if (Double.isNaN(coef)) {
        coef = 1.; //ref value is returned
    }
    final double dlTerm = norm < SMALL ? 0. : -0.5 * norm * coef;

    final double res = dlTerm + spotTerm + strikeTerm;
    return Double.isNaN(res) ? 0. : res;
}