double b5 = 1.330274429; double t = 1 / (1 + b0 * a); return 1 - UNIT_NORMAL.pdf(a) * t * (b1 + t * (b2 + t * (b3 + t * (b4 + t * b5))));
/** * Returns a random number from the distribution. */ @Override public double nextDouble() { // Uses polar Box-Muller transformation. if (cacheFilled) { cacheFilled = false; return cache; } double x; double y; double r; do { x = 2.0 * randomDouble() - 1.0; y = 2.0 * randomDouble() - 1.0; r = x * x + y * y; } while (r >= 1.0); double z = Math.sqrt(-2.0 * Math.log(r) / r); cache = this.mean + this.standardDeviation * x * z; cacheFilled = true; return this.mean + this.standardDeviation * y * z; }
/** * @param mean The mean of the resulting distribution. * @param standardDeviation The standard deviation of the distribution. * @param randomGenerator The random number generator to use. This can be null if you don't * need to generate any numbers. */ public Normal(double mean, double standardDeviation, Random randomGenerator) { setRandomGenerator(randomGenerator); setState(mean, standardDeviation); }
@Override AbstractDistribution create(Random random) { return new Normal(0.1, 0.1, random); } };
@Test public void testEntropy() { Auc auc = new Auc(); Random gen = RandomUtils.getRandom(); Normal n0 = new Normal(-1, 1, gen); Normal n1 = new Normal(1, 1, gen); for (int i=0; i<100000; i++) { double score = n0.nextDouble(); double p = n1.pdf(score) / (n0.pdf(score) + n1.pdf(score)); auc.add(0, p); score = n1.nextDouble(); p = n1.pdf(score) / (n0.pdf(score) + n1.pdf(score)); auc.add(1, p); } Matrix m = auc.entropy(); assertEquals(-0.35, m.get(0, 0), 0.02); assertEquals(-2.36, m.get(0, 1), 0.02); assertEquals(-2.36, m.get(1, 0), 0.02); assertEquals(-0.35, m.get(1, 1), 0.02); } }
@Test public void testStdDev2() throws Exception { Path input = getTestTempFilePath("stdDev/counts.file"); Path output = getTestTempFilePath("stdDev/output.file"); FileSystem fs = FileSystem.get(input.toUri(), conf); SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, input, IntWritable.class, DoubleWritable.class); Random random = RandomUtils.getRandom(); Normal normal = new Normal(5, 3, random); for (int i = 0; i < 1000000; i++) { writer.append(new IntWritable(i), new DoubleWritable((long) normal.nextInt())); } writer.close(); double v = BasicStats.stdDev(input, output, conf); assertEquals(3, v, 0.02); }
/** * Returns a random number from the distribution. * * @return A new sample from this distribution. */ @Override public double nextDouble() { double variance = nextVariance(); double mean = nd.nextDouble() * Math.sqrt(variance / n) + m; return nd.nextDouble() * Math.sqrt(variance) + mean; }
@Test public void testCdf() { Random gen = RandomUtils.getRandom(); double offset = 0; double scale = 1; for (int k = 0; k < 20; k++) { Normal dist = new Normal(offset, scale, null); DistributionChecks.checkCdf(offset, scale, dist, breaks, quantiles); offset = gen.nextGaussian(); scale = Math.exp(3 * gen.nextGaussian()); } }
/** * @param mean The mean of the resulting distribution. * @param standardDeviation The standard deviation of the distribution. * @param randomGenerator The random number generator to use. This can be null if you don't * need to generate any numbers. */ public Normal(double mean, double standardDeviation, Random randomGenerator) { setRandomGenerator(randomGenerator); setState(mean, standardDeviation); }
@Override public JsonNode sample() { double x; do { x = rand.nextDouble(); } while (x < min || x > max); return new DoubleNode(x); } }
@Test public void consistency() { Random gen = RandomUtils.getRandom(); double offset = 0; double scale = 1; Normal dist = new Normal(offset, scale, RandomUtils.getRandom()); for (int k = 0; k < 20; k++) { dist.setState(offset, scale); DistributionChecks.checkDistribution(dist, breaks, offset, scale, 10000); offset = gen.nextGaussian(); scale = Math.exp(3 * gen.nextGaussian()); } }
@Test public void testNarrowNormal() { // this mixture of a uniform and normal distribution has a very narrow peak which is centered // near the median. Our system should be scale invariant and work well regardless. final Random gen = RandomUtils.getRandom(); AbstractContinousDistribution mix = new AbstractContinousDistribution() { AbstractContinousDistribution normal = new Normal(0, 1e-5, gen); AbstractContinousDistribution uniform = new Uniform(-1, 1, gen); @Override public double nextDouble() { double x; if (gen.nextDouble() < 0.5) { x = uniform.nextDouble(); } else { x = normal.nextDouble(); } return x; } }; for (int i = 0; i < repeats(); i++) { runTest(mix, 100, new double[]{0.001, 0.01, 0.1, 0.3, 0.5, 0.7, 0.9, 0.99, 0.999}, "mixture", false, gen); } }
/** * @param mean The mean of the resulting distribution. * @param standardDeviation The standard deviation of the distribution. * @param randomGenerator The random number generator to use. This can be null if you don't * need to generate any numbers. */ public Normal(double mean, double standardDeviation, Random randomGenerator) { setRandomGenerator(randomGenerator); setState(mean, standardDeviation); }
double b5 = 1.330274429; double t = 1 / (1 + b0 * a); return 1 - UNIT_NORMAL.pdf(a) * t * (b1 + t * (b2 + t * (b3 + t * (b4 + t * b5))));
@Override public double nextMean() { double sd = Math.sqrt(nextVariance() / n); return nd.nextDouble() * sd + m; }
/** * Returns a random number from the distribution. */ @Override public double nextDouble() { // Uses polar Box-Muller transformation. if (cacheFilled) { cacheFilled = false; return cache; } double x; double y; double r; do { x = 2.0 * randomDouble() - 1.0; y = 2.0 * randomDouble() - 1.0; r = x * x + y * y; } while (r >= 1.0); double z = Math.sqrt(-2.0 * Math.log(r) / r); cache = this.mean + this.standardDeviation * x * z; cacheFilled = true; return this.mean + this.standardDeviation * y * z; }