|
1 | 1 | import torch |
2 | | -from torch.distributions import Distribution |
| 2 | +from torch.distributions import Distribution, constraints |
| 3 | +from functools import cached_property |
| 4 | +from typing import Any, Optional |
3 | 5 |
|
4 | 6 |
|
5 | 7 | class SamplingDistribution(Distribution): |
6 | | - def __init__(self, base_distribution: Distribution, n=100): |
| 8 | + """ |
| 9 | + A wrapper for a PyTorch distribution that calculates statistics via sampling. |
| 10 | +
|
| 11 | + This distribution is useful when the analytical statistics of a base |
| 12 | + distribution are not available or not desired. Instead, it computes |
| 13 | + properties like mean, stddev, variance, and mode by drawing samples from the |
| 14 | + base distribution. |
| 15 | +
|
| 16 | + To improve efficiency, it caches the generated samples and the computed |
| 17 | + statistics, ensuring that repeated access to these properties does not |
| 18 | + trigger redundant computations. |
| 19 | +
|
| 20 | + Args: |
| 21 | + base_distribution (Distribution): The underlying distribution to sample from. |
| 22 | + n (int, optional): The number of samples to draw for calculating |
| 23 | + statistics. Defaults to 100. |
| 24 | + """ |
| 25 | + |
| 26 | + __slots__ = ["base_dist", "n"] |
| 27 | + |
| 28 | + def __init__(self, base_distribution: Distribution, *, n: int = 100): |
| 29 | + if not isinstance(base_distribution, Distribution): |
| 30 | + raise TypeError( |
| 31 | + "base_distribution must be a torch.distributions.Distribution" |
| 32 | + ) |
| 33 | + if not isinstance(n, int) or n <= 0: |
| 34 | + raise ValueError("n must be a positive integer") |
| 35 | + |
7 | 36 | self.base_dist = base_distribution |
8 | 37 | self.n = n |
9 | 38 |
|
10 | | - def __getattr__(self, name): |
| 39 | + super().__init__( |
| 40 | + batch_shape=self.base_dist.batch_shape, |
| 41 | + event_shape=self.base_dist.event_shape, |
| 42 | + validate_args=False, # We defer validation to the base distribution |
| 43 | + ) |
| 44 | + |
| 45 | + def __repr__(self) -> str: |
| 46 | + return f"SamplingDistribution(base_dist={self.base_dist}, n={self.n})" |
| 47 | + |
| 48 | + def __getattr__(self, name: str) -> Any: |
| 49 | + """Delegates attribute access to the base distribution.""" |
11 | 50 | return getattr(self.base_dist, name) |
12 | 51 |
|
| 52 | + @cached_property |
| 53 | + def _samples(self) -> torch.Tensor: |
| 54 | + """ |
| 55 | + Cached samples from the base distribution. |
| 56 | +
|
| 57 | + Uses rsample if available for reparameterization-friendly gradients, |
| 58 | + otherwise falls back to sample. |
| 59 | + """ |
| 60 | + sample_shape = torch.Size((self.n,)) |
| 61 | + if self.base_dist.has_rsample: |
| 62 | + return self.base_dist.rsample(sample_shape) |
| 63 | + return self.base_dist.sample(sample_shape) |
| 64 | + |
13 | 65 | @property |
14 | | - def has_rsample(self): |
| 66 | + def has_rsample(self) -> bool: |
15 | 67 | return self.base_dist.has_rsample |
16 | 68 |
|
17 | | - def rsample(self, sample_shape=torch.Size()): |
| 69 | + def rsample(self, sample_shape: Any = torch.Size()) -> torch.Tensor: |
| 70 | + """Delegates rsample to the base distribution.""" |
18 | 71 | return self.base_dist.rsample(sample_shape) |
19 | 72 |
|
20 | | - def sample(self, sample_shape=torch.Size()): |
| 73 | + def sample(self, sample_shape: Any = torch.Size()) -> torch.Tensor: |
| 74 | + """Delegates sample to the base distribution.""" |
21 | 75 | return self.base_dist.sample(sample_shape) |
22 | 76 |
|
23 | | - @property |
24 | | - def mean(self): |
25 | | - return self.base_dist.rsample((self.n,)).mean(0) |
| 77 | + @cached_property |
| 78 | + def mean(self) -> torch.Tensor: # type: ignore |
| 79 | + """Mean of the distribution, computed as the mean of cached samples.""" |
| 80 | + return self._samples.float().mean(0) |
26 | 81 |
|
27 | | - @property |
28 | | - def stddev(self): |
29 | | - return self.base_dist.rsample((self.n,)).std(0) |
| 82 | + @cached_property |
| 83 | + def stddev(self) -> torch.Tensor: # type: ignore |
| 84 | + """Standard deviation of the distribution, computed from cached samples.""" |
| 85 | + return self._samples.float().std(0) |
30 | 86 |
|
31 | | - @property |
32 | | - def variance(self): |
33 | | - return self.base_dist.rsample((self.n,)).var(0) |
| 87 | + @cached_property |
| 88 | + def variance(self) -> torch.Tensor: # type: ignore |
| 89 | + """Variance of the distribution, computed from cached samples.""" |
| 90 | + return self._samples.float().var(0) |
34 | 91 |
|
35 | | - @property |
36 | | - def mode(self): |
37 | | - samples = self.base_dist.sample((self.n,)) |
38 | | - log_probs = self.base_dist.log_prob(samples).view(self.n, -1) |
39 | | - index = torch.argmax(log_probs, dim=0) |
| 92 | + @cached_property |
| 93 | + def mode(self) -> torch.Tensor: # type: ignore |
| 94 | + """ |
| 95 | + Mode of the distribution. |
| 96 | +
|
| 97 | + Tries to return the analytical mode if available. Otherwise, it computes |
| 98 | + the mode via Monte Carlo approximation by finding the sample with the |
| 99 | + highest log probability. |
| 100 | + """ |
| 101 | + try: |
| 102 | + return self.base_dist.mode |
| 103 | + except (AttributeError, NotImplementedError): |
| 104 | + pass # Fall back to sampling |
| 105 | + |
| 106 | + log_probs = self.base_dist.log_prob(self._samples) |
| 107 | + max_indices = torch.argmax(log_probs, dim=0) |
40 | 108 |
|
41 | | - selected = torch.gather(samples.view(self.n, -1), 0, index.unsqueeze(0)) |
42 | | - return selected |
| 109 | + # Use advanced indexing to gather the modes efficiently |
| 110 | + return self._samples.gather( |
| 111 | + 0, max_indices.reshape(1, *max_indices.shape, *(1,) * len(self.event_shape)) |
| 112 | + ).squeeze(0) |
43 | 113 |
|
44 | | - def entropy(self): |
45 | | - samples = self.base_dist.rsample((self.n,)) |
46 | | - logprob = self.base_dist.log_prob(samples) |
47 | | - return -logprob.mean(0) |
| 114 | + def entropy(self) -> torch.Tensor: |
| 115 | + """ |
| 116 | + Entropy of the distribution, estimated via Monte Carlo. |
48 | 117 |
|
49 | | - def log_prob(self, value): |
| 118 | + Calculates the negative mean of the log probabilities of cached samples. |
| 119 | + """ |
| 120 | + log_prob = self.base_dist.log_prob(self._samples) |
| 121 | + return -log_prob.mean(0) |
| 122 | + |
| 123 | + def log_prob(self, value: torch.Tensor) -> torch.Tensor: |
| 124 | + """Delegates log probability calculation to the base distribution.""" |
50 | 125 | return self.base_dist.log_prob(value) |
| 126 | + |
| 127 | + @property |
| 128 | + def support(self) -> Optional[constraints.Constraint]: |
| 129 | + """Delegates support to the base distribution.""" |
| 130 | + return self.base_dist.support |
| 131 | + |
| 132 | + @property |
| 133 | + def arg_constraints(self) -> dict: |
| 134 | + """Delegates argument constraints to the base distribution.""" |
| 135 | + return self.base_dist.arg_constraints |
0 commit comments