normal#
- class brainstate.random.normal(loc=None, scale=None, size=None, key=None, dtype=None)#
Draw random samples from a normal (Gaussian) distribution.
The probability density function of the normal distribution, first derived by De Moivre and 200 years later by both Gauss and Laplace independently [2], is often called the bell curve because of its characteristic shape (see the example below).
The normal distributions occurs often in nature. For example, it describes the commonly occurring distribution of samples influenced by a large number of tiny, random disturbances, each with its own unique distribution [2].
- Parameters:
loc (float or array_like of floats) – Mean (“centre”) of the distribution.
scale (float or array_like of floats) – Standard deviation (spread or “width”) of the distribution. Must be non-negative.
size (
int|Sequence[int] |integer|Sequence[integer] |None) – Output shape. If the given shape is, e.g.,(m, n, k), thenm * n * ksamples are drawn. If size isNone(default), a single value is returned iflocandscaleare both scalars. Otherwise,np.broadcast(loc, scale).sizesamples are drawn.key (
int|Array|ndarray|None) – The key for the random number generator. If not given, the default random number generator is used.
- Returns:
out – Drawn samples from the parameterized normal distribution.
- Return type:
ndarray or scalar
Notes
The probability density for the Gaussian distribution is
\[p(x) = \frac{1}{\sqrt{ 2 \pi \sigma^2 }} e^{ - \frac{ (x - \mu)^2 } {2 \sigma^2} },\]where \(\mu\) is the mean and \(\sigma\) the standard deviation. The square of the standard deviation, \(\sigma^2\), is called the variance.
The function has its peak at the mean, and its “spread” increases with the standard deviation (the function reaches 0.607 times its maximum at \(x + \sigma\) and \(x - \sigma\) [2]). This implies that normal is more likely to return samples lying close to the mean, rather than those far away.
References
Examples
Draw samples from the distribution:
>>> import brainstate >>> import numpy as np >>> mu, sigma = 0, 0.1 # mean and standard deviation >>> s = brainstate.random.normal(mu, sigma, 1000) >>> print(s.shape) # (1000,) >>> print(abs(mu - np.mean(s)) < 0.1) # True (approximately correct mean) >>> print(abs(sigma - np.std(s, ddof=1)) < 0.1) # True (approximately correct std)
Two-by-four array of samples from the normal distribution with mean 3 and standard deviation 2.5:
>>> samples = brainstate.random.normal(3, 2.5, size=(2, 4)) >>> print(samples.shape) # (2, 4)