Alternatives to `Distribution.support` for Exploring Probability Distribution Support in PyTorch


Support in Probability Distributions

  • For example, the support of a uniform distribution between 0 and 1 is the interval [0, 1]. Values outside this range (like -2 or 3) have a probability of zero of being drawn.
  • The support of a probability distribution refers to the set of all possible values that a random variable drawn from that distribution can take.

Distribution.support in PyTorch

  • However, it's important to note that currently, it's only fully implemented for discrete distributions.
    • For these distributions, support returns a representation of the possible values the distribution can take.
  • This property aims to represent the support of the probability distribution.
  • The documentation for specific distributions within torch.distributions might mention their support or how it's handled.
    • It's recommended to refer to the documentation for the specific distribution you're using for details on its support.
  • For continuous distributions, support might not be directly implemented or provide a complete picture.
    • Continuous distributions can theoretically take on an infinite number of values within a specific range.


Discrete Distribution Example (Bernoulli)

import torch
from torch.distributions import Bernoulli

# Define a Bernoulli distribution with probability of success 0.7
bernoulli = Bernoulli(probs=0.7)

# Access the support (possible values: 0 or 1)
support = bernoulli.support
print(support)  # Output: tensor([ 0.,  1.])

This code defines a Bernoulli distribution, which is a discrete distribution representing coin flips (0: heads, 1: tails). The support property correctly returns a tensor with the two possible values (0 and 1).

Continuous Distribution Example (Normal)

import torch
from torch.distributions import Normal

# Define a normal distribution with mean 0 and standard deviation 1
normal = Normal(loc=0, scale=1)

try:
  # Attempt to access support (might raise an error)
  support = normal.support
  print(support)
except NotImplementedError:
  print("Support not fully implemented for continuous distributions.")

This code defines a normal distribution. Currently, support isn't fully implemented for continuous distributions, so attempting to access it might raise a NotImplementedError. This highlights the limitation of support for continuous cases.

  • For continuous distributions, refer to the specific distribution's documentation for information on its support.
  • support is most reliable for discrete distributions.


Manual Definition (Discrete Distributions)

  • This might involve creating a tensor with the allowed values.
  • If you know the exact range of possible values for a discrete distribution, you can define it manually.

Example (Binomial Distribution)

# Define a binomial distribution with total count 10 and probability 0.5
total_count = 10
probs = 0.5

# Manually define support (possible values from 0 to 10)
support = torch.arange(0, total_count + 1)
print(support)  # Output: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Visualization (Continuous Distributions)

  • Libraries like matplotlib can be used to plot the PDF.
  • For continuous distributions, visualizing the probability density function (PDF) can give you a good idea of the support.

Example (Normal Distribution)

import matplotlib.pyplot as plt
from torch.distributions import Normal

# Define a normal distribution with mean 0 and standard deviation 1
normal = Normal(loc=0, scale=1)

# Sample values and plot the PDF
x = torch.linspace(-5, 5, 100)
y = normal.pdf(x)
plt.plot(x.numpy(), y.numpy())
plt.xlabel("x")
plt.ylabel("Probability Density")
plt.show()

Documentation Reference

  • It might mention the support or how it's handled for that particular distribution.
  • Consult the documentation for the specific distribution you're using.

Custom Logic (Advanced)

  • For advanced cases, you might write custom logic to define the support based on the distribution parameters.
  • Custom logic is best reserved for complex scenarios where built-in functionalities fall short.
  • Refer to documentation for specific details on a distribution's support.
  • Visualization is a good option for continuous distributions to get a qualitative understanding.
  • For simple understanding of support in discrete distributions, manual definition might suffice.