Normal

class Normal[source]

Bases: ContinuousDistribution

Sampling from and properties of the normal distribution.

Parameters

\(\mu \in \mathbb{R}\), mean

\(\sigma \in \mathbb{R}_+^*\), standard deviation

Support

\(x \in \mathbb{R}\)

classmethod cdf(support: Tensor | float, loc: Tensor | float, scale: Tensor | float) Tensor[source]

Computes the cumulative distribution function.

\[P(X \leq x; \mu, \sigma) = \frac{1}{2} \left[ 1 + \text{erf} \left( \frac{x - \mu}{\sigma \sqrt{2}} \right) \right]\]
Parameters:
Returns:

resulting cumulative probabilities.

Return type:

torch.Tensor

classmethod logcdf(support: Tensor | float, loc: Tensor | float, scale: Tensor | float) Tensor[source]

Computes the natural logarithm of the cumulative distribution function.

\[\log P(X \leq x; \mu, \sigma) = \log \frac{1}{2} \left[ 1 + \text{erf} \left( \frac{x - \mu}{\sigma \sqrt{2}} \right) \right]\]
Parameters:
Returns:

log of the resulting cumulative probabilities.

Return type:

torch.Tensor

classmethod logpdf(support: Tensor | float, loc: Tensor | float, scale: Tensor | float) Tensor[source]

Computes the natural logarithm of the probability density function.

\[\log P(X=x; \mu, \sigma) = - \log \sigma - \frac{1}{2} \left[ \log 2 \pi + \left(\frac{\mu - x}{\sigma}\right)^2 \right]\]
Parameters:
Returns:

log of the resulting relative likelihoods.

Return type:

torch.Tensor

classmethod mean(loc: Tensor | float) Tensor[source]

Computes the expected value of the distribution.

\[\text{E}[X \mid X \sim \mathcal{N}(\mu, \sigma)] = \mu\]
Parameters:

loc (torch.Tensor | float) – mean of the distribution, \(\mu\).

Returns:

mean of the distribution with given parameters.

Return type:

torch.Tensor

classmethod params_mv(mean: Tensor | float, variance: Tensor | float) tuple[Tensor, Tensor][source]

Computes parameters of the normal distribution targeting a given mean and variance.

Computes the location \(\mu\) and scale \(\sigma\) as follows.

\[\mu = \mu_X \qquad \sigma = \sqrt{\sigma_X^2}\]
Parameters:
Returns:

tuple of the corresponding loc and scale.

Return type:

tuple[torch.Tensor, torch.Tensor]

classmethod pdf(support: Tensor | float, loc: Tensor | float, scale: Tensor | float) Tensor[source]

Computes the probability density function.

\[P(X=x; \mu, \sigma) = \frac{1}{\sigma \sqrt{2 \pi}} \exp \left( - \frac{1}{2} \left( \frac{x - \mu}{\sigma} \right)^2 \right)\]
Parameters:
Returns:

resulting relative likelihoods.

Return type:

torch.Tensor

classmethod sample(loc: Tensor | float, scale: Tensor | float, generator: Generator | None = None) Tensor[source]

Samples random variates from a normal distribution.

\[X \sim \mathcal{N}(\mu, \sigma)\]
Parameters:
  • loc (torch.Tensor | float) – mean of the distribution, \(\mu\).

  • scale (torch.Tensor | float) – standard deviation of the distribution, \(\sigma\).

  • generator (torch.Generator | None, optional) – pseudorandom number generator to use for sampling. Defaults to None.

Returns:

resulting random variates \(X\).

Return type:

torch.Tensor

classmethod sample_mv(mean: Tensor | float, variance: Tensor | float, generator: Generator | None = None) Tensor[source]

Samples random variates with desired mean and variance from a normal distribution.

\[\log X \sim \mathcal{N}\left(\mu, \sqrt{\sigma_X^2}\right)\]
Parameters:
  • mean (torch.Tensor | float) – target sample mean, \(\mu_X\).

  • variance (torch.Tensor | float) – target sample variance, \(\sigma_X^2\).

  • generator (torch.Generator | None, optional) – pseudorandom number generator to use for sampling. Defaults to None.

Returns:

resulting random variates \(X\).

Return type:

torch.Tensor

classmethod validate(loc: Tensor | float | None = None, scale: Tensor | float | None = None, support: Tensor | float | None = None) dict[str, bool | None][source]

Tests if the arguments are valid for a Normal distribution.

Parameters:
  • loc (torch.Tensor | float) – mean of the distribution, \(\mu\).

  • scale (torch.Tensor | float | None, optional) – standard deviation of the distribution, \(\sigma\). Defaults to None.

  • support (torch.Tensor | float | None, optional) – location of observation, \(x\). Defaults to None.

Returns:

argument name and if it is valid, returned as a tensor of dtype torch.bool if a non-scalar tensor is given, None if not given.

Return type:

dict[str, torch.Tensor | bool | None]

classmethod variance(scale: Tensor | float) Tensor[source]

Computes the variance of the distribution.

\[\text{Var}[X \mid X \sim \mathcal{N}(\mu, \sigma)] = \sigma^2\]
Parameters:

scale (torch.Tensor | float) – standard deviation of the distribution, \(\sigma\).

Returns:

variance of the distribution with given parameters.

Return type:

torch.Tensor