/** * Natural logarithm. * * @param x a double * @return log(x) */ public static double log(final double x) { return log(x, null); }
/** * Return the probability density for a particular point. * * @param x The point at which the density should be computed. * @return The pdf at point x. * @since 2.1 */ @Override public double density(double x) { recomputeZ(); if (x < 0 || x > 1) { return 0; } else if (x == 0) { if (alpha < 1) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.CANNOT_COMPUTE_BETA_DENSITY_AT_0_FOR_SOME_ALPHA, alpha); } return 0; } else if (x == 1) { if (beta < 1) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.CANNOT_COMPUTE_BETA_DENSITY_AT_1_FOR_SOME_BETA, beta); } return 0; } else { double logX = FastMath.log(x); double log1mX = FastMath.log1p(-x); return FastMath.exp((alpha - 1) * logX + (beta - 1) * log1mX - z); } }
/** * {@inheritDoc} */ @Override public void increment(final double d) { value += FastMath.log(d); n++; }
/** {@inheritDoc} */ @Override public RealVector mapLogToSelf() { for (int i = 0; i < data.length; i++) { data[i] = FastMath.log(data[i]); } return this; }
/** {@inheritDoc} */ @Override public double value(double d) { return FastMath.log(d); } };
/** * <p>Returns the * <a href="http://mathworld.wolfram.com/Logarithm.html">logarithm</a> * for base <code>b</code> of <code>x</code>. * </p> * <p>Returns <code>NaN<code> if either argument is negative. If * <code>base</code> is 0 and <code>x</code> is positive, 0 is returned. * If <code>base</code> is positive and <code>x</code> is 0, * <code>Double.NEGATIVE_INFINITY</code> is returned. If both arguments * are 0, the result is <code>NaN</code>.</p> * * @param base the base of the logarithm, must be greater than 0 * @param x argument, must be greater than 0 * @return the value of the logarithm - the number y such that base^y = x. * @since 1.2 */ public static double log(double base, double x) { return FastMath.log(x)/FastMath.log(base); }
/** Compute the inverse hyperbolic tangent of a number. * @param a number on which evaluation is done * @return inverse hyperbolic tangent of a */ public static double atanh(double a) { boolean negative = false; if (a < 0) { negative = true; a = -a; } double absAtanh; if (a > 0.15) { absAtanh = 0.5 * FastMath.log((1 + a) / (1 - a)); } else { final double a2 = a * a; if (a > 0.087) { absAtanh = a * (1 + a2 * (1.0 / 3.0 + a2 * (1.0 / 5.0 + a2 * (1.0 / 7.0 + a2 * (1.0 / 9.0 + a2 * (1.0 / 11.0 + a2 * (1.0 / 13.0 + a2 * (1.0 / 15.0 + a2 * (1.0 / 17.0))))))))); } else if (a > 0.031) { absAtanh = a * (1 + a2 * (1.0 / 3.0 + a2 * (1.0 / 5.0 + a2 * (1.0 / 7.0 + a2 * (1.0 / 9.0 + a2 * (1.0 / 11.0 + a2 * (1.0 / 13.0))))))); } else if (a > 0.003) { absAtanh = a * (1 + a2 * (1.0 / 3.0 + a2 * (1.0 / 5.0 + a2 * (1.0 / 7.0 + a2 * (1.0 / 9.0))))); } else { absAtanh = a * (1 + a2 * (1.0 / 3.0 + a2 * (1.0 / 5.0))); } } return negative ? -absAtanh : absAtanh; }
/** Compute the base 10 logarithm. * @param x a number * @return log10(x) */ public static double log10(final double x) { final double hiPrec[] = new double[2]; final double lores = log(x, hiPrec); if (Double.isInfinite(lores)){ // don't allow this to be converted to NaN return lores; } final double tmp = hiPrec[0] * HEX_40000000; final double lna = hiPrec[0] + tmp - tmp; final double lnb = hiPrec[0] - lna + hiPrec[1]; final double rln10a = 0.4342944622039795; final double rln10b = 1.9699272335463627E-8; return rln10b * lnb + rln10b * lna + rln10a * lnb + rln10a * lna; }
/** * Returns the probability density for a particular point. * * @param x The point at which the density should be computed. * @return The pdf at point x. * @since 2.1 */ @Override public double density(double x) { final double nhalf = numeratorDegreesOfFreedom / 2; final double mhalf = denominatorDegreesOfFreedom / 2; final double logx = FastMath.log(x); final double logn = FastMath.log(numeratorDegreesOfFreedom); final double logm = FastMath.log(denominatorDegreesOfFreedom); final double lognxm = FastMath.log(numeratorDegreesOfFreedom * x + denominatorDegreesOfFreedom); return FastMath.exp(nhalf*logn + nhalf*logx - logx + mhalf*logm - nhalf*lognxm - mhalf*lognxm - Beta.logBeta(nhalf, mhalf)); }
/** Compute the inverse hyperbolic cosine of a number. * @param a number on which evaluation is done * @return inverse hyperbolic cosine of a */ public static double acosh(final double a) { return FastMath.log(a + FastMath.sqrt(a * a - 1)); }
/** * Access the initial domain value, based on <code>p</code>, used to * bracket a CDF root. This method is used by * {@link #inverseCumulativeProbability(double)} to find critical values. * * @param p the desired probability for the critical value * @return initial domain value */ @Override protected double getInitialDomain(double p) { // use median return FastMath.pow(scale * FastMath.log(2.0), 1.0 / shape); }
/** * Returns the probability density for a particular point. * * @param x The point at which the density should be computed. * @return The pdf at point x. * @since 2.1 */ @Override public double density(double x) { final double n = degreesOfFreedom; final double nPlus1Over2 = (n + 1) / 2; return FastMath.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (FastMath.log(FastMath.PI) + FastMath.log(n)) - Gamma.logGamma(n/2) - nPlus1Over2 * FastMath.log(1 + x * x /n)); }
/** * For this distribution, X, this method returns the critical point x, such * that P(X < x) = <code>p</code>. * <p> * Returns 0 for p=0 and <code>Double.POSITIVE_INFINITY</code> for p=1.</p> * * @param p the desired probability * @return x, such that P(X < x) = <code>p</code> * @throws MathException if the inverse cumulative probability can not be * computed due to convergence or other numerical errors. * @throws IllegalArgumentException if p < 0 or p > 1. */ @Override public double inverseCumulativeProbability(double p) throws MathException { double ret; if (p < 0.0 || p > 1.0) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.OUT_OF_RANGE_SIMPLE, p, 0.0, 1.0); } else if (p == 1.0) { ret = Double.POSITIVE_INFINITY; } else { ret = -mean * FastMath.log(1.0 - p); } return ret; }
/** Compute the inverse hyperbolic sine of a number. * @param a number on which evaluation is done * @return inverse hyperbolic sine of a */ public static double asinh(double a) { boolean negative = false; if (a < 0) { negative = true; a = -a; } double absAsinh; if (a > 0.167) { absAsinh = FastMath.log(FastMath.sqrt(a * a + 1) + a); } else { final double a2 = a * a; if (a > 0.097) { absAsinh = a * (1 - a2 * (1 / 3.0 - a2 * (1 / 5.0 - a2 * (1 / 7.0 - a2 * (1 / 9.0 - a2 * (1.0 / 11.0 - a2 * (1.0 / 13.0 - a2 * (1.0 / 15.0 - a2 * (1.0 / 17.0) * 15.0 / 16.0) * 13.0 / 14.0) * 11.0 / 12.0) * 9.0 / 10.0) * 7.0 / 8.0) * 5.0 / 6.0) * 3.0 / 4.0) / 2.0); } else if (a > 0.036) { absAsinh = a * (1 - a2 * (1 / 3.0 - a2 * (1 / 5.0 - a2 * (1 / 7.0 - a2 * (1 / 9.0 - a2 * (1.0 / 11.0 - a2 * (1.0 / 13.0) * 11.0 / 12.0) * 9.0 / 10.0) * 7.0 / 8.0) * 5.0 / 6.0) * 3.0 / 4.0) / 2.0); } else if (a > 0.0036) { absAsinh = a * (1 - a2 * (1 / 3.0 - a2 * (1 / 5.0 - a2 * (1 / 7.0 - a2 * (1 / 9.0) * 7.0 / 8.0) * 5.0 / 6.0) * 3.0 / 4.0) / 2.0); } else { absAsinh = a * (1 - a2 * (1 / 3.0 - a2 * (1 / 5.0) * 3.0 / 4.0) / 2.0); } } return negative ? -absAsinh : absAsinh; }
/** * Returns the sum of the natural logs of the entries in the specified portion of * the input array, or <code>Double.NaN</code> if the designated subarray * is empty. * <p> * Throws <code>IllegalArgumentException</code> if the array is null.</p> * <p> * See {@link SumOfLogs}.</p> * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the natural logs of the values or Double.NaN if * length = 0 * @throws IllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values, final int begin, final int length) { double sumLog = Double.NaN; if (test(values, begin, length)) { sumLog = 0.0; for (int i = begin; i < begin + length; i++) { sumLog += FastMath.log(values[i]); } } return sumLog; }
/** * Returns the natural logarithm of n!. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> <code>n >= 0</code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * </ul></p> * * @param n argument * @return <code>n!</code> * @throws IllegalArgumentException if preconditions are not met. */ public static double factorialLog(final int n) { if (n < 0) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n); } if (n < 21) { return FastMath.log(factorial(n)); } double logSum = 0; for (int i = 2; i <= n; i++) { logSum += FastMath.log(i); } return logSum; }
/** * For this distribution, X, this method returns the critical point x, such * that P(X < x) = <code>p</code>. * <p> * Returns <code>Double.NEGATIVE_INFINITY</code> for p=0 and * <code>Double.POSITIVE_INFINITY</code> for p=1.</p> * * @param p the desired probability * @return x, such that P(X < x) = <code>p</code> * @throws IllegalArgumentException if <code>p</code> is not a valid * probability. */ @Override public double inverseCumulativeProbability(double p) { double ret; if (p < 0.0 || p > 1.0) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.OUT_OF_RANGE_SIMPLE, p, 0.0, 1.0); } else if (p == 0) { ret = 0.0; } else if (p == 1) { ret = Double.POSITIVE_INFINITY; } else { ret = scale * FastMath.pow(-FastMath.log(1.0 - p), 1.0 / shape); } return ret; }
/** {@inheritDoc} */ public double nextGaussian() { final double random; if (Double.isNaN(nextGaussian)) { // generate a new pair of gaussian numbers final double x = nextDouble(); final double y = nextDouble(); final double alpha = 2 * FastMath.PI * x; final double r = FastMath.sqrt(-2 * FastMath.log(y)); random = r * FastMath.cos(alpha); nextGaussian = r * FastMath.sin(alpha); } else { // use the second element of the pair already generated random = nextGaussian; nextGaussian = Double.NaN; } return random; }
/** * Returns a random value from an Exponential distribution with the given * mean. * <p> * <strong>Algorithm Description</strong>: Uses the <a * href="http://www.jesus.ox.ac.uk/~clifford/a5/chap1/node5.html"> Inversion * Method</a> to generate exponentially distributed random values from * uniform deviates. * </p> * * @param mean the mean of the distribution * @return the random Exponential value * @throws NotStrictlyPositiveException if {@code mean <= 0}. */ public double nextExponential(double mean) { if (mean <= 0.0) { throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean); } final RandomGenerator generator = getRan(); double unif = generator.nextDouble(); while (unif == 0.0d) { unif = generator.nextDouble(); } return -mean * FastMath.log(unif); }
return createComplex(FastMath.log(abs()), FastMath.atan2(imaginary, real));