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 | floatlocTensor | float= 0.00.0.scaleTensor | float= 1.01.0.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Attributes
dfTensorlocTensorscaleTensorNotes
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,))Methods (7)
__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 | floatlocTensor | float= 0.00.0.scaleTensor | float= 1.01.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.0)mean
→Tensormean: TensorExpected 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. Following the convention of most distribution libraries, this property returns unconditionally — callers are responsible for checking when that matters.
Returns
TensorLocation parameter , shape batch_shape.
Examples
>>> StudentT(df=5.0, loc=3.0).mean
Tensor(3.0)variance
→Tensorvariance: TensorVariance 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.0)rsample
→Tensorrsample(sample_shape: tuple[int, ...] = ())Reparameterised sample: gradient flows through the Normal variate.
T = loc + scale · z / sqrt(g / df) where z ~ N(0,1)
is differentiable and g ~ Chi²(df) is detached (standard
practice — the marginal gradient w.r.t. df is not tracked).
sample
→Tensorsample(sample_shape: tuple[int, ...] = ())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,))log_prob
→Tensorlog_prob(value: Tensor)Log-density of value under the Student's t-distribution.
where .
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.1447)entropy
→Tensorentropy()Shannon 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.5310)