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:cz.cuni.mff.d3s.spl.interpretation.MannWhitneyInterpretation.java

/** {@inheritDoc} */
@Override/*  w  w w  .  j a va 2  s  .  c o  m*/
public ComparisonResult compare(DataSnapshot left, DataSnapshot right) {
    double[] leftSamples = mergeSamples(left);
    double[] rightSamples = mergeSamples(right);

    double uStatMax = utest.mannWhitneyU(leftSamples, rightSamples);

    long lengthsMultiplied = (long) leftSamples.length * rightSamples.length;

    double uStatMin = lengthsMultiplied - uStatMax;

    /* https://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U_test#Normal_approximation */
    double meanU = lengthsMultiplied / 2.0;
    double varU = lengthsMultiplied * (leftSamples.length + rightSamples.length + 1) / 12.0;

    double z = (uStatMin - meanU) / Math.sqrt(varU);

    NormalDistribution distribution = new NormalDistribution(0.0, 1.0);

    return new DistributionBasedComparisonResult(z, distribution);
}

From source file:com.jaxzin.iraf.demo.Main.java

private static IntervalXYDataset createData(final int count) {
    final HistogramDataset data = new HistogramDataset();
    data.setType(HistogramType.SCALE_AREA_TO_1);
    data.addSeries("freq", createNormalDist(count, 0), (int) Math.round(Math.sqrt(count)));
    data.addSeries("rel_freq", createNormalDist(count, 2), (int) Math.round(Math.sqrt(count)));
    data.addSeries("area", createNormalDist(count, -2), (int) Math.round(Math.sqrt(count)));
    return data;//from   w  ww  . j a v a 2  s.c  o  m
}

From source file:com.opengamma.analytics.financial.timeseries.analysis.SampleAutocorrelationIIDHypothesis.java

@Override
public boolean testIID(final DoubleTimeSeries<?> x) {
    Validate.notNull(x, "x");
    if (x.size() < _h) {
        throw new IllegalArgumentException("Time series must have at least " + _h + " points");
    }//from w w  w.  j  a v  a 2 s.  c om
    final double[] autocorrelations = _calculator.evaluate(x);
    final double upper = _criticalValue / Math.sqrt(x.size());
    final double lower = -upper;
    double violations = 0;
    double value;
    for (int i = 1; i < _h; i++) {
        value = autocorrelations[i];
        if (value > upper || value < lower) {
            violations++;
        }
    }
    if (violations / _h > _level) {
        return false;
    }
    return true;
}

From source file:dr.math.distributions.NegativeBinomialDistribution.java

public double quantile(double y) {
    // TB - I'm having trouble implementing this
    // LM - A first stab using simple minimisation to invert the function (under absolute loss)
    // Implementation based on the qnbinom.c function used in R
    final double stdev = Math.sqrt(mean + (mean * mean * alpha));
    final double r = -1 * (mean * mean) / (mean - stdev * stdev);
    final double p = mean / (stdev * stdev);
    final double prob = y;

    final double Q = 1.0 / p;
    final double P = (1.0 - p) * Q;
    final double gamma = (Q + P) / stdev;
    final double z = Math.sqrt(2.0) * ErrorFunction.inverseErf(2.0 * y - 1.0);
    final double crudeY = mean + stdev * (z + gamma * (z * z - 1) / 6);

    UnivariateFunction f = new UnivariateFunction() {
        double tent = Double.NaN;

        public double evaluate(final double argument) {
            try {
                tent = Beta.regularizedBeta(p, r, argument + 1);
            } catch (MathException e) {
                return Double.NaN;
            }// ww w.  j ava  2 s .  co  m
            double score = Math.abs(tent - prob);
            return score;
        }

        public int getNumArguments() {
            return 1;
        }

        public double getLowerBound() { // 20% window should cut it. Probably too large even...
            return Math.min(crudeY - .2 * crudeY, 0);
        }

        public double getUpperBound() {
            return crudeY + .2 * crudeY;
        }
    };
    UnivariateMinimum minimum = new UnivariateMinimum();
    double q = minimum.findMinimum(f);
    return Math.ceil(q);
}

From source file:bide.math.NormalDistribution.java

/**
  * quantiles (=inverse cumulative density function)
  *//  ww  w.jav  a 2  s  . c om
  * @param z  argument
  * @param m  mean
  * @param sd standard deviation
  * @return icdf at z
  */
public static double quantile(double z, double m, double sd) {
    return m + Math.sqrt(2.0) * sd * ErrorFunction.inverseErf(2.0 * z - 1.0);
}

From source file:net.adamjak.thomas.graph.application.commons.StatisticsUtils.java

public static double getConfidenceInterval(DescriptiveStatistics inputStatistics, NormCritical uAlpha)
        throws IllegalArgumentException {
    if (inputStatistics == null || uAlpha == null)
        throw new IllegalArgumentException("Params inputStatistics or uAlpha can not be null!");

    return (inputStatistics.getStandardDeviation() * uAlpha.getCriticalValue())
            / Math.sqrt(inputStatistics.getValues().length);
}

From source file:net.gtaun.shoebill.data.Vector3D.java

public float distance(Vector3D pos) {
    float dx = pos.getX() - getX(), dy = pos.getY() - getY(), dz = pos.getZ() - getZ();
    return (float) Math.sqrt(dx * dx + dy * dy + dz * dz);
}

From source file:beast.math.distributions.NormalDistribution.java

/**
 * probability density function//  w w  w. j a  v  a2 s. c  o  m
 *
 * @param x  argument
 * @param m  mean
 * @param sd standard deviation
 * @return pdf at x
 */
public static double pdf(double x, double m, double sd) {
    double a = 1.0 / (Math.sqrt(2.0 * Math.PI) * sd);
    double b = -(x - m) * (x - m) / (2.0 * sd * sd);

    return a * Math.exp(b);
}

From source file:com.joptimizer.util.CholeskyFactorization.java

/**
 * Cholesky factorization L of psd matrix, Q = L.LT
 *//*from   w w  w. j  a va2  s. c om*/
private void factorize() throws Exception {
    if (!MatrixUtils.isSymmetric(new Array2DRowRealMatrix(Q), Utils.getDoubleMachineEpsilon())) {
        throw new Exception("Matrix is not symmetric");
    }

    int N = Q.length;
    double[][] L = new double[N][N];
    this.eigenvalues = new ArrayList<Double>();

    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= i; j++) {
            double sum = 0.0;
            for (int k = 0; k < j; k++) {
                sum += L[i][k] * L[j][k];
            }
            if (i == j) {
                double d = Math.sqrt(Q[i][i] - sum);
                if (Double.isNaN(d) || d * d < Utils.getDoubleMachineEpsilon()) {//d*d is a Q's eigenvalue
                    Log.w(MainActivity.JOPTIMIZER_LOGTAG, "Not positive eigenvalues: " + d * d);
                    throw new Exception("not positive definite matrix");
                }
                L[i][i] = d;
                this.eigenvalues.add(this.eigenvalues.size(), d * d);
            } else {
                L[i][j] = 1.0 / L[j][j] * (Q[i][j] - sum);
            }
        }
    }

    this.L = L;
}

From source file:com.opengamma.analytics.financial.timeseries.util.TimeSeriesWeightedVolatilityOperator.java

@Override
public DateDoubleTimeSeries<?> evaluate(DateDoubleTimeSeries<?> ts) {
    Validate.notNull(ts, "time series");
    Validate.isTrue(ts.size() > 1, "time series length must be > 1");
    DateDoubleTimeSeries<?> percentageChangeSeries = PERCENTAGE_CHANGE.evaluate(ts);
    int n = percentageChangeSeries.size();
    double[] weightedVariances = new double[n];
    double[] weightedVolatilities = new double[n];
    double oldestPercentageChange = percentageChangeSeries.getEarliestValueFast();
    weightedVariances[0] = oldestPercentageChange * oldestPercentageChange;
    weightedVolatilities[0] = Math.abs(oldestPercentageChange);
    for (int i = 1; i < n; i++) {
        double percentageChange = percentageChangeSeries.getValueAtIndexFast(i);
        weightedVariances[i] = ((1 - _lambda) * percentageChange * percentageChange)
                + (_lambda * weightedVariances[i - 1]);
        weightedVolatilities[i] = Math.sqrt(weightedVariances[i]);
    }/*from www  .  j a  v  a2 s .com*/

    return ImmutableLocalDateDoubleTimeSeries.of(percentageChangeSeries.timesArrayFast(), weightedVolatilities);
}