Skip to content

parameterspace.priors.truncated_normal

TruncatedNormal (BasePrior)

Truncated normal prior for bounded parameters.

Source code in parameterspace/priors/truncated_normal.py
class TruncatedNormal(BasePrior):
    """Truncated normal prior for bounded parameters."""

    @store_init_arguments
    def __init__(self, mean: float = 0.5, std: float = 1):
        """
        Args:
            mean: Mean of the distribution.
            std: Standard deviation of the distribution
        """
        super().__init__((0, 1))
        a, b = (self.bounds - mean) / std
        self.mean = mean
        self.std = std
        self.sps_dist = sps.truncnorm(a, b, loc=mean, scale=std)

    def pdf(self, value):
        return self.sps_dist.pdf(value)

    def loglikelihood(self, value):
        return self.sps_dist.logpdf(value)

    def sample(self, num_samples=None, random_state=npr):
        return self.sps_dist.rvs(size=num_samples, random_state=random_state)

    def __repr__(self):
        return (
            f"Truncated normal (Interval [{self.bounds[0]}, {self.bounds[1]}]) with "
            + f"parameters mean={self.mean}, std={self.std}"
        )

    def __eq__(self, other):
        return super().__eq__(other) and np.allclose(
            [self.mean, self.std], [other.mean, other.std]
        )

loglikelihood(self, value)

Compute the log PDF (up to an additive constant) of a given value.

Note

Values for the priors are always after the transformation!

Parameters:

Name Type Description Default
value

[description]

required

Returns:

Type Description

[descriptions]

Source code in parameterspace/priors/truncated_normal.py
def loglikelihood(self, value):
    return self.sps_dist.logpdf(value)

pdf(self, value)

Computes the PDF of a given value.

Note

Values for the priors are always after the transformation!

Parameters:

Name Type Description Default
value

[description]

required

Returns:

Type Description

[descriptions]

Source code in parameterspace/priors/truncated_normal.py
def pdf(self, value):
    return self.sps_dist.pdf(value)

sample(self, num_samples=None, random_state=<module 'numpy.random' from '/home/runner/.cache/pypoetry/virtualenvs/parameterspace-9AYrJA9h-py3.8/lib/python3.8/site-packages/numpy/random/__init__.py'>)

Draw random samples from the prior.

Parameters:

Name Type Description Default
num_samples

[description]

None

Returns:

Type Description

[descriptions]

Source code in parameterspace/priors/truncated_normal.py
def sample(self, num_samples=None, random_state=npr):
    return self.sps_dist.rvs(size=num_samples, random_state=random_state)