Source code for qdpy.hdsobol

# -*- utf-8 -*-
#pylint: disable=W0603, E1101, I0011
## @file
# @brief Origin-free Sobol points and inverse-normal quasi-random samples.

'''Sobol sampling. The original Python implementation was based on sobol.cc.

Please refer to:
http://web.maths.unsw.edu.au/%7Efkuo/sobol/index.html

And the following explanations:
http://web.maths.unsw.edu.au/~fkuo/sobol/joe-kuo-notes.pdf

-----------------------------------------------------------------------------
Frances Y. Kuo

Email: <f.kuo@unsw.edu.au>
School of Mathematics and Statistics
University of New South Wales
Sydney NSW 2052, Australia

Last updated: 21 October 2008

You may incorporate this source code into your own program
  provided that you
  1) acknowledge the copyright owner in your program and publication
  2) notify the copyright owner by email
  3) offer feedback regarding your experience with different direction numbers

-----------------------------------------------------------------------------
Licence pertaining to sobol.cc and the accompanying sets of direction numbers
-----------------------------------------------------------------------------
Copyright (c) 2008, Frances Y. Kuo and Stephen Joe
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

    * Neither the names of the copyright holders nor the names of the
      University of New South Wales and the University of Waikato
      and its contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------------
'''
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import division

import numpy as np
from operator import index
from scipy.stats import norm, qmc
from ._directions import DIRECTIONS_D6_21201

__all__ = ['gen_sobol_vectors', 'gen_sobol_randn']


## @brief Generate the 32-bit Joe–Kuo sequence using SciPy.
# @param num Requested output length after the origin is dropped.
# @param dim Number of coordinates.
# @return An array with one vector per requested point.
# @details Draw and discard the origin before requesting the remaining points.
#     Sequential calls with different lengths share the same prefix.
[docs] def gen_sobol_vectors(num, dim): """Return the requested number of Sobol points, skipping the origin. SciPy generates the unscrambled 32-bit Joe–Kuo sequence. Requests for zero points return an empty array with the requested width. :param num: Number of points, from zero through 2**32 - 1. :param dim: Number of coordinates, from one through 21,201. :returns: Floating-point array of shape (num, dim). :raises ValueError: If the requested count or dimension is outside the supported range. """ num = index(num) dim = index(dim) if not 0 <= num < 2**32: raise ValueError("The sample count must be between 0 and 2**32 - 1.") if not 1 <= dim <= len(DIRECTIONS_D6_21201) + 1: raise ValueError(f"The Sobol dimension must be between 1 and {len(DIRECTIONS_D6_21201) + 1}.") if num == 0: return np.empty((0, dim)) sampler = qmc.Sobol(dim, scramble=False, bits=32) # Advance past the origin with a power-of-two first draw. The # requested continuation can have any supported length. sampler.random(1) return sampler.random(num)
[docs] def gen_sobol_randn(num): """Transform one-dimensional Sobol points into quasi-normal samples. Applying the inverse standard-normal CDF to the origin-free sequence gives finite samples with no random generator state. """ return norm.ppf(gen_sobol_vectors(num, 1)[:, 0])