List of usage examples for java.lang Math log
@HotSpotIntrinsicCandidate public static double log(double a)
From source file:com.opengamma.analytics.financial.model.option.pricing.analytic.twoasset.RelativeOutperformanceOptionModel.java
/** * Gets the pricing function for a European-style relative outperformance option * @param definition The option definition * @return The pricing function/*from w w w. j a v a 2 s. co m*/ * @throws IllegalArgumentException If the definition is null */ @Override public Function1D<StandardTwoAssetOptionDataBundle, Double> getPricingFunction( final RelativeOutperformanceOptionDefinition definition) { Validate.notNull(definition, "definition"); return new Function1D<StandardTwoAssetOptionDataBundle, Double>() { @SuppressWarnings("synthetic-access") @Override public Double evaluate(final StandardTwoAssetOptionDataBundle data) { Validate.notNull(data, "data"); final double s1 = data.getFirstSpot(); final double s2 = data.getSecondSpot(); final double k = definition.getStrike(); final double b1 = data.getFirstCostOfCarry(); final double b2 = data.getSecondCostOfCarry(); final double t = definition.getTimeToExpiry(data.getDate()); final double r = data.getInterestRate(t); final double sigma1 = data.getFirstVolatility(t, k); final double sigma2 = data.getSecondVolatility(t, k); final double rho = data.getCorrelation(); final double sigma = Math.sqrt(sigma1 * sigma1 + sigma2 * sigma2 - 2 * rho * sigma1 * sigma2); final double sigmaT = sigma * Math.sqrt(t); final double f = s1 * Math.exp(t * (b1 - b2 + sigma2 * sigma2 - rho * sigma1 * sigma2)) / s2; final double d1 = (Math.log(f / k) + t * sigma * sigma / 2) / sigmaT; final double d2 = d1 - sigmaT; final int sign = definition.isCall() ? 1 : -1; return Math.exp(-r * t) * sign * (f * NORMAL.getCDF(sign * d1) - k * NORMAL.getCDF(sign * d2)); } }; }
From source file:dr.evomodel.branchmodel.RandomBranchModel.java
private void setup() { DataType dataType = baseSubstitutionModel.getDataType(); FrequencyModel freqModel = baseSubstitutionModel.getFrequencyModel(); Parameter kappaParameter = new Parameter.Default("kappa", 1, baseSubstitutionModel.getKappa()); substitutionModels = new LinkedList<SubstitutionModel>(); branchAssignmentMap = new LinkedHashMap<NodeRef, Integer>(); int branchClass = 0; for (NodeRef node : treeModel.getNodes()) { if (!treeModel.isRoot(node)) { double nodeHeight = treeModel.getNodeHeight(node); double parentHeight = treeModel.getNodeHeight(treeModel.getParent(node)); double time = 0.5 * (parentHeight + nodeHeight); double baseOmega = baseSubstitutionModel.getOmega(); double fixed = baseOmega * time; double epsilon = (Math.log(1 - random.nextDouble()) / (-rate)); //Math.exp((random.nextGaussian() * stdev + mean)); double value = fixed + epsilon; Parameter omegaParameter = new Parameter.Default("omega", 1, value); GY94CodonModel gy94 = new GY94CodonModel((Codons) dataType, omegaParameter, kappaParameter, freqModel);//ww w . java2 s . co m substitutionModels.add(gy94); branchAssignmentMap.put(node, branchClass); branchClass++; } //END: root check } // END: nodes loop }
From source file:com.opengamma.analytics.financial.model.option.pricing.analytic.twoasset.TwoAssetCorrelationOptionModel.java
/** * Gets the pricing function for a European-style two-asset correlation option * @param definition The option definition * @return The pricing function/*from w ww .j ava2s . c o m*/ * @throws IllegalArgumentException If the definition is null */ @Override public Function1D<StandardTwoAssetOptionDataBundle, Double> getPricingFunction( final TwoAssetCorrelationOptionDefinition definition) { Validate.notNull(definition, "definition"); return new Function1D<StandardTwoAssetOptionDataBundle, Double>() { @SuppressWarnings("synthetic-access") @Override public Double evaluate(final StandardTwoAssetOptionDataBundle data) { Validate.notNull(data, "data"); final double s1 = data.getFirstSpot(); final double s2 = data.getSecondSpot(); final double k = definition.getStrike(); final double payout = definition.getPayoutLevel(); final double b1 = data.getFirstCostOfCarry(); final double b2 = data.getSecondCostOfCarry(); final double t = definition.getTimeToExpiry(data.getDate()); final double r = data.getInterestRate(t); final double sigma1 = data.getFirstVolatility(t, k); final double sigma2 = data.getSecondVolatility(t, k); final double rho = data.getCorrelation(); final double tSqrt = Math.sqrt(t); final double sigmaT1 = sigma1 * tSqrt; final double sigmaT2 = sigma2 * tSqrt; final double d1 = (Math.log(s1 / k) + t * (b1 - sigma1 * sigma1 / 2)) / sigmaT1; final double d2 = (Math.log(s2 / payout) + t * (b2 - sigma2 * sigma2 / 2)) / sigmaT2; final double df1 = Math.exp(t * (b2 - r)); final double df2 = Math.exp(-r * t); final int sign = definition.isCall() ? 1 : -1; return sign * (s2 * df1 * BIVARIATE.getCDF(new double[] { sign * (d2 + sigmaT2), sign * (d1 + rho * sigmaT2), rho }) - payout * df2 * BIVARIATE.getCDF(new double[] { sign * d2, sign * d1, rho })); } }; }
From source file:de.tudarmstadt.ukp.dkpro.spelling.detector.ngram.LMBasedDetector.java
protected double getSentenceProbability(List<String> words) throws AnalysisEngineProcessException { double sentenceProbability = 0.0; if (words.size() < 1) { return 0.0; }/* ww w .j av a 2 s .co m*/ long nrOfUnigrams; try { nrOfUnigrams = provider.getNrOfTokens(); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } List<String> trigrams = new ArrayList<String>(); // in the google n-grams this is not represented (only single BOS markers) // but I leave it in place in case we add another n-gram provider trigrams.add(NGramDetectorUtils.getTrigram(BOS, BOS, words.get(0))); if (words.size() > 1) { trigrams.add(NGramDetectorUtils.getTrigram(BOS, words.get(0), words.get(1))); } for (String trigram : new NGramStringIterable(words, 3, 3)) { trigrams.add(trigram); } // FIXME - implement backoff or linear interpolation for (String trigram : trigrams) { long trigramFreq = getNGramCount(trigram); String[] parts = StringUtils.split(trigram, " "); String bigram = StringUtils.join(Arrays.copyOfRange(parts, 0, 2), " "); long bigramFreq = getNGramCount(bigram); String unigram = StringUtils.join(Arrays.copyOfRange(parts, 0, 1), " "); long unigramFreq = getNGramCount(unigram); if (trigramFreq < 1) { trigramFreq = 1; } if (bigramFreq < 1) { bigramFreq = 1; } if (unigramFreq < 1) { unigramFreq = 1; } double trigramProb = Math.log((double) trigramFreq / bigramFreq); double bigramProb = Math.log((double) bigramFreq / unigramFreq); double unigramProb = Math.log((double) unigramFreq / nrOfUnigrams); double interpolated = (trigramProb + bigramProb + unigramProb) / 3.0; sentenceProbability += interpolated; } return Math.exp(sentenceProbability); }
From source file:mlflex.helper.MathUtilities.java
/** Returns the log2 value of a number. * * @param number Numeric value to be transformed * @return Transformed value//from ww w.j a v a2s .co m */ public static double Log2(double number) { return Math.log(number) / Math.log(2); }
From source file:gedi.util.math.stat.testing.DirichletLikelihoodRatioTest.java
public static double effectSizeMultinomials(double pseudo, double[]... samples) { double[][] psamples = new double[samples.length][]; assert samples.length > 1; 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);//from ww w . j ava2 s .c om } nans.not(); double sum = 0; for (int i = 0; i < samples.length; i++) { psamples[i] = ArrayUtils.restrict(samples[i], nans); assert ArrayUtils.min(psamples[i]) >= 0; sum += ArrayUtils.sum(psamples[i]); } for (int i = 0; i < samples.length; i++) { ArrayUtils.add(psamples[i], pseudo * ArrayUtils.sum(psamples[i]) / sum); } int dim = psamples[0].length; int obj = psamples.length; if (dim < 2) return Double.NaN; if (obj != 2) throw new RuntimeException("Can only compute the effect size for a pair of samples!"); double exp = (Math.log(ArrayUtils.sum(psamples[0])) - Math.log(ArrayUtils.sum(psamples[1]))) / Math.log(2); double re = 0; for (int i = 0; i < dim; i++) { re += Math.abs(exp - (Math.log(psamples[0][i]) - Math.log(psamples[1][i])) / Math.log(2)); } return re; }
From source file:edu.berkeley.sparrow.examples.BackendBenchmarkProfiler.java
/** * This generates an arrival delay according to an exponential distribution with * average 1\{@code lambda}. Generating arrival delays from such a distribution creates * a Poission process with an average arrival rate of {@code lambda} events per second. *///from w w w. j a v a 2s . c o m public static double generateInterarrivalDelay(Random r, double lambda) { double u = r.nextDouble(); return -Math.log(u) / lambda; }
From source file:eu.edisonproject.utility.commons.IDFSort.java
@Override public Map<String, Double> sort(Map<String, Double> termDictionaray, String dirPath) throws IOException, InterruptedException { Map<String, Double> newTermDictionaray = new HashMap<>(); File dir = new File(dirPath); File[] docs = dir.listFiles(); // GrepOptionSets go = new GrepOptionSets(); for (String term : termDictionaray.keySet()) { int numOfDocsWithTerm = 0; for (File f : docs) { int count = 0; if (FilenameUtils.getExtension(f.getName()).endsWith("txt")) { count++;//from w ww . j a v a2s . c om Logger.getLogger(IDFSort.class.getName()).log(Level.FINE, "{0}: {1} of {2}", new Object[] { f.getName(), count, docs.length }); ReaderFile rf = new ReaderFile(f.getAbsolutePath()); String contents = rf.readFileWithN(); cleanStopWord.setDescription(contents); contents = cleanStopWord.execute(); cleanStopWord.setDescription(term.replaceAll("_", " ")); String cTerm = cleanStopWord.execute(); // int lastIndex = 0; // int wcount = 0; // // while (lastIndex != -1) { // // lastIndex = contents.indexOf(cTerm, lastIndex); // // if (lastIndex != -1) { // wcount++; // lastIndex += cTerm.length(); // } // } // System.out.println(f.getName() + "," + cTerm + "," + wcount); // // wcount = StringUtils.countMatches(contents, cTerm); // System.out.println(f.getName() + "," + cTerm + "," + wcount); if (contents.contains(cTerm)) { numOfDocsWithTerm++; // System.out.println(f.getName() + "," + cTerm + "," + numOfDocsWithTerm); } // try (InputStream fis = new FileInputStream(f)) { // String result = Unix4j.from(fis).grep(go.i.count, cTerm).toStringResult(); // Integer lineCount = Integer.valueOf(result); // if (lineCount > 0) { // numOfDocsWithTerm++; // } // } } } double idf = 0; if (numOfDocsWithTerm > 0) { idf = Math.log((double) docs.length / (double) numOfDocsWithTerm); } newTermDictionaray.put(term, idf); } return newTermDictionaray; }
From source file:datafu.pig.stats.WilsonBinConf.java
/** * @param x The number of positive (success) outcomes * @param n The number of observations//from ww w . j av a2 s.c om * @return The (lower,upper) confidence interval */ public Tuple binconf(Long x, Long n) throws IOException { NormalDistribution normalDist = new NormalDistributionImpl(); if (x == null || n == null) return null; if (x < 0 || n < 0) throw new IllegalArgumentException("non-negative values expected"); if (x > n) throw new IllegalArgumentException("invariant violation: number of successes > number of obs"); if (n == 0) return tupleFactory.newTuple(ImmutableList.of(Double.valueOf(0), Double.valueOf(0))); try { double zcrit = -1.0 * normalDist.inverseCumulativeProbability(alpha / 2); double z2 = zcrit * zcrit; double p = x / (double) n; double a = p + z2 / 2 / n; double b = zcrit * Math.sqrt((p * (1 - p) + z2 / 4 / n) / n); double c = (1 + z2 / n); double lower = (a - b) / c; double upper = (a + b) / c; // Add corrections for when x is very close to n. This improves the estimates. // For more info on wilson binomial confidence interval, see paper: // L.D. Brown, T.T. Cai and A. DasGupta, Interval estimation for a binomial proportion (with discussion), // _Statistical Science,_*16*:101-133, 2001. // http://www-stat.wharton.upenn.edu/~tcai/paper/Binomial-StatSci.pdf if (x == 1) lower = -Math.log(1 - alpha) / n; if (x == (n - 1)) upper = 1 + Math.log(1 - alpha) / n; return tupleFactory.newTuple(ImmutableList.of(lower, upper)); } catch (MathException e) { throw new IOException("math error", e); } }
From source file:it.uniud.ailab.dcore.annotation.annotators.RawTdidfAnnotator.java
private double idf(String term) { double n = 0; for (String doc : documents.values()) { n += doc.contains(term) ? 1 : 0; }/*w ww .j ava 2 s. c o m*/ return Math.log(documents.size() / n); }