public static void acceptOtherPositionWithProbability(double probability, Tree tree, Tree otherTree, KeanuRandom random) { if (random.nextDouble() < probability) { tree.acceptedPosition = otherTree.acceptedPosition; tree.gradientAtAcceptedPosition = otherTree.gradientAtAcceptedPosition; tree.logOfMasterPAtAcceptedPosition = otherTree.logOfMasterPAtAcceptedPosition; tree.sampleAtAcceptedPosition = otherTree.sampleAtAcceptedPosition; } }
private static int sample(double p, int n, KeanuRandom random) { int sum = 0; for (int i = 0; i < n; i++) { if (random.nextDouble() < p) { sum++; } } return sum; }
public double nextDoubleNonZero() { double randomValue; while ((randomValue = nextDouble()) == 0.0) ; return randomValue; }
private static double sample(double mu, double beta, KeanuRandom random) { if (beta <= 0.0) { throw new IllegalArgumentException("Invalid value for beta: " + beta); } if (random.nextDouble() > 0.5) { return mu + beta * Math.log(random.nextDouble()); } else { return mu - beta * Math.log(random.nextDouble()); } }
private static double sampleWhileKLessThanOne(double c, double k, double theta, KeanuRandom random) { while (true) { double p = c * random.nextDouble(); if (p > 1.) { double y = -log((c - p) / k); if (random.nextDouble() <= pow(y, k - 1.)) return theta * y; } else { double y = pow(p, 1. / k); if (random.nextDouble() <= exp(-y)) return theta * y; } } }
/** * @param lambda shape * @param random source of randomness * @return a random number from the Exponential distribution */ private static double exponentialSample(double lambda, KeanuRandom random) { if (lambda <= 0.0) { throw new IllegalArgumentException("Invalid value for b"); } return -lambda * Math.log(random.nextDouble()); }
@Override public ArithmeticDouble nextDouble(double min, double max) { double randomDouble = random.nextDouble(min, max); return new ArithmeticDouble(randomDouble); }
@Override public BooleanTensor sample(long[] shape, KeanuRandom random) { DoubleTensor uniforms = random.nextDouble(shape); return uniforms.lessThan(probTrue); }
public DoubleTensor nextDouble(long[] shape) { if (Arrays.equals(shape, Tensor.SCALAR_SHAPE)) { return new ScalarDoubleTensor(nextDouble()); } else { return new Nd4jDoubleTensor(doubleNextDouble(shape)); } }
@Override public DoubleTensor sample(long[] shape, KeanuRandom random) { return random.nextDouble(shape).timesInPlace(xMax.minus(xMin)).plusInPlace(xMin); }
@Before public void setup() { alwaysAccept = mock(KeanuRandom.class); when(alwaysAccept.nextDouble()).thenReturn(0.0); when(alwaysAccept.nextGaussian(any())).thenReturn(DoubleTensor.ZERO_SCALAR); alwaysReject = mock(KeanuRandom.class); when(alwaysReject.nextDouble()).thenReturn(1.0); when(alwaysReject.nextGaussian(any())).thenReturn(DoubleTensor.ZERO_SCALAR); }
@Test public void canSampleUniformScalar() { assertEquals(0, random.nextDouble(new long[0]).getRank()); assertEquals(1, random.nextDouble(new long[]{1}).getRank()); assertEquals(2, random.nextDouble(new long[]{1, 1}).getRank()); assertEquals(2, random.nextDouble(new long[]{2, 3}).getRank()); }
@Test public void randomNumberGenerationIsPlatformIndependent() { KeanuRandom keanuRandom = new KeanuRandom(1); assertEquals(keanuRandom.nextDouble(), new MersenneTwister(1L).nextDouble()); assertEquals(keanuRandom.nextDouble(), 0.41782887182714457, 1e-16); } }
@Override public DoubleTensor sample(long[] shape, KeanuRandom random) { Preconditions.checkArgument(scale.greaterThan(0.).allTrue(), "scale must be greater than 0. scale: " + scale); DoubleTensor unityCauchy = random.nextDouble(shape); return unityCauchy.minusInPlace(0.5).timesInPlace(Math.PI).tanInPlace().timesInPlace(scale).plusInPlace(location); }
@Override public DoubleTensor sample(long[] shape, KeanuRandom random) { return random.nextDouble(shape).reciprocalInPlace().minusInPlace(1).logInPlace().timesInPlace(mu.minus(s)); }
@Override public DoubleTensor sample(long[] shape, KeanuRandom random) { return random.nextDouble(shape).unaryMinusInPlace().plusInPlace(1.0).powInPlace(scale.reciprocal()) .reciprocalInPlace().timesInPlace(location); }
@Override public IntegerTensor sample(long[] shape, KeanuRandom random) { DoubleTensor minDouble = xMin.toDouble(); DoubleTensor delta = xMax.toDouble().minus(minDouble); DoubleTensor randoms = random.nextDouble(shape); return delta.timesInPlace(randoms).plusInPlace(minDouble).toInteger(); }
@Test public void ifYourRandomReturnsOneItSamplesFromTheLastCategory() { KeanuRandom mockRandomAlwaysZero = mock(KeanuRandom.class); when(mockRandomAlwaysZero.nextDouble()).thenReturn(1.); IntegerTensor n = IntegerTensor.scalar(100).reshape(1, 1); DoubleTensor p = DoubleTensor.create(0.1, 0.2, .3, 0.4).reshape(4, 1); Multinomial multinomial = Multinomial.withParameters(n, p); IntegerTensor samples = multinomial.sample(new long[]{1, 1}, mockRandomAlwaysZero); assertThat(samples, hasValue(0, 0, 0, 100)); }
@Test public void ifYourRandomReturnsZeroItSamplesFromTheFirstCategory() { KeanuRandom mockRandomAlwaysZero = mock(KeanuRandom.class); when(mockRandomAlwaysZero.nextDouble()).thenReturn(0.); IntegerTensor n = IntegerTensor.scalar(100).reshape(1, 1); DoubleTensor p = DoubleTensor.create(0.1, 0.2, .3, 0.4).reshape(4, 1); Multinomial multinomial = Multinomial.withParameters(n, p); IntegerTensor samples = multinomial.sample(new long[]{1, 1}, mockRandomAlwaysZero); assertThat(samples, hasValue(100, 0, 0, 0)); }
@Override public IntegerTensor sample(long[] shape, KeanuRandom random) { DoubleTensor numerator = random.nextDouble(shape).logInPlace(); DoubleTensor denominator = p.unaryMinus().plusInPlace(1.0).logInPlace(); return numerator.divInPlace(denominator).floorInPlace().toInteger().plusInPlace(1); }