Example usage for java.lang Math sqrt

List of usage examples for java.lang Math sqrt

Introduction

In this page you can find the example usage for java.lang Math sqrt.

Prototype

@HotSpotIntrinsicCandidate
public static double sqrt(double a) 

Source Link

Document

Returns the correctly rounded positive square root of a double value.

Usage

From source file:net.ajaskey.market.ta.methods.RegressionOutput.java

/**
 * This method serves as a constructor for the class.
 *
 * @param regressionMethods//from   w  ww .  ja va2  s  .  c om
 *
 * @param parameters
 * @param varcov
 * @param isSymmetricCompressed
 * @param nobs
 * @param rank
 * @param sumy
 * @param sumysq
 * @param sse
 * @param containsConstant
 * @param copyData
 */
public RegressionOutput(RegressionMethods reg, long days) {
    this.results = reg.regress();

    this.count = this.results.getN();
    this.predictedPrice = reg.predict(days);
    this.meanErr = Math.sqrt(this.results.getMeanSquareError());
    this.slope = (reg.getSlope() * 180.0) / Math.PI;
    this.r2 = this.results.getRSquared();

}

From source file:com.opengamma.analytics.financial.model.option.pricing.tree.LogNormalBinomialTreeBuilder.java

@Override
protected DoublesPair getCentralNodePair(double dt, double sigma, double forward, double centreLevel) {

    Function1D<Double, Double> func = new CentreNode(dt, sigma, forward, centreLevel);
    double[] limits = s_bracketRoot.getBracketedPoints(func, forward,
            forward * Math.exp(sigma * Math.sqrt(dt)));

    double upper = s_root.getRoot(func, limits[0], limits[1]);
    double lower = centreLevel * centreLevel / upper;
    return new DoublesPair(lower, upper);
}

From source file:edu.byu.nlp.stats.RandomGenerators.java

/**
 * This routine generates a random number between 0 and n inclusive, following
 * the binomial distribution with probability p and n trials. The routine is
 * based on the BTPE algorithm, described in:
 * /*from ww  w . jav a  2s . com*/
 * Voratas Kachitvichyanukul and Bruce W. Schmeiser:
 * Binomial Random Variate Generation
 * Communications of the ACM, Volume 31, Number 2, February 1988, pages 216-222.
 * 
 * Snagged on March 19, 2009 from:
 * 
 * http://chianti.ucsd.edu/svn/csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/kmeans/KCluster.java
 * 
 * by Robbie who also converted the uniform() calls to RandomGenerator.nextDouble()
 * 
 * @param p The probability of a single event.  This should be less than or equal to 0.5.
 * @param n The number of trials
 * @return An integer drawn from a binomial distribution with parameters (p, n).
 */
public static int nextBinom(RandomGenerator rnd, int n, double p) {
    // rah67 March 19, 2007; didn't properly handle the degenerate case
    if (p == 1.0)
        return n;

    double q = 1 - p;
    if (n * p < 30.0) /* Algorithm BINV */
    {
        double s = p / q;
        double a = (n + 1) * s;
        //         double r = Math.exp(n*Math.log(q)); /* pow() causes a crash on AIX */
        double r = Math.pow(q, n); /* rah67 March 19, 2007 */
        int x = 0;
        double u = rnd.nextDouble();
        while (true) {
            if (u < r)
                return x;
            u -= r;
            x++;
            r *= (a / x) - s;
        }
    } else /* Algorithm BTPE */
    { /* Step 0 */
        double fm = n * p + p;
        int m = (int) fm;
        double p1 = Math.floor(2.195 * Math.sqrt(n * p * q) - 4.6 * q) + 0.5;
        double xm = m + 0.5;
        double xl = xm - p1;
        double xr = xm + p1;
        double c = 0.134 + 20.5 / (15.3 + m);
        double a = (fm - xl) / (fm - xl * p);
        double b = (xr - fm) / (xr * q);
        double lambdal = a * (1.0 + 0.5 * a);
        double lambdar = b * (1.0 + 0.5 * b);
        double p2 = p1 * (1 + 2 * c);
        double p3 = p2 + c / lambdal;
        double p4 = p3 + c / lambdar;
        while (true) { /* Step 1 */
            int y;
            int k;
            double u = rnd.nextDouble();
            double v = rnd.nextDouble();
            u *= p4;
            if (u <= p1)
                return (int) (xm - p1 * v + u);
            /* Step 2 */
            if (u > p2) { /* Step 3 */
                if (u > p3) { /* Step 4 */
                    y = (int) (xr - Math.log(v) / lambdar);
                    if (y > n)
                        continue;
                    /* Go to step 5 */
                    v = v * (u - p3) * lambdar;
                } else {
                    y = (int) (xl + Math.log(v) / lambdal);
                    if (y < 0)
                        continue;
                    /* Go to step 5 */
                    v = v * (u - p2) * lambdal;
                }
            } else {
                double x = xl + (u - p1) / c;
                v = v * c + 1.0 - Math.abs(m - x + 0.5) / p1;
                if (v > 1)
                    continue;
                /* Go to step 5 */
                y = (int) x;
            }
            /* Step 5 */
            /* Step 5.0 */
            k = Math.abs(y - m);
            if (k > 20 && k < 0.5 * n * p * q - 1.0) { /* Step 5.2 */
                double rho = (k / (n * p * q))
                        * ((k * (k / 3.0 + 0.625) + 0.1666666666666) / (n * p * q) + 0.5);
                double t = -k * k / (2 * n * p * q);
                double A = Math.log(v);
                if (A < t - rho)
                    return y;
                else if (A > t + rho)
                    continue;
                else { /* Step 5.3 */
                    double x1 = y + 1;
                    double f1 = m + 1;
                    double z = n + 1 - m;
                    double w = n - y + 1;
                    double x2 = x1 * x1;
                    double f2 = f1 * f1;
                    double z2 = z * z;
                    double w2 = w * w;
                    if (A > xm * Math.log(f1 / x1) + (n - m + 0.5) * Math.log(z / w)
                            + (y - m) * Math.log(w * p / (x1 * q))
                            + (13860. - (462. - (132. - (99. - 140. / f2) / f2) / f2) / f2) / f1 / 166320.
                            + (13860. - (462. - (132. - (99. - 140. / z2) / z2) / z2) / z2) / z / 166320.
                            + (13860. - (462. - (132. - (99. - 140. / x2) / x2) / x2) / x2) / x1 / 166320.
                            + (13860. - (462. - (132. - (99. - 140. / w2) / w2) / w2) / w2) / w / 166320.)
                        continue;
                    return y;
                }
            } else { /* Step 5.1 */
                int i;
                double s = p / q;
                double aa = s * (n + 1);
                double f = 1.0;
                for (i = m; i < y; f *= (aa / (++i) - s))
                    ;
                for (i = y; i < m; f /= (aa / (++i) - s))
                    ;
                if (v > f)
                    continue;
                return y;
            }
        }
    }
}

From source file:gr.eap.LSHDB.HammingKey.java

public int getLc() {
    double p = 1 - (t * 1.0) / (this.size * 1.0);
    p = Math.pow(p, k);/* w  w  w .  j a  v a2s  .  com*/
    double exp = (L * p);
    double std = Math.sqrt(exp * (1 - p));
    int C = (int) Math.round(exp - std);

    double x = (Math.sqrt(Math.log(delta) * Math.log(delta) - 2 * C * Math.log(delta)) - Math.log(delta) + C)
            / p;
    int Lc = (int) Math.ceil(x);
    double b = Lc * p;
    if (C > b)
        System.out.println("does not apply C > np.");
    BinomialDistribution bd1 = new BinomialDistribution(L, p);
    for (int l = L; l < L * 2; l++) {
        bd1 = new BinomialDistribution(l, p);
        double result = bd1.cumulativeProbability(C - 1);
        if (result < delta) {
            Lc = l;
            break;
        }
    }
    System.out.println("Lc reduced to=" + Lc);
    return Lc;
}

From source file:com.opengamma.analytics.financial.model.interestrate.BlackDermanToyYieldOnlyInterestRateModel.java

public Function1D<StandardDiscountBondModelDataBundle, RecombiningBinomialTree<Triple<Double, Double, Double>>> getTrees(
        final ZonedDateTime time) {
    Validate.notNull(time, "time");
    return new Function1D<StandardDiscountBondModelDataBundle, RecombiningBinomialTree<Triple<Double, Double, Double>>>() {

        @SuppressWarnings({ "unchecked", "synthetic-access" })
        @Override// w w  w  .  j  a va  2 s .c o  m
        public RecombiningBinomialTree<Triple<Double, Double, Double>> evaluate(
                final StandardDiscountBondModelDataBundle data) {
            Validate.notNull(data, "data");
            final double[][] r = new double[_n + 1][_j];
            final double[][] q = new double[_n + 1][_j];
            final double[][] d = new double[_n + 1][_j];
            final double[] u = new double[_n + 1];
            final double[] p = new double[_n + 2];
            final double t = DateUtils.getDifferenceInYears(data.getDate(), time);
            final double dt = t / _n;
            final double dtSqrt = Math.sqrt(dt);
            final double r1 = data.getShortRate(dt);
            final double sigma = data.getShortRateVolatility(dt);
            p[0] = 1.0;
            for (int i = 1; i <= _n + 1; i++) {
                p[i] = 1. / Math.pow((1 + data.getShortRate(i) * dt), dt * i);
            }
            q[0][0] = 1.;
            u[0] = r1;
            r[0][0] = r1;
            d[0][0] = 1. / (1 + r1 * dt);
            for (int i = 1; i <= _n; i++) {
                q[i][0] = 0.5 * q[i - 1][0] * d[i - 1][0];
                q[i][i] = 0.5 * q[i - 1][i - 1] * d[i - 1][i - 1];
                for (int j = -i + 2, k = 1; j <= i - 2; j += 2, k++) {
                    q[i][k] = 0.5 * (q[i - 1][k - 1] * d[i - 1][k - 1] + q[i - 1][k] * d[i - 1][k]);
                }
                u[i] = _rootFinder.getRoot(getMedian(sigma, i, dt, q, p[i + 1]), 0., 1.);
                for (int j = -i, k = 0; j <= i; j += 2, k++) {
                    r[i][k] = u[i] * Math.exp(sigma * j * dtSqrt);
                    d[i][k] = 1. / (1 + r[i][k] * dt);
                }
            }
            final Triple<Double, Double, Double>[][] result = new Triple[_n + 1][_j];
            for (int i = 0; i <= _n; i++) {
                for (int j = 0; j < _j; j++) {
                    result[i][j] = new Triple<>(r[i][j], d[i][j], q[i][j]);
                }
            }
            return new RecombiningBinomialTree<>(result);
        }

    };
}

From source file:com.itemanalysis.psychometrics.irt.estimation.RaschScaleQualityStatistics.java

/**
 * The square root of mean square error.
 *
 * @return standard error.
 */
public double rootMeanSquareError() {
    return Math.sqrt(meanSquareError());
}

From source file:com.opengamma.analytics.math.statistics.descriptive.PartialMomentCalculator.java

/**
 * @param x The array of data, not null or empty
 * @return The partial moment/* w  w w  . jav  a  2s .c o  m*/
 */
@Override
public Double evaluate(final double[] x) {
    Validate.notNull(x, "x");
    Validate.isTrue(x.length > 0, "x cannot be empty");
    final int n = x.length;
    final double[] copyX = Arrays.copyOf(x, n);
    Arrays.sort(copyX);
    double sum = 0;
    if (_useDownSide) {
        int i = 0;
        if (copyX[i] > _threshold) {
            return 0.;
        }
        while (i < n && copyX[i] < _threshold) {
            sum += (copyX[i] - _threshold) * (copyX[i] - _threshold);
            i++;
        }
        return Math.sqrt(sum / i);
    }
    int i = n - 1;
    int count = 0;
    if (copyX[i] < _threshold) {
        return 0.;
    }
    while (i >= 0 && copyX[i] > _threshold) {
        sum += (copyX[i] - _threshold) * (copyX[i] - _threshold);
        count++;
        i--;
    }
    return Math.sqrt(sum / count);
}

From source file:com.opengamma.analytics.math.statistics.distribution.BivariateNormalDistribution.java

/**
 * @param x The parameters for the function, $(x, y, \rho$, with $-1 \geq \rho \geq 1$, not null 
 * @return The cdf//from w  w w.jav a 2  s  .  c o  m
 */
@Override
public double getCDF(final double[] x) {
    Validate.notNull(x);
    Validate.isTrue(x.length == 3, "Need a, b and rho values");
    Validate.isTrue(x[2] >= -1 && x[2] <= 1, "Correlation must be >= -1 and <= 1");
    final double a = x[0];
    double b = x[1];
    final double rho = x[2];
    if (a == Double.POSITIVE_INFINITY || b == Double.POSITIVE_INFINITY) {
        return 1;
    }
    if (a == Double.NEGATIVE_INFINITY || b == Double.NEGATIVE_INFINITY) {
        return 0;
    }
    final double sumSq = (a * a + b * b) / 2.;
    double rho1, rho2, rho3, ab, absDiff, h5, c, d, mult = 0, rho3Sq, eab, e, result;
    if (Math.abs(rho) >= 0.7) {
        rho1 = 1 - rho * rho;
        rho2 = Math.sqrt(rho1);
        if (rho < 0) {
            b *= -1;
        }
        ab = a * b;
        eab = Math.exp(-ab / 2.);
        if (Math.abs(rho) < 1) {
            absDiff = Math.abs(a - b);
            h5 = absDiff * absDiff / 2.;
            absDiff = absDiff / rho2;
            c = 0.5 - ab / 8.;
            d = 3. - 2. * c * h5;
            mult = 0.13298076 * absDiff * d * (1 - NORMAL.getCDF(absDiff))
                    - Math.exp(-h5 / rho1) * (d + c * rho1) * 0.053051647;
            for (int i = 0; i < 5; i++) {
                rho3 = rho2 * X[i];
                rho3Sq = rho3 * rho3;
                rho1 = Math.sqrt(1 - rho3Sq);
                if (eab == 0) {
                    e = 0;
                } else {
                    e = Math.exp(-ab / (1 + rho1)) / rho1 / eab;
                }
                mult = mult - Y[i] * Math.exp(-h5 / rho3Sq) * (e - 1 - c * rho3Sq);
            }
        }
        result = mult * rho2 * eab + NORMAL.getCDF(Math.min(a, b));
        if (rho < 0) {
            result = NORMAL.getCDF(a) - result;
        }
        return result;
    }
    ab = a * b;
    if (rho != 0) {
        for (int i = 0; i < 5; i++) {
            rho3 = rho * X[i];
            rho1 = 1 - rho3 * rho3;
            mult = mult + Y[i] * Math.exp((rho3 * ab - sumSq) / rho1) / Math.sqrt(rho1);
        }
    }
    return NORMAL.getCDF(a) * NORMAL.getCDF(b) + rho * mult;
}

From source file:com.opengamma.analytics.financial.model.volatility.smile.function.SVIVolatilityFunction.java

private double getVolatility(final double kappa, final SVIFormulaData data) {
    Validate.notNull(data, "null SVI parameters");
    final double d = kappa - data.getM();
    final double nu = data.getNu();
    return Math.sqrt(data.getA() + data.getB() * (data.getRho() * d + Math.sqrt(d * d + nu * nu)));
}