public Model(Data data) { this.data = data; KeanuRandom.setDefaultRandomSeed(1); }
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; } }
/** * @param model network for which to choose sampling algorithm. * @return recommended sampling algorithm for this network. */ public PosteriorSamplingAlgorithm withDefaultConfigFor(KeanuProbabilisticModel model) { return withDefaultConfigFor(model, KeanuRandom.getDefaultRandom()); }
public DoubleTensor nextDouble(long[] shape) { if (Arrays.equals(shape, Tensor.SCALAR_SHAPE)) { return new ScalarDoubleTensor(nextDouble()); } else { return new Nd4jDoubleTensor(doubleNextDouble(shape)); } }
public DoubleTensor nextGaussian(long[] shape) { if (Arrays.equals(shape, Tensor.SCALAR_SHAPE)) { return new ScalarDoubleTensor(nextGaussian()); } else { return new Nd4jDoubleTensor(doubleNextGaussian(shape)); } }
@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); }
@Before public void setup() { random = new KeanuRandom(1); }
@Override public ArithmeticDouble nextGaussian(double mu, double sigma) { double randomDouble = random.nextGaussian(mu, sigma); return new ArithmeticDouble(randomDouble); } }
@Test public void randomNumberGenerationIsPlatformIndependent() { KeanuRandom keanuRandom = new KeanuRandom(1); assertEquals(keanuRandom.nextDouble(), new MersenneTwister(1L).nextDouble()); assertEquals(keanuRandom.nextDouble(), 0.41782887182714457, 1e-16); } }
public void canCalculateLogProb(ProbabilisticModel probabilisticModel) { double defaultLogProb = probabilisticModel.logProb(); double logProb = probabilisticModel.logProb(ImmutableMap.of( A.getId(), initialA, B.getId(), initialB )); assertEquals(defaultLogProb, logProb); double expectedInitialLogProb = expectedLogProb(); assertEquals(expectedInitialLogProb, logProb, 1e-5); DoubleTensor newA = KeanuRandom.getDefaultRandom().nextDouble(initialA.getShape()); double postUpdateLogProb = probabilisticModel.logProb(ImmutableMap.of( A.getId(), newA )); double expectedPostUpdateLogProb = expectedLogProb(); assertEquals(expectedPostUpdateLogProb, postUpdateLogProb, 1e-5); }
@Override public DoubleTensor sample(long[] shape, KeanuRandom random) { final DoubleTensor gammaSample = random.nextGamma(shape, beta.reciprocal(), alpha); return gammaSample.reciprocal(); }
@Before public void setup() { random = new KeanuRandom(1); }
private static void initializeMomentumForEachVariable(List<? extends Variable<DoubleTensor, ?>> variables, Map<VariableReference, DoubleTensor> momentums, KeanuRandom random) { for (Variable<DoubleTensor, ?> variable : variables) { momentums.put(variable.getReference(), random.nextGaussian(variable.getShape())); } }
@Override public DoubleTensor sample(long[] shape, KeanuRandom random) { return random.nextGamma(shape, DoubleTensor.TWO_SCALAR, k.toDouble().div(2)); }
@Override public Statement apply(final Statement base, final Description description) { KeanuRandom.setDefaultRandomSeed(1); VertexId.ID_GENERATOR.set(1); return base; } }
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; }
@Before public void setup() { random = new KeanuRandom(1); }
default T sampleWithShape(long[] shape) { return sampleWithShape(shape, KeanuRandom.getDefaultRandom()); } }
@Override public ArithmeticDouble nextGaussian(double mu, ArithmeticDouble sigma) { double randomDouble = random.nextGaussian(mu, sigma.getValue()); return new ArithmeticDouble(randomDouble); }
@Test public void canSampleGammaScalar() { assertEquals(0, random.nextGamma(new long[0], DoubleTensor.scalar(2), DoubleTensor.scalar(2)).getRank()); assertEquals(1, random.nextGamma(new long[]{1}, DoubleTensor.scalar(2), DoubleTensor.scalar(2)).getRank()); assertEquals(2, random.nextGamma(new long[]{1, 1}, DoubleTensor.scalar(2), DoubleTensor.scalar(2)).getRank()); assertEquals(2, random.nextGamma(new long[]{2, 3}, DoubleTensor.scalar(2), DoubleTensor.scalar(2)).getRank()); }