Example usage for org.apache.commons.math3.util Precision EPSILON

List of usage examples for org.apache.commons.math3.util Precision EPSILON

Introduction

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

Prototype

double EPSILON

To view the source code for org.apache.commons.math3.util Precision EPSILON.

Click Source Link

Document

Largest double-precision floating-point number such that 1 + EPSILON is numerically equal to 1.

Usage

From source file:com.clust4j.optimize.TestOptimizer.java

@Test
public void testBrentSimple() {
    BrentDownhillOptimizer optimizer = new BrentDownhillOptimizer(caller);
    double res = optimizer.optimize(); // optimize
    assertTrue(Precision.equals(res, 1.5, Precision.EPSILON));
    assertTrue(Precision.equals(optimizer.getFunctionResult(), -min_val, Precision.EPSILON));

    // if we try to "reoptimize" we get the same res:
    assertTrue(res == optimizer.optimize());
}

From source file:com.clust4j.optimize.TestOptimizer.java

@Test
public void testBrentWithBracket() {
    BrentDownhillOptimizer optimizer = new BrentDownhillOptimizer(caller, -3, -2);
    double res = optimizer.optimize(); // optimize
    assertTrue(Precision.equals(res, 1.5, Precision.EPSILON));
    assertTrue(optimizer.getNumFunctionCalls() == 5);
}

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

/**
 * If density of x is zero (x <= 0) then return small slightly greater than zero that has a non-zero density
 * @param x/*from   w  w w. j  ava  2  s.  c o m*/
 * @return
 */
public double nearestNonZero(double x) {
    return (x <= 0.0) ? Precision.EPSILON : x;
}

From source file:com.clust4j.algo.preprocess.PreProcessorTests.java

@Test
public void testMeanCenter() {
    final double[][] data = new double[][] { new double[] { 0.005, 0.182751, 0.1284 },
            new double[] { 3.65816, 0.29518, 2.123316 }, new double[] { 4.1234, 0.27395, 1.8900002 } };

    final Array2DRowRealMatrix d = new Array2DRowRealMatrix(data, false);
    MeanCenterer norm = new MeanCenterer().fit(d);
    RealMatrix scaled = norm.transform(d);
    double[][] operated = scaled.getData();

    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 0)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 1)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 2)), 0, Precision.EPSILON));

    // test copy/*from   w ww . j  a  v  a  2s  . c o m*/
    operated = norm.copy().transform(data);
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 0)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 1)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 2)), 0, Precision.EPSILON));

    // Test inverse transform.
    assertTrue(MatUtils.equalsWithTolerance(data, norm.inverseTransform(scaled).getData(), 1e-8));

    // copy coverage
    norm.copy();

    // test dim mismatch
    boolean a = false;
    try {
        norm.inverseTransform(TestSuite.getRandom(2, 2));
    } catch (DimensionMismatchException dim) {
        a = true;
    } finally {
        assertTrue(a);
    }

    // test not fit
    a = false;
    try {
        new MeanCenterer().transform(d);
    } catch (ModelNotFitException dim) {
        a = true;
    } finally {
        assertTrue(a);
    }
}

From source file:com.itemanalysis.psychometrics.uncmin.DefaultUncminOptimizer.java

/**
 * Wrapper for calling in Uncmin_f77.optif9_f77. It includes many of the default values from
 * Uncmin_f77.optif0_f77 but allows access to some additional options.
 *
 *
 * @param minclass a class that implements Uncmin_methods to provide the value of the objective funciton, gradientAt, and hessian.
 * @param initialValue initial value for the prameters. It should be an array of teh same size as the number of parameters.
 * @param analyticGradient true if analytic gradientAt is provided in minClass. False otherwise and will compute gradientAt numerically.
 * @param analyticHessian true if analytic Hessian is provided in minClass. False otherwise and will compute Hessian numerically.
 * @param maxIter maximum number of iterations.
 *///from   w  ww  .ja  v  a2s.c o m
public void minimize(Uncmin_methods minclass, double[] initialValue, boolean analyticGradient,
        boolean analyticHessian, int maxIter, double maxStep) throws UncminException {
    n = initialValue.length;//should be the actual size of the array, not arrySize+1. It will be resized later
    int np1 = n + 1;

    double dlt[] = new double[2];
    double fscale[] = new double[2];
    double stepmx[] = new double[2];
    double gradtl[] = new double[2];
    double steptl[] = new double[2];
    int ndigit[] = new int[2];
    int method[] = new int[2];
    int iexp[] = new int[2];
    int itnlim[] = new int[2];
    int iagflg[] = new int[2];
    int iahflg[] = new int[2];

    int msg[] = new int[2];
    itrmcd = new int[2];//termination code
    fpls = new double[2];//value of objective function

    //initial value array resized for arrays with a base index of 1.
    double[] x = new double[np1];
    xpls = new double[np1];
    for (int i = 0; i < n; i++) {
        x[i + 1] = initialValue[i];
        xpls[i + 1] = initialValue[i];
    }

    gpls = new double[np1];
    a = new double[np1][np1];
    udiag = new double[np1];

    //SET TYPICAL SIZE OF X AND MINIMIZATION FUNCTION
    double[] typsiz = new double[np1];
    for (int i = 1; i <= n; i++) {
        typsiz[i] = 1.0;
    }
    fscale[1] = 1.0;

    //SET FLAGS
    method[1] = 1;
    iexp[1] = 1;
    msg[1] = messageLevel;//See information below for details about this argument.
    ndigit[1] = -1;
    itnlim[1] = maxIter;//maximum number of iterations
    iagflg[1] = 0;//0 if numerical gradientAt, 1 if analytic gradientAt provided
    iahflg[1] = 0;//0 if use numerical Hessian, 1 if analytic Hessian provided

    if (analyticGradient)
        iagflg[1] = 1;
    if (analyticHessian)
        iahflg[1] = 1;

    // SET TOLERANCES
    double epsm = Precision.EPSILON;

    dlt[1] = -1.0;
    gradtl[1] = Math.pow(epsm, 1.0 / 3.0);
    steptl[1] = Math.sqrt(epsm);
    stepmx[1] = maxStep;//should be greater than 0. Values between 0.1 and 3 work well for IRT estimation (using 2).

    //For debugging
    //        optimizer.showDetails(true);

    optimizer.optif9_f77(n, x, minclass, typsiz, fscale, method, iexp, msg, ndigit, itnlim, iagflg, iahflg, dlt,
            gradtl, stepmx, steptl, xpls, fpls, gpls, itrmcd, a, udiag);

}

From source file:com.clust4j.utils.VectorTests.java

@Test
public void testVecOps() {
    // Inner//w  ww .j av  a2 s  .c  om
    final double[] a = new double[] { 1, 1, 1, 1 };
    final double[] b = new double[] { 1, 2, 3, 4 };
    assertTrue(VecUtils.innerProduct(a, b) == 10d);

    // Scalar mult
    final double[] c = new double[] { 2, 4, 6, 8 };
    assertTrue(VecUtils.equalsExactly(c, VecUtils.scalarMultiply(b, 2)));

    // Equals exactly and with tolerance
    assertTrue(VecUtils.equalsWithTolerance(c, VecUtils.scalarMultiply(b, 2)));
    assertTrue(VecUtils.equalsWithTolerance(c, VecUtils.scalarMultiply(b, 2), 0));

    // Mult
    final double[] d = new double[] { 2, 8, 18, 32 };
    assertTrue(VecUtils.equalsExactly(d, VecUtils.multiply(b, c)));

    // Scalar add
    final double[] scadd = new double[] { 3, 4, 5, 6 };
    assertTrue(VecUtils.equalsExactly(scadd, VecUtils.scalarAdd(b, 2)));

    // add
    final double[] add = new double[] { 2, 3, 4, 5 };
    assertTrue(VecUtils.equalsExactly(add, VecUtils.add(a, b)));

    // Scalar div
    final double[] scdiv = new double[] { 2, 2, 2, 2 };
    assertTrue(VecUtils.equalsExactly(b, VecUtils.scalarDivide(c, 2)));

    // div
    assertTrue(VecUtils.equalsExactly(scdiv, VecUtils.divide(c, b)));

    // Scalar sub
    final double[] scsub = new double[] { 0, 2, 4, 6 };
    assertTrue(VecUtils.equalsExactly(scsub, VecUtils.scalarSubtract(c, 2)));

    // sub
    assertTrue(VecUtils.equalsExactly(scsub, VecUtils.subtract(c, scdiv)));

    // Outer prod
    final double[] by = new double[] { 2, 3, 4 };
    final double[][] ab = VecUtils.outerProduct(b, by);

    assertTrue(ab.length == 4);
    assertTrue(VecUtils.equalsExactly(ab[0], by));
    assertTrue(VecUtils.equalsExactly(ab[1], new double[] { 4, 6, 8 }));
    assertTrue(VecUtils.equalsExactly(ab[2], new double[] { 6, 9, 12 }));
    assertTrue(VecUtils.equalsExactly(ab[3], new double[] { 8, 12, 16 }));

    // Abs
    final double[] neg = new double[] { -2, 3, -4 };
    assertTrue(VecUtils.equalsExactly(by, VecUtils.abs(neg)));
    assertTrue(VecUtils.l1Norm(neg) == 9);
    assertTrue(VecUtils.lpNorm(neg, 1) == 9);
    assertTrue(VecUtils.lpNorm(neg, 2) == VecUtils.l2Norm(neg));

    assertTrue(Precision.equals(VecUtils.lpNorm(neg, 2.0000000000000001), VecUtils.l2Norm(neg),
            Precision.EPSILON));
}

From source file:com.clust4j.algo.preprocess.PreProcessorTests.java

@Test
public void testMedianCenter() {
    final double[][] data = new double[][] { new double[] { 0.005, 0.182751, 0.1284 },
            new double[] { 3.65816, 0.29518, 2.123316 }, new double[] { 4.1234, 0.27395, 1.8900002 } };

    final Array2DRowRealMatrix d = new Array2DRowRealMatrix(data, false);
    MedianCenterer norm = new MedianCenterer().fit(d);
    RealMatrix scaled = norm.transform(d);
    double[][] operated = scaled.getData();

    assertTrue(Precision.equals(VecUtils.median(MatUtils.getColumn(operated, 0)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.median(MatUtils.getColumn(operated, 1)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.median(MatUtils.getColumn(operated, 2)), 0, Precision.EPSILON));

    // test copy//from  w ww . jav a2  s  .com
    operated = norm.copy().transform(data);
    assertTrue(Precision.equals(VecUtils.median(MatUtils.getColumn(operated, 0)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.median(MatUtils.getColumn(operated, 1)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.median(MatUtils.getColumn(operated, 2)), 0, Precision.EPSILON));

    // Test inverse transform.
    assertTrue(MatUtils.equalsWithTolerance(data, norm.inverseTransform(scaled).getData(), 1e-8));

    // copy coverage
    norm.copy();

    // test dim mismatch
    boolean a = false;
    try {
        norm.inverseTransform(TestSuite.getRandom(2, 2));
    } catch (DimensionMismatchException dim) {
        a = true;
    } finally {
        assertTrue(a);
    }

    // test not fit
    a = false;
    try {
        new MedianCenterer().transform(d);
    } catch (ModelNotFitException dim) {
        a = true;
    } finally {
        assertTrue(a);
    }
}

From source file:com.clust4j.utils.MatUtils.java

/**
 * Assess whether every element in the matrices are equal within 
 * a default tolerance of {@value Precision#EPSILON}
 * @param a/*from w  w  w. j av a 2 s .c  o m*/
 * @param b
 * @throws IllegalArgumentException if the matrix rows are empty
 * @throws DimensionMismatchException if the matrix dims don't match
 * @return true if all equal, false otherwise
 */
public static boolean equalsWithTolerance(final double[][] a, final double[][] b) {
    return equalsWithTolerance(a, b, Precision.EPSILON);
}

From source file:com.clust4j.utils.VecUtils.java

/**
 * Returns true if every element in the vector A
 * equals the corresponding element in the vector B within
 * a default tolerance of {@link Precision#EPSILON}
 * @param a/*from w ww. ja v  a2 s .  c  o  m*/
 * @param b
 * @throws DimensionMismatchException if the dims don't match
 * @return true if all equal, false otherwise
 */
public static boolean equalsWithTolerance(final double[] a, final double[] b) {
    return equalsWithTolerance(a, b, Precision.EPSILON);
}

From source file:com.clust4j.utils.VecUtils.java

/**
 * Assess whether two vectors are orthogonal, i.e., their inner product is 0.
 * @param a//w w  w .j a  v  a2  s.  c  o m
 * @param b
 * @throws DimensionMismatchException if the dims don't match
 * @return true if the inner product equals 0
 */
public static boolean isOrthogonalTo(final double[] a, final double[] b) {
    // Will auto determine whether parallel is necessary or allowed...
    return Precision.equals(innerProduct(a, b), 0, Precision.EPSILON);
}