T_DIST

Overview

The T_DIST function computes values for Student’s t-distribution, a continuous probability distribution that arises when estimating the mean of a normally distributed population from small sample sizes with unknown variance. It is fundamental to t-tests, confidence intervals, and hypothesis testing in statistics.

Student’s t-distribution was introduced by William Sealy Gosset in 1908 under the pseudonym “Student” while working at the Guinness Brewery in Dublin. The distribution approaches the standard normal distribution as the degrees of freedom increase, but has heavier tails for smaller sample sizes, accounting for additional uncertainty when variance is estimated rather than known.

This implementation uses the scipy.stats.t module from the SciPy library. The probability density function (PDF) for the t-distribution is defined as:

f(x, \nu) = \frac{\Gamma((\nu+1)/2)}{\sqrt{\pi \nu} \, \Gamma(\nu/2)} \left(1 + \frac{x^2}{\nu}\right)^{-(\nu+1)/2}

where x is the input value, \nu (nu) is the degrees of freedom parameter (df), and \Gamma is the gamma function.

The function supports multiple calculation methods:

  • PDF (Probability Density Function): Returns the height of the probability curve at a given point
  • CDF (Cumulative Distribution Function): Returns the probability that a random variable is less than or equal to a given value
  • ICDF (Inverse CDF / Percent Point Function): Returns the value for a given cumulative probability
  • SF (Survival Function): Returns 1 - CDF, useful for upper-tail probabilities
  • ISF (Inverse Survival Function): Returns the value for a given upper-tail probability
  • Mean, Median, Var, Std: Returns descriptive statistics of the distribution

The loc parameter shifts the distribution along the x-axis, and the scale parameter stretches or compresses the distribution. For the standard t-distribution, use loc=0 and scale=1.

Note that the mean is only defined for \nu > 1, and the variance is only defined for \nu > 2. For additional details, see the SciPy t-distribution documentation and the GitHub source code.

This example function is provided as-is without any representation of accuracy.

Excel Usage

=T_DIST(value, df, loc, scale, t_dist_method)
  • value (float, optional, default: null): Input value x for pdf/cdf/sf, or probability q for icdf/isf (0 to 1).
  • df (float, optional, default: 1): Degrees of freedom (must be > 0).
  • loc (float, optional, default: 0): Location parameter (shifts the distribution).
  • scale (float, optional, default: 1): Scale parameter (must be > 0).
  • t_dist_method (str, optional, default: “pdf”): Method to compute (pdf, cdf, icdf, sf, isf, mean, median, var, std).

Returns (float): Distribution result (float), or error message string.

Examples

Example 1: PDF at center of standard t distribution

Inputs:

value df loc scale t_dist_method
0 5 0 1 pdf

Excel formula:

=T_DIST(0, 5, 0, 1, "pdf")

Expected output:

0.3796

Example 2: CDF at center equals 0.5 by symmetry

Inputs:

value df loc scale t_dist_method
0 5 0 1 cdf

Excel formula:

=T_DIST(0, 5, 0, 1, "cdf")

Expected output:

0.5

Example 3: Critical value at 97.5% quantile

Inputs:

value df loc scale t_dist_method
0.975 5 0 1 icdf

Excel formula:

=T_DIST(0.975, 5, 0, 1, "icdf")

Expected output:

2.5706

Example 4: Mean of t distribution with df=5

Inputs:

df loc scale t_dist_method
5 0 1 mean

Excel formula:

=T_DIST(5, 0, 1, "mean")

Expected output:

0

Example 5: ISF check

Inputs:

value df loc scale t_dist_method
0.025 5 0 1 isf

Excel formula:

=T_DIST(0.025, 5, 0, 1, "isf")

Expected output:

2.5706

Python Code

from scipy.stats import t as scipy_t
import math

def t_dist(value=None, df=1, loc=0, scale=1, t_dist_method='pdf'):
    """
    Student's t distribution function supporting multiple methods from scipy.stats.t.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.t.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        value (float, optional): Input value x for pdf/cdf/sf, or probability q for icdf/isf (0 to 1). Default is None.
        df (float, optional): Degrees of freedom (must be > 0). Default is 1.
        loc (float, optional): Location parameter (shifts the distribution). Default is 0.
        scale (float, optional): Scale parameter (must be > 0). Default is 1.
        t_dist_method (str, optional): Method to compute (pdf, cdf, icdf, sf, isf, mean, median, var, std). Valid options: PDF, CDF, ICDF, SF, ISF, Mean, Median, Var, Std. Default is 'pdf'.

    Returns:
        float: Distribution result (float), or error message string.
    """
    valid_methods = {'pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std'}

    if not isinstance(t_dist_method, str):
        return "Invalid input: t_dist_method must be a string."

    method = t_dist_method.lower()
    if method not in valid_methods:
        return f"Invalid method: {t_dist_method}. Must be one of {', '.join(sorted(valid_methods))}."

    try:
        df = float(df)
        loc = float(loc)
        scale = float(scale)
    except (ValueError, TypeError):
        return "Invalid input: df, loc, and scale must be numbers."

    if df <= 0:
        return "Invalid input: df must be > 0."
    if scale <= 0:
        return "Invalid input: scale must be > 0."

    dist = scipy_t(df, loc, scale)

    # Methods that require value
    if method in ['pdf', 'cdf', 'icdf', 'sf', 'isf']:
        if value is None:
            return f"Invalid input: missing required argument 'value' for method '{method}'."
        try:
            value = float(value)
        except (ValueError, TypeError):
            return "Invalid input: value must be a number."

        try:
            if method == 'pdf':
                result = dist.pdf(value)
            elif method == 'cdf':
                result = dist.cdf(value)
            elif method == 'sf':
                result = dist.sf(value)
            elif method == 'isf':
                if not (0 <= value <= 1):
                    return "Invalid input: value (probability) must be between 0 and 1 for isf."
                result = dist.isf(value)
            elif method == 'icdf':
                if not (0 <= value <= 1):
                    return "Invalid input: value (probability) must be between 0 and 1 for icdf."
                result = dist.ppf(value)
        except Exception as e:
            return f"scipy.stats.t error: {str(e)}"

        if isinstance(result, float):
            if math.isnan(result):
                return "Result is NaN (not a number)"
            if math.isinf(result):
                return "inf" if result > 0 else "-inf"
        return result

    # Methods that do not require value
    try:
        if method == 'mean':
            result = dist.mean()
        elif method == 'median':
            result = dist.median()
        elif method == 'var':
            result = dist.var()
        elif method == 'std':
            result = dist.std()
    except Exception as e:
        return f"scipy.stats.t error: {str(e)}"

    if isinstance(result, float):
        if math.isnan(result):
            return "Result is NaN (not a number)"
        if math.isinf(result):
            return "inf" if result > 0 else "-inf"

    return result

Online Calculator