List of usage examples for java.lang Double isNaN
public static boolean isNaN(double v)
From source file:jhn.eda.EDA.java
@Override public double logLikelihood() throws Exception { if (!usingSymmetricAlpha) throw new UnsupportedOperationException( "Log likelihood computation is unreasonably slow with assymetric alphas"); //NOTE: assumes symmetric alphas! final double alpha = alphas[0]; final double Kalpha = numTopics * alpha; final double log_gamma_alpha = Gamma.logGamma(alpha); double ll = Gamma.logGamma(Kalpha); ll -= numTopics * log_gamma_alpha;/*from w w w . ja v a 2s .c o m*/ int nonZeroTopics; int zeroTopics; IntIntCounter docTopicCounts; for (int docNum = 0; docNum < numDocs; docNum++) { docTopicCounts = docTopicCounts(docNum); // Deal with non-zero-count topics: for (Int2IntMap.Entry entry : docTopicCounts.int2IntEntrySet()) { ll += Gamma.logGamma(entry.getIntValue() + alpha); } // Deal with zero-count topics: nonZeroTopics = docTopicCounts.size(); zeroTopics = numTopics - nonZeroTopics; ll += zeroTopics * log_gamma_alpha; ll -= Gamma.logGamma(tokens[docNum].length + Kalpha); for (int position = 0; position < docLengths[docNum]; position++) { double topicWordProb = Double.NaN; Iterator<TopicCount> it = typeTopicCounts.typeTopicCounts(tokens[docNum][position]); while (it.hasNext()) { TopicCount tc = it.next(); if (tc.topic == topics[docNum][position]) { topicWordProb = (double) tc.count / (double) topicCorpusTopicCounts.topicCount(tc.topic); break; } } if (Double.isNaN(topicWordProb)) { System.err.println(topics[docNum][position]); } else { ll += FastMath.log(topicWordProb); } } } return ll; }
From source file:com.cloudera.oryx.kmeans.computation.local.Standardize.java
@Override public List<List<RealVector>> call() throws IOException { File[] inputFiles = inputDir.listFiles(IOUtils.NOT_HIDDEN); if (inputFiles == null || inputFiles.length == 0) { log.info("No input files in {}", inputDir); return null; }/* w w w . j a v a 2 s .c o m*/ Config config = ConfigUtils.getDefaultConfig(); InboundSettings inboundSettings = InboundSettings.create(config); int numFeatures = inboundSettings.getColumnNames().size(); NormalizeSettings settings = NormalizeSettings.create(config); Crossfold crossfold = new Crossfold(config.getInt("model.cross-folds")); RandomGenerator rand = crossfold.getRandomGenerator(); Collection<Integer> ignoredColumns = inboundSettings.getIgnoredColumns(); Collection<Integer> idColumns = inboundSettings.getIdColumns(); int expansion = -ignoredColumns.size() + summary.getNetLevels() - (!idColumns.isEmpty() && !ignoredColumns.contains(idColumns.iterator().next()) ? 1 : 0); boolean sparse = settings.getSparse() != null ? settings.getSparse() : expansion > 2 * (summary.getFieldCount() - ignoredColumns.size()); List<List<RealVector>> ret = new ArrayList<>(); for (int i = 0; i < crossfold.getNumFolds(); i++) { ret.add(new LinkedList<RealVector>()); } for (File inputFile : inputFiles) { log.info("Standardizing input from {}", inputFile.getName()); for (String line : new FileLineIterable(inputFile)) { if (line.isEmpty()) { continue; } String[] tokens = DelimitedDataUtils.decode(line); RealVector v = sparse ? Vectors.sparse(tokens.length + expansion) : Vectors.dense(tokens.length + expansion); int offset = 0; for (int i = 0; i < numFeatures; i++) { if (!inboundSettings.isIgnored(i)) { if (inboundSettings.isNumeric(i)) { SummaryStats ss = summary.getStats(i); Transform t = settings.getTransform(i); double raw = asDouble(tokens[i]); if (!Double.isNaN(raw)) { double n = t.apply(raw, ss) * settings.getScale(i); v.setEntry(offset, n); } offset++; } else if (inboundSettings.isCategorical(i)) { SummaryStats ss = summary.getStats(i); int index = ss.index(tokens[i]); if (index >= 0) { v.setEntry(offset + index, settings.getScale(i)); offset += ss.numLevels(); } else { log.warn("Unrecognized value for category {}: {}", i, tokens[i]); } } } } if (!inboundSettings.getIdColumns().isEmpty()) { // TODO: multiple ID columns v = new NamedRealVector(v, tokens[inboundSettings.getIdColumns().iterator().next()]); } // Assign the vector to a fold int fold = rand.nextInt(crossfold.getNumFolds()); ret.get(fold).add(v); } } return ret; }
From source file:com.opengamma.analytics.math.function.PiecewisePolynomialWithSensitivityFunction1D.java
/** * @param pp {@link PiecewisePolynomialResultsWithSensitivity} * @param xKey /*from w w w. j ava2 s . c om*/ * @return Node sensitivity of derivative value at x=xKey */ public DoubleMatrix1D differentiateNodeSensitivity(final PiecewisePolynomialResultsWithSensitivity pp, final double xKey) { ArgumentChecker.notNull(pp, "null pp"); ArgumentChecker.isFalse(Double.isNaN(xKey), "xKey containing NaN"); ArgumentChecker.isFalse(Double.isInfinite(xKey), "xKey containing Infinity"); if (pp.getDimensions() > 1) { throw new NotImplementedException(); } final int nCoefs = pp.getOrder(); ArgumentChecker.isFalse(nCoefs < 2, "Polynomial degree is too low"); final double[] knots = pp.getKnots().getData(); final int nKnots = knots.length; int interval = FunctionUtils.getLowerBoundIndex(knots, xKey); if (interval == nKnots - 1) { interval--; // there is 1 less interval that knots } final double s = xKey - knots[interval]; final DoubleMatrix2D a = pp.getCoefficientSensitivity(interval); DoubleMatrix1D res = (DoubleMatrix1D) MA.scale(a.getRowVector(0), nCoefs - 1); for (int i = 1; i < nCoefs - 1; i++) { res = (DoubleMatrix1D) MA.scale(res, s); res = (DoubleMatrix1D) MA.add(res, MA.scale(a.getRowVector(i), nCoefs - 1 - i)); } return res; }
From source file:com.cloudera.oryx.kmeans.computation.local.Standarize.java
@Override public List<List<RealVector>> call() throws IOException { File[] inputFiles = inputDir.listFiles(IOUtils.CSV_COMPRESSED_FILTER); if (inputFiles == null || inputFiles.length == 0) { log.info("No .csv input files in {}", inputDir); return null; }//from www . ja va 2s . c o m Config config = ConfigUtils.getDefaultConfig(); InboundSettings inboundSettings = InboundSettings.create(config); int numFeatures = inboundSettings.getColumnNames().size(); NormalizeSettings settings = NormalizeSettings.create(config); Crossfold crossfold = new Crossfold(config.getInt("model.cross-folds")); RandomGenerator rand = crossfold.getRandomGenerator(); Collection<Integer> ignoredColumns = inboundSettings.getIgnoredColumns(); Collection<Integer> idColumns = inboundSettings.getIdColumns(); int expansion = -ignoredColumns.size() + summary.getNetLevels() - (!idColumns.isEmpty() && !ignoredColumns.contains(idColumns.iterator().next()) ? 1 : 0); boolean sparse = settings.getSparse() != null ? settings.getSparse() : expansion > 2 * (summary.getFieldCount() - ignoredColumns.size()); List<List<RealVector>> ret = Lists.newArrayList(); for (int i = 0; i < crossfold.getNumFolds(); i++) { ret.add(Lists.<RealVector>newLinkedList()); } for (File inputFile : inputFiles) { log.info("Standardizing input from {}", inputFile.getName()); for (String line : new FileLineIterable(inputFile)) { if (line.isEmpty()) { continue; } String[] tokens = DelimitedDataUtils.decode(line); RealVector v = sparse ? Vectors.sparse(tokens.length + expansion) : Vectors.dense(tokens.length + expansion); int offset = 0; for (int i = 0; i < numFeatures; i++) { if (inboundSettings.isIgnored(i)) { // Do nothing } else if (inboundSettings.isNumeric(i)) { SummaryStats ss = summary.getStats(i); Transform t = settings.getTransform(i); double raw = asDouble(tokens[i]); if (!Double.isNaN(raw)) { double n = t.apply(raw, ss) * settings.getScale(i); v.setEntry(offset, n); } offset++; } else if (inboundSettings.isCategorical(i)) { SummaryStats ss = summary.getStats(i); int index = ss.index(tokens[i]); if (index >= 0) { v.setEntry(offset + index, settings.getScale(i)); offset += ss.numLevels(); } else { log.warn("Unrecognized value for category {}: {}", i, tokens[i]); } } } if (!inboundSettings.getIdColumns().isEmpty()) { // TODO: multiple ID columns v = new NamedRealVector(v, tokens[inboundSettings.getIdColumns().iterator().next()]); } // Assign the vector to a fold int fold = rand.nextInt(crossfold.getNumFolds()); ret.get(fold).add(v); } } return ret; }
From source file:com.rapidminer.operator.preprocessing.filter.Real2Integer.java
@Override public ExampleSet applyOnFiltered(ExampleSet exampleSet) throws OperatorException { boolean round = getParameterAsBoolean(PARAMETER_ROUND); List<Attribute> newAttributes = new LinkedList<Attribute>(); Iterator<Attribute> a = exampleSet.getAttributes().iterator(); while (a.hasNext()) { Attribute attribute = a.next(); if ((Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.NUMERICAL)) && (!Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.INTEGER))) { Attribute newAttribute = AttributeFactory.createAttribute(attribute.getName(), Ontology.INTEGER); newAttributes.add(newAttribute); exampleSet.getExampleTable().addAttribute(newAttribute); for (Example example : exampleSet) { double originalValue = example.getValue(attribute); if (Double.isNaN(originalValue)) { example.setValue(newAttribute, Double.NaN); } else { long newValue = round ? Math.round(originalValue) : (long) originalValue; example.setValue(newAttribute, newValue); }//from w w w . j a va 2s .co m } a.remove(); } } for (Attribute attribute : newAttributes) { exampleSet.getAttributes().addRegular(attribute); } return exampleSet; }
From source file:edu.cmu.tetrad.data.MultiGeneralAndersonDarlingTest.java
private void runTest() { int n = data.get(0).size(); double h = 0.0; int numSummed = 0; for (int g = 0; g < data.size(); g++) { List<Double> _data = data.get(g); for (int i = 1; i <= n; i++) { double x1 = _data.get(i - 1); double a1 = Math.log(distributions.get(g).cumulativeProbability(x1)); double x2 = _data.get(n + 1 - i - 1); double a2 = Math.log(1.0 - distributions.get(g).cumulativeProbability(x2)); double k = (2 * i - 1) * (a1 + a2); if (!(Double.isNaN(a1) || Double.isNaN(a2) || Double.isInfinite(a1) || Double.isInfinite(a2))) { h += k;//ww w . j a v a2 s. com numSummed++; } } } System.out.println("n = " + n + " numSummed = " + numSummed); double a = -n - (1.0 / numSummed) * h; double aa = (1 + 0.75 / numSummed + 2.25 / Math.pow(numSummed, 2)) * a; double p; if (aa < 0.2) { p = 1 - Math.exp(-13.436 + 101.14 * aa - 223.73 * aa * aa); } else if (aa < 0.34) { p = 1 - Math.exp(-8.318 + 42.796 * aa - 59.938 * aa * aa); } else if (aa < 0.6) { p = Math.exp(0.9177 - 4.279 * aa - 1.38 * aa * aa); } else { p = Math.exp(1.2937 - 5.709 * aa + 0.0186 * aa * aa); } this.aSquared = a; this.aSquaredStar = aa; this.p = p; }
From source file:beast.math.distributions.NormalDistribution.java
/** * A more accurate and faster implementation of the cdf (taken from function pnorm in the R statistical language) * This implementation has discrepancies depending on the programming language and system architecture * In Java, returned values become zero once z reaches -37.5193 exactly on the machine tested * In the other implementation, the returned value 0 at about z = -8 * In C, this 0 value is reached approximately z = -37.51938 * <p/>// ww w. j ava 2 s .c o m * Will later need to be optimised for BEAST * * @param x argument * @param mu mean * @param sigma standard deviation * @param log_p is p logged * @return cdf at x */ public static double cdf(double x, double mu, double sigma, boolean log_p) { if (Double.isNaN(x) || Double.isNaN(mu) || Double.isNaN(sigma)) { return Double.NaN; } if (Double.isInfinite(x) && mu == x) { /* x-mu is NaN */ return Double.NaN; } if (sigma <= 0) { if (sigma < 0) { return Double.NaN; } return (x < mu) ? 0.0 : 1.0; } double p = (x - mu) / sigma; if (Double.isInfinite(p)) { return (x < mu) ? 0.0 : 1.0; } return standardCDF(p, log_p); }
From source file:lirmm.inria.fr.math.OpenLongToDoubleHashMapTest.java
@Test public void testGetAbsent() { Map<Long, Double> generated = generateAbsent(); OpenLongToDoubleHashMap map = createFromJavaMap(); for (Map.Entry<Long, Double> mapEntry : generated.entrySet()) { Assert.assertTrue(Double.isNaN(map.get(mapEntry.getKey()))); }//from ww w . j a va2s . co m }
From source file:org.jfree.data.statistics.HistogramBin.java
/** * Returns <code>true</code> if the specified value belongs in the bin, * and <code>false</code> otherwise. * * @param value the value./*from w ww . j a v a 2s. co m*/ * * @return A boolean. */ public boolean accepts(double value) { if (Double.isNaN(value)) { return false; } if (value < this.lowerBound) { return false; } if (value > this.upperBound) { return false; } if (value == this.lowerBound) { return this.includeLowerBound; } if (value == this.upperBound) { return this.includeUpperBound; } return true; }
From source file:de.jfachwert.pruefung.NumberValidator.java
/** * Verifiziert die uebergebene Nummer, ob sie eine gueltige Nummer und * nicht unendlich oder 'NaN' ist. Falls die Nummer unendlich oder 'NaN' * ist, wird eine {@link ArithmeticException} geworfen. * * @param number zu pruefende Nummer//from w w w . j a v a2 s.c o m * @return die Nummer selbst zur Weiterverarbeitung */ public Number verifyNumber(Number number) { if ((number instanceof Double) || (number instanceof Float)) { double dValue = number.doubleValue(); if (Double.isNaN(dValue) || Double.isInfinite(dValue)) { throw new LocalizedArithmeticException(dValue, NUMBER); } } return number; }