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:gov.nih.nci.caarray.util.CaArrayUtils.java

private static String toXmlString(double value) {
    if (Double.isNaN(value)) {
        return "NaN";
    } else if (value == Double.POSITIVE_INFINITY) {
        return "INF";
    } else if (value == Double.NEGATIVE_INFINITY) {
        return "-INF";
    } else {//from  www .j a  v a  2s. co m
        return Double.toString(value);
    }
}

From source file:edu.byu.nlp.crowdsourcing.models.gibbs.BlockCollapsedMultiAnnModelMath.java

static double[] computeYSums(int docIndex, Map<Integer, Integer> instanceLabels, double[] logCountOfY,
        double[][][] countOfJYAndA, double[][] numAnnsPerJAndY, int[][][] a, int[][] docJCount,
        int numAnnotators, double lambda) {
    double[] ySums = DoubleArrays.of(0, logCountOfY.length);
    Integer label = instanceLabels != null && instanceLabels.containsKey(docIndex)
            ? instanceLabels.get(docIndex)
            : null;//  w w  w .j a  v  a 2s. co  m

    //  double[] ySums = logCountOfY.clone();
    for (int c = 0; c < ySums.length; c++) {
        // labeled item (uses delta function prob)
        if (label != null) {
            ySums[c] = c == label ? 0 : Double.NEGATIVE_INFINITY; // prob = 0/1
        }
        // no label (just theta prior and annotations) 
        else {
            // theta
            ySums[c] = GammaFunctions.logRatioOfGammasByDifference(Math.exp(logCountOfY[c]), lambda);

            // gamma
            for (int j = 0; j < numAnnotators; j++) {
                ySums[c] += computeYSum(countOfJYAndA[j][c], numAnnsPerJAndY[j][c], a[docIndex][j],
                        docJCount[docIndex][j]);
            }
        }
    }
    return ySums;
}

From source file:fri.cbw.ThermodynamicSimulationEngine.DDE23.java

/**
 *
 * @param ddes delay differential equation
 * @param lags array of time delay values, does not have to be sorted
 * @param history values of variables for t < 0
 * @param t0 time to start the integration, it should be 0
 * @param tfinal the end time/*ww w. ja v  a 2s . c om*/
 * @return Class representing the result of the integration
 */
public static IntegrationResult integrate(FirstOrderDelayDifferentialEquations ddes, double[] lags,
        double[] history, double t0, double tfinal) {
    IntegrationResult sol = new IntegrationResult();

    //% Initialize statistics.
    int nsteps = 0;
    int nfailed = 0;
    int nfevals = 0;

    if (tfinal <= t0) {
        throw new IllegalArgumentException("Must have t0 < tfinal.");
    }

    double[] temp;
    temp = new double[history.length];
    System.arraycopy(history, 0, temp, 0, history.length);

    double[] y0;
    y0 = new double[temp.length];
    System.arraycopy(temp, 0, y0, 0, temp.length);
    int maxlevel = 4;

    double t = t0;
    double[] y;
    y = new double[y0.length];
    System.arraycopy(y0, 0, y, 0, y0.length);
    int neq = ddes.getDimension();

    //% If solving a DDE, locate potential discontinuities. We need to step to
    //% each of the points of potential lack of smoothness. Because we start at
    //% t0, we can remove it from discont.  The solver always steps to tfinal,
    //% so it is convenient to add it to discont.
    List<Double> discont = new ArrayList<Double>();
    double minlag = Double.POSITIVE_INFINITY;
    if (lags.length == 0) {
        discont.add(tfinal);
    } else {
        for (int i = 0; i < lags.length; i++) {
            if (lags[i] < minlag) {
                minlag = lags[i];
            }
        }
        if (minlag <= 0) {
            throw new IllegalArgumentException("The lags must all be positive.");
        }
        List<Double> vl = new ArrayList<Double>();
        vl.add(t0);

        for (int level = 1; level < maxlevel; level++) { // it is not <=, comparing discont with matlab
            List<Double> vlp1 = new ArrayList<Double>();
            for (int i = 0; i < vl.size(); i++) {
                for (int x = 0; x < lags.length; x++) { // should probably be from 1 // probably not
                    vlp1.add(vl.get(i) + lags[x]);
                }
            }
            //% Restrict to tspan.
            vl.clear(); // FIXME: this looks like a mistake
            for (double x : vlp1) {
                if (x <= tfinal) {
                    vl.add(x);
                }
                if (vl.isEmpty()) {
                    break;
                }
                int nvl = vl.size();
                if (nvl > 1) //% Purge duplicates in vl.
                {
                    vl = purgeDuplicates(vl);
                }
            }
            discont.addAll(vl); // FIXME: make sure this is where it should be
        }
        if (discont.size() > 1) {
            //% Purge duplicates.
            discont = purgeDuplicates(discont);
        }
    }

    //% Add tfinal to the list of discontinuities if it is not already included.
    int end = discont.size() - 1;
    if (Math.abs(tfinal - discont.get(end)) <= 10 * eps * Math.abs(tfinal)) {
        discont.set(end, tfinal);
    } else {
        discont.add(tfinal);
    }
    sol.setDiscont(discont);
    //% Discard t0 and discontinuities in the history.
    List<Integer> indices = new ArrayList<Integer>();
    for (int i = 0; i < discont.size(); i++) {
        if (discont.get(i) <= t0) {
            indices.add(i);
        }
    }
    discont.removeAll(indices);

    int nextdsc = 0; // zero based
    List<Integer> ndxdsc = new ArrayList<Integer>();
    ndxdsc.add(1);

    //% Get options, and set defaults.
    double rtol = 1e-3;

    //        if (rtol < 100 * eps) {
    //           rtol = 100 * eps;
    //           warning(['RelTol has been increased to ' num2str(rtol) '.']);
    //        }
    double atol = 1e-6;
    double threshold = atol / rtol;

    //% By default, hmax is 1/10 of the interval of integration.
    double hmax = Math.min((tfinal - t), Math.abs(0.1 * (tfinal - t)));
    if (hmax <= 0) {
        throw new IllegalArgumentException("MaxStep must be greater than zero.");
    }
    double minstep = 0;
    boolean rept_minh = false;

    boolean printstats = true; // FIXME: for debugging

    //% Allocate storage for output arrays and initialize them.
    int chunk = Math.min(100, (int) Math.floor((2 ^ 13) / neq));

    double[] tout = new double[chunk];
    double[][] yout = new double[chunk][neq]; // flip to C style
    double[][] ypout = new double[chunk][neq];
    int nout = 1; // FIXME: should be 0-based? // probably not
    tout[nout] = t;
    for (int k = 0; k < neq; k++) {
        for (int l = 0; l < chunk; l++) {
            yout[l][k] = y[k]; // FIXME: think about this
        }
    }

    //% Initialize method parameters.
    double pow = 1.0 / 3;
    double[][] B = { // transposed
            { 1.0 / 2, 0, 0, 0 }, { 0, 3.0 / 4, 0, 0, 0 }, { 2.0 / 9, 1.0 / 3, 4.0 / 9.0, 0 } };
    double[] E = { -5.0 / 72, 1.0 / 12, 1.0 / 9, -1.0 / 8 };

    double[][] f = new double[4][neq]; // flipped to C indexing

    //% Evaluate initial history at t0 - lags.
    double[][] emptyarr = new double[0][];
    double[] fakeX = { t }; // FIXME: try to understand what is happening here with t/X
    double[][] fakeY = { y }; // FIXME: DTTO
    double[][] Z = lagvals(t, lags, history, fakeX, fakeY, emptyarr);

    double[] f0 = new double[neq];
    ddes.computeDerivatives(t, y, Z, f0);
    for (int k = 0; k < neq; k++) {
        ypout[nout][k] = f0[k]; // FIXME: think about this // should be correct now
    }
    nfevals = nfevals + 1; //% stats
    int m = f0.length;
    if (m != neq) {
        // FIXME: find better class to throw?
        throw new IllegalArgumentException("returned derivative and history vectors of different lengths.");
    }

    double hmin = 16 * eps * t;

    //% Compute an initial step size h using y'(t).
    double h = Math.min(hmax, tfinal - t0);
    double n = Double.NEGATIVE_INFINITY; // compute norm(xk, inf)
    for (int k = 0; k < f0.length; k++) {
        double xk = Math.abs(f0[k] / Math.max(Math.abs(y[k]), threshold));
        if (xk > n) {
            n = xk;
        }
    }
    double rh = n / (0.8 * Math.pow(rtol, pow));
    if (h * rh > 1) {
        h = 1.0 / rh; // NOTE: used to have 1 / rh there, did not work :(
    }
    h = Math.max(h, hmin);

    //% Make sure that the first step is explicit so that the code can
    //% properly initialize the interpolant.
    h = Math.min(h, 0.5 * minlag);

    System.arraycopy(f0, 0, f[0], 0, neq);

    //% THE MAIN LOOP
    double[] last_y = new double[neq];
    boolean done = false;
    while (!done) {
        //% By default, hmin is a small number such that t+hmin is only slightly
        //% different than t.  It might be 0 if t is 0.
        hmin = 16 * eps * t;
        h = Math.min(hmax, Math.max(hmin, h)); //% couldn't limit h until new hmin

        //% Adjust step size to hit discontinuity. tfinal = discont(end).
        boolean hitdsc = false;
        double distance = discont.get(nextdsc) - t;
        if (Math.min(1.1 * h, hmax) >= distance) { //% stretch
            h = distance;
            hitdsc = true;
        } else if (2 * h >= distance) { //% look-ahead
            h = distance / 2;
        }
        if (!hitdsc && (minlag < h) && (h < 2 * minlag)) {
            h = minlag;
        }

        //% LOOP FOR ADVANCING ONE STEP.
        double tnew;
        double ynew[] = new double[neq];
        boolean itfail;
        boolean nofailed = true; //% no failed attempts
        double err;
        while (true) {
            double[][] hB = new double[3][4]; // dimensions of B
            for (int k = 0; k < 3; k++) {
                for (int l = 0; l < 4; l++) {
                    hB[k][l] = h * B[k][l];
                }
            }
            double t1 = t + 0.5 * h;
            double t2 = t + 0.75 * h;
            tnew = t + h;

            //% If a lagged argument falls in the current step, we evaluate the
            //% formula by iteration. Extrapolation is used for the evaluation
            //% of the history terms in the first iteration and the tnew,ynew,
            //% ypnew of the current iteration are used in the evaluation of
            //% these terms in the next iteration.
            int maxit;
            if (minlag < h) {
                maxit = 5;
            } else {
                maxit = 1;
            }
            // FIXME: maybe it should be implemented as appending to a List
            double[] X = new double[nout + 1]; // looks like +1 is unavoidable. nout is the largest index, it is 1 based
            System.arraycopy(tout, 0, X, 0, nout + 1); // so need nout+1 length
            double[][] Y = new double[nout + 1][neq];
            System.arraycopy(yout, 0, Y, 0, nout + 1);
            double[][] YP = new double[nout + 1][neq];
            System.arraycopy(ypout, 0, YP, 0, nout + 1);

            itfail = false;
            for (int iter = 1; iter <= maxit; iter++) { //FIXME: 0-based? // no, from 1, maybe <= // try <=
                double[] vecmul1 = new double[neq];
                for (int k = 0; k < neq; k++) { // FIXME: merge the loops, it should be possible // it is not
                    for (int l = 0; l < 4; l++) {
                        vecmul1[k] += f[l][k] * hB[0][l];
                    }
                }
                double yt1[] = new double[neq];
                for (int k = 0; k < f[0].length; k++) { // changed from f.length
                    yt1[k] = y[k] + vecmul1[k]; // FIXME: indices?
                }

                Z = lagvals(t1, lags, history, X, Y, YP);
                ddes.computeDerivatives(t1, yt1, Z, f[1]);

                double[] vecmul2 = new double[neq];
                for (int k = 0; k < neq; k++) { // FIXME: merge the loops, it should be possible // it is not
                    for (int l = 0; l < 4; l++) {
                        vecmul2[k] += f[l][k] * hB[1][l];
                    }
                }
                double yt2[] = new double[neq];
                for (int k = 0; k < f[0].length; k++) { // changed from f.length
                    yt2[k] = y[k] + vecmul2[k];
                }

                Z = lagvals(t2, lags, history, X, Y, YP);
                ddes.computeDerivatives(t2, yt2, Z, f[2]);

                double[] vecmul3 = new double[neq];
                for (int k = 0; k < neq; k++) {
                    for (int l = 0; l < 4; l++) {
                        vecmul3[k] += f[l][k] * hB[2][l];
                    }
                }
                for (int k = 0; k < f[0].length; k++) {
                    ynew[k] = y[k] + vecmul3[k];
                }

                Z = lagvals(tnew, lags, history, X, Y, YP);
                ddes.computeDerivatives(tnew, ynew, Z, f[3]);

                nfevals = nfevals + 3;
                if (maxit > 1) {
                    if (iter > 1) {
                        double errit = Double.NEGATIVE_INFINITY;
                        for (int k = 0; k < neq; k++) { // another norm(erritk, inf)
                            // missed this norm the first time
                            double erritk = Math.abs((ynew[k] - last_y[k])
                                    / Math.max(Math.max(Math.abs(y[k]), Math.abs(ynew[k])), threshold));
                            if (erritk > errit) {
                                errit = erritk;
                            }
                        }

                        if (errit <= 0.1 * rtol) {
                            break;
                        }
                    }
                    //% Use the tentative solution at tnew in the evaluation of the
                    //% history terms of the next iteration.
                    X = Arrays.copyOf(X, X.length + 1);
                    X[X.length - 1] = tnew;

                    Y = Arrays.copyOf(Y, Y.length + 1);
                    Y[Y.length - 1] = ynew;

                    YP = Arrays.copyOf(YP, YP.length + 1);
                    YP[YP.length - 1] = f[3];

                    System.arraycopy(ynew, 0, last_y, 0, ynew.length); // had switched last_y and ynew
                    itfail = (iter == maxit);
                }
            }

            // FIXME: matrix multiplication?
            // had bug: first compute n, then do h*n, not the other way
            double[] vecmul = new double[neq];
            for (int l = 0; l < neq; l++) {
                for (int o = 0; o < E.length; o++) {
                    vecmul[l] += (f[o][l] * E[o]);
                }
            }
            n = Double.NEGATIVE_INFINITY;
            for (int k = 0; k < neq; k++) {
                // had bug: infty norm is in abs value
                // another ona, had multiplication instead of division. took me two hours to realize :(
                double nk = Math
                        .abs(vecmul[k] / Math.max(Math.max(Math.abs(y[k]), Math.abs(ynew[k])), threshold)); // FIXME: indexing of f
                if (nk > n) {
                    n = nk;
                }
            }

            //% Estimate the error.
            err = h * n;

            //% If h <= minstep, adjust err so that the step will be accepted.
            //% Note that minstep < minlag, so maxit = 1 and itfail = false.  Report
            //% once that a step of minimum size was taken.
            if (h <= minstep) {
                err = Math.min(err, rtol);
                if (rept_minh) {
                    Logger.getGlobal().log(Level.INFO, "Steps of size MinStep were taken.");
                    rept_minh = false;
                }
            }

            //% Accept the solution only if the weighted error is no more than the
            //% tolerance rtol.  Estimate an h that will yield an error of rtol on
            //% the next step or the next try at taking this step, as the case may be,
            //% and use 0.8 of this value to avoid failures.
            if ((err > rtol) || itfail) { //% Failed step
                nfailed = nfailed + 1;
                Logger logger = Logger.getGlobal();
                if (h <= hmin) {
                    String msg = String.format("Failure at t=%e.  Unable to meet integration "
                            + "tolerances without reducing the step size below "
                            + "the smallest value allowed (%e) at time t.\n", t, hmin);
                    logger.log(Level.WARNING, msg);
                    if (printstats) {
                        logger.log(Level.INFO, "%g successful steps", nsteps);
                        logger.log(Level.INFO, "%g failed attempts", nfailed);
                        logger.log(Level.INFO, "%g function evaluations", nfevals);
                    }
                    //% Trim output arrays, place in solution structure, and return.
                    sol = trim(sol, history, nout, tout, yout, ypout, ndxdsc);
                    return sol;
                }

                if (itfail) {
                    h = 0.5 * h;
                    if (h < 2 * minlag) {
                        h = minlag;
                    }
                } else if (nofailed) {
                    nofailed = false;
                    h = Math.max(hmin, h * Math.max(0.5, 0.8 * Math.pow(rtol / err, pow)));
                } else {
                    h = Math.max(hmin, 0.5 * h);
                }
                hitdsc = false;
            } else { //% Successful step
                break;
            }
        }
        nsteps = nsteps + 1; //% stats

        //% Advance the integration one step.
        t = tnew;
        y = ynew;
        System.arraycopy(f[3], 0, f[0], 0, neq); //% BS(2,3) is FSAL.
        nout = nout + 1;

        // reallocate arrays
        if (nout > tout.length - 1) {
            tout = Arrays.copyOf(tout, tout.length + chunk);

            yout = Arrays.copyOf(yout, yout.length + chunk);
            ypout = Arrays.copyOf(ypout, ypout.length + chunk);
            // allocate the second dimensions
            int upto = yout.length;
            for (int k = yout.length - chunk; k < upto; k++) {
                yout[k] = new double[neq];
            }
            upto = ypout.length;
            for (int k = ypout.length - chunk; k < upto; k++) {
                ypout[k] = new double[neq];
            }
        }
        tout[nout] = t;
        for (int k = 0; k < neq; k++) {
            yout[nout][k] = y[k];
            ypout[nout][k] = f[0][k];
        }

        //% If there were no failures, compute a new h.
        if (nofailed && !itfail) {
            //% Note that h may shrink by 0.8, and that err may be 0.
            double temp2 = 1.25 * Math.pow(err / rtol, pow);
            if (temp2 > 0.2) {
                h = h / temp2;
            } else {
                h = 5 * h;
            }
            h = Math.max(h, minstep); //FIXME: NetBeans says value never used
        }

        //% Have we hit tfinal = discont(end)?
        if (hitdsc) {
            nextdsc = nextdsc + 1;
            done = nextdsc > discont.size() - 1;
            if (!done) {
                ndxdsc.add(nout);
            }
        }
    }
    //% Successful integration:
    if (printstats) {
        if (printstats) {
            Logger logger = Logger.getGlobal();
            logger.log(Level.INFO, "%g successful steps", nsteps);
            logger.log(Level.INFO, "%g failed attempts", nfailed);
            logger.log(Level.INFO, "%g function evaluations", nfevals);
        }
    }

    //% Trim output arrays, place in solution structure, and return.
    sol = trim(sol, history, nout, tout, yout, ypout, ndxdsc);
    return sol;
    //% End of function dde23.
}

