Skip to content

ghermite_scipy module

Tensor Gauss-Hermite quadrature for integrals against a Gaussian density.

gauss_hermite_grid(dim, points_per_dim) cached

Return tensor Gauss-Hermite nodes and normalized weights for N(0, I).

The result depends only on dim and points_per_dim, so it is cached. The returned arrays are shared between calls and must not be modified.

Parameters:

Name Type Description Default
dim int

Number of dimensions.

required
points_per_dim int

Number of quadrature points in each dimension.

required

Returns:

Type Description
ndarray

tuple[np.ndarray, np.ndarray]: standard_nodes, a

ndarray

(points_per_dim**dim, dim) array of nodes, and standard_weights,

tuple[ndarray, ndarray]

a (points_per_dim**dim,) array of weights that sum to 1.

Source code in frac_blp/ghermite_scipy.py
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
@lru_cache(maxsize=None)
def gauss_hermite_grid(dim: int, points_per_dim: int) -> tuple[np.ndarray, np.ndarray]:
    """Return tensor Gauss-Hermite nodes and normalized weights for N(0, I).

    The result depends only on ``dim`` and ``points_per_dim``, so it is cached.
    The returned arrays are shared between calls and must not be modified.

    Args:
        dim (int): Number of dimensions.
        points_per_dim (int): Number of quadrature points in each dimension.

    Returns:
        tuple[np.ndarray, np.ndarray]: ``standard_nodes``, a
        ``(points_per_dim**dim, dim)`` array of nodes, and ``standard_weights``,
        a ``(points_per_dim**dim,)`` array of weights that sum to 1.
    """
    # 1. Get 1D physicists' Gauss-Hermite nodes and strictly positive weights
    # They integrate against the weight function w(x) = exp(-x^2)
    x_1d, w_1d = roots_hermite(points_per_dim)

    # 2. Build the multi-dimensional grid using meshgrid
    mesh_args = [x_1d] * dim
    grids = np.meshgrid(*mesh_args, indexing="ij")
    standard_nodes = np.stack([g.ravel() for g in grids], axis=-1)

    # 3. Build the multi-dimensional weights (outer product of 1D weights)
    w_grids = np.meshgrid(*[w_1d] * dim, indexing="ij")
    standard_weights = np.prod(np.stack([w.ravel() for w in w_grids], axis=-1), axis=1)

    # Normalize weights so they sum up to 1 (converting physicists' weights to a probability distribution)
    # The weight function for physicists' Hermite is exp(-x^2), but normal distributions use exp(-x^2/2) / sqrt(2*pi)
    standard_weights = standard_weights / (np.pi ** (dim / 2))
    return standard_nodes, standard_weights

integrate_gaussian(f, dim_f, mu, sigma, points_per_dim=5)

Integrate f(X) for X ~ N(mu, sigma) by tensor Gauss-Hermite quadrature.

Supports up to 5 dimensions. The quadrature weights are positive.

Parameters:

Name Type Description Default
f Callable[[ndarray], ndarray | float]

Function of one node, a (dim,) array. Returns a scalar if dim_f is 1, and a (dim_f,) array otherwise.

required
dim_f int

Dimension of the output of f. Use 1 for a scalar.

required
mu ndarray

Mean of the Gaussian, a (dim,) array.

required
sigma ndarray

Covariance matrix of the Gaussian, a (dim, dim) positive definite array.

required
points_per_dim int

Number of quadrature points in each dimension. Default is 5.

5

Returns:

Type Description
ndarray | float

np.ndarray | float: The integral of f: a scalar if dim_f is 1,

ndarray | float

and a (dim_f,) array otherwise.

Raises:

Type Description
AssertionError

If mu has more than 5 elements.

Source code in frac_blp/ghermite_scipy.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def integrate_gaussian(
    f: Callable[[np.ndarray], np.ndarray | float],
    dim_f: int,
    mu: np.ndarray,
    sigma: np.ndarray,
    points_per_dim: int = 5,
) -> np.ndarray | float:
    """Integrate f(X) for X ~ N(mu, sigma) by tensor Gauss-Hermite quadrature.

    Supports up to 5 dimensions. The quadrature weights are positive.

    Args:
        f (Callable[[np.ndarray], np.ndarray | float]): Function of one
            node, a ``(dim,)`` array. Returns a scalar if ``dim_f`` is 1, and
            a ``(dim_f,)`` array otherwise.
        dim_f (int): Dimension of the output of ``f``. Use 1 for a scalar.
        mu (np.ndarray): Mean of the Gaussian, a ``(dim,)`` array.
        sigma (np.ndarray): Covariance matrix of the Gaussian, a ``(dim, dim)``
            positive definite array.
        points_per_dim (int): Number of quadrature points in each dimension.
            Default is 5.

    Returns:
        np.ndarray | float: The integral of ``f``: a scalar if ``dim_f`` is 1,
        and a ``(dim_f,)`` array otherwise.

    Raises:
        AssertionError: If ``mu`` has more than 5 elements.
    """
    dim = len(mu)
    assert dim <= 5, "This specific grid generation script is optimized for d <= 5"

    standard_nodes, standard_weights = gauss_hermite_grid(dim, points_per_dim)

    # 4. Transform nodes from Standard Normal space to the target Gaussian N(mu, sigma)
    # Handle covariance via Cholesky decomposition: X = mu + L * (sqrt(2) * standard_nodes)
    L = np.linalg.cholesky(sigma)
    transformed_nodes = mu + (np.sqrt(2) * standard_nodes) @ L.T

    # 5. Evaluate function and calculate the dot product
    # Every single weight in 'standard_weights' is strictly >
    if dim_f == 1:
        f_values = np.array([f(node) for node in transformed_nodes])
        integral = np.dot(f_values, standard_weights)
    else:
        f_values = np.zeros((dim_f, transformed_nodes.shape[0]))
        for i_node, node in enumerate(transformed_nodes):
            f_values[:, i_node] = f(node)
        integral = np.dot(f_values, standard_weights)

    return cast(np.ndarray | float, integral)

my_function(x)

Return the sum of squares of the coordinates of a point.

Parameters:

Name Type Description Default
x ndarray

A point, a vector of shape (d,).

required

Returns:

Name Type Description
float float

The sum of the squared entries of x.

Source code in frac_blp/ghermite_scipy.py
106
107
108
109
110
111
112
113
114
115
def my_function(x: np.ndarray) -> float:
    """Return the sum of squares of the coordinates of a point.

    Args:
        x (np.ndarray): A point, a vector of shape (d,).

    Returns:
        float: The sum of the squared entries of ``x``.
    """
    return float(np.sum(x**2))

my_function2(x)

Return the square of the first coordinate and the fourth power of the second.

Parameters:

Name Type Description Default
x ndarray

A point, a vector of shape (d,) with d >= 2.

required

Returns:

Type Description
ndarray

np.ndarray: The vector [x[0]**2, x[1]**4], of shape (2,).

Source code in frac_blp/ghermite_scipy.py
118
119
120
121
122
123
124
125
126
127
def my_function2(x: np.ndarray) -> np.ndarray:
    """Return the square of the first coordinate and the fourth power of the second.

    Args:
        x (np.ndarray): A point, a vector of shape (d,) with d >= 2.

    Returns:
        np.ndarray: The vector ``[x[0]**2, x[1]**4]``, of shape (2,).
    """
    return np.array([x[0] ** 2, x[1] ** 4])