QDpy
Loading...
Searching...
No Matches
__init__.py
Go to the documentation of this file.
1# -*- utf-8 -*-
2#pylint: disable=W0603, E1101, I0011
3
5
6'''Sobol sampling. The original Python implementation was based on sobol.cc.
7
8Please refer to:
9http://web.maths.unsw.edu.au/%7Efkuo/sobol/index.html
10
11And the following explanations:
12http://web.maths.unsw.edu.au/~fkuo/sobol/joe-kuo-notes.pdf
13
14-----------------------------------------------------------------------------
15Frances Y. Kuo
16
17Email: <f.kuo@unsw.edu.au>
18School of Mathematics and Statistics
19University of New South Wales
20Sydney NSW 2052, Australia
21
22Last updated: 21 October 2008
23
24You may incorporate this source code into your own program
25 provided that you
26 1) acknowledge the copyright owner in your program and publication
27 2) notify the copyright owner by email
28 3) offer feedback regarding your experience with different direction numbers
29
30-----------------------------------------------------------------------------
31Licence pertaining to sobol.cc and the accompanying sets of direction numbers
32-----------------------------------------------------------------------------
33Copyright (c) 2008, Frances Y. Kuo and Stephen Joe
34All rights reserved.
35
36Redistribution and use in source and binary forms, with or without
37modification, are permitted provided that the following conditions are met:
38
39 * Redistributions of source code must retain the above copyright
40 notice, this list of conditions and the following disclaimer.
41
42 * Redistributions in binary form must reproduce the above copyright
43 notice, this list of conditions and the following disclaimer in the
44 documentation and/or other materials provided with the distribution.
45
46 * Neither the names of the copyright holders nor the names of the
47 University of New South Wales and the University of Waikato
48 and its contributors may be used to endorse or promote products derived
49 from this software without specific prior written permission.
50
51THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
52EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
55DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61-----------------------------------------------------------------------------
62'''
63from __future__ import print_function
64from __future__ import absolute_import
65from __future__ import unicode_literals
66from __future__ import division
67
68import numpy as np
69from operator import index
70from scipy.stats import norm, qmc
71from ._directions import DIRECTIONS_D6_21201
72
73__all__ = ['gen_sobol_vectors', 'gen_sobol_randn']
74
75
76
82def gen_sobol_vectors(num, dim):
83 """Return the requested number of Sobol points, skipping the origin.
84
85 SciPy generates the unscrambled 32-bit Joe–Kuo sequence. Requests
86 for zero points return an empty array with the requested width.
87
88 :param num: Number of points, from zero through 2**32 - 1.
89 :param dim: Number of coordinates, from one through 21,201.
90 :returns: Floating-point array of shape (num, dim).
91 :raises ValueError: If the requested count or dimension is outside
92 the supported range.
93 """
94 num = index(num)
95 dim = index(dim)
96 if not 0 <= num < 2**32:
97 raise ValueError("The sample count must be between 0 and 2**32 - 1.")
98 if not 1 <= dim <= len(DIRECTIONS_D6_21201) + 1:
99 raise ValueError(f"The Sobol dimension must be between 1 and {len(DIRECTIONS_D6_21201) + 1}.")
100 if num == 0:
101 return np.empty((0, dim))
102 sampler = qmc.Sobol(dim, scramble=False, bits=32)
103 # Advance past the origin with a power-of-two first draw. The
104 # requested continuation can have any supported length.
105 sampler.random(1)
106 return sampler.random(num)
107
108
109def gen_sobol_randn(num):
110 """Transform one-dimensional Sobol points into quasi-normal samples.
111
112 Applying the inverse standard-normal CDF to the origin-free
113 sequence gives finite samples with no random generator state.
114 """
115 return norm.ppf(gen_sobol_vectors(num, 1)[:, 0])