HaltonSequenceGenerator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.random;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.exception.NullArgumentException;
  25. import org.hipparchus.util.MathUtils;

  26. /**
  27.  * Implementation of a Halton sequence.
  28.  * <p>
  29.  * A Halton sequence is a low-discrepancy sequence generating points in the interval [0, 1] according to
  30.  * <pre>
  31.  *   H(n) = d_0 / b + d_1 / b^2 .... d_j / b^j+1
  32.  *
  33.  *   with
  34.  *
  35.  *   n = d_j * b^j-1 + ... d_1 * b + d_0 * b^0
  36.  * </pre>
  37.  * For higher dimensions, subsequent prime numbers are used as base, e.g. { 2, 3, 5 } for a Halton sequence in R^3.
  38.  * <p>
  39.  * Halton sequences are known to suffer from linear correlation for larger prime numbers, thus the individual digits
  40.  * are usually scrambled. This implementation already comes with support for up to 40 dimensions with optimal weight
  41.  * numbers from <a href="http://etd.lib.fsu.edu/theses/available/etd-07062004-140409/unrestricted/dissertation1.pdf">
  42.  * H. Chi: Scrambled quasirandom sequences and their applications</a>.
  43.  * <p>
  44.  * The generator supports two modes:
  45.  * <ul>
  46.  *   <li>sequential generation of points: {@link #nextVector()}</li>
  47.  *   <li>random access to the i-th point in the sequence: {@link #skipTo(int)}</li>
  48.  * </ul>
  49.  *
  50.  * @see <a href="http://en.wikipedia.org/wiki/Halton_sequence">Halton sequence (Wikipedia)</a>
  51.  * @see <a href="https://lirias.kuleuven.be/bitstream/123456789/131168/1/mcm2005_bartv.pdf">
  52.  * On the Halton sequence and its scramblings</a>
  53.  */
  54. public class HaltonSequenceGenerator implements RandomVectorGenerator {

  55.     /** The first 40 primes. */
  56.     private static final int[] PRIMES = {
  57.         2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
  58.         71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139,
  59.         149, 151, 157, 163, 167, 173
  60.     };

  61.     /** The optimal weights used for scrambling of the first 40 dimension. */
  62.     private static final int[] WEIGHTS = {
  63.         1, 2, 3, 3, 8, 11, 12, 14, 7, 18, 12, 13, 17, 18, 29, 14, 18, 43, 41,
  64.         44, 40, 30, 47, 65, 71, 28, 40, 60, 79, 89, 56, 50, 52, 61, 108, 56,
  65.         66, 63, 60, 66
  66.     };

  67.     /** Space dimension. */
  68.     private final int dimension;

  69.     /** The current index in the sequence. */
  70.     private int count;

  71.     /** The base numbers for each component. */
  72.     private final int[] base;

  73.     /** The scrambling weights for each component. */
  74.     private final int[] weight;

  75.     /**
  76.      * Construct a new Halton sequence generator for the given space dimension.
  77.      *
  78.      * @param dimension the space dimension
  79.      * @throws MathIllegalArgumentException if the space dimension is outside the allowed range of [1, 40]
  80.      */
  81.     public HaltonSequenceGenerator(final int dimension) throws MathIllegalArgumentException {
  82.         this(dimension, PRIMES, WEIGHTS);
  83.     }

  84.     /**
  85.      * Construct a new Halton sequence generator with the given base numbers and weights for each dimension.
  86.      * The length of the bases array defines the space dimension and is required to be &gt; 0.
  87.      *
  88.      * @param dimension the space dimension
  89.      * @param bases the base number for each dimension, entries should be (pairwise) prime, may not be null
  90.      * @param weights the weights used during scrambling, may be null in which case no scrambling will be performed
  91.      * @throws NullArgumentException if base is null
  92.      * @throws MathIllegalArgumentException if the space dimension is outside the range [1, len], where
  93.      *   len refers to the length of the bases array
  94.      * @throws MathIllegalArgumentException if weights is non-null and the length of the input arrays differ
  95.      */
  96.     public HaltonSequenceGenerator(final int dimension, final int[] bases, final int[] weights)
  97.             throws MathIllegalArgumentException, NullArgumentException {

  98.         MathUtils.checkNotNull(bases);
  99.         MathUtils.checkRangeInclusive(dimension, 1, bases.length);

  100.         if (weights != null && weights.length != bases.length) {
  101.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  102.                                                    weights.length, bases.length);
  103.         }

  104.         this.dimension = dimension;
  105.         this.base = bases.clone();
  106.         this.weight = weights == null ? null : weights.clone();
  107.         count = 0;
  108.     }

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     public double[] nextVector() {
  112.         final double[] v = new double[dimension];
  113.         for (int i = 0; i < dimension; i++) {
  114.             int index = count;
  115.             double f = 1.0 / base[i];

  116.             int j = 0;
  117.             while (index > 0) {
  118.                 final int digit = scramble(i, j, base[i], index % base[i]);
  119.                 v[i] += f * digit;
  120.                 index /= base[i]; // floor( index / base )
  121.                 f /= base[i];
  122.             }
  123.         }
  124.         count++;
  125.         return v;
  126.     }

  127.     /**
  128.      * Performs scrambling of digit {@code d_j} according to the formula:
  129.      * <pre>
  130.      *   ( weight_i * d_j ) mod base
  131.      * </pre>
  132.      * Implementations can override this method to do a different scrambling.
  133.      *
  134.      * @param i the dimension index
  135.      * @param j the digit index
  136.      * @param b the base for this dimension
  137.      * @param digit the j-th digit
  138.      * @return the scrambled digit
  139.      */
  140.     protected int scramble(final int i, final int j, final int b, final int digit) {
  141.         return weight != null ? (weight[i] * digit) % b : digit;
  142.     }

  143.     /**
  144.      * Skip to the i-th point in the Halton sequence.
  145.      * <p>
  146.      * This operation can be performed in O(1).
  147.      *
  148.      * @param index the index in the sequence to skip to
  149.      * @return the i-th point in the Halton sequence
  150.      * @throws MathIllegalArgumentException if index &lt; 0
  151.      */
  152.     public double[] skipTo(final int index) throws MathIllegalArgumentException {
  153.         count = index;
  154.         return nextVector();
  155.     }

  156.     /**
  157.      * Returns the index i of the next point in the Halton sequence that will be returned
  158.      * by calling {@link #nextVector()}.
  159.      *
  160.      * @return the index of the next point
  161.      */
  162.     public int getNextIndex() {
  163.         return count;
  164.     }

  165. }