xref: /aosp_15_r20/external/pytorch/torch/distributions/half_normal.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# mypy: allow-untyped-defs
2import math
3
4import torch
5from torch import inf
6from torch.distributions import constraints
7from torch.distributions.normal import Normal
8from torch.distributions.transformed_distribution import TransformedDistribution
9from torch.distributions.transforms import AbsTransform
10
11
12__all__ = ["HalfNormal"]
13
14
15class HalfNormal(TransformedDistribution):
16    r"""
17    Creates a half-normal distribution parameterized by `scale` where::
18
19        X ~ Normal(0, scale)
20        Y = |X| ~ HalfNormal(scale)
21
22    Example::
23
24        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
25        >>> m = HalfNormal(torch.tensor([1.0]))
26        >>> m.sample()  # half-normal distributed with scale=1
27        tensor([ 0.1046])
28
29    Args:
30        scale (float or Tensor): scale of the full Normal distribution
31    """
32    arg_constraints = {"scale": constraints.positive}
33    support = constraints.nonnegative
34    has_rsample = True
35
36    def __init__(self, scale, validate_args=None):
37        base_dist = Normal(0, scale, validate_args=False)
38        super().__init__(base_dist, AbsTransform(), validate_args=validate_args)
39
40    def expand(self, batch_shape, _instance=None):
41        new = self._get_checked_instance(HalfNormal, _instance)
42        return super().expand(batch_shape, _instance=new)
43
44    @property
45    def scale(self):
46        return self.base_dist.scale
47
48    @property
49    def mean(self):
50        return self.scale * math.sqrt(2 / math.pi)
51
52    @property
53    def mode(self):
54        return torch.zeros_like(self.scale)
55
56    @property
57    def variance(self):
58        return self.scale.pow(2) * (1 - 2 / math.pi)
59
60    def log_prob(self, value):
61        if self._validate_args:
62            self._validate_sample(value)
63        log_prob = self.base_dist.log_prob(value) + math.log(2)
64        log_prob = torch.where(value >= 0, log_prob, -inf)
65        return log_prob
66
67    def cdf(self, value):
68        if self._validate_args:
69            self._validate_sample(value)
70        return 2 * self.base_dist.cdf(value) - 1
71
72    def icdf(self, prob):
73        return self.base_dist.icdf((prob + 1) / 2)
74
75    def entropy(self):
76        return self.base_dist.entropy() - math.log(2)
77