Example usage for org.apache.commons.math3.util MathArrays copyOf

List of usage examples for org.apache.commons.math3.util MathArrays copyOf

Introduction

In this page you can find the example usage for org.apache.commons.math3.util MathArrays copyOf.

Prototype

public static double[] copyOf(double[] source) 

Source Link

Document

Creates a copy of the source array.

Usage

From source file:de.bund.bfr.math.LinearFunction.java

public LinearFunction(double[] x, double[] y) throws NullArgumentException, NoDataException,
        DimensionMismatchException, NonMonotonicSequenceException {
    if (x == null || y == null) {
        throw new NullArgumentException();
    }/*ww w  . j  a v  a2 s .  c o m*/
    if (x.length == 0 || y.length == 0) {
        throw new NoDataException();
    }
    if (y.length != x.length) {
        throw new DimensionMismatchException(y.length, x.length);
    }
    MathArrays.checkOrder(x);

    abscissa = MathArrays.copyOf(x);
    ordinate = MathArrays.copyOf(y);
}

From source file:MultivariateNormalDistribution.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix.//from  ww w. j a v  a2  s .c  o m
 * <br/>
 * The number of dimensions is equal to the length of the mean vector
 * and to the number of rows and columns of the covariance matrix.
 * It is frequently written as "p" in formulae.
 *
 * @param rng Random Number Generator.
 * @param means Vector of means.
 * @param covariances Covariance matrix.
 * @throws DimensionMismatchException if the arrays length are
 * inconsistent.
 * @throws SingularMatrixException if the eigenvalue decomposition cannot
 * be performed on the provided covariance matrix.
 * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is
 * negative.
 */
public MultivariateNormalDistribution(RandomGenerator rng, final double[] means, final double[][] covariances)
        throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException {
    super(rng, means.length);

    final int dim = means.length;

    if (covariances.length != dim) {
        throw new DimensionMismatchException(covariances.length, dim);
    }

    for (int i = 0; i < dim; i++) {
        if (dim != covariances[i].length) {
            throw new DimensionMismatchException(covariances[i].length, dim);
        }
    }

    this.means = MathArrays.copyOf(means);

    covarianceMatrix = new Array2DRowRealMatrix(covariances);

    // Covariance matrix eigen decomposition.
    final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix);

    // Compute and store the inverse.
    covarianceMatrixInverse = covMatDec.getSolver().getInverse();
    // Compute and store the determinant.
    covarianceMatrixDeterminant = covMatDec.getDeterminant();

    // Eigenvalues of the covariance matrix.
    final double[] covMatEigenvalues = covMatDec.getRealEigenvalues();

    for (int i = 0; i < covMatEigenvalues.length; i++) {
        if (covMatEigenvalues[i] < 0) {
            throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0);
        }
    }

    // Matrix where each column is an eigenvector of the covariance matrix.
    final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim);
    for (int v = 0; v < dim; v++) {
        final double[] evec = covMatDec.getEigenvector(v).toArray();
        covMatEigenvectors.setColumn(v, evec);
    }

    final RealMatrix tmpMatrix = covMatEigenvectors.transpose();

    // Scale each eigenvector by the square root of its eigenvalue.
    for (int row = 0; row < dim; row++) {
        final double factor = FastMath.sqrt(covMatEigenvalues[row]);
        for (int col = 0; col < dim; col++) {
            tmpMatrix.multiplyEntry(row, col, factor);
        }
    }

    samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
}

From source file:MultivariateNormalDistribution.java

/**
 * Gets the mean vector.
 *
 * @return the mean vector.
 */
public double[] getMeans() {
    return MathArrays.copyOf(means);
}

From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java

/**
 * Computes the <i>p-value</i>, or <i>observed significance level</i>, of a two-sample <a
 * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"> Kolmogorov-Smirnov test</a>
 * evaluating the null hypothesis that {@code x} and {@code y} are samples drawn from the same
 * probability distribution. Specifically, what is returned is an estimate of the probability
 * that the {@link #kolmogorovSmirnovStatistic(double[], double[])} associated with a randomly
 * selected partition of the combined sample into subsamples of sizes {@code x.length} and
 * {@code y.length} will strictly exceed (if {@code strict} is {@code true}) or be at least as
 * large as {@code strict = false}) as {@code kolmogorovSmirnovStatistic(x, y)}.
 * <ul>//www .j  ava  2  s .c o  m
 * <li>For small samples (where the product of the sample sizes is less than
 * {@value #LARGE_SAMPLE_PRODUCT}), the exact p-value is computed using the method presented
 * in [4], implemented in {@link #exactP(double, int, int, boolean)}. </li>
 * <li>When the product of the sample sizes exceeds {@value #LARGE_SAMPLE_PRODUCT}, the
 * asymptotic distribution of \(D_{n,m}\) is used. See {@link #approximateP(double, int, int)}
 * for details on the approximation.</li>
 * </ul><p>
 * If {@code x.length * y.length} < {@value #LARGE_SAMPLE_PRODUCT} and the combined set of values in
 * {@code x} and {@code y} contains ties, random jitter is added to {@code x} and {@code y} to
 * break ties before computing \(D_{n,m}\) and the p-value. The jitter is uniformly distributed
 * on (-minDelta / 2, minDelta / 2) where minDelta is the smallest pairwise difference between
 * values in the combined sample.</p>
 * <p>
 * If ties are known to be present in the data, {@link #bootstrap(double[], double[], int, boolean)}
 * may be used as an alternative method for estimating the p-value.</p>
 *
 * @param x first sample dataset
 * @param y second sample dataset
 * @param strict whether or not the probability to compute is expressed as a strict inequality
 *        (ignored for large samples)
 * @return p-value associated with the null hypothesis that {@code x} and {@code y} represent
 *         samples from the same distribution
 * @throws InsufficientDataException if either {@code x} or {@code y} does not have length at
 *         least 2
 * @throws NullArgumentException if either {@code x} or {@code y} is null
 * @see #bootstrap(double[], double[], int, boolean)
 */
public double kolmogorovSmirnovTest(double[] x, double[] y, boolean strict) {
    final long lengthProduct = (long) x.length * y.length;
    double[] xa = null;
    double[] ya = null;
    if (lengthProduct < LARGE_SAMPLE_PRODUCT && hasTies(x, y)) {
        xa = MathArrays.copyOf(x);
        ya = MathArrays.copyOf(y);
        fixTies(xa, ya);
    } else {
        xa = x;
        ya = y;
    }
    if (lengthProduct < LARGE_SAMPLE_PRODUCT) {
        return exactP(kolmogorovSmirnovStatistic(xa, ya), x.length, y.length, strict);
    }
    return approximateP(kolmogorovSmirnovStatistic(x, y), x.length, y.length);
}

From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java

public double kolmogorovSmirnovTestFlag(double[] x, double[] y, boolean strict) {
    final long lengthProduct = (long) x.length * y.length;
    double[] xa = null;
    double[] ya = null;
    if (lengthProduct < LARGE_SAMPLE_PRODUCT && hasTies(x, y)) {
        xa = MathArrays.copyOf(x);
        ya = MathArrays.copyOf(y);//  w  w  w  .j a v  a2s.c o  m
        fixTies(xa, ya);
    } else {
        xa = x;
        ya = y;
    }
    if (lengthProduct < LARGE_SAMPLE_PRODUCT) {
        return exactP(kolmogorovSmirnovStatisticFlag(xa, ya), x.length, y.length, strict);
    }
    return approximateP(kolmogorovSmirnovStatisticFlag(x, y), x.length, y.length);
}

From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java

/**
 * Computes the two-sample Kolmogorov-Smirnov test statistic, \(D_{n,m}=\sup_x |F_n(x)-F_m(x)|\)
 * where \(n\) is the length of {@code x}, \(m\) is the length of {@code y}, \(F_n\) is the
 * empirical distribution that puts mass \(1/n\) at each of the values in {@code x} and \(F_m\)
 * is the empirical distribution of the {@code y} values. Finally \(n m D_{n,m}\) is returned
 * as long value. /* ww  w . ja  v  a  2 s .c o  m*/
 *
 * Implementation added to indicate whether the supremum was gained from a positive or negative 
 * (negFlag =  0 or 1 respectively) difference
 *
 * @param x first sample
 * @param y second sample
 * @return test statistic \(n m D_{n,m}\) used to evaluate the null hypothesis that {@code x} and
 *         {@code y} represent samples from the same underlying distribution
 * @throws InsufficientDataException if either {@code x} or {@code y} does not have length at
 *         least 2
 * @throws NullArgumentException if either {@code x} or {@code y} is null
 */
private long integralKolmogorovSmirnovStatisticFlag(double[] x, double[] y) {
    checkArray(x);
    checkArray(y);
    // Copy and sort the sample arrays
    final double[] sx = MathArrays.copyOf(x);
    final double[] sy = MathArrays.copyOf(y);
    Arrays.sort(sx);
    Arrays.sort(sy);

    final int n = sx.length;
    final int m = sy.length;

    int rankX = 0;
    int rankY = 0;
    long curD = 0l;
    negFlag = 0;
    // Find the max difference between cdf_x and cdf_y
    long supD = 0l;
    do {
        double z = Double.compare(sx[rankX], sy[rankY]) <= 0 ? sx[rankX] : sy[rankY];
        while (rankX < n && Double.compare(sx[rankX], z) == 0) {
            rankX += 1;
            curD += m;
        }
        while (rankY < m && Double.compare(sy[rankY], z) == 0) {
            rankY += 1;
            curD -= n;
        }
        if (curD > supD) {
            negFlag = 0;
            supD = curD;
        }
        //the current largest difference is -ve and hence y lies left of x
        else if (-curD > supD) {
            negFlag = 1;
            supD = -curD;
        }
    } while (rankX < n && rankY < m);
    return supD;
}

From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java

private long integralKolmogorovSmirnovStatistic(double[] x, double[] y) {
    checkArray(x);//from   w  ww . j av  a2s.  c  o  m
    checkArray(y);
    // Copy and sort the sample arrays
    final double[] sx = MathArrays.copyOf(x);
    final double[] sy = MathArrays.copyOf(y);
    Arrays.sort(sx);
    Arrays.sort(sy);
    final int n = sx.length;
    final int m = sy.length;

    int rankX = 0;
    int rankY = 0;
    long curD = 0l;

    // Find the max difference between cdf_x and cdf_y
    long supD = 0l;
    do {
        double z = Double.compare(sx[rankX], sy[rankY]) <= 0 ? sx[rankX] : sy[rankY];
        while (rankX < n && Double.compare(sx[rankX], z) == 0) {
            rankX += 1;
            curD += m;
        }
        while (rankY < m && Double.compare(sy[rankY], z) == 0) {
            rankY += 1;
            curD -= n;
        }
        if (curD > supD) {
            supD = curD;
        } else if (-curD > supD) {
            supD = -curD;
        }
    } while (rankX < n && rankY < m);
    return supD;
}

From source file:org.pmad.gmm.MyMND.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix.//from w  w w .  j av  a  2s  .  c  o m
 * <br/>
 * The number of dimensions is equal to the length of the mean vector
 * and to the number of rows and columns of the covariance matrix.
 * It is frequently written as "p" in formulae.
 *
 * @param rng Random Number Generator.
 * @param means Vector of means.
 * @param covariances Covariance matrix.
 * @throws DimensionMismatchException if the arrays length are
 * inconsistent.
 * @throws SingularMatrixException if the eigenvalue decomposition cannot
 * be performed on the provided covariance matrix.
 * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is
 * negative.
 */
public MyMND(RandomGenerator rng, final double[] means, final double[][] covariances)
        throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException {
    super(rng, means.length);

    final int dim = means.length;

    if (covariances.length != dim) {
        throw new DimensionMismatchException(covariances.length, dim);
    }

    for (int i = 0; i < dim; i++) {
        if (dim != covariances[i].length) {
            throw new DimensionMismatchException(covariances[i].length, dim);
        }
    }

    this.means = MathArrays.copyOf(means);
    double msum = 0;
    for (int i = 0; i < covariances.length; i++) {
        for (int j = 0; j < covariances.length; j++) {
            msum += covariances[i][j];
        }
    }
    msum /= covariances.length * covariances.length;
    //        System.out.print("in");
    MyEDC covMatDec = null;
    double a = -1;
    while (true) {
        try {

            covarianceMatrix = new Array2DRowRealMatrix(covariances);

            covMatDec = new MyEDC(covarianceMatrix);

            // Compute and store the inverse.
            covarianceMatrixInverse = covMatDec.getSolver().getInverse();
            a *= -1;
            break;
        } catch (NoDataException e) {
            e.printStackTrace();
        } catch (NullArgumentException e) {
            e.printStackTrace();
        } catch (MathArithmeticException e) {
            e.printStackTrace();
        } catch (SingularMatrixException e) {
            //            System.out.print("S");
            for (int i = 0; i < covariances.length; i++) {
                double add = covariances[i][i] == 0 ? msum : covariances[i][i];
                covariances[i][i] += new Random().nextDouble() * add * 0.01;
            }
        }
        //         catch (MaxCountExceededException e) {
        ////            e.printStackTrace();
        ////            System.out.print("M"+msum);
        //            for (int i = 0; i < covariances.length; i++) {
        //               for (int j = i; j < covariances.length; j++) {
        //                  double add = covariances[i][j] == 0?msum:covariances[i][j];
        //                  add = new Random().nextDouble()*add*0.1*a;
        //                  covariances[i][j] += add;
        //                  covariances[j][i] += add;
        //               }
        //            }
        ////            break;
        //         }
    }
    // Compute and store the determinant.
    covarianceMatrixDeterminant = covMatDec.getDeterminant();

    // Eigenvalues of the covariance matrix.
    final double[] covMatEigenvalues = covMatDec.getRealEigenvalues();

    for (int i = 0; i < covMatEigenvalues.length; i++) {
        if (covMatEigenvalues[i] < 0) {
            throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0);
        }
    }

    // Matrix where each column is an eigenvector of the covariance matrix.
    final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim);
    for (int v = 0; v < dim; v++) {
        final double[] evec = covMatDec.getEigenvector(v).toArray();
        covMatEigenvectors.setColumn(v, evec);
    }

    final RealMatrix tmpMatrix = covMatEigenvectors.transpose();

    // Scale each eigenvector by the square root of its eigenvalue.
    for (int row = 0; row < dim; row++) {
        final double factor = FastMath.sqrt(covMatEigenvalues[row]);
        for (int col = 0; col < dim; col++) {
            tmpMatrix.multiplyEntry(row, col, factor);
        }
    }

    samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
}

From source file:xyz.lejon.sampling.FastMultivariateNormalDistribution.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix.//w w w. j a v a 2  s.  c o m
 * <br/>
 * The number of dimensions is equal to the length of the mean vector
 * and to the number of rows and columns of the covariance matrix.
 * It is frequently written as "p" in formulae.
 *
 * @param rng Random Number Generator.
 * @param means Vector of means.
 * @param covariances Covariance matrix.
 * @throws DimensionMismatchException if the arrays length are
 * inconsistent.
 * @throws SingularMatrixException if the eigenvalue decomposition cannot
 * be performed on the provided covariance matrix.
 * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is
 * negative.
 */
public FastMultivariateNormalDistribution(RandomGenerator rng, final double[] means,
        final double[][] covariances)
        throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException {
    super(rng, means.length);

    final int dim = means.length;

    if (covariances.length != dim) {
        throw new DimensionMismatchException(covariances.length, dim);
    }

    for (int i = 0; i < dim; i++) {
        if (dim != covariances[i].length) {
            throw new DimensionMismatchException(covariances[i].length, dim);
        }
    }

    this.means = MathArrays.copyOf(means);

    covarianceMatrix = new Array2DRowRealMatrix(covariances);

    // Covariance matrix eigen decomposition.
    final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix);

    // Compute and store the inverse.
    covarianceMatrixInverse = covMatDec.getSolver().getInverse();
    // Compute and store the determinant.
    covarianceMatrixDeterminant = covMatDec.getDeterminant();

    // Eigenvalues of the covariance matrix.
    final double[] covMatEigenvalues = covMatDec.getRealEigenvalues();

    for (int i = 0; i < covMatEigenvalues.length; i++) {
        if (covMatEigenvalues[i] < 0) {
            throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0);
        }
    }

    // Matrix where each column is an eigenvector of the covariance matrix.
    final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim);
    final Array2DRowRealMatrix tmpMatrix = new Array2DRowRealMatrix(dim, dim);
    for (int v = 0; v < dim; v++) {
        final double factor = FastMath.sqrt(covMatEigenvalues[v]);
        final double[] evec = covMatDec.getEigenvector(v).toArray();
        covMatEigenvectors.setColumn(v, evec);
        tmpMatrix.setRow(v, evec);
        for (int col = 0; col < dim; col++) {
            tmpMatrix.multiplyEntry(v, col, factor);
        }
    }

    samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
}