From source file:org.jfree.data.xy.XYSeriesCollection.java

/**
 * Returns the range of the values in this dataset's domain.
 *
 * @param includeInterval  a flag that determines whether or not the
 *                         x-interval is taken into account.
 *
 * @return The range (or <code>null</code> if the dataset contains no
 *     values).//from  w w  w  .j  av a  2  s  .c o  m
 */
@Override
public Range getDomainBounds(boolean includeInterval) {
    if (includeInterval) {
        return this.intervalDelegate.getDomainBounds(includeInterval);
    } else {
        double lower = Double.POSITIVE_INFINITY;
        double upper = Double.NEGATIVE_INFINITY;
        int seriesCount = getSeriesCount();
        for (int s = 0; s < seriesCount; s++) {
            XYSeries series = getSeries(s);
            double minX = series.getMinX();
            if (!Double.isNaN(minX)) {
                lower = Math.min(lower, minX);
            }
            double maxX = series.getMaxX();
            if (!Double.isNaN(maxX)) {
                upper = Math.max(upper, maxX);
            }
        }
        if (lower > upper) {
            return null;
        } else {
            return new Range(lower, upper);
        }
    }
}

From source file:byps.BBufferJson.java

public double getDouble() {
    StringBuilder sbuf = new StringBuilder(30);
    for (;;) {//from ww w . java 2 s. c  o  m
        char c = nextJsonChar(true);
        if ((c >= '0' && c <= '9') || (c == '.') || (c == 'e') || (c == 'E') || (c == '-') || (c == '+')) {
            sbuf.append(c);
        } else if (c == 'N') {
            internalSkip(2);
            nextJsonChar(false); // update this.lastChar
            return Double.NaN;
        } else if (c == 'I') {
            boolean neg = sbuf.length() != 0 && sbuf.charAt(0) == '-';
            internalSkip(7);
            nextJsonChar(false); // update this.lastChar
            return neg ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
        } else {
            oneCharBack();
            break;
        }
    }
    return Double.parseDouble(sbuf.toString());
}

