-
-
Notifications
You must be signed in to change notification settings - Fork 453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Halton Sequence and Quasi Random Numbers #182
Comments
Sorry but I don't think it belongs in Use of |
That's exactly what I started with actually. It wasn't hard to simply replace the default definitions of the However, the To play off the example from the Readme: use rand::Rng;
use rand::distributions::{IndependentSample, Range};
use std::f64::consts::PI;
// Define a function to take two random number generators from which to sample
fn monte_carlo_pi<R: Rng>(points: usize, mut s1: R, mut s2: R) -> f64 {
let between = Range::new(-1f64, 1.);
let mut in_circle = 0;
for _ in 0..points {
let a = between.ind_sample(&mut s1);
let b = between.ind_sample(&mut s2);
if a*a + b*b <= 1. {
in_circle += 1;
}
}
4. * (in_circle as f64) / (points as f64)
}
// Create two halton sequence generators with coprime bases 17 and 19
let h_est = monte_carlo_pi(10_000, Halton::new(1,17), Halton::new(1,19));
// Estimate using standard thread_rng
let r_est = monte_carlo_pi(1_000_000, rand::thread_rng(), rand::thread_rng());
// The error is less than the standard number generator with 100 times less points
assert!((h_est-PI).abs() < (r_est-PI).abs()); |
I suppose we could add this as a "mock RNG" (i.e. add a Anyone want to make a PR? Doesn't look too hard. |
Just to chime in, I’ve been recently working on a qrng crate and I just got a Gray-code based Sobol sequence generator working which is a few times faster than both the GSL one and the SmallRng gen (just so as to establish some baseline). Sobol sequence is probably one of the best options currently available for high dim data, especially if scrambled. I’m planning to add a few scrambling options, SIMD support and perhaps a few weaker options like Halton as well. If it would be of any interest, I could post it back here once it’s in any usable state. I’m not sure it fits the rand interface though since for high dimensional samples you really want to do it all at once, without allocations, ideally while exploiting simd etc - which means writing to the same buffer over and over. But that we can figure out later. |
I’m not sure it fits the rand interface though since for high dimensional
samples you really want to do it all at once, without allocations, ideally
while exploiting simd etc - which means writing to the same buffer over and
over. But that we can figure out later.
I think the `fill` interface satisfies your requirements, doesn't it?
… |
You mean In this case, we're talking about slice-type samples. |
Are you are talking about some type of correlation within matrix / higher-dimensional tensors or just efficiently filling the data space? I assume the latter, though the same API may work for both. For generating a fixed-size object like /// The generator state
pub struct GenState { ... }
/// Constructing objects
pub trait GenNew<T> {
pub fn gen_new(state: &mut GenState) -> T;
}
/// Filling objects
pub trait GenFill<T> {
pub fn gen_fill(obj: &mut T, state: &mut GenState);
} At this point you basically have your own lib which shares no code with Note that we only partially solved the question of how to efficiently generate SIMD values within this project. Block-rngs like ChaCha may use SIMD internally but there is no way to directly output large values from the RNG (we did discuss adding |
Closing this issue, hopefully there are no objections after all this time 😄 |
For an optimization library I'm writing I need a fast sampling algorithm so I started by implemented a fast Halton sequence generator. The rand crate provides a really nice API for randomness and I don't want to go through the trouble of re-implementing a similar API for my sampling algorithms so I implemented
Rng
trait for my generator.The halton sequence is a range of floating point values from 0 to 1 and is defined by its base, which should be a prime number, and will produce regular values with a base of 2 like:
and with a base of 3 like:
To fill the domain in a pretty even way. You can read more on Wikipedia.
Is there any space in the rand crate for quasi-random number generators like the halton sequence? I'd love to open a PR and add my implementation if it's appropriate.
The text was updated successfully, but these errors were encountered: