SobolSequenceGenerator.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 java.io.BufferedReader;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.InputStreamReader;
  26. import java.nio.charset.Charset;
  27. import java.util.Arrays;
  28. import java.util.NoSuchElementException;
  29. import java.util.StringTokenizer;

  30. import org.hipparchus.exception.LocalizedCoreFormats;
  31. import org.hipparchus.exception.MathIllegalArgumentException;
  32. import org.hipparchus.exception.MathIllegalStateException;
  33. import org.hipparchus.exception.MathRuntimeException;
  34. import org.hipparchus.util.FastMath;
  35. import org.hipparchus.util.MathUtils;

  36. /**
  37.  * Implementation of a Sobol sequence.
  38.  * <p>
  39.  * A Sobol sequence is a low-discrepancy sequence with the property that for all values of N,
  40.  * its subsequence (x1, ... xN) has a low discrepancy. It can be used to generate pseudo-random
  41.  * points in a space S, which are equi-distributed.
  42.  * <p>
  43.  * The implementation already comes with support for up to 21201 dimensions with direction numbers
  44.  * calculated from <a href="http://web.maths.unsw.edu.au/~fkuo/sobol/">Stephen Joe and Frances Kuo</a>.
  45.  * <p>
  46.  * The generator supports two modes:
  47.  * <ul>
  48.  *   <li>sequential generation of points: {@link #nextVector()}</li>
  49.  *   <li>random access to the i-th point in the sequence: {@link #skipTo(int)}</li>
  50.  * </ul>
  51.  *
  52.  * @see <a href="http://en.wikipedia.org/wiki/Sobol_sequence">Sobol sequence (Wikipedia)</a>
  53.  * @see <a href="http://web.maths.unsw.edu.au/~fkuo/sobol/">Sobol sequence direction numbers</a>
  54.  *
  55.  */
  56. public class SobolSequenceGenerator implements RandomVectorGenerator {

  57.     /** The number of bits to use. */
  58.     private static final int BITS = 52;

  59.     /** The scaling factor. */
  60.     private static final double SCALE = FastMath.pow(2, BITS);

  61.     /** The maximum supported space dimension. */
  62.     private static final int MAX_DIMENSION = 21201;

  63.     /** The resource containing the direction numbers. */
  64.     private static final String RESOURCE_NAME = "/assets/org/hipparchus/random/new-joe-kuo-6.21201";

  65.     /** Character set for file input. */
  66.     private static final String FILE_CHARSET = "US-ASCII";

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

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

  71.     /** The direction vector for each component. */
  72.     private final long[][] direction;

  73.     /** The current state. */
  74.     private final long[] x;

  75.     /**
  76.      * Construct a new Sobol 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, 1000]
  80.      */
  81.     public SobolSequenceGenerator(final int dimension) throws MathIllegalArgumentException {
  82.         MathUtils.checkRangeInclusive(dimension, 1, MAX_DIMENSION);

  83.         // initialize the other dimensions with direction numbers from a resource
  84.         try (InputStream is = getClass().getResourceAsStream(RESOURCE_NAME)) {
  85.             if (is == null) {
  86.                 throw MathRuntimeException.createInternalError();
  87.             }

  88.             this.dimension = dimension;

  89.             // init data structures
  90.             direction = new long[dimension][BITS + 1];
  91.             x = new long[dimension];

  92.             initFromStream(is);
  93.         } catch (IOException | MathIllegalStateException e) {
  94.             // the internal resource file could not be parsed -> should not happen
  95.             throw MathRuntimeException.createInternalError(e);
  96.         }
  97.     }

  98.     /**
  99.      * Construct a new Sobol sequence generator for the given space dimension with
  100.      * direction vectors loaded from the given stream.
  101.      * <p>
  102.      * The expected format is identical to the files available from
  103.      * <a href="http://web.maths.unsw.edu.au/~fkuo/sobol/">Stephen Joe and Frances Kuo</a>.
  104.      * The first line will be ignored as it is assumed to contain only the column headers.
  105.      * The columns are:
  106.      * <ul>
  107.      *  <li>d: the dimension</li>
  108.      *  <li>s: the degree of the primitive polynomial</li>
  109.      *  <li>a: the number representing the coefficients</li>
  110.      *  <li>m: the list of initial direction numbers</li>
  111.      * </ul>
  112.      * Example:
  113.      * <pre>
  114.      * d       s       a       m_i
  115.      * 2       1       0       1
  116.      * 3       2       1       1 3
  117.      * </pre>
  118.      * <p>
  119.      * The input stream <i>must</i> be an ASCII text containing one valid direction vector per line.
  120.      *
  121.      * @param dimension the space dimension
  122.      * @param is the stream to read the direction vectors from
  123.      * @throws MathIllegalArgumentException if the space dimension is &lt; 1
  124.      * @throws MathIllegalArgumentException if the space dimension is outside the range [1, max], where
  125.      *   max refers to the maximum dimension found in the input stream
  126.      * @throws MathIllegalStateException if the content in the stream could not be parsed successfully
  127.      * @throws IOException if an error occurs while reading from the input stream
  128.      */
  129.     public SobolSequenceGenerator(final int dimension, final InputStream is)
  130.             throws MathIllegalArgumentException, MathIllegalStateException, IOException {

  131.         if (dimension < 1) {
  132.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
  133.                                                    dimension, 1);
  134.         }

  135.         this.dimension = dimension;

  136.         // init data structures
  137.         direction = new long[dimension][BITS + 1];
  138.         x = new long[dimension];

  139.         // initialize the other dimensions with direction numbers from the stream
  140.         int lastDimension = initFromStream(is);
  141.         MathUtils.checkRangeInclusive(dimension, 1, lastDimension);
  142.     }

  143.     /**
  144.      * Load the direction vector for each dimension from the given stream.
  145.      * <p>
  146.      * The input stream <i>must</i> be an ASCII text containing one
  147.      * valid direction vector per line.
  148.      *
  149.      * @param is the input stream to read the direction vector from
  150.      * @return the last dimension that has been read from the input stream
  151.      * @throws IOException if the stream could not be read
  152.      * @throws MathIllegalStateException if the content could not be parsed successfully
  153.      */
  154.     private int initFromStream(final InputStream is) throws MathIllegalStateException, IOException {

  155.         // special case: dimension 1 -> use unit initialization
  156.         for (int i = 1; i <= BITS; i++) {
  157.             direction[0][i] = 1L << (BITS - i);
  158.         }

  159.         final Charset charset = Charset.forName(FILE_CHARSET);
  160.         int dim = -1;

  161.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset))) {
  162.             // ignore first line
  163.             reader.readLine();

  164.             int lineNumber = 2;
  165.             int index = 1;
  166.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  167.                 StringTokenizer st = new StringTokenizer(line, " ");
  168.                 try {
  169.                     dim = Integer.parseInt(st.nextToken());
  170.                     if (dim >= 2 && dim <= dimension) { // we have found the right dimension
  171.                         final int s = Integer.parseInt(st.nextToken());
  172.                         final int a = Integer.parseInt(st.nextToken());
  173.                         final int[] m = new int[s + 1];
  174.                         for (int i = 1; i <= s; i++) {
  175.                             m[i] = Integer.parseInt(st.nextToken());
  176.                         }
  177.                         initDirectionVector(index++, a, m);
  178.                     }

  179.                     if (dim > dimension) {
  180.                         return dim;
  181.                     }
  182.                 } catch (NoSuchElementException|NumberFormatException e) {
  183.                     throw new MathIllegalStateException(e, LocalizedCoreFormats.CANNOT_PARSE,
  184.                                                         line, lineNumber);
  185.                 }
  186.                 lineNumber++;
  187.             }
  188.         }

  189.         return dim;
  190.     }

  191.     /**
  192.      * Calculate the direction numbers from the given polynomial.
  193.      *
  194.      * @param d the dimension, zero-based
  195.      * @param a the coefficients of the primitive polynomial
  196.      * @param m the initial direction numbers
  197.      */
  198.     private void initDirectionVector(final int d, final int a, final int[] m) {
  199.         final int s = m.length - 1;
  200.         for (int i = 1; i <= s; i++) {
  201.             direction[d][i] = ((long) m[i]) << (BITS - i);
  202.         }
  203.         for (int i = s + 1; i <= BITS; i++) {
  204.             direction[d][i] = direction[d][i - s] ^ (direction[d][i - s] >> s);
  205.             for (int k = 1; k <= s - 1; k++) {
  206.                 direction[d][i] ^= ((a >> (s - 1 - k)) & 1) * direction[d][i - k];
  207.             }
  208.         }
  209.     }

  210.     /** {@inheritDoc} */
  211.     @Override
  212.     public double[] nextVector() {
  213.         final double[] v = new double[dimension];
  214.         if (count == 0) {
  215.             count++;
  216.             return v;
  217.         }

  218.         // find the index c of the rightmost 0
  219.         int c = 1;
  220.         int value = count - 1;
  221.         while ((value & 1) == 1) {
  222.             value >>= 1;
  223.             c++;
  224.         }

  225.         for (int i = 0; i < dimension; i++) {
  226.             x[i] ^= direction[i][c];
  227.             v[i] = x[i] / SCALE;
  228.         }
  229.         count++;
  230.         return v;
  231.     }

  232.     /**
  233.      * Skip to the i-th point in the Sobol sequence.
  234.      * <p>
  235.      * This operation can be performed in O(1).
  236.      *
  237.      * @param index the index in the sequence to skip to
  238.      * @return the i-th point in the Sobol sequence
  239.      * @throws MathIllegalArgumentException if index &lt; 0
  240.      */
  241.     public double[] skipTo(final int index) throws MathIllegalArgumentException {
  242.         if (index == 0) {
  243.             // reset x vector
  244.             Arrays.fill(x, 0);
  245.         } else {
  246.             final int i = index - 1;
  247.             final long grayCode = i ^ (i >> 1); // compute the gray code of i = i XOR floor(i / 2)
  248.             for (int j = 0; j < dimension; j++) {
  249.                 long result = 0;
  250.                 for (int k = 1; k <= BITS; k++) {
  251.                     final long shift = grayCode >> (k - 1);
  252.                     if (shift == 0) {
  253.                         // stop, as all remaining bits will be zero
  254.                         break;
  255.                     }
  256.                     // the k-th bit of i
  257.                     final long ik = shift & 1;
  258.                     result ^= ik * direction[j][k];
  259.                 }
  260.                 x[j] = result;
  261.             }
  262.         }
  263.         count = index;
  264.         return nextVector();
  265.     }

  266.     /**
  267.      * Returns the index i of the next point in the Sobol sequence that will be returned
  268.      * by calling {@link #nextVector()}.
  269.      *
  270.      * @return the index of the next point
  271.      */
  272.     public int getNextIndex() {
  273.         return count;
  274.     }

  275. }