Skip to content

Instantly share code, notes, and snippets.

@tupui
Last active October 9, 2022 12:20
Show Gist options
  • Save tupui/cea0a91cc127ea3890ac0f002f887bae to your computer and use it in GitHub Desktop.
Save tupui/cea0a91cc127ea3890ac0f002f887bae to your computer and use it in GitHub Desktop.
Halton Sequence in python
"""Halton low discrepancy sequence.
This snippet implements the Halton sequence following the generalization of
a sequence of *Van der Corput* in n-dimensions.
---------------------------
MIT License
Copyright (c) 2017 Pamphile Tupui ROY
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import numpy as np
def primes_from_2_to(n):
"""Prime number from 2 to n.
From `StackOverflow <https://stackoverflow.com/questions/2068372>`_.
:param int n: sup bound with ``n >= 6``.
:return: primes in 2 <= p < n.
:rtype: list
"""
sieve = np.ones(n // 3 + (n % 6 == 2), dtype=np.bool)
for i in range(1, int(n ** 0.5) // 3 + 1):
if sieve[i]:
k = 3 * i + 1 | 1
sieve[k * k // 3::2 * k] = False
sieve[k * (k - 2 * (i & 1) + 4) // 3::2 * k] = False
return np.r_[2, 3, ((3 * np.nonzero(sieve)[0][1:] + 1) | 1)]
def van_der_corput(n_sample, base=2):
"""Van der Corput sequence.
:param int n_sample: number of element of the sequence.
:param int base: base of the sequence.
:return: sequence of Van der Corput.
:rtype: list (n_samples,)
"""
sequence = []
for i in range(n_sample):
n_th_number, denom = 0., 1.
while i > 0:
i, remainder = divmod(i, base)
denom *= base
n_th_number += remainder / denom
sequence.append(n_th_number)
return sequence
def halton(dim, n_sample):
"""Halton sequence.
:param int dim: dimension
:param int n_sample: number of samples.
:return: sequence of Halton.
:rtype: array_like (n_samples, n_features)
"""
big_number = 10
while 'Not enought primes':
base = primes_from_2_to(big_number)[:dim]
if len(base) == dim:
break
big_number += 1000
# Generate a sample using a Van der Corput sequence per dimension.
sample = [van_der_corput(n_sample + 1, dim) for dim in base]
sample = np.stack(sample, axis=-1)[1:]
return sample
print(van_der_corput(10))
# [0.0, 0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875, 0.0625, 0.5625]
print(halton(2, 5))
# [[ 0.5 0.33333333]
# [ 0.25 0.66666667]
# [ 0.75 0.11111111]
# [ 0.125 0.44444444]
# [ 0.625 0.77777778]]
@tupui
Copy link
Author

tupui commented Jun 18, 2020

@molinav Thanks for finding this out! I am currently working on a PR to have this in SciPy. So this is useful.

@jdavidd
Copy link

jdavidd commented Dec 14, 2020

hello @tupui! How can I use this to generate Halton sequence in a given rectangle? :)

@tupui
Copy link
Author

tupui commented Dec 15, 2020

Hi @jdavidd, once you generate a sample, you just have to scale the values from [0, 1) to [a, b), b>a the bounds you want.
For instance if you have two parameters the first range is [-2, 6] and the second [0, 5]:

bounds = [[-2, 0], [6, 5]]
bounds = np.array(bounds)
min_ = np.min(bounds, axis=0)
max_ = np.max(bounds, axis=0)
sample = sample * (max_ - min_) + min_

But have a look at the PR I have in scipy for more stuff like discrepancy: scipy/scipy#10844

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment