Skip to content

sobol

Sobol

Bases: Designs

Super class for sobol designs.

Attributes:

Name Type Description
k int

The number of factors.

seed int

The seed for the random number generator.

Source code in spotpython/design/sobol.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Sobol(Designs):
    """
    Super class for sobol designs.

    Attributes:
        k (int): The number of factors.
        seed (int): The seed for the random number generator.
    """

    def __init__(self, k: int = 2, seed: int = 123) -> None:
        """
        Initializes a sobol design object.

        Args:
            k (int): The number of factors (dimension). Defaults to 2.
            seed (int): The seed for the random number generator. Defaults to 123.
        """
        super().__init__(k, seed)
        self.k = k
        self.seed = seed

    def generate_sobol_design(self, n_points: int, seed: int = None) -> np.ndarray:
        """Generates a Sobol sequence design

        Args:
            n_points (int):
                The number of points to generate in the Sobol sequence.
            seed (Optional[int]):
                The seed for the random number generator.
                If None, uses the instance's seed.

        Returns:
            np.ndarray: An array of shape (n_points, n_dim) containing the generated Sobol sequence points.

        Notes:
            - The Sobol sequence is generated with a length that is a power of 2. The function generates at least n_points and returns the first n_points.
            - For n_points not being a power of 2, extra points are generated and truncated.
            - Scrambling is enabled for improved uniformity.

        Examples:
            >>> from spotpython.design.sobol import Sobol
            >>> sobol_design = Sobol(k=3, seed=42)
            >>> sobol_points = sobol_design.generate_sobol_design(n_points=10)
            >>> print(sobol_points.shape)
            (10, 3)
        """
        if seed is not None:
            self.seed = seed
        sampler = qmc.Sobol(d=self.k, scramble=True, seed=seed)
        m = int(np.ceil(np.log2(n_points)))
        return sampler.random_base2(m=m)[:n_points, :]

__init__(k=2, seed=123)

Initializes a sobol design object.

Parameters:

Name Type Description Default
k int

The number of factors (dimension). Defaults to 2.

2
seed int

The seed for the random number generator. Defaults to 123.

123
Source code in spotpython/design/sobol.py
15
16
17
18
19
20
21
22
23
24
25
def __init__(self, k: int = 2, seed: int = 123) -> None:
    """
    Initializes a sobol design object.

    Args:
        k (int): The number of factors (dimension). Defaults to 2.
        seed (int): The seed for the random number generator. Defaults to 123.
    """
    super().__init__(k, seed)
    self.k = k
    self.seed = seed

generate_sobol_design(n_points, seed=None)

Generates a Sobol sequence design

Parameters:

Name Type Description Default
n_points int

The number of points to generate in the Sobol sequence.

required
seed Optional[int]

The seed for the random number generator. If None, uses the instance’s seed.

None

Returns:

Type Description
ndarray

np.ndarray: An array of shape (n_points, n_dim) containing the generated Sobol sequence points.

Notes
  • The Sobol sequence is generated with a length that is a power of 2. The function generates at least n_points and returns the first n_points.
  • For n_points not being a power of 2, extra points are generated and truncated.
  • Scrambling is enabled for improved uniformity.

Examples:

>>> from spotpython.design.sobol import Sobol
>>> sobol_design = Sobol(k=3, seed=42)
>>> sobol_points = sobol_design.generate_sobol_design(n_points=10)
>>> print(sobol_points.shape)
(10, 3)
Source code in spotpython/design/sobol.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def generate_sobol_design(self, n_points: int, seed: int = None) -> np.ndarray:
    """Generates a Sobol sequence design

    Args:
        n_points (int):
            The number of points to generate in the Sobol sequence.
        seed (Optional[int]):
            The seed for the random number generator.
            If None, uses the instance's seed.

    Returns:
        np.ndarray: An array of shape (n_points, n_dim) containing the generated Sobol sequence points.

    Notes:
        - The Sobol sequence is generated with a length that is a power of 2. The function generates at least n_points and returns the first n_points.
        - For n_points not being a power of 2, extra points are generated and truncated.
        - Scrambling is enabled for improved uniformity.

    Examples:
        >>> from spotpython.design.sobol import Sobol
        >>> sobol_design = Sobol(k=3, seed=42)
        >>> sobol_points = sobol_design.generate_sobol_design(n_points=10)
        >>> print(sobol_points.shape)
        (10, 3)
    """
    if seed is not None:
        self.seed = seed
    sampler = qmc.Sobol(d=self.k, scramble=True, seed=seed)
    m = int(np.ceil(np.log2(n_points)))
    return sampler.random_base2(m=m)[:n_points, :]