|
| 1 | +module TuringExt |
| 2 | + |
| 3 | +# We only import `truncated` now, as we will be defining our own wrappers |
| 4 | +# with the same names as the distributions. |
| 5 | +import Turing: truncated |
| 6 | +# We explicitly refer to the Turing distributions to avoid method overwriting. |
| 7 | +import Turing |
| 8 | +import DocStringExtensions: SIGNATURES |
| 9 | +using DispatchDoctor |
| 10 | +import MacroModelling: Normal, Beta, Cauchy, Gamma, InverseGamma |
| 11 | + |
| 12 | +@stable default_mode = "disable" begin |
| 13 | + |
| 14 | +#========================================================================================== |
| 15 | + Beta Distribution |
| 16 | +==========================================================================================# |
| 17 | + |
| 18 | +""" |
| 19 | +$(SIGNATURES) |
| 20 | +Constructs a `Beta` distribution, optionally parameterized by its mean and standard deviation. |
| 21 | +
|
| 22 | +# Arguments |
| 23 | +- `μ` [Type: `Real`]: The first parameter (α) of the distribution, or the mean when `μσ=true`. |
| 24 | +- `σ` [Type: `Real`]: The second parameter (β) of the distribution, or the standard deviation when `μσ=true`. |
| 25 | +
|
| 26 | +# Keyword Arguments |
| 27 | +- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the `α` and `β` parameters. |
| 28 | +""" |
| 29 | +function Beta(μ::Real, σ::Real; μσ::Bool=false) |
| 30 | + if μσ |
| 31 | + # Calculate alpha and beta from mean (μ) and standard deviation (σ) |
| 32 | + ν = μ * (1 - μ) / σ^2 - 1 |
| 33 | + α = μ * ν |
| 34 | + β = (1 - μ) * ν |
| 35 | + return Turing.Beta(α, β) |
| 36 | + end |
| 37 | + # By default, treat μ and σ as the distribution parameters α and β |
| 38 | + return Turing.Beta(μ, σ) |
| 39 | +end |
| 40 | + |
| 41 | +""" |
| 42 | +$(SIGNATURES) |
| 43 | +Constructs a truncated `Beta` distribution, optionally parameterized by its mean and standard deviation. |
| 44 | +
|
| 45 | +# Arguments |
| 46 | +- `μ` [Type: `Real`]: The first parameter (α) of the distribution, or the mean when `μσ=true`. |
| 47 | +- `σ` [Type: `Real`]: The second parameter (β) of the distribution, or the standard deviation when `μσ=true`. |
| 48 | +- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution. |
| 49 | +- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution. |
| 50 | +
|
| 51 | +# Keyword Arguments |
| 52 | +- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the `α` and `β` parameters. |
| 53 | +""" |
| 54 | +function Beta(μ::Real, σ::Real, lower_bound::Real, upper_bound::Real; μσ::Bool=false) |
| 55 | + # Create the base distribution, then truncate it |
| 56 | + dist = Beta(μ, σ; μσ=μσ) |
| 57 | + return truncated(dist, lower_bound, upper_bound) |
| 58 | +end |
| 59 | + |
| 60 | + |
| 61 | +#========================================================================================== |
| 62 | + InverseGamma Distribution |
| 63 | +==========================================================================================# |
| 64 | + |
| 65 | +""" |
| 66 | +$(SIGNATURES) |
| 67 | +Constructs an `InverseGamma` distribution, optionally parameterized by its mean and standard deviation. |
| 68 | +
|
| 69 | +# Arguments |
| 70 | +- `μ` [Type: `Real`]: The shape parameter (α) of the distribution, or the mean when `μσ=true`. |
| 71 | +- `σ` [Type: `Real`]: The scale parameter (β) of the distribution, or the standard deviation when `μσ=true`. |
| 72 | +
|
| 73 | +# Keyword Arguments |
| 74 | +- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the shape `α` and scale `β` parameters. |
| 75 | +""" |
| 76 | +function InverseGamma(μ::Real, σ::Real; μσ::Bool=false) |
| 77 | + if μσ |
| 78 | + # Calculate shape (α) and scale (β) from mean (μ) and standard deviation (σ) |
| 79 | + α = (μ / σ)^2 + 2 |
| 80 | + β = μ * ((μ / σ)^2 + 1) |
| 81 | + return Turing.InverseGamma(α, β) |
| 82 | + end |
| 83 | + # By default, treat μ and σ as the distribution parameters α and β |
| 84 | + return Turing.InverseGamma(μ, σ) |
| 85 | +end |
| 86 | + |
| 87 | +""" |
| 88 | +$(SIGNATURES) |
| 89 | +Constructs a truncated `InverseGamma` distribution, optionally parameterized by its mean and standard deviation. |
| 90 | +
|
| 91 | +# Arguments |
| 92 | +- `μ` [Type: `Real`]: The shape parameter (α) of the distribution, or the mean when `μσ=true`. |
| 93 | +- `σ` [Type: `Real`]: The scale parameter (β) of the distribution, or the standard deviation when `μσ=true`. |
| 94 | +- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution. |
| 95 | +- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution. |
| 96 | +
|
| 97 | +# Keyword Arguments |
| 98 | +- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the shape `α` and scale `β` parameters. |
| 99 | +""" |
| 100 | +function InverseGamma(μ::Real, σ::Real, lower_bound::Real, upper_bound::Real; μσ::Bool=false) |
| 101 | + # Create the base distribution, then truncate it |
| 102 | + dist = InverseGamma(μ, σ; μσ=μσ) |
| 103 | + return truncated(dist, lower_bound, upper_bound) |
| 104 | +end |
| 105 | + |
| 106 | + |
| 107 | +#========================================================================================== |
| 108 | + Gamma Distribution |
| 109 | +==========================================================================================# |
| 110 | + |
| 111 | +""" |
| 112 | +$(SIGNATURES) |
| 113 | +Constructs a `Gamma` distribution, optionally parameterized by its mean and standard deviation. |
| 114 | +
|
| 115 | +# Arguments |
| 116 | +- `μ` [Type: `Real`]: The shape parameter (α) of the distribution, or the mean when `μσ=true`. |
| 117 | +- `σ` [Type: `Real`]: The rate parameter (θ) of the distribution, or the standard deviation when `μσ=true`. |
| 118 | +
|
| 119 | +# Keyword Arguments |
| 120 | +- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the shape `α` and scale `θ` parameters. |
| 121 | +""" |
| 122 | +function Gamma(μ::Real, σ::Real; μσ::Bool=false) |
| 123 | + if μσ |
| 124 | + # Calculate shape (α) and scale (θ) from mean (μ) and standard deviation (σ) |
| 125 | + θ = σ^2 / μ |
| 126 | + α = μ / θ |
| 127 | + return Turing.Gamma(α, θ) |
| 128 | + end |
| 129 | + # By default, treat μ and σ as the distribution parameters α and θ |
| 130 | + return Turing.Gamma(μ, σ) |
| 131 | +end |
| 132 | + |
| 133 | +""" |
| 134 | +$(SIGNATURES) |
| 135 | +Constructs a truncated `Gamma` distribution, optionally parameterized by its mean and standard deviation. |
| 136 | +
|
| 137 | +# Arguments |
| 138 | +- `μ` [Type: `Real`]: The shape parameter (α) of the distribution, or the mean when `μσ=true`. |
| 139 | +- `σ` [Type: `Real`]: The rate parameter (θ) of the distribution, or the standard deviation when `μσ=true`. |
| 140 | +- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution. |
| 141 | +- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution. |
| 142 | +
|
| 143 | +# Keyword Arguments |
| 144 | +- `μσ` [Type: `Bool`, Default: `false`]: If `true`, `μ` and `σ` are interpreted as the mean and standard deviation to calculate the shape `α` and scale `θ` parameters. |
| 145 | +""" |
| 146 | +function Gamma(μ::Real, σ::Real, lower_bound::Real, upper_bound::Real; μσ::Bool=false) |
| 147 | + # Create the base distribution, then truncate it |
| 148 | + dist = Gamma(μ, σ; μσ=μσ) |
| 149 | + return truncated(dist, lower_bound, upper_bound) |
| 150 | +end |
| 151 | + |
| 152 | + |
| 153 | +#========================================================================================== |
| 154 | + Simple Truncation Wrappers |
| 155 | +==========================================================================================# |
| 156 | + |
| 157 | +""" |
| 158 | +$(SIGNATURES) |
| 159 | +Convenience wrapper for the truncated `Normal` distribution. |
| 160 | +
|
| 161 | +# Arguments |
| 162 | +- `μ` [Type: `Real`]: The mean of the distribution. |
| 163 | +- `σ` [Type: `Real`]: The standard deviation of the distribution. |
| 164 | +- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution. |
| 165 | +- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution. |
| 166 | +""" |
| 167 | +function Normal(μ::Real, σ::Real, lower_bound::Real, upper_bound::Real) |
| 168 | + truncated(Turing.Normal(μ, σ), lower_bound, upper_bound) |
| 169 | +end |
| 170 | + |
| 171 | +function Normal(μ::Real, σ::Real) |
| 172 | + Turing.Normal(μ, σ) |
| 173 | +end |
| 174 | + |
| 175 | +""" |
| 176 | +$(SIGNATURES) |
| 177 | +Convenience wrapper for the truncated `Cauchy` distribution. |
| 178 | +
|
| 179 | +# Arguments |
| 180 | +- `μ` [Type: `Real`]: The location parameter. |
| 181 | +- `σ` [Type: `Real`]: The scale parameter. |
| 182 | +- `lower_bound` [Type: `Real`]: The truncation lower bound of the distribution. |
| 183 | +- `upper_bound` [Type: `Real`]: The truncation upper bound of the distribution. |
| 184 | +""" |
| 185 | +function Cauchy(μ::Real, σ::Real, lower_bound::Real, upper_bound::Real) |
| 186 | + truncated(Turing.Cauchy(μ, σ), lower_bound, upper_bound) |
| 187 | +end |
| 188 | + |
| 189 | +function Cauchy(μ::Real, σ::Real) |
| 190 | + Turing.Cauchy(μ, σ) |
| 191 | +end |
| 192 | + |
| 193 | + |
| 194 | +end # @stable |
| 195 | + |
| 196 | +end # module |
0 commit comments