Attend the Women in ML Symposium on December 7

# tfp.distributions.HalfStudentT

Stay organized with collections Save and categorize content based on your preferences.

Half-Student's t distribution.

Inherits From: `Distribution`, `AutoCompositeTensor`

The half-Student's t distribution has three parameters: degree of freedom `df`, location `loc`, and scale `scale`. It represents the right half of the two symmetric halves in a Student's t distribution.

#### Mathematical Details

The probability density function (pdf) for the half-Student's t distribution is given by

``````pdf(x; df, loc, scale) = (1 + y**2 / df)**(-0.5 (df + 1)) / Z,
where
y = (x - loc) / scale
Z = 2 * scale * sqrt(df * pi) * gamma(0.5 * df) / gamma(0.5 * (df + 1))

``````

where:

• `df` is a positive scalar in `R`,
• `loc` is a scalar in `R`,
• `scale` is a positive scalar in `R`,
• `Z` is the normalization constant, and
• `Gamma` is the gamma function.

The support of the distribution is given by the interval `[loc, infinity)`.

Samples of this distribution are reparameterized (pathwise differentiable). The derivatives are computed using the approach described in the paper

Michael Figurnov, Shakir Mohamed, Andriy Mnih. Implicit Reparameterization Gradients, 2018

#### Examples

``````import tensorflow_probability as tfp
tfd = tfp.distributions

# Define a single scalar Student t distribution.
single_dist = tfd.HalfStudentT(df=3, loc=0, scale=1)

# Evaluate the pdf at 1, returning a scalar Tensor.
single_dist.prob(1.)

# Define a batch of two scalar valued half Student t's.
# The first has degrees of freedom 2, mean 1, and scale 11.
# The second 3, 2 and 22.
multi_dist = tfd.HalfStudentT(df=[2, 3], loc=[1, 2], scale=[11, 22])

# Evaluate the pdf of the first distribution at 1.5, and the second on 2.5,
# returning a length two tensor.
multi_dist.prob([1.5, 2.5])

# Get 3 samples, returning a 3 x 2 tensor.
multi_dist.sample(3)
``````

Arguments are broadcast when possible.

``````# Define a batch of two half Student's t distributions.
# Both have df 2 and mean 1, but different scales.
dist = tfd.HalfStudentT(df=2, loc=1, scale=[11, 22.])

# Evaluate the pdf of both distributions on the same point, 3.0,
# returning a length 2 tensor.
dist.prob(3.0)
``````

Compute the gradients of samples w.r.t. the parameters via implicit reparameterization through the gamma:

``````df = tf.constant(2.0)
loc = tf.constant(2.0)
scale = tf.constant(11.0)
dist = tfd.HalfStudentT(df=df, loc=loc, scale=scale)
with tf.GradientTape() as tape:
tape.watch((df, loc, scale))
loss = tf.reduce_mean(dist.sample(5))
# Unbiased stochastic gradients of the loss function
grads = tape.gradient(loss, (df, loc, scale))
``````

`df` Floating-point `Tensor`. The degrees of freedom of the distribution(s). `df` must contain only positive values.
`loc` Floating-point `Tensor`; the location(s) of the distribution(s).
`scale` Floating-point `Tensor`; the scale(s) of the distribution(s). Must contain only positive values.
`validate_args` Python `bool`, default `False`. When `True` distribution parameters are checked for validity despite possibly degrading runtime performance. When `False` invalid inputs may silently render incorrect outputs. Default value: `False` (i.e. do not validate args).
`allow_nan_stats` Python `bool`, default `True`. When `True`, statistics (e.g., mean, mode, variance) use the value "`NaN`" to indicate the result is undefined. When `False`, an exception is raised if one or more of the statistic's batch members are undefined. Default value: `True`.
`name` Python `str` name prefixed to Ops created by this class. Default value: 'HalfStudentT'.

`TypeError` if `loc` and `scale` have different `dtype`.

`allow_nan_stats` Python `bool` describing behavior when a stat is undefined.

Stats return +/- infinity when it makes sense. E.g., the variance of a Cauchy distribution is infinity. However, sometimes the statistic is undefined, e.g., if a distribution's pdf does not achieve a maximum within the support of the distribution, the mode is undefined. If the mean is undefined, then by definition the variance is undefined. E.g. the mean for Student's T for df = 1 is undefined (no clear way to say it is either + or - infinity), so the variance = E[(X - mean)**2] is also undefined.

`batch_shape` Shape of a single sample from a single event index as a `TensorShape`.

May be partially defined or unknown.

The batch dimensions are indexes into independent, non-identical parameterizations of this distribution.

`df` Distribution parameter for the degrees of freedom.
`dtype` The `DType` of `Tensor`s handled by this `Distribution`.
`event_shape` Shape of a single sample from a single batch as a `TensorShape`.

May be partially defined or unknown.

`experimental_shard_axis_names` The list or structure of lists of active shard axis names.
`loc` Distribution parameter for the location.
`name` Name prepended to all ops created by this `Distribution`.
`name_scope` Returns a `tf.name_scope` instance for this class.
`non_trainable_variables` Sequence of non-trainable variables owned by this module and its submodules.

`parameters` Dictionary of parameters used to instantiate this `Distribution`.
`reparameterization_type` Describes how samples from the distribution are reparameterized.

Currently this is one of the static instances `tfd.FULLY_REPARAMETERIZED` or `tfd.NOT_REPARAMETERIZED`.

`scale` Distribution parameter for the scale.
`submodules` Sequence of all sub-modules.

Submodules are modules which are properties of this module, or found as properties of modules which are properties of this module (and so on).

````a = tf.Module()`
`b = tf.Module()`
`c = tf.Module()`
`a.b = b`
`b.c = c`
`list(a.submodules) == [b, c]`
`True`
`list(b.submodules) == [c]`
`True`
`list(c.submodules) == []`
`True`
```

`trainable_variables` Sequence of trainable variables owned by this module and its submodules.

`validate_args` Python `bool` indicating possibly expensive checks are enabled.
`variables` Sequence of variables owned by this module and its submodules.

## Methods

### `batch_shape_tensor`

View source

Shape of a single sample from a single event index as a 1-D `Tensor`.

The batch dimensions are indexes into independent, non-identical parameterizations of this distribution.

Args
`name` name to give to the op

Returns
`batch_shape` `Tensor`.

### `cdf`

View source

Cumulative distribution function.

Given random variable `X`, the cumulative distribution function `cdf` is:

``````cdf(x) := P[X <= x]
``````

Args
`value` `float` or `double` `Tensor`.
`name` Python `str` prepended to names of ops created by this function.
`**kwargs` Named arguments forwarded to subclass implementation.

Returns
`cdf` a `Tensor` of shape `sample_shape(x) + self.batch_shape` with values of type `self.dtype`.

### `copy`

View source

Creates a deep copy of the distribution.

Args
`**override_parameters_kwargs` String/value dictionary of initialization arguments to override with new values.

Returns
`distribution` A new instance of `type(self)` initialized from the union of self.parameters and override_parameters_kwargs, i.e., `dict(self.parameters, **override_parameters_kwargs)`.

### `covariance`

View source

Covariance.

Covariance is (possibly) defined only for non-scalar-event distributions.

For example, for a length-`k`, vector-valued distribution, it is calculated as,

``````Cov[i, j] = Covariance(X_i, X_j) = E[(X_i - E[X_i]) (X_j - E[X_j])]
``````

where `Cov` is a (batch of) `k x k` matrix, `0 <= (i, j) < k`, and `E` denotes expectation.

Alternatively, for non-vector, multivariate distributions (e.g., matrix-valued, Wishart), `Covariance` shall return a (batch of) matrices under some vectorization of the events, i.e.,

``````Cov[i, j] = Covariance(Vec(X)_i, Vec(X)_j) = [as above]
``````

where `Cov` is a (batch of) `k' x k'` matrices, `0 <= (i, j) < k' = reduce_prod(event_shape)`, and `Vec` is some function mapping indices of this distribution's event dimensions to indices of a length-`k'` vector.

Args
`name` Python `str` prepended to names of ops created by this function.
`**kwargs` Named arguments forwarded to subclass implementation.