Example usage for java.lang Double NaN

List of usage examples for java.lang Double NaN

Introduction

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

Prototype

double NaN

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

Click Source Link

Document

A constant holding a Not-a-Number (NaN) value of type double .

Usage

From source file:geogebra.util.MyMath.java

final public static double erf(double mean, double standardDeviation, double x) {
    try {/*  w ww  . j  a  va2s  .c om*/
        return Erf.erf((x - mean) / (standardDeviation * Math.sqrt(2.0)));
    } catch (Exception ex) {
        if (x < (mean - 20 * standardDeviation)) { // JDK 1.5 blows at 38
            return 0.0d;
        } else if (x > (mean + 20 * standardDeviation)) {
            return 1.0d;
        } else {
            return Double.NaN;
        }
    }
}

From source file:gedi.util.math.stat.testing.DirichletLikelihoodRatioTest.java

/**
 * Tests the 0 Null hypothesis that all sample vectors come from a multinomial with equal probability vector
 * //from ww w .  j a  va2 s  .  c  o  m
 * Removes all components where one of the vectors has NaN!
 * 
 * TODO: Test with more than two samples (are the df right?)
 * 
 * @param pseudo
 * @param samples is changed!
 * 
 * @return
 */
public static double testMultinomials(MutableDouble statistic, double pseudo, double[]... samples) {

    double[][] psamples = new double[samples.length][];
    assert samples.length > 1;

    // remove NaNs
    BitVector nans = new BitVector(samples[0].length);
    for (int i = 0; i < samples.length; i++) {
        assert samples[i].length == samples[0].length;
        for (int j = 0; j < samples[i].length; j++)
            if (Double.isNaN(samples[i][j]))
                nans.putQuick(j, true);
    }
    nans.not();
    for (int i = 0; i < samples.length; i++) {
        psamples[i] = ArrayUtils.restrict(samples[i], nans);
        assert ArrayUtils.min(psamples[i]) >= 0;
    }

    // remove all zero components
    nans.clear();
    for (int i = 0; i < samples.length; i++)
        for (int j = 0; j < samples[i].length; j++)
            if (samples[i][j] > 0)
                nans.putQuick(j, true);
    for (int i = 0; i < samples.length; i++) {
        psamples[i] = ArrayUtils.restrict(psamples[i], nans);
    }

    int dim = psamples[0].length;
    int obj = psamples.length;
    if (dim < 2)
        return Double.NaN;

    // add pseudocounts proportional to total sum of counts
    double[] n1 = new double[dim];
    for (int i = 0; i < obj; i++)
        ArrayUtils.add(n1, psamples[i]);
    ArrayUtils.normalize(n1);

    for (int i = 0; i < obj; i++)
        for (int j = 0; j < dim; j++)
            psamples[i][j] += pseudo * n1[j];

    double[] concat = ArrayUtils.concat(psamples);
    double[] norm = concat.clone();
    ArrayUtils.normalize(norm);

    // normalize for L1
    double[] c1 = new double[dim];
    for (int i = 0; i < norm.length; i += dim) {
        for (int j = 0; j < dim; j++)
            c1[j] += norm[i + j];
    }
    ArrayUtils.normalize(c1);

    // normalize for L2
    double[] cnorm = new double[concat.length];
    for (int i = 0; i < norm.length; i += dim) {
        double s = ArrayUtils.sum(norm, i, i + dim);
        for (int j = 0; j < dim; j++)
            cnorm[i + j] = c1[j] * s;
    }

    // do the test
    double L1 = logProbability(concat, norm);
    double L2 = logProbability(concat, cnorm);

    return ChiSquare.cumulative(2 * (L1 - L2), (dim - 1) * (obj - 1), false, false);
}

From source file:model.DecomposableModel.java

/**
 * Performs a modification of the graph (add an edge to the graph).
 * /*from w w w .  j av a2s . com*/
 * @param actionToPerform
 *            the action to perform
 * @param the
 *            model from which the current one has been created
 */
public void performAction(GraphAction actionToPerform, DecomposableModel fromModel) {
    List<GraphAction> actionsToPerform = fromModel.actionsForInteraction.get(actionToPerform);
    for (GraphAction action : actionsToPerform) {
        switch (action.type) {
        case ADD:
            graph.addSecuredEdge(action.getV1(), action.getV2());
            break;
        case REMOVE:
            graph.removeSecuredEdge(action.getV1(), action.getV2());
            break;
        default:
            break;
        }
    }
    entropyComputed = false;
    entropy = Double.NaN; // invalidate the entropy
    nbParameters = -1; // invalidate the nb degrees of freedom
}

From source file:org.jfree.data.statistics.BoxAndWhiskerCalculator.java

/**
 * Calculates the first quartile for a list of numbers in ascending order.
 * If the items in the list are not in ascending order, the result is
 * unspecified.  If the list contains items that are <code>null</code>, not
 * an instance of <code>Number</code>, or equivalent to
 * <code>Double.NaN</code>, the result is unspecified.
 *
 * @param values  the numbers in ascending order (<code>null</code> not
 *     permitted)./*from  ww w .  j  a  v a  2  s.  com*/
 *
 * @return The first quartile.
 */
public static double calculateQ1(List values) {
    ParamChecks.nullNotPermitted(values, "values");

    double result = Double.NaN;
    int count = values.size();
    if (count > 0) {
        if (count % 2 == 1) {
            if (count > 1) {
                result = Statistics.calculateMedian(values, 0, count / 2);
            } else {
                result = Statistics.calculateMedian(values, 0, 0);
            }
        } else {
            result = Statistics.calculateMedian(values, 0, count / 2 - 1);
        }

    }
    return result;
}

From source file:com.clust4j.algo.preprocess.ImputationTests.java

@Test
public void testFullyNaN() {
    boolean a = false;
    MeanImputation m = new MeanImputation();

    try {/*w  ww.  j ava  2 s  .  c  o  m*/
        final double[][] d = new double[][] { new double[] { 1, Double.NaN, 2 },
                new double[] { 1, Double.NaN, 3 }, new double[] { 8.5, Double.NaN, 6 },
                new double[] { 9, Double.NaN, 7 } };

        m.transform(d);
    } catch (NaNException n) {
        a = true;
    } finally {
        assertTrue(a);
    }

    /*
     * Coverage love...
     */
    m.trace("coverage love");
    m.debug("coverage love");
    m.warn("blah");
    assertTrue(m.hasWarnings());
}

From source file:com.bce.gis.io.zweidm.SmsParser.java

private void interpreteNodeLine(final String line, final LineNumberReader lnReader) {
    final String[] nodeLineStrings = StringUtils.split(line);

    if (!"ND".equals(nodeLineStrings[0])) //$NON-NLS-1$
    {//from   ww w .ja va  2  s .  c  o  m
        addStatus(lnReader, IStatus.WARNING, Messages.getString("SmsParser_5")); //$NON-NLS-1$
        return;
    }

    final int id = Integer.parseInt(nodeLineStrings[1]);
    final double easting = Double.parseDouble(nodeLineStrings[2]);
    final double northing = Double.parseDouble(nodeLineStrings[3]);
    double elevation = Double.parseDouble(nodeLineStrings[4]);
    // TODO: the value '-9999' represents the NODATA-value, should be discussed
    if (elevation == -9999)
        elevation = Double.NaN;

    m_model.addNode(line, id, easting, northing, elevation);
}

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

/**
 * Returns true just in the case the x and y vectors are independent,
 * once undefined values have been removed. Left public so it can be
 * accessed separately./* ww  w .  j  ava 2s.  c om*/
 */
public boolean independent(double[] x, double[] y) {
    int both = 0;

    for (int i = 0; i < x.length; i++) {
        if (!Double.isNaN(x[i]) && !Double.isNaN(y[i])) {
            both++;
        }
    }

    // Get rid of NaN's.
    double[] _rXZ = new double[both];
    double[] _rYZ = new double[both];

    if (both != x.length) {
        int index = -1;

        for (int i = 0; i < x.length; i++) {
            if (!Double.isNaN(x[i]) && !Double.isNaN(y[i])) {
                ++index;
                _rXZ[index] = x[i];
                _rYZ[index] = y[i];
            }
        }

        x = _rXZ;
        y = _rYZ;
    }

    if (x.length < 10) {
        minP = Double.NaN;
        return false; // For PC, should not remove the edge for this reason.
    }

    double[] _x = new double[x.length];
    double[] _y = new double[x.length];

    List<Double> p = new ArrayList<Double>();

    for (int m = 0; m < getNumFunctions(); m++) {
        for (int n = 0; n < getNumFunctions(); n++) {
            for (int i = 0; i < x.length; i++) {
                _x[i] = function(m, x[i]);
                _y[i] = function(n, y[i]);
            }

            double sigmaXY = covariance(_x, _y);
            double sigmaXX = covariance(_x, _x);
            double sigmaYY = covariance(_y, _y);

            double r = sigmaXY / sqrt(sigmaXX * sigmaYY);

            if (r > 1)
                r = 1;
            if (r < -1)
                r = -1;

            // Non-parametric Fisher Z test.
            double _z = 0.5 * (log(1.0 + r) - log(1.0 - r));
            double w = sqrt(x.length) * _z;

            // Testing the hypothesis that _x and _y are uncorrelated and assuming that 4th moments of _x and _y
            // are finite and that the sample is large.
            standardize(_x);
            standardize(_y);

            double t2 = moment22(_x, _y);

            double t = sqrt(t2);
            double _p = 2.0 * (1.0 - normalCdf(0.0, t, abs(w)));

            if (!Double.isNaN(_p)) {
                p.add(_p);
            }
        }
    }

    Collections.sort(p);
    double cutoff = fdr(alpha, p, true);
    double min = p.size() == 0 ? Double.NaN : p.get(0);
    this.minP = min;

    this.p = p;

    if (Double.isNaN(min)) {
        this.minP = Double.NaN;
        return true; // No basis on which to remove an edge for PC.
    }

    return minP > cutoff;
    //        return getQ(p) > alpha;
}

From source file:org.jfree.data.statistics.Statistics.java

/**
 * Calculates the median for a list of values ({@code Number} objects).
 * If {@code copyAndSort} is {@code false}, the list is assumed
 * to be presorted in ascending order by value.
 *
 * @param values  the values ({@code null} permitted).
 * @param copyAndSort  a flag that controls whether the list of values is
 *                     copied and sorted.
 *
 * @return The median.//from ww w  .j a v a 2  s  .c  o m
 */
public static double calculateMedian(List values, boolean copyAndSort) {

    double result = Double.NaN;
    if (values != null) {
        if (copyAndSort) {
            int itemCount = values.size();
            List copy = new ArrayList(itemCount);
            for (int i = 0; i < itemCount; i++) {
                copy.add(i, values.get(i));
            }
            Collections.sort(copy);
            values = copy;
        }
        int count = values.size();
        if (count > 0) {
            if (count % 2 == 1) {
                if (count > 1) {
                    Number value = (Number) values.get((count - 1) / 2);
                    result = value.doubleValue();
                } else {
                    Number value = (Number) values.get(0);
                    result = value.doubleValue();
                }
            } else {
                Number value1 = (Number) values.get(count / 2 - 1);
                Number value2 = (Number) values.get(count / 2);
                result = (value1.doubleValue() + value2.doubleValue()) / 2.0;
            }
        }
    }
    return result;
}

From source file:com.alibaba.citrus.util.internal.apache.lang.EqualsBuilderTests.java

public void testDouble() {
    double o1 = 1;
    double o2 = 2;
    assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
    assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
    assertTrue(!new EqualsBuilder().append(o1, Double.NaN).isEquals());
    assertTrue(new EqualsBuilder().append(Double.NaN, Double.NaN).isEquals());
    assertTrue(new EqualsBuilder().append(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY).isEquals());
}

From source file:com.cinnober.msgcodec.json.JsonValueHandlerTest.java

@Test
public void testFloat64EncodeNaN() throws IOException {
    StringWriter out = new StringWriter();
    JsonGenerator g = f.createGenerator(out);

    JsonValueHandler.FLOAT64.writeValue(Double.NaN, g);
    g.flush();/*w  w  w. j  a  v  a  2 s.c  o m*/
    assertEquals("\"NaN\"", out.toString());
}