Skip to content

Instantly share code, notes, and snippets.

@tupui
Last active October 9, 2022 12:20

Revisions

  1. tupui revised this gist Oct 27, 2017. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions halton.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    """Halton low discrepancy sequence.
    This snippet implements the Halton sequence following the generalization of
    a sequence of *Van der Corrupt* in n-dimensions.
    a sequence of *Van der Corput* in n-dimensions.
    ---------------------------
    @@ -48,12 +48,12 @@ def primes_from_2_to(n):
    return np.r_[2, 3, ((3 * np.nonzero(sieve)[0][1:] + 1) | 1)]


    def van_der_corrupt(n_sample, base=2):
    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 Corrupt.
    :return: sequence of Van der Corput.
    :rtype: list (n_samples,)
    """
    sequence = []
    @@ -83,14 +83,14 @@ def halton(dim, n_sample):
    break
    big_number += 1000

    # Generate a sample using a Van der Corrupt sequence per dimension.
    sample = [van_der_corrupt(n_sample + 1, dim) for dim in base]
    # 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_corrupt(10))
    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]
  2. tupui revised this gist Oct 26, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion halton.py
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    MIT License
    Copyright (c) 2017 Pamphile ROY
    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
  3. tupui revised this gist Oct 26, 2017. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions halton.py
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,30 @@
    This snippet implements the Halton sequence following the generalization of
    a sequence of *Van der Corrupt* in n-dimensions.
    ---------------------------
    MIT License
    Copyright (c) 2017 Pamphile 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

  4. tupui created this gist Oct 26, 2017.
    76 changes: 76 additions & 0 deletions halton.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    """Halton low discrepancy sequence.
    This snippet implements the Halton sequence following the generalization of
    a sequence of *Van der Corrupt* in n-dimensions.
    """
    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_corrupt(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 Corrupt.
    :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 Corrupt sequence per dimension.
    sample = [van_der_corrupt(n_sample + 1, dim) for dim in base]
    sample = np.stack(sample, axis=-1)[1:]

    return sample


    print(van_der_corrupt(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]]