@Override AbstractDistribution create(Random random) { return new Normal(0.1, 0.1, random); } };
@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()); } }
@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 testToString() { assertEquals("org.apache.mahout.math.jet.random.Normal(m=1.300000, sd=5.900000)", new Normal(1.3, 5.9, null).toString()); } }
@Test public void testSetState() throws Exception { Normal dist = new Normal(0, 1, RandomUtils.getRandom()); dist.setState(1.3, 5.9); DistributionChecks.checkDistribution(dist, breaks, 1.3, 5.9, 10000); }
@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); } }
@Override public AbstractContinousDistribution posteriorDistribution() { return new Normal(m, Math.sqrt(ss / n), gen); }
@Override public DistributionWithMean nextDistribution() { double mean = gen.nextDouble(); return new DistributionWithMean(new Normal(mean, sd, gen), mean); } }
private void init() { if (max <= min) { throw new IllegalArgumentException("Parameter max must be greater than min"); } if (max - min < 0.1 * sd) { throw new IllegalArgumentException("Value of max-min is too small, should be > 0.1 * sd"); } if (Double.isNaN(sd)) { sd = 1 / precision; } if (seed != Integer.MAX_VALUE) { rand = new Normal(mean, sd, new Random(seed)); } else { rand = new Normal(mean, sd, new Random()); } }
@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()); } }
@Test public void consistency() throws Exception { 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 testToString() { assertEquals("org.apache.mahout.math.jet.random.Normal(m=1.300000, sd=5.900000)", new Normal(1.3, 5.9, null).toString()); } }
@Test public void testSetState() throws Exception { Normal dist = new Normal(0, 1, RandomUtils.getRandom()); dist.setState(1.3, 5.9); DistributionChecks.checkDistribution(dist, breaks, 1.3, 5.9, 10000); }
@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); }
@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); } }