Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Pyro ppl Pyro AffineBeta

From Leeroopedia


Knowledge Sources
Domains Probability_Distributions, Deep_Learning
Last Updated 2026-02-09 09:00 GMT

Overview

A Beta distribution scaled and shifted by an affine transformation, supporting bounded random variables on arbitrary intervals.

Description

The AffineBeta distribution is a TransformedDistribution that applies an affine transformation to a standard Beta distribution. Given a Beta-distributed random variable X, the AffineBeta produces Y = loc + scale * X, which has support on the interval [loc, loc + scale].

The distribution is parameterized by:

  • concentration1 (alpha) -- The first concentration parameter of the underlying Beta distribution. Controls the shape near the lower bound.
  • concentration0 (beta) -- The second concentration parameter of the underlying Beta distribution. Controls the shape near the upper bound.
  • loc -- The location (shift) parameter, equal to the lower bound of the support.
  • scale -- The scale parameter, where the upper bound equals loc + scale.

The class extends Pyro's TransformedDistribution with a Beta base distribution and an AffineTransform. It provides both sample and rsample methods, each applying the affine transform and then clamping the output to avoid numerical issues at the boundaries. The clamping uses a machine-epsilon-based buffer (eps * scale) to prevent NaN and Inf values in gradients that can occur at exact boundary values.

Key computed properties include:

  • low -- The lower bound of the distribution, equal to loc.
  • high -- The upper bound of the distribution, equal to loc + scale.
  • sample_size -- The sum concentration1 + concentration0, representing the effective sample size of the Beta prior.
  • mean -- Computed as loc + scale * Beta.mean.
  • variance -- Computed as scale^2 * Beta.variance.

Usage

Use AffineBeta when you need a bounded distribution on an arbitrary interval [loc, loc + scale] rather than the standard [0, 1] interval of a Beta distribution. This is common in Bayesian models where parameters have known physical bounds, or when modeling proportions relative to a known range.

Code Reference

Source Location

Signature

class AffineBeta(TransformedDistribution):
    arg_constraints = {
        "concentration1": constraints.positive,
        "concentration0": constraints.positive,
        "loc": constraints.real,
        "scale": constraints.positive,
    }

    def __init__(self, concentration1, concentration0, loc, scale, validate_args=None)

Import

from pyro.distributions import AffineBeta

I/O Contract

Inputs

Name Type Required Description
concentration1 float or torch.Tensor Yes First concentration parameter (alpha) of the Beta distribution. Must be positive.
concentration0 float or torch.Tensor Yes Second concentration parameter (beta) of the Beta distribution. Must be positive.
loc float or torch.Tensor Yes Location (shift) parameter. Defines the lower bound of the support.
scale float or torch.Tensor Yes Scale parameter. Must be positive. Upper bound is loc + scale.
validate_args bool or None No Whether to enable argument validation.

Outputs

Name Type Description
sample torch.Tensor Samples from the affine-transformed Beta, clamped to (low + eps, high - eps) for numerical stability.
rsample torch.Tensor Reparameterized samples with the same clamping behavior.
log_prob torch.Tensor Log probability density (inherited from TransformedDistribution).
mean torch.Tensor Mean of the distribution: loc + scale * alpha / (alpha + beta).
variance torch.Tensor Variance of the distribution: scale^2 * alpha * beta / ((alpha + beta)^2 * (alpha + beta + 1)).
low torch.Tensor Lower bound of the support (equals loc).
high torch.Tensor Upper bound of the support (equals loc + scale).
support constraints.interval The interval constraint [low, high].

Usage Examples

Basic Usage

import torch
from pyro.distributions import AffineBeta

# Beta distribution on [0, 10] with mode near 3
dist = AffineBeta(
    concentration1=torch.tensor(3.0),
    concentration0=torch.tensor(7.0),
    loc=torch.tensor(0.0),
    scale=torch.tensor(10.0),
)

samples = dist.sample(torch.Size([1000]))
print(samples.min().item(), samples.max().item())  # approximately 0..10

log_p = dist.log_prob(torch.tensor(3.0))
print(dist.mean)      # tensor(3.0)
print(dist.variance)  # tensor(1.9091)

Modeling Bounded Parameters

import pyro
import pyro.distributions as dist
import torch

# Temperature constrained between -40 and 50 degrees
temp = pyro.sample(
    "temperature",
    dist.AffineBeta(
        concentration1=torch.tensor(5.0),
        concentration0=torch.tensor(3.0),
        loc=torch.tensor(-40.0),
        scale=torch.tensor(90.0),  # high = -40 + 90 = 50
    ),
)

Batched Distribution

import torch
from pyro.distributions import AffineBeta

# Batch of 4 distributions with different bounds
dist = AffineBeta(
    concentration1=torch.ones(4) * 2,
    concentration0=torch.ones(4) * 2,
    loc=torch.tensor([0.0, 1.0, -1.0, 5.0]),
    scale=torch.tensor([1.0, 2.0, 3.0, 0.5]),
)

samples = dist.sample()
print(samples.shape)  # torch.Size([4])
print(dist.low)       # tensor([ 0.,  1., -1.,  5.])
print(dist.high)      # tensor([1., 3., 2., 5.5])

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment