/** * Creates an instance. * * @param mean the mean * @param sigma the sigma */ public NormalRandomNumberGenerator(double mean, double sigma) { ArgChecker.notNegativeOrZero(sigma, "standard deviation"); this.normal = new NormalDistribution(mean, sigma); }
private double getFraserApproxCDF(double x) { double s = Math.sqrt(_lambdaOverTwo * 2.0); double mu = Math.sqrt(x); double z; if (Double.doubleToLongBits(mu) == Double.doubleToLongBits(s)) { z = (1 - _dofOverTwo * 2.0) / 2 / s; } else { z = mu - s - (_dofOverTwo * 2.0 - 1) / 2 * (Math.log(mu) - Math.log(s)) / (mu - s); } return (new NormalDistribution(0, 1)).getCDF(z); }
public void testObject() { NormalDistribution other = new NormalDistribution(0, 1, ENGINE); assertEquals(NORMAL, other); assertEquals(NORMAL.hashCode(), other.hashCode()); other = new NormalDistribution(0, 1); assertEquals(NORMAL, other); assertEquals(NORMAL.hashCode(), other.hashCode()); other = new NormalDistribution(0.1, 1, ENGINE); assertFalse(NORMAL.equals(other)); other = new NormalDistribution(0, 1.1, ENGINE); assertFalse(NORMAL.equals(other)); } }
public void testFit() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final double[] y = new double[Y.length]; for (int i = 0; i < Y.length; i++) { y[i] = Y[i] + SIGMA[i] * NORMAL.nextRandom(); } final LeastSquareResults results = gls.solve(X, y, SIGMA, SIN_FUNCTIONS); assertTrue(results.getChiSq() < 3 * Y.length); }
public void solveRandomNoiseTest() { final MatrixAlgebra ma = new OGMatrixAlgebra(); final double[] y = new double[20]; for (int i = 0; i < 20; i++) { y[i] = Y.get(i) + SIGMA.get(i) * NORMAL.nextRandom(); } final DoubleArray start = DoubleArray.of(0.7, 1.4, 0.2, -0.3); final NonLinearLeastSquare ls = new NonLinearLeastSquare(); final LeastSquareResults res = ls.solve(X, DoubleArray.copyOf(y), SIGMA, PARAM_FUNCTION, PARAM_GRAD, start); final double chiSqDoF = res.getChiSq() / 16; assertTrue(chiSqDoF > 0.25); assertTrue(chiSqDoF < 3.0); final DoubleArray trueValues = DoubleArray.of(1, 1, 0, 0); final DoubleArray delta = (DoubleArray) ma.subtract(res.getFitParameters(), trueValues); final LUDecompositionCommons decmp = new LUDecompositionCommons(); final LUDecompositionResult decmpRes = decmp.apply(res.getCovariance()); final DoubleMatrix invCovariance = decmpRes.solve(DoubleMatrix.identity(4)); double z = ma.getInnerProduct(delta, ma.multiply(invCovariance, delta)); z = Math.sqrt(z); assertTrue(z < 3.0); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeSigmaDistribution() { new NormalDistribution(1, -0.4); }
public void smallPertubationTest() { final MatrixAlgebra ma = new OGMatrixAlgebra(); final double[] dy = new double[20]; for (int i = 0; i < 20; i++) { dy[i] = 0.1 * SIGMA.get(i) * NORMAL.nextRandom(); } final DoubleArray deltaY = DoubleArray.copyOf(dy); final DoubleArray solution = DoubleArray.of(1.0, 1.0, 0.0, 0.0); final NonLinearLeastSquare ls = new NonLinearLeastSquare(); final DoubleMatrix res = ls.calInverseJacobian(SIGMA, FUNCTION, GRAD, solution); final DoubleArray deltaParms = (DoubleArray) ma.multiply(res, deltaY); final DoubleArray y = (DoubleArray) ma.add(Y, deltaY); final LeastSquareResults lsRes = ls.solve(X, y, SIGMA, PARAM_FUNCTION, PARAM_GRAD, solution); final DoubleArray trueDeltaParms = (DoubleArray) ma.subtract(lsRes.getFitParameters(), solution); assertEquals(trueDeltaParms.get(0), deltaParms.get(0), 5e-5); assertEquals(trueDeltaParms.get(1), deltaParms.get(1), 5e-5); assertEquals(trueDeltaParms.get(2), deltaParms.get(2), 5e-5); assertEquals(trueDeltaParms.get(3), deltaParms.get(3), 5e-5); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullEngine() { new NormalDistribution(0, 1, null); }
/** * Creates an instance. * * @param mean the mean * @param sigma the sigma * @param engine the random number engine */ public NormalRandomNumberGenerator(double mean, double sigma, RandomEngine engine) { ArgChecker.notNegativeOrZero(sigma, "standard deviation"); ArgChecker.notNull(engine, "engine"); this.normal = new NormalDistribution(mean, sigma, engine); }
@Test public void testNormal() { final ProbabilityDistribution<Double> highDOF = new StudentTDistribution(1000000, ENGINE); final ProbabilityDistribution<Double> normal = new NormalDistribution(0, 1, ENGINE); final double eps = 1e-4; double x; for (int i = 0; i < 100; i++) { x = RANDOM.nextDouble(); assertEquals(highDOF.getCDF(x), normal.getCDF(x), eps); assertEquals(highDOF.getPDF(x), normal.getPDF(x), eps); assertEquals(highDOF.getInverseCDF(x), normal.getInverseCDF(x), eps); } }