covmats.CovViaSparsePrecisionCholesky.sample_mvnormal#

CovViaSparsePrecisionCholesky.sample_mvnormal(shape: Sequence[int], random_state: int | RandomState | Generator | None = None) ndarray[tuple[Any, ...], dtype[float64]]#

Draw samples from the multivariate normal N(0, A).

Parameters:
  • shape (Sequence[int]) – Number of random vectors to sample. The resulting array will be of shape (shape, n), n being the number of elements per random vector (the covariance) matrix has shape (n, n).

  • random_state (Optional[Union[int, np.random.Generator, np.random.RandomState]]) – Pseudorandom number generator state used to generate resamples. If random_state is None (or np.random), the numpy.random.RandomState singleton is used. If random_state is an int, a new RandomState instance is used, seeded with random_state. If random_state is already a Generator or RandomState instance then that instance is used. The default is None.

Returns:

  • X (The transformed array of points. It has shape (input shape, n), n being)

  • the number of elements per random vector (the covariance)

  • matrix has shape (n, n).

Examples

>>> import numpy as np
>>> import scipy as sp
>>> import covmats
>>> covd = covmats.CovViaDiagonal(np.array([5.0, 10.0, 15.0]))
>>> rng_seed = 42
>>> covd.sample_mvnormal(shape=[2], random_state=rng_seed)
array([[ 1.11068661, -0.43723011,  2.50848692],
    [ 3.40559829, -0.74045799, -0.90680853]])
>>> x = covd.sample_mvnormal(shape=[2, 4], random_state=rng_seed)
>>> x
array([[[ 1.11068661, -0.43723011,  2.50848692],
        [ 3.40559829, -0.74045799, -0.90680853],
        [ 3.53122721,  2.4268417 , -1.81826648],
        [ 1.21320114, -1.46545542, -1.80376358]],
       [[ 0.54104409, -6.05032338, -6.68057804],
        [-1.25731314, -3.20285323,  1.21707469],
        [-2.03040356, -4.46609644,  5.67643327],
        [-0.50485116,  0.21354293, -5.518026  ]]])
>>> x.shape
(2, 4, 3)
>>> cov_cho = covmats.CovViaCholesky(sp.linalg.cholesky(covd.todense()))
>>> cov_cho.sample_mvnormal(shape=[2], random_state=rng_seed)
array([[-0.95777013, -1.11354406,  2.06162461],
    [ 0.81715777,  1.30517512,  1.66856257]])
>>> cov_cho.sample_mvnormal(shape=[2, 2], random_state=rng_seed)
array([[[ 1.11068661, -0.43723011,  2.50848692],
        [ 3.40559829, -0.74045799, -0.90680853],
        [ 3.53122721,  2.4268417 , -1.81826648],
        [ 1.21320114, -1.46545542, -1.80376358]],
       [[ 0.54104409, -6.05032338, -6.68057804],
        [-1.25731314, -3.20285323,  1.21707469],
        [-2.03040356, -4.46609644,  5.67643327],
        [-0.50485116,  0.21354293, -5.518026  ]]])