Understanding `variance` property in PyTorch's Negative Binomial Distribution


  • Property
    variance - This property calculates the variance of the negative binomial distribution based on its parameters.
  • Class
    NegativeBinomial - This class represents the negative binomial distribution in PyTorch.

PyTorch doesn't explicitly define the variance property within the NegativeBinomial class itself. Instead, it relies on another property called mean. The actual calculation for variance is likely implemented using the following relationship:

Variance = mean / (1 - probability of success)

This formula relates the variance of a negative binomial distribution to its mean and the probability of success in each Bernoulli trial.



import torch

# Define parameters for the negative binomial distribution
success_prob = 0.4  # Probability of success in each trial
failure_count = 10  # Number of failures before stopping

# Create a negative binomial distribution object
dist = torch.distributions.NegativeBinomial(total_count=failure_count, probs=success_prob)

# Calculate the mean using another property (likely internally used for variance)
mean = dist.mean

# Calculate the variance using the formula
variance = mean / (1 - success_prob)

# Print the mean and variance
print("Mean of the negative binomial distribution:", mean)
print("Variance of the negative binomial distribution:", variance)

This code defines a negative binomial distribution with a specific probability of success and desired number of failures. Then, it creates a NegativeBinomial object using torch.distributions.

Since variance isn't directly exposed, we use the mean property (which is likely used internally for variance calculation). Finally, we calculate the variance using the provided formula and print both the mean and variance values.



  1. Manual Calculation
    As shown in the previous example, you can calculate the variance yourself using the formula:
variance = mean / (1 - success_prob)

This approach offers flexibility but requires you to remember the formula.

  1. Custom Function
    You can define a custom function that takes the NegativeBinomial object or its parameters (total_count and probs) and returns the variance:
import torch

def calculate_negative_binomial_variance(dist):
  """
  Calculates the variance of a negative binomial distribution.

  Args:
      dist: A torch.distributions.NegativeBinomial object.

  Returns:
      A torch.Tensor representing the variance.
  """
  mean = dist.mean
  success_prob = dist.probs  # Assuming probs is defined
  variance = mean / (1 - success_prob)
  return variance

# Usage example
dist = torch.distributions.NegativeBinomial(total_count=10, probs=0.4)
variance = calculate_negative_binomial_variance(dist)
print(variance)

This approach gives you more control and reusability.

  1. Third-Party Libraries
    Some libraries like scipy.stats (if you're using NumPy alongside PyTorch) might offer functions for calculating the variance of a negative binomial distribution. However, using these functions would involve converting your PyTorch tensors to NumPy arrays and back, which might be less efficient.