List of usage examples for java.lang Math log
@HotSpotIntrinsicCandidate public static double log(double a)
From source file:com.aliyun.fs.oss.nat.BufferReader.java
private void prepareBeforeFetch() throws IOException { if (algorithmVersion == 1) { this.fileContentLength = store.retrieveMetadata(key).getLength(); this.lengthToFetch = fileContentLength - pos; this.bufferSize = lengthToFetch < 16 * 1024 * 1024 ? 1024 * 1024 : (lengthToFetch > 1024 * 1024 * 1024 ? 64 * 1024 * 1024 : (int) (lengthToFetch / 16)); if (Math.log(bufferSize) / Math.log(2) != 0) { int power = (int) Math.ceil(Math.log(bufferSize) / Math.log(2)); this.bufferSize = (int) Math.pow(2, power); }//from w w w .ja va2 s . co m if (buffer == null) { buffer = new byte[bufferSize]; } this.concurrentStreams = conf.getInt("fs.oss.reader.concurrent.number", 4); if ((Math.log(concurrentStreams) / Math.log(2)) != 0) { int power = (int) Math.ceil(Math.log(concurrentStreams) / Math.log(2)); this.concurrentStreams = (int) Math.pow(2, power); } this.readers = new ConcurrentReader[concurrentStreams]; this.splitContentSize = new int[concurrentStreams * 2]; this.splitSize = bufferSize / concurrentStreams / 2; initializeTaskEngine(); } else { in = store.retrieve(key, pos); } }
From source file:com.opengamma.analytics.financial.var.JohnsonSUDeltaGammaVaRCalculator.java
@SuppressWarnings("unchecked") @Override// ww w .j a v a2 s . c o m public VaRCalculationResult evaluate(final NormalVaRParameters parameters, final T... data) { ArgumentChecker.notNull(parameters, "parameters"); ArgumentChecker.notNull(data, "data"); // TODO if skewness is positive then need to fit to -x and take from upper tail of distribution final double k = _kurtosisCalculator.evaluate(data); if (k < 0) { throw new IllegalArgumentException( "Johnson SU distribution cannot be used for data with negative excess kurtosis"); } final double mult = parameters.getTimeScaling(); final double z = parameters.getZ(); final double t = _skewCalculator.evaluate(data); final double mu = _meanCalculator.evaluate(data) * mult * mult; final double sigma = _stdCalculator.evaluate(data) * mult; if (t == 0 && k == 0) { return new VaRCalculationResult(z * sigma - mu, null); } final double wUpper = Math.sqrt(Math.sqrt(2 * (k + 2)) - 1); final double wLower = Math.max(getW0(t), getW1(k + 3)); final double w = ROOT_FINDER.getRoot(getFunction(t, k), wLower, wUpper); final double w2 = w * w; final double l = 4 + 2 * (w2 - (k + 6) / (w2 + 2 * w + 3)); if (l < 0) { throw new IllegalArgumentException("Tried to find the square root of a negative number"); } final double m = -2 + Math.sqrt(l); if (m == 0 || (m < 0 && w > -1) || (m > 0 && w < -1) || (w - 1 - m) < 0) { throw new IllegalArgumentException("Invalid parameters"); } final double sign = Math.signum(t); final double u = Math.sqrt(Math.log(w)); final double v = Math.sqrt((w + 1) * (w - 1 - m) / (2 * w * m)); final double omega = -sign * TrigonometricFunctionUtils.asinh(v); final double delta = 1. / u; final double gamma = omega / u; final double lambda = sigma / (w - 1) * Math.sqrt(2 * m / (w + 1)); final double ksi = mu - sign * sigma * Math.sqrt(w - 1 - m) / (w - 1); return new VaRCalculationResult(-lambda * Math.sinh((-z - gamma) / delta) - ksi, null); }
From source file:com.analog.lyric.dimple.factorfunctions.ExchangeableDirichlet.java
@Override public final double evalEnergy(Value[] arguments) { int index = 0; if (!_parametersConstant) { _alpha = arguments[index++].getDouble(); // First variable is parameter value if (_alpha <= 0) return Double.POSITIVE_INFINITY; _logBetaAlpha = logBeta(_alpha); }//from w ww . java 2s. c om double sum = 0; final int length = arguments.length; final int N = length - index; // Number of non-parameter variables for (; index < length; index++) { final double[] x = arguments[index].getDoubleArray(); // Remaining inputs are Dirichlet distributed random variable vectors if (x.length != _dimension) throw new DimpleException( "Dimension of variable does not equal to the dimension of the parameter vector."); double xSum = 0; for (int i = 0; i < _dimension; i++) { final double xi = x[i]; if (xi <= 0) return Double.POSITIVE_INFINITY; else sum -= Math.log(xi); // -log(x_i ^ (a_i-1)) xSum += xi; } if (!almostEqual(xSum, 1, SIMPLEX_THRESHOLD * _dimension)) // Values must be on the probability simplex return Double.POSITIVE_INFINITY; } return sum * (_alpha - 1) + N * _logBetaAlpha; }
From source file:eu.amidst.core.exponentialfamily.EF_Dirichlet.java
/** * {@inheritDoc}// w ww . j av a 2s .c o m */ @Override public SufficientStatistics getSufficientStatistics(double val) { SufficientStatistics vec = this.createZeroSufficientStatistics(); vec.set((int) val, Math.log(val)); return vec; }
From source file:com.opengamma.analytics.financial.model.volatility.BlackFormulaRepository.java
/** * The <b>forward</b> price of an option using the Black formula * @param forward The forward value of the underlying * @param strike The Strike// w w w . java2s . c o m * @param timeToExpiry The time-to-expiry * @param lognormalVol The log-normal volatility * @param isCall True for calls, false for puts * @return The <b>forward</b> price */ @ExternalFunction public static double price(final double forward, final double strike, final double timeToExpiry, final double lognormalVol, final boolean isCall) { ArgumentChecker.isTrue(forward >= 0.0, "negative/NaN forward; have {}", forward); ArgumentChecker.isTrue(strike >= 0.0, "negative/NaN strike; have {}", strike); ArgumentChecker.isTrue(timeToExpiry >= 0.0, "negative/NaN timeToExpiry; have {}", timeToExpiry); ArgumentChecker.isTrue(lognormalVol >= 0.0, "negative/NaN lognormalVol; have {}", lognormalVol); double sigmaRootT = lognormalVol * Math.sqrt(timeToExpiry); if (Double.isNaN(sigmaRootT)) { s_logger.info("lognormalVol * Math.sqrt(timeToExpiry) ambiguous"); sigmaRootT = 1.; } final int sign = isCall ? 1 : -1; final boolean bFwd = (forward > LARGE); final boolean bStr = (strike > LARGE); final boolean bSigRt = (sigmaRootT > LARGE); double d1 = 0.; double d2 = 0.; if (bFwd && bStr) { s_logger.info("(large value)/(large value) ambiguous"); return isCall ? (forward >= strike ? forward : 0.) : (strike >= forward ? strike : 0.); } if (sigmaRootT < SMALL) { return Math.max(sign * (forward - strike), 0.0); } if (Math.abs(forward - strike) < SMALL || bSigRt) { d1 = 0.5 * sigmaRootT; d2 = -0.5 * sigmaRootT; } else { d1 = Math.log(forward / strike) / sigmaRootT + 0.5 * sigmaRootT; d2 = d1 - sigmaRootT; } final double nF = NORMAL.getCDF(sign * d1); final double nS = NORMAL.getCDF(sign * d2); final double first = nF == 0. ? 0. : forward * nF; final double second = nS == 0. ? 0. : strike * nS; final double res = sign * (first - second); return Math.max(0., res); }
From source file:com.opengamma.analytics.financial.model.option.pricing.analytic.JuZhongModel.java
@Override public Function1D<StandardOptionDataBundle, Double> getPricingFunction( final AmericanVanillaOptionDefinition definition) { Validate.notNull(definition);//from ww w .j av a2 s . c o m final double phi = definition.isCall() ? 1 : -1; final Function1D<StandardOptionDataBundle, Double> pricingFunction = new Function1D<StandardOptionDataBundle, Double>() { @SuppressWarnings("synthetic-access") @Override public Double evaluate(final StandardOptionDataBundle data) { Validate.notNull(data); final GreekResultCollection bsmResult = BSM.getGreeks(definition, data, PRICE); final double bsmPrice = bsmResult.get(Greek.FAIR_PRICE); final double s = data.getSpot(); final double k = definition.getStrike(); final double t = definition.getTimeToExpiry(data.getDate()); final double r = data.getInterestRate(t); final double b = data.getCostOfCarry(); final double sigma = data.getVolatility(t, k); final double sigmaSq = sigma * sigma; final double h = getH(r, t); final double alpha = getAlpha(r, sigmaSq); final double beta = getBeta(r, b, sigmaSq); final double lambda = getLambda(phi, alpha, beta, h); final double lambdaDash = getLambdaDash(phi, alpha, beta, h); final Function1D<Double, Double> function = getFunction(phi, Math.exp(-b * t), k, t, sigmaSq, b, lambda, definition, data); final double sEstimate = FINDER.getRoot(function, 0., s * 2); if (phi * (sEstimate - s) <= 0) { return phi * (s - k); } final double estimatePrice = BSM.getGreeks(definition, data.withSpot(sEstimate), PRICE) .get(Greek.FAIR_PRICE); final double hA = phi * (sEstimate - k) - estimatePrice; final double derivative = getDerivative(k, r, b, t, sigma, phi, sEstimate); final double c = getC(h, alpha, lambdaDash, lambda, beta); final double d = getD(h, alpha, derivative, hA, lambdaDash, lambda, beta); final double ratio = Math.log(s / sEstimate); final double chi = c * Math.pow(ratio, 2) + d * ratio; return bsmPrice + hA * Math.pow(s / sEstimate, lambda) / (1 - chi); } }; return pricingFunction; }
From source file:com.datumbox.framework.common.utilities.PHPMethods.java
/** * Returns the logarithm of a number at an arbitrary base. * // w w w . ja va 2s . c om * @param d * @param base * @return */ public static double log(double d, double base) { if (base == 1.0 || base <= 0.0) { throw new IllegalArgumentException("Invalid base for logarithm."); } return Math.log(d) / Math.log(base); }
From source file:edu.stanford.cfuller.imageanalysistools.filter.Kernel.java
public void formatTransformFrom1DInput(int size0, int size1) { int ydimPowOfTwo = size0; int xdimPowOfTwo = size1; if (!org.apache.commons.math3.util.ArithmeticUtils.isPowerOfTwo(ydimPowOfTwo) || !org.apache.commons.math3.util.ArithmeticUtils.isPowerOfTwo(xdimPowOfTwo)) { xdimPowOfTwo = (int) Math.pow(2, Math.ceil(Math.log(size1) / Math.log(2))); ydimPowOfTwo = (int) Math.pow(2, Math.ceil(Math.log(size0) / Math.log(2))); }/*from w w w.j a va 2s . com*/ Complex[][] colMajorImage = new Complex[xdimPowOfTwo][ydimPowOfTwo]; int counter = 0; for (int i = 0; i < colMajorImage.length; i++) { for (int j = 0; j < colMajorImage[0].length; j++) { colMajorImage[i][j] = new Complex(this.weights[counter++], this.weights[counter++]); } } this.transform = colMajorImage; }
From source file:hu.ppke.itk.nlpg.purepos.model.internal.HashSuffixGuesser.java
@Override public Map<T, Double> getSmoothedTagLogProbabilities(String word) { HashMap<T, Double> ret = new HashMap<T, Double>(); // Set<T> tags = freqTable.get("").getLeft().keySet(); // for (T tag : tags) { // ret.put(tag, getTagLogProbability(word, tag)); // }/*ww w . j ava2 s.c o m*/ // return ret; Map<T, Double> probs = getTagProbabilities(word); for (Map.Entry<T, Double> entry : probs.entrySet()) { ret.put(entry.getKey(), Math.log(entry.getValue())); } return ret; }
From source file:edu.cmu.tetrad.data.GeneralAndersonDarlingTest.java
private void runTest() { int n = data.size(); double h = 0.0; int numSummed = 0; for (int i = 1; i <= n; i++) { double x1 = data.get(i - 1); double a1 = Math.log(dist.cumulativeProbability(x1)); double x2 = data.get(n + 1 - i - 1); double a2 = Math.log(1.0 - dist.cumulativeProbability(x2)); double k = (2 * i - 1) * (a1 + a2); if (!(Double.isNaN(a1) || Double.isNaN(a2) || Double.isInfinite(a1) || Double.isInfinite(a2))) { h += k;/*w ww .j av a2 s . c om*/ 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; }