StudentT
DistributionStudentT(df: Tensor | float, loc: Tensor | float = 0.0, scale: Tensor | float = 1.0, validate_args: bool | None = None)Student's t-distribution with location, scale, and degrees of freedom.
StudentT(df=ν, loc=μ, scale=σ) defines the three-parameter
location-scale generalisation of Student's t. It arises naturally in:
- Bayesian inference: the posterior predictive for a Normal likelihood with unknown mean and variance (Normal-InverseGamma conjugate model).
- Robust regression: as a heavy-tailed alternative to the Normal for outlier-tolerant models.
- Limit behaviour: as , the t-distribution converges to .
Parameters
dfTensor | float0.0.1.0.validate_ argsbool | None= NoneTrue, validate parameter constraints at construction time.Attributes
Notes
PDF:
Log-PDF:
where .
Moments:
- Mean ():
- Variance ():
- The distribution has no finite variance for and no finite mean for .
Reparameterised sampling uses the representation
where is the differentiable variate and is detached (the Gamma sampler uses rejection sampling whose path is not differentiable w.r.t. ).
Examples
>>> import lucid
>>> from lucid.distributions import StudentT
>>> # Heavy-tailed (Cauchy)
>>> cauchy = StudentT(df=1.0, loc=0.0, scale=1.0)
>>> # Near-Normal
>>> approx_normal = StudentT(df=100.0, loc=0.0, scale=1.0)
>>> samples = approx_normal.rsample((500,))Used by 1
Constructors
1__init__
→None__init__(df: Tensor | float, loc: Tensor | float = 0.0, scale: Tensor | float = 1.0, validate_args: bool | None = None)Construct a Student's t-distribution.
Parameters
dfTensor | float0.0.1.0.validate_ argsbool | None= NoneTrue, validate parameter constraints at construction time.Notes
All three parameters are broadcast against each other so that
batches with mixed scalar / tensor parameters work naturally.
The resulting batch_shape is the broadcast shape of
(df, loc, scale).
Examples
>>> from lucid.distributions import StudentT
>>> d = StudentT(df=5.0, loc=2.0, scale=0.5)
>>> d.mean
tensor(2.)Properties
3Expected value of the Student's t-distribution.
The mean is only mathematically defined for . For
(e.g., the Cauchy distribution) the first
moment does not exist and this property returns nan, as the
reference framework does. The mode, which exists for every
, is .
Returns
TensorLocation parameter where , nan
elsewhere; shape batch_shape.
Examples
>>> StudentT(df=5.0, loc=3.0).mean
tensor(3.)Mode of Student's t: the location , for every df.
Variance of the Student's t-distribution.
The variance is only finite for . For the distribution has a defined mean but infinite variance. For neither moment exists. This property computes algebraically; the caller must guard against as the result will be negative or infinite in those cases.
Returns
TensorVariance , shape batch_shape.
Examples
>>> StudentT(df=4.0, scale=1.0).variance # 4/(4-2) = 2.0
tensor(2.)Instance methods
5Shannon entropy of the Student's t-distribution (in nats).
where is the Beta function and is the digamma function.
As this converges to the entropy of , which is .
Returns
TensorEntropy in nats, shape batch_shape.
Examples
>>> StudentT(df=1.0, loc=0.0, scale=1.0).entropy() # Cauchy: log(4π) ≈ 2.531
tensor(2.531)Return a new StudentT with batch dimensions expanded to batch_shape.
Log-density of value under the Student's t-distribution.
where .
The difference and the
term are evaluated without cancelling, so the density, and its
gradients in df, loc, scale and value, hold to a few
ulp for any from 1e-3 to 1e30 and beyond. As
it tends to the Normal log-density, which
is also the answer at df = inf.
Parameters
valueTensorReturns
TensorElement-wise log-densities, shape batch_shape.
Examples
>>> d = StudentT(df=1.0, loc=0.0, scale=1.0) # Cauchy
>>> d.log_prob(lucid.tensor(0.0)) # -log(π) ≈ -1.145
tensor(-1.145)Reparameterised sample: gradient flows through both the Normal and Chi² variates.
T = loc + scale · z / sqrt(g / df) where z ~ N(0,1) and
g ~ Chi²(df) = Gamma(df/2, 1/2).
Draw non-differentiable samples from the Student's t-distribution.
Wraps rsample inside a no_grad context so that the
returned samples have no gradient history. Use rsample
directly when gradient flow through the location-scale
reparameterisation is required.
Parameters
sample_ shapetuple[int, ...]= ()().Returns
TensorDetached samples of shape sample_shape + batch_shape.
Examples
>>> d = StudentT(df=3.0, loc=0.0, scale=1.0)
>>> x = d.sample((200,))