From source file:dbseer.gui.chart.DBSeerChartFactory.java

public static JFreeChart createXYLinePredictionChart(PredictionCenter center) throws Exception {
    StatisticalPackageRunner runner = DBSeerGUI.runner;

    String title = runner.getVariableString("title");
    Object[] legends = (Object[]) runner.getVariableCell("legends");
    Object[] xCellArray = (Object[]) runner.getVariableCell("Xdata");
    Object[] yCellArray = (Object[]) runner.getVariableCell("Ydata");
    String xLabel = runner.getVariableString("Xlabel");
    String yLabel = runner.getVariableString("Ylabel");

    XYSeriesCollection dataSet = new XYSeriesCollection();

    int numLegends = legends.length;
    int numXCellArray = xCellArray.length;
    int numYCellArray = yCellArray.length;
    int dataCount = 0;

    if (numXCellArray != numYCellArray) {
        JOptionPane.showMessageDialog(null, "The number of X dataset and Y dataset does not match.",
                "The number of X dataset and Y dataset does not match.", JOptionPane.ERROR_MESSAGE);
        System.out.println(numXCellArray + " : " + numYCellArray);
        return null;
    }//from w  w  w .  j a  v a2s.c  om

    final java.util.List<String> transactionNames = center.getTrainConfig().getDataset(0)
            .getTransactionTypeNames();
    for (int i = 0; i < numLegends; ++i) {
        String legend = (String) legends[i];
        for (int j = 0; j < transactionNames.size(); ++j) {
            if (legend.contains("Type " + (j + 1))) {
                legends[i] = legend.replace("Type " + (j + 1), transactionNames.get(j));
                break;
            }
        }
    }
    for (int j = 0; j < transactionNames.size(); ++j) {
        if (xLabel.contains("Type " + (j + 1))) {
            xLabel = xLabel.replace("Type " + (j + 1), transactionNames.get(j));
            break;
        }
    }
    for (int j = 0; j < transactionNames.size(); ++j) {
        if (yLabel.contains("Type " + (j + 1))) {
            yLabel = yLabel.replace("Type " + (j + 1), transactionNames.get(j));
            break;
        }
    }

    for (int i = 0; i < numYCellArray; ++i) {
        double[] xArray = (double[]) xCellArray[i];
        runner.eval("yArraySize = size(Ydata{" + (i + 1) + "});");
        runner.eval("yArray = Ydata{" + (i + 1) + "};");
        double[] yArraySize = runner.getVariableDouble("yArraySize");
        double[] yArray = runner.getVariableDouble("yArray");

        int xLength = xArray.length;
        int row = (int) yArraySize[0];
        int col = (int) yArraySize[1];

        for (int c = 0; c < col; ++c) {
            XYSeries series;
            int legendIdx = (dataCount >= numLegends) ? numLegends - 1 : dataCount;
            String legend = (String) legends[legendIdx];
            if (numLegends == 0) {
                series = new XYSeries("Data " + dataCount + 1);
            } else if (dataCount >= numLegends) {
                series = new XYSeries(legend + (dataCount + 1));
            } else {
                series = new XYSeries(legend);
            }

            for (int r = 0; r < row; ++r) {
                int xRow = (r >= xLength) ? xLength - 1 : r;
                double yValue = yArray[r + c * row];
                // remove negatives & NaN & infs.
                if (yValue < 0 || yValue == Double.NaN || yValue == Double.POSITIVE_INFINITY
                        || yValue == Double.NEGATIVE_INFINITY) {
                    yValue = 0.0;
                }
                series.add(xArray[xRow], yValue);
            }
            dataSet.addSeries(series);
            ++dataCount;
        }
    }

    JFreeChart chart = ChartFactory.createXYLineChart(title, xLabel, yLabel, dataSet);

    // change 'predicted' data to have dotted lines.
    BasicStroke dashStroke = toStroke(STYLE_DASH);
    BasicStroke dotStroke = toStroke(STYLE_DOT);
    BasicStroke lineStroke = toStroke(STYLE_LINE);
    for (int i = 0; i < dataSet.getSeriesCount(); ++i) {
        String legend = (String) dataSet.getSeriesKey(i);
        XYPlot plot = chart.getXYPlot();
        XYItemRenderer renderer = plot.getRenderer();
        if (legend.contains("predicted") || legend.contains("Predicted")) {
            renderer.setSeriesStroke(i, dotStroke);
        } else {
            renderer.setSeriesStroke(i, lineStroke);
        }
    }

    return chart;
}

From source file:hivemall.smile.classification.GradientTreeBoostingClassifierUDTF.java

/**
 * Train L-k tree boost./*from  w  ww .  jav  a2  s  . c om*/
 */
private void traink(final double[][] x, final int[] y, final int k) throws HiveException {
    final int numVars = SmileExtUtils.computeNumInputVars(_numVars, x);
    if (logger.isInfoEnabled()) {
        logger.info("k: " + k + ", numTrees: " + _numTrees + ", shirinkage: " + _eta + ", subsample: "
                + _subsample + ", numVars: " + numVars + ", minSamplesSplit: " + _minSamplesSplit
                + ", maxDepth: " + _maxDepth + ", maxLeafs: " + _maxLeafNodes + ", seed: " + _seed);
    }

    final int numInstances = x.length;
    final int numSamples = (int) Math.round(numInstances * _subsample);

    final double[][] h = new double[k][numInstances]; // boost tree output.
    final double[][] p = new double[k][numInstances]; // posteriori probabilities.
    final double[][] response = new double[k][numInstances]; // pseudo response.

    final int[][] order = SmileExtUtils.sort(_attributes, x);
    final RegressionTree.NodeOutput[] output = new LKNodeOutput[k];
    for (int i = 0; i < k; i++) {
        output[i] = new LKNodeOutput(response[i], k);
    }

    final BitSet sampled = new BitSet(numInstances);
    final int[] bag = new int[numSamples];
    final int[] perm = new int[numSamples];
    for (int i = 0; i < numSamples; i++) {
        perm[i] = i;
    }

    long s = (this._seed == -1L) ? SmileExtUtils.generateSeed() : new smile.math.Random(_seed).nextLong();
    final smile.math.Random rnd1 = new smile.math.Random(s);
    final smile.math.Random rnd2 = new smile.math.Random(rnd1.nextLong());

    // out-of-bag prediction
    final int[] prediction = new int[numInstances];

    for (int m = 0; m < _numTrees; m++) {
        for (int i = 0; i < numInstances; i++) {
            double max = Double.NEGATIVE_INFINITY;
            for (int j = 0; j < k; j++) {
                final double h_ji = h[j][i];
                if (max < h_ji) {
                    max = h_ji;
                }
            }
            double Z = 0.0d;
            for (int j = 0; j < k; j++) {
                double p_ji = Math.exp(h[j][i] - max);
                p[j][i] = p_ji;
                Z += p_ji;
            }
            for (int j = 0; j < k; j++) {
                p[j][i] /= Z;
            }
        }

        final RegressionTree[] trees = new RegressionTree[k];

        Arrays.fill(prediction, -1);
        double max_h = Double.NEGATIVE_INFINITY;
        int oobTests = 0, oobErrors = 0;

        for (int j = 0; j < k; j++) {
            reportProgress(_progressReporter);

            final double[] response_j = response[j];
            final double[] p_j = p[j];
            final double[] h_j = h[j];

            for (int i = 0; i < numInstances; i++) {
                if (y[i] == j) {
                    response_j[i] = 1.0d;
                } else {
                    response_j[i] = 0.0d;
                }
                response_j[i] -= p_j[i];
            }

            SmileExtUtils.shuffle(perm, rnd1);
            for (int i = 0; i < numSamples; i++) {
                int index = perm[i];
                bag[i] = index;
                sampled.set(i);
            }

            RegressionTree tree = new RegressionTree(_attributes, x, response[j], numVars, _maxDepth,
                    _maxLeafNodes, _minSamplesSplit, _minSamplesLeaf, order, bag, output[j], rnd2);
            trees[j] = tree;

            for (int i = 0; i < numInstances; i++) {
                double h_ji = h_j[i] + _eta * tree.predict(x[i]);
                h_j[i] += h_ji;
                if (h_ji > max_h) {
                    max_h = h_ji;
                    prediction[i] = j;
                }
            }

        } // for each k

        // out-of-bag error estimate
        for (int i = sampled.nextClearBit(0); i < numInstances; i = sampled.nextClearBit(i + 1)) {
            oobTests++;
            if (prediction[i] != y[i]) {
                oobErrors++;
            }
        }
        sampled.clear();
        float oobErrorRate = 0.f;
        if (oobTests > 0) {
            oobErrorRate = ((float) oobErrors) / oobTests;
        }

        // forward a row
        forward(m + 1, 0.d, _eta, oobErrorRate, trees);

    } // for each m
}

From source file:ml.shifu.shifu.core.binning.UpdateBinningInfoMapper.java

private void populateStats(String[] units, String tag, Double weight, int columnIndex, int newCCIndex) {
    ColumnConfig columnConfig = this.columnConfigList.get(columnIndex);

    CountAndFrequentItems countAndFrequentItems = this.variableCountMap.get(newCCIndex);
    if (countAndFrequentItems == null) {
        countAndFrequentItems = new CountAndFrequentItems();
        this.variableCountMap.put(newCCIndex, countAndFrequentItems);
    }/*from   w w  w.  jav  a2  s  .  c  o  m*/
    countAndFrequentItems.offer(this.missingOrInvalidValues, units[columnIndex]);

    boolean isMissingValue = false;
    boolean isInvalidValue = false;

    BinningInfoWritable binningInfoWritable = this.columnBinningInfo.get(newCCIndex);
    if (binningInfoWritable == null) {
        return;
    }
    binningInfoWritable.setTotalCount(binningInfoWritable.getTotalCount() + 1L);
    if (columnConfig.isHybrid()) {
        int binNum = 0;
        if (units[columnIndex] == null || missingOrInvalidValues.contains(units[columnIndex].toLowerCase())) {
            isMissingValue = true;
        }
        String str = units[columnIndex];
        double douVal = BinUtils.parseNumber(str);

        Double hybridThreshold = columnConfig.getHybridThreshold();
        if (hybridThreshold == null) {
            hybridThreshold = Double.NEGATIVE_INFINITY;
        }
        // douVal < hybridThreshould which will also be set to category
        boolean isCategory = Double.isNaN(douVal) || douVal < hybridThreshold;
        boolean isNumber = !Double.isNaN(douVal);

        if (isMissingValue) {
            binningInfoWritable.setMissingCount(binningInfoWritable.getMissingCount() + 1L);
            binNum = binningInfoWritable.getBinCategories().size()
                    + binningInfoWritable.getBinBoundaries().size();
        } else if (isCategory) {
            // get categorical bin number in category list
            binNum = quickLocateCategoricalBin(this.categoricalBinMap.get(newCCIndex), str);
            if (binNum < 0) {
                isInvalidValue = true;
            }
            if (isInvalidValue) {
                // the same as missing count
                binningInfoWritable.setMissingCount(binningInfoWritable.getMissingCount() + 1L);
                binNum = binningInfoWritable.getBinCategories().size()
                        + binningInfoWritable.getBinBoundaries().size();
            } else {
                // if real category value, binNum should + binBoundaries.size
                binNum += binningInfoWritable.getBinBoundaries().size();
                ;
            }
        } else if (isNumber) {
            binNum = getBinNum(binningInfoWritable.getBinBoundaries(), douVal);
            if (binNum == -1) {
                throw new RuntimeException("binNum should not be -1 to this step.");
            }

            // other stats are treated as numerical features
            binningInfoWritable.setSum(binningInfoWritable.getSum() + douVal);
            double squaredVal = douVal * douVal;
            binningInfoWritable.setSquaredSum(binningInfoWritable.getSquaredSum() + squaredVal);
            binningInfoWritable.setTripleSum(binningInfoWritable.getTripleSum() + squaredVal * douVal);
            binningInfoWritable.setQuarticSum(binningInfoWritable.getQuarticSum() + squaredVal * squaredVal);

            if (Double.compare(binningInfoWritable.getMax(), douVal) < 0) {
                binningInfoWritable.setMax(douVal);
            }
            if (Double.compare(binningInfoWritable.getMin(), douVal) > 0) {
                binningInfoWritable.setMin(douVal);
            }
        }
        if (posTags.contains(tag)) {
            binningInfoWritable.getBinCountPos()[binNum] += 1L;
            binningInfoWritable.getBinWeightPos()[binNum] += weight;
        } else if (negTags.contains(tag)) {
            binningInfoWritable.getBinCountNeg()[binNum] += 1L;
            binningInfoWritable.getBinWeightNeg()[binNum] += weight;
        }
    } else if (columnConfig.isCategorical()) {
        int lastBinIndex = binningInfoWritable.getBinCategories().size();

        int binNum = 0;
        if (units[columnIndex] == null || missingOrInvalidValues.contains(units[columnIndex].toLowerCase())) {
            isMissingValue = true;
        } else {
            String str = units[columnIndex];
            binNum = quickLocateCategoricalBin(this.categoricalBinMap.get(newCCIndex), str);
            if (binNum < 0) {
                isInvalidValue = true;
            }
        }

        if (isInvalidValue || isMissingValue) {
            binningInfoWritable.setMissingCount(binningInfoWritable.getMissingCount() + 1L);
            binNum = lastBinIndex;
        }

        if (modelConfig.isRegression()) {
            if (posTags.contains(tag)) {
                binningInfoWritable.getBinCountPos()[binNum] += 1L;
                binningInfoWritable.getBinWeightPos()[binNum] += weight;
            } else if (negTags.contains(tag)) {
                binningInfoWritable.getBinCountNeg()[binNum] += 1L;
                binningInfoWritable.getBinWeightNeg()[binNum] += weight;
            }
        } else {
            // for multiple classification, set bin count to BinCountPos and leave BinCountNeg empty
            binningInfoWritable.getBinCountPos()[binNum] += 1L;
            binningInfoWritable.getBinWeightPos()[binNum] += weight;
        }
    } else if (columnConfig.isNumerical()) {
        int lastBinIndex = binningInfoWritable.getBinBoundaries().size();
        double douVal = 0.0;
        if (units[columnIndex] == null || units[columnIndex].length() == 0
                || missingOrInvalidValues.contains(units[columnIndex].toLowerCase())) {
            isMissingValue = true;
        } else {
            try {
                douVal = Double.parseDouble(units[columnIndex].trim());
            } catch (Exception e) {
                isInvalidValue = true;
            }
        }

        // add logic the same as CalculateNewStatsUDF
        if (Double.compare(douVal, modelConfig.getNumericalValueThreshold()) > 0) {
            isInvalidValue = true;
        }

        if (isInvalidValue || isMissingValue) {
            binningInfoWritable.setMissingCount(binningInfoWritable.getMissingCount() + 1L);
            if (modelConfig.isRegression()) {
                if (posTags.contains(tag)) {
                    binningInfoWritable.getBinCountPos()[lastBinIndex] += 1L;
                    binningInfoWritable.getBinWeightPos()[lastBinIndex] += weight;
                } else if (negTags.contains(tag)) {
                    binningInfoWritable.getBinCountNeg()[lastBinIndex] += 1L;
                    binningInfoWritable.getBinWeightNeg()[lastBinIndex] += weight;
                }
            }
        } else {
            // For invalid or missing values, no need update sum, squaredSum, max, min ...
            int binNum = getBinNum(binningInfoWritable.getBinBoundaries(), units[columnIndex]);
            if (binNum == -1) {
                throw new RuntimeException("binNum should not be -1 to this step.");
            }
            if (modelConfig.isRegression()) {
                if (posTags.contains(tag)) {
                    binningInfoWritable.getBinCountPos()[binNum] += 1L;
                    binningInfoWritable.getBinWeightPos()[binNum] += weight;
                } else if (negTags.contains(tag)) {
                    binningInfoWritable.getBinCountNeg()[binNum] += 1L;
                    binningInfoWritable.getBinWeightNeg()[binNum] += weight;
                }
            }
            binningInfoWritable.setSum(binningInfoWritable.getSum() + douVal);
            double squaredVal = douVal * douVal;
            binningInfoWritable.setSquaredSum(binningInfoWritable.getSquaredSum() + squaredVal);
            binningInfoWritable.setTripleSum(binningInfoWritable.getTripleSum() + squaredVal * douVal);
            binningInfoWritable.setQuarticSum(binningInfoWritable.getQuarticSum() + squaredVal * squaredVal);

            if (Double.compare(binningInfoWritable.getMax(), douVal) < 0) {
                binningInfoWritable.setMax(douVal);
            }
            if (Double.compare(binningInfoWritable.getMin(), douVal) > 0) {
                binningInfoWritable.setMin(douVal);
            }
        }
    }
}

From source file:gov.nih.nci.caarray.util.CaArrayUtils.java

private static double xmlStringToDouble(String value) {
    if ("NaN".equals(value)) {
        return Double.NaN;
    } else if ("INF".equals(value)) {
        return Double.POSITIVE_INFINITY;
    } else if ("-INF".equals(value)) {
        return Double.NEGATIVE_INFINITY;
    } else {/*  ww  w  .ja va  2 s.  co m*/
        return Double.parseDouble(value);
    }
}

From source file:edu.cmu.tetrad.search.Lofs2.java

private void resolveOneEdgeMax2(Graph graph, Node x, Node y, boolean strong) {
    TetradLogger.getInstance().log("info", "\nEDGE " + x + " --- " + y);

    SortedMap<Double, String> scoreReports = new TreeMap<Double, String>();

    List<Node> neighborsx = new ArrayList<Node>();

    for (Node _node : graph.getAdjacentNodes(x)) {
        if (!knowledge.isForbidden(_node.getName(), x.getName())) {
            //                if (!knowledge.edgeForbidden(x.getName(), _node.getName())) {
            neighborsx.add(_node);//from  w  w w.  ja  va  2 s  .c o  m
        }
    }

    //        neighborsx.remove(y);

    double max = Double.NEGATIVE_INFINITY;
    boolean left = false;
    boolean right = false;

    DepthChoiceGenerator genx = new DepthChoiceGenerator(neighborsx.size(), neighborsx.size());
    int[] choicex;

    while ((choicex = genx.next()) != null) {
        List<Node> condxMinus = GraphUtils.asList(choicex, neighborsx);

        if (condxMinus.contains(y))
            continue;

        List<Node> condxPlus = new ArrayList<Node>(condxMinus);

        condxPlus.add(y);

        double xPlus = score(x, condxPlus);
        double xMinus = score(x, condxMinus);

        double p = pValue(x, condxPlus);

        if (p > alpha) {
            continue;
        }

        double p2 = pValue(x, condxMinus);

        if (p2 > alpha) {
            continue;
        }

        List<Node> neighborsy = new ArrayList<Node>();

        for (Node _node : graph.getAdjacentNodes(y)) {
            if (!knowledge.isForbidden(_node.getName(), y.getName())) {
                neighborsy.add(_node);
            }
        }

        DepthChoiceGenerator geny = new DepthChoiceGenerator(neighborsy.size(), neighborsy.size());
        int[] choicey;

        while ((choicey = geny.next()) != null) {
            List<Node> condyMinus = GraphUtils.asList(choicey, neighborsy);

            if (condyMinus.contains(x))
                continue;

            List<Node> condyPlus = new ArrayList<Node>(condyMinus);
            condyPlus.add(x);

            double yPlus = score(y, condyPlus);
            double yMinus = score(y, condyMinus);

            double p3 = pValue(y, condyPlus);

            if (p3 > alpha) {
                continue;
            }

            double p4 = pValue(y, condyMinus);

            if (p4 > alpha) {
                continue;
            }

            boolean forbiddenLeft = knowledge.isForbidden(y.getName(), x.getName());
            boolean forbiddenRight = knowledge.isForbidden(x.getName(), y.getName());

            double delta = 0.0;

            if (strong) {
                if (yPlus <= xPlus + delta && xMinus <= yMinus + delta) {
                    double score = combinedScore(xPlus, yMinus);

                    if ((yPlus <= yMinus + delta && xMinus <= xPlus + delta) || forbiddenRight) {
                        StringBuilder builder = new StringBuilder();

                        builder.append("\nStrong ").append(y).append("->").append(x).append(" ").append(score);
                        builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                        builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                        scoreReports.put(-score, builder.toString());

                        if (score > max) {
                            max = score;
                            left = true;
                            right = false;
                        }
                    } else {
                        StringBuilder builder = new StringBuilder();

                        builder.append("\nNo directed edge ").append(x).append("--").append(y).append(" ")
                                .append(score);
                        builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                        builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                        scoreReports.put(-score, builder.toString());
                    }
                } else if ((xPlus <= yPlus + delta && yMinus <= xMinus + delta) || forbiddenLeft) {
                    double score = combinedScore(yPlus, xMinus);

                    if (yMinus <= yPlus + delta && xPlus <= xMinus + delta) {
                        StringBuilder builder = new StringBuilder();

                        builder.append("\nStrong ").append(x).append("->").append(y).append(" ").append(score);
                        builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                        builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                        scoreReports.put(-score, builder.toString());

                        if (score > max) {
                            max = score;
                            left = false;
                            right = true;
                        }
                    } else {
                        StringBuilder builder = new StringBuilder();

                        builder.append("\nNo directed edge ").append(x).append("--").append(y).append(" ")
                                .append(score);
                        builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                        builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                        scoreReports.put(-score, builder.toString());
                    }
                } else if (yPlus <= xPlus + delta && yMinus <= xMinus + delta) {
                    double score = combinedScore(yPlus, xMinus);

                    StringBuilder builder = new StringBuilder();

                    builder.append("\nNo directed edge ").append(x).append("--").append(y).append(" ")
                            .append(score);
                    builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                    builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                    scoreReports.put(-score, builder.toString());
                } else if (xPlus <= yPlus + delta && xMinus <= yMinus + delta) {
                    double score = combinedScore(yPlus, xMinus);

                    StringBuilder builder = new StringBuilder();

                    builder.append("\nNo directed edge ").append(x).append("--").append(y).append(" ")
                            .append(score);
                    builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                    builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                    scoreReports.put(-score, builder.toString());
                }
            } else {
                if ((yPlus <= xPlus + delta && xMinus <= yMinus + delta) || forbiddenRight) {
                    double score = combinedScore(xPlus, yMinus);

                    StringBuilder builder = new StringBuilder();

                    builder.append("\nWeak ").append(y).append("->").append(x).append(" ").append(score);
                    builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                    builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                    scoreReports.put(-score, builder.toString());

                    if (score > max) {
                        max = score;
                        left = true;
                        right = false;
                    }
                } else if ((xPlus <= yPlus + delta && yMinus <= xMinus + delta) || forbiddenLeft) {
                    double score = combinedScore(yPlus, xMinus);

                    StringBuilder builder = new StringBuilder();

                    builder.append("\nWeak ").append(x).append("->").append(y).append(" ").append(score);
                    builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                    builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                    scoreReports.put(-score, builder.toString());

                    if (score > max) {
                        max = score;
                        left = false;
                        right = true;
                    }
                } else if (yPlus <= xPlus + delta && yMinus <= xMinus + delta) {
                    double score = combinedScore(yPlus, xMinus);

                    StringBuilder builder = new StringBuilder();

                    builder.append("\nNo directed edge ").append(x).append("--").append(y).append(" ")
                            .append(score);
                    builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                    builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                    scoreReports.put(-score, builder.toString());
                } else if (xPlus <= yPlus + delta && xMinus <= yMinus + delta) {
                    double score = combinedScore(yPlus, xMinus);

                    StringBuilder builder = new StringBuilder();

                    builder.append("\nNo directed edge ").append(x).append("--").append(y).append(" ")
                            .append(score);
                    builder.append("\n   Parents(").append(x).append(") = ").append(condxMinus);
                    builder.append("\n   Parents(").append(y).append(") = ").append(condyMinus);

                    scoreReports.put(-score, builder.toString());
                }
            }
        }
    }

    for (double score : scoreReports.keySet()) {
        TetradLogger.getInstance().log("info", scoreReports.get(score));
    }

    graph.removeEdges(x, y);

    if (left) {
        graph.addDirectedEdge(y, x);
    }

    if (right) {
        graph.addDirectedEdge(x, y);
    }

    if (!graph.isAdjacentTo(x, y)) {
        graph.addUndirectedEdge(x, y);
    }
}