|
| 1 | +defmodule Exstatic.Distribution.StandardizedT do |
| 2 | + @moduledoc """ |
| 3 | + The standardized Student's t-distribution, used in statistical hypothesis testing. |
| 4 | +
|
| 5 | + This implementation ensures that: |
| 6 | + - The mean is always `0.0`. |
| 7 | + - The variance exists for `df > 1` (it is infinite for `1 < df ≤ 2`). |
| 8 | + - The distribution is well-defined only for `df > 1`. |
| 9 | +
|
| 10 | + ## Examples |
| 11 | +
|
| 12 | + iex> alias Exstatic.Distribution.StandardizedT |
| 13 | + iex> {:ok, t} = StandardizedT.new(5.0) |
| 14 | + iex> StandardizedT.mean(t) |
| 15 | + 0.0 |
| 16 | + iex> pdf = StandardizedT.pdf(t, 0.0) |
| 17 | + iex> TestHelper.assert_in_delta(pdf, 0.37960669, 1.0e-6) |
| 18 | + true |
| 19 | + iex> result = StandardizedT.cdf(t, 0.0) |
| 20 | + iex> TestHelper.assert_in_delta(result, 0.5) |
| 21 | + true |
| 22 | + """ |
| 23 | + |
| 24 | + @behaviour Exstatic.Distribution |
| 25 | + @behaviour Exstatic.Continuous |
| 26 | + @behaviour Exstatic.ContinuousCDF |
| 27 | + |
| 28 | + defstruct [:df] |
| 29 | + |
| 30 | + @type t :: %__MODULE__{df: float()} |
| 31 | + |
| 32 | + @doc """ |
| 33 | + Creates a new standardized Student's t-distribution with the given degrees of freedom. |
| 34 | +
|
| 35 | + ## Parameters |
| 36 | + - `df` - The degrees of freedom (`df > 1` required). |
| 37 | +
|
| 38 | + ## Examples |
| 39 | +
|
| 40 | + iex> alias Exstatic.Distribution.StandardizedT |
| 41 | + iex> StandardizedT.new(5.0) |
| 42 | + {:ok, %StandardizedT{df: 5.0}} |
| 43 | +
|
| 44 | + iex> StandardizedT.new(1.0) |
| 45 | + {:error, :invalid_df} |
| 46 | +
|
| 47 | + iex> StandardizedT.new(-5.0) |
| 48 | + {:error, :invalid_df} |
| 49 | + """ |
| 50 | + def new(df) when is_number(df) and df > 1 do |
| 51 | + {:ok, %__MODULE__{df: df}} |
| 52 | + end |
| 53 | + |
| 54 | + def new(_df), do: {:error, :invalid_df} |
| 55 | + |
| 56 | + @doc """ |
| 57 | + Returns the mean of the t-distribution. |
| 58 | +
|
| 59 | + The mean is always `0.0` for standardized t-distributions since `df > 1`. |
| 60 | +
|
| 61 | + ## Examples |
| 62 | +
|
| 63 | + iex> {:ok, t} = StandardizedT.new(5.0) |
| 64 | + iex> StandardizedT.mean(t) |
| 65 | + 0.0 |
| 66 | + """ |
| 67 | + @impl Exstatic.Distribution |
| 68 | + def mean(_t), do: 0.0 |
| 69 | + |
| 70 | + @doc """ |
| 71 | + Returns the variance of the t-distribution. |
| 72 | +
|
| 73 | + - If `1 < df ≤ 2`, the variance is `:infinity`. |
| 74 | + - Otherwise, the variance is computed using `Exstatic.Native.standardized_t_variance/1`. |
| 75 | +
|
| 76 | + ## Examples |
| 77 | +
|
| 78 | + iex> {:ok, t} = StandardizedT.new(5.0) |
| 79 | + iex> TestHelper.assert_in_delta(StandardizedT.variance(t), 5.0 / (5.0 - 2.0), 1.0e-10) |
| 80 | + true |
| 81 | +
|
| 82 | + iex> {:ok, t} = StandardizedT.new(1.5) |
| 83 | + iex> StandardizedT.variance(t) |
| 84 | + :infinity |
| 85 | + """ |
| 86 | + @impl Exstatic.Distribution |
| 87 | + @spec variance(t) :: float() | :infinity |
| 88 | + def variance(%__MODULE__{df: df}) do |
| 89 | + if df <= 2.0, do: :infinity, else: Exstatic.Native.standardized_t_variance(df) |
| 90 | + end |
| 91 | + |
| 92 | + @doc """ |
| 93 | + Computes the probability density function (PDF) at `x`. |
| 94 | +
|
| 95 | + ## Examples |
| 96 | +
|
| 97 | + iex> {:ok, t} = StandardizedT.new(5.0) |
| 98 | + iex> TestHelper.assert_in_delta(StandardizedT.pdf(t, 0.0), 0.37960669, 1.0e-6) |
| 99 | + true |
| 100 | + """ |
| 101 | + @impl Exstatic.Continuous |
| 102 | + def pdf(%__MODULE__{} = dist, x) when is_number(x) do |
| 103 | + Exstatic.Native.standardized_t_pdf(dist.df, x) |
| 104 | + end |
| 105 | + |
| 106 | + @doc """ |
| 107 | + Computes the cumulative distribution function (CDF) at `x`. |
| 108 | +
|
| 109 | + ## Examples |
| 110 | +
|
| 111 | + iex> {:ok, t} = StandardizedT.new(5.0) |
| 112 | + iex> TestHelper.assert_in_delta(StandardizedT.cdf(t, 0.0), 0.5, 1.0e-10) |
| 113 | + true |
| 114 | + """ |
| 115 | + @impl Exstatic.ContinuousCDF |
| 116 | + def cdf(%__MODULE__{} = dist, x) when is_number(x) do |
| 117 | + Exstatic.Native.standardized_t_cdf(dist.df, x) |
| 118 | + end |
| 119 | + |
| 120 | + @doc """ |
| 121 | + Computes the survival function (SF) at `x`, which is `1 - CDF(x)`. |
| 122 | +
|
| 123 | + ## Examples |
| 124 | +
|
| 125 | + iex> {:ok, t} = StandardizedT.new(5.0) |
| 126 | + iex> TestHelper.assert_in_delta(StandardizedT.sf(t, 0.0), 0.5, 1.0e-10) |
| 127 | + true |
| 128 | + """ |
| 129 | + @impl Exstatic.ContinuousCDF |
| 130 | + def sf(%__MODULE__{} = dist, x) when is_number(x) do |
| 131 | + Exstatic.Native.standardized_t_sf(dist.df, x) |
| 132 | + end |
| 133 | +end |
0 commit comments