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.fourier.FFTModelGreeks.java
/** * /*from w w w .j a v a 2 s .c om*/ * @param forward The forward value of the underlying * @param discountFactor * @param t Time to expiry * @param isCall true for call * @param ce The Characteristic Exponent (log of characteristic function) of the returns of the underlying * @param lowestStrike The lowest strike to return (the actual value will depend on the set up, but is guaranteed to be less than this) * @param highestStrike The highest strike to return (the actual value will depend on the set up, but is guaranteed to be greater than this) * @param minStrikesDisplayed minimum number of strikes returned (actual number depends on set up) * @param limitSigma An estimate of the implied vol used to calculate limits in the numerical routines * @param alpha Regularization factor. Values of 0 or -1 are not allowed. -0.5 is recommended * @param tol Tolerance - smaller values give higher accuracy * @return an array of arrays where is first array is the strikes, the second the prices, the first the derivatives of price wrt the first parameter etc */ //TODO this is cut and paste from FFTPricer - the calculation of the sample size and spacing should be extracted public double[][] getGreeks(final double forward, final double discountFactor, final double t, final boolean isCall, final MartingaleCharacteristicExponent ce, final double lowestStrike, final double highestStrike, final int minStrikesDisplayed, final double limitSigma, final double alpha, final double tol) { Validate.notNull(ce, "characteristic exponent"); Validate.isTrue(tol > 0.0, "need tol > 0"); Validate.isTrue(alpha != 0.0 && alpha != -1.0, "alpha cannot be -1 or 0"); Validate.isTrue(lowestStrike <= forward, "need lowestStrike <= forward"); Validate.isTrue(highestStrike >= forward, "need highestStrike >= forward"); Validate.isTrue(limitSigma > 0.0, "need limitSigma > 0"); double kMax; final double limitSigmaRootT = limitSigma * Math.sqrt(t); final double atm = NORMAL.getCDF(limitSigmaRootT / 2.0); if (alpha > 0) { kMax = -Math.log((2 * atm - 1) * tol) / alpha; } else if (alpha < -1.0) { kMax = Math.log((2 * atm - 1) * tol) / (1 + alpha); } else { kMax = -Math.log(2 * (1 - atm) * tol) * Math.max(-1.0 / alpha, 1 / (1 + alpha)); } final EuropeanCallFourierTransform psi = new EuropeanCallFourierTransform(ce); final Function1D<ComplexNumber, ComplexNumber> psiFunction = psi.getFunction(t); final double xMax = LIMIT_CALCULATOR.solve(psiFunction, alpha, tol); double deltaK; if (highestStrike == lowestStrike) { deltaK = Math.PI / xMax; } else { deltaK = Math.min(Math.log(highestStrike / lowestStrike) / (minStrikesDisplayed - 1), Math.PI / xMax); } final double log2 = Math.log(2); final int twoPow = (int) Math.ceil(Math.log(kMax / deltaK) / log2); final int n = (int) Math.pow(2, twoPow); final double delta = 2 * Math.PI / n / deltaK; final int m = (int) (xMax * deltaK * n / 2 / Math.PI); final int nLowStrikes = (int) Math.ceil(Math.log(forward / lowestStrike) / deltaK); final int nHighStrikes = (int) Math.ceil(Math.log(highestStrike / forward) / deltaK); return getGreeks(forward, discountFactor, t, isCall, ce, nLowStrikes, nHighStrikes, alpha, delta, n, m); }
From source file:com.opengamma.analytics.financial.model.option.DistributionFromImpliedVolatility.java
private double getD1Prime(final double k, final double sigma, final double sigmaPrime) { final double res = -1 / k / sigma / _rootT - (Math.log(_f / k) / sigma / sigma / _rootT - 0.5 * _rootT) * sigmaPrime; return res;//from www.j a va 2 s . c o m }
From source file:fungus.VisualizerTransformers.java
private double logScale(int cap) { // FIXME: Doesn't update if node capacity distribution changes if (!rangeScaled) { double logMax = Math.log(StateObserver.getMaxCapacity()); logMin = Math.log(StateObserver.getMinCapacity()); if (logMax == 0.0) { logMax = 1.0;/*from w ww . j a v a2 s .c o m*/ logMin = 0.0; } // Avoid / 0 double preRange = logMax - logMin; double preScale = preRange / logMax; double largest = 15.0; smallest = 7.0; double rangeScale = largest - smallest; scale = preScale * rangeScale; rangeScaled = true; } double logCap = Math.log(cap); return ((logCap - logMin) * scale) + smallest; }
From source file:peakml.util.jfreechart.LognAxis.java
protected double log(double value) { boolean isneg = value < 0; if (isneg)/* w w w. j a v a2s .c om*/ value = -value; if (value < logscale) value += (logscale - value) / logscale; return (isneg ? -Math.log(value) : Math.log(value)) / lognvalue; }
From source file:utils.RandomVariable.java
/** * Generate a random number from a LogNormal random variable. * * @param mu mean of the Normal random variable. * @param sigma standard deviation of the Normal random variable. * @return a double./*from w ww. j a va 2 s . c o m*/ */ public static double logNormal(double mu, double sigma) { double x = mu + sigma * Math.cos(2 * Math.PI * rand()) * Math.sqrt(-2 * Math.log(rand())); return x; }
From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java
/** * // w w w. j av a2 s.c o m * @param bicValue * The bicValue of the model represented by only one Gaussian. * This parameter it's useful when this function is called * repeatedly for different frame values and the same features * parameter * @param frame * the frame which is tested for being a change point * @param features * the feature vectors matrix * @return the likelihood ratio */ double getLikelihoodRatio(double bicValue, int frame, Array2DRowRealMatrix features) { double bicValue1, bicValue2; int d = Segment.FEATURES_SIZE; double penalty = 0.5 * (d + 0.5 * d * (d + 1)) * Math.log(features.getRowDimension()) * 2; int nrows = features.getRowDimension(), ncols = features.getColumnDimension(); Array2DRowRealMatrix sub1, sub2; sub1 = (Array2DRowRealMatrix) features.getSubMatrix(0, frame - 1, 0, ncols - 1); sub2 = (Array2DRowRealMatrix) features.getSubMatrix(frame, nrows - 1, 0, ncols - 1); bicValue1 = getBICValue(sub1); bicValue2 = getBICValue(sub2); return (bicValue - bicValue1 - bicValue2 - penalty); }
From source file:beast.math.distributions.BetaDistribution.java
/** * probability density function of the distribution * * @param x argument/*from w w w .j ava 2 s. c o m*/ * @return pdf value */ public double pdf(double x) { recomputeZ(); if (x < 0 || x > 1) { return 0; } else if (x == 0) { if (alpha < 1) { // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime // exceptions is bad. Better to return NaN and let the calling code deal with it. return Double.NaN; // throw MathRuntimeException.createIllegalArgumentException( // "Cannot compute beta density at 0 when alpha = {0,number}", alpha); } return 0; } else if (x == 1) { if (beta < 1) { // AR - throwing exceptions deep in numerical code causes trouble. Catching runtime // exceptions is bad. Better to return NaN and let the calling code deal with it. return Double.NaN; // throw MathRuntimeException.createIllegalArgumentException( // "Cannot compute beta density at 1 when beta = %.3g", beta); } return 0; } else { double logX = Math.log(x); double log1mX = Math.log1p(-x); return Math.exp((alpha - 1) * logX + (beta - 1) * log1mX - z); } }
From source file:etomica.math.SpecialFunctions.java
private static double gser(double a, double x) { int nmax = 500; double epsilon = 3.0e-12; double ap = a; double sum = 1.0 / a; double del = sum; for (int n = 1; n <= nmax; n++) { ap++;// www .j a va 2 s .co m del *= x / ap; sum += del; if (Math.abs(del) < Math.abs(sum) * epsilon) break; } return sum * Math.exp(-x + a * Math.log(x) - lnGamma(a)); }
From source file:bide.core.Likelihood.java
private double calLogLikeli(double[] expSpot, int noTotal, double u, double p, double sd) { int express = expSpot.length; int notExpress = noTotal - express; double logP = (p == 0) ? 0 : Math.log(p) * express; // n*log(p)+ double logNotExp = (1 - p) + p * NormalDistribution.cdf(lowerLim, u, sd); logNotExp = (logNotExp == 0) ? 0 : Math.log(logNotExp) * notExpress; double likeli = StatUtils.sum(NormalDistribution.logPdfBT(expSpot, u, sd, lowerLim, upperLim)) + logP + logNotExp;// ww w. j a v a 2 s . c o m return likeli; }
From source file:com.opengamma.analytics.financial.model.option.pricing.analytic.twoasset.EuropeanExchangeAssetOptionModel.java
/** * Gets the pricing function for a European-style exchange asset option * @param definition The option definition * @return The pricing function/* w w w .j av a 2s . co m*/ * @throws IllegalArgumentException If the definition is null */ @Override public Function1D<StandardTwoAssetOptionDataBundle, Double> getPricingFunction( final EuropeanExchangeAssetOptionDefinition 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 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, s1); final double sigma2 = data.getSecondVolatility(t, s2); final double rho = data.getCorrelation(); final double q1 = definition.getFirstQuantity(); final double q2 = definition.getSecondQuantity(); final double sigma = Math.sqrt(sigma1 * sigma1 + sigma2 * sigma2 - 2 * rho * sigma1 * sigma2); final double sigmaT = sigma * Math.sqrt(t); final double d1 = (Math.log(q1 * s1 / q2 / s2) + t * (b1 - b2 + sigma * sigma / 2)) / sigmaT; final double d2 = d1 - sigmaT; return q1 * s1 * Math.exp(t * (b1 - r)) * NORMAL.getCDF(d1) - q2 * s2 * Math.exp(t * (b2 - r)) * NORMAL.getCDF(d2); } }; }