|
| 1 | +""" |
| 2 | + LocationScale(μ,σ,ρ) |
| 3 | +
|
| 4 | +A location-scale transformed distribution with location parameter `μ`, |
| 5 | +scale parameter `σ`, and given univariate distribution `ρ`. |
| 6 | +
|
| 7 | +If ``Z`` is a random variable with distribution `ρ`, then the distribution of the random |
| 8 | +variable |
| 9 | +```math |
| 10 | +X = μ + σ Z |
| 11 | +``` |
| 12 | +is the location-scale transformed distribution with location parameter `μ` and scale |
| 13 | +parameter `σ`. |
| 14 | +
|
| 15 | +If `ρ` is a discrete distribution, the probability mass function of |
| 16 | +the transformed distribution is given by |
| 17 | +```math |
| 18 | +P(X = x) = P\\left(Z = \\frac{x-μ}{σ} \\right). |
| 19 | +``` |
| 20 | +If `ρ` is a continuous distribution, the probability density function of |
| 21 | +the transformed distribution is given by |
| 22 | +```math |
| 23 | +f(x) = \\frac{1}{σ} ρ \\! \\left( \\frac{x-μ}{σ} \\right). |
| 24 | +``` |
| 25 | +
|
| 26 | +```julia |
| 27 | +LocationScale(μ,σ,ρ) # location-scale transformed distribution |
| 28 | +params(d) # Get the parameters, i.e. (μ, σ, and the base distribution) |
| 29 | +location(d) # Get the location parameter |
| 30 | +scale(d) # Get the scale parameter |
| 31 | +``` |
| 32 | +
|
| 33 | +External links |
| 34 | +[Location-Scale family on Wikipedia](https://en.wikipedia.org/wiki/Location%E2%80%93scale_family) |
| 35 | +""" |
| 36 | +struct LocationScale{T<:Real, S<:ValueSupport, D<:UnivariateDistribution{S}} <: UnivariateDistribution{S} |
| 37 | + μ::T |
| 38 | + σ::T |
| 39 | + ρ::D |
| 40 | + function LocationScale{T,S,D}(μ::T, σ::T, ρ::D; check_args=true) where {T<:Real, S<:ValueSupport, D<:UnivariateDistribution{S}} |
| 41 | + check_args && @check_args(LocationScale, σ > zero(σ)) |
| 42 | + new{T, S, D}(μ, σ, ρ) |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +function LocationScale(μ::T, σ::T, ρ::UnivariateDistribution; check_args=true) where {T<:Real} |
| 47 | + _T = promote_type(eltype(ρ), T) |
| 48 | + D = typeof(ρ) |
| 49 | + S = value_support(D) |
| 50 | + return LocationScale{_T,S,D}(_T(μ), _T(σ), ρ; check_args=check_args) |
| 51 | +end |
| 52 | + |
| 53 | +LocationScale(μ::Real, σ::Real, ρ::UnivariateDistribution) = LocationScale(promote(μ, σ)..., ρ) |
| 54 | + |
| 55 | +# aliases |
| 56 | +const ContinuousLocationScale{T<:Real,D<:ContinuousUnivariateDistribution} = LocationScale{T,Continuous,D} |
| 57 | +const DiscreteLocationScale{T<:Real,D<:DiscreteUnivariateDistribution} = LocationScale{T,Discrete,D} |
| 58 | + |
| 59 | +Base.eltype(::Type{<:LocationScale{T}}) where T = T |
| 60 | + |
| 61 | +minimum(d::LocationScale) = d.μ + d.σ * minimum(d.ρ) |
| 62 | +maximum(d::LocationScale) = d.μ + d.σ * maximum(d.ρ) |
| 63 | + |
| 64 | +LocationScale(μ::Real, σ::Real, d::LocationScale) = LocationScale(μ + d.μ * σ, σ * d.σ, d.ρ) |
| 65 | + |
| 66 | +#### Conversions |
| 67 | + |
| 68 | +convert(::Type{LocationScale{T}}, μ::Real, σ::Real, ρ::D) where {T<:Real, D<:UnivariateDistribution} = LocationScale(T(μ),T(σ),ρ) |
| 69 | +convert(::Type{LocationScale{T}}, d::LocationScale{S}) where {T<:Real, S<:Real} = LocationScale(T(d.μ),T(d.σ),d.ρ, check_args=false) |
| 70 | + |
| 71 | +#### Parameters |
| 72 | + |
| 73 | +location(d::LocationScale) = d.μ |
| 74 | +scale(d::LocationScale) = d.σ |
| 75 | +params(d::LocationScale) = (d.μ,d.σ,d.ρ) |
| 76 | +partype(::LocationScale{T}) where {T} = T |
| 77 | + |
| 78 | +#### Statistics |
| 79 | + |
| 80 | +mean(d::LocationScale) = d.μ + d.σ * mean(d.ρ) |
| 81 | +median(d::LocationScale) = d.μ + d.σ * median(d.ρ) |
| 82 | +mode(d::LocationScale) = d.μ + d.σ * mode(d.ρ) |
| 83 | +modes(d::LocationScale) = d.μ .+ d.σ .* modes(d.ρ) |
| 84 | + |
| 85 | +var(d::LocationScale) = d.σ^2 * var(d.ρ) |
| 86 | +std(d::LocationScale) = d.σ * std(d.ρ) |
| 87 | +skewness(d::LocationScale) = skewness(d.ρ) |
| 88 | +kurtosis(d::LocationScale) = kurtosis(d.ρ) |
| 89 | + |
| 90 | +isplatykurtic(d::LocationScale) = isplatykurtic(d.ρ) |
| 91 | +isleptokurtic(d::LocationScale) = isleptokurtic(d.ρ) |
| 92 | +ismesokurtic(d::LocationScale) = ismesokurtic(d.ρ) |
| 93 | + |
| 94 | +entropy(d::ContinuousLocationScale) = entropy(d.ρ) + log(d.σ) |
| 95 | +entropy(d::DiscreteLocationScale) = entropy(d.ρ) |
| 96 | + |
| 97 | +mgf(d::LocationScale,t::Real) = exp(d.μ*t) * mgf(d.ρ,d.σ*t) |
| 98 | + |
| 99 | +#### Evaluation & Sampling |
| 100 | + |
| 101 | +pdf(d::ContinuousLocationScale,x::Real) = pdf(d.ρ,(x-d.μ)/d.σ) / d.σ |
| 102 | +pdf(d::DiscreteLocationScale, x::Real) = pdf(d.ρ,(x-d.μ)/d.σ) |
| 103 | + |
| 104 | +logpdf(d::ContinuousLocationScale,x::Real) = logpdf(d.ρ,(x-d.μ)/d.σ) - log(d.σ) |
| 105 | +logpdf(d::DiscreteLocationScale, x::Real) = logpdf(d.ρ,(x-d.μ)/d.σ) |
| 106 | + |
| 107 | +# additional definitions are required to fix ambiguity errors and incorrect defaults |
| 108 | +for f in (:cdf, :ccdf, :logcdf, :logccdf) |
| 109 | + _f = Symbol(:_, f) |
| 110 | + @eval begin |
| 111 | + $f(d::LocationScale, x::Real) = $_f(d, x) |
| 112 | + $f(d::DiscreteLocationScale, x::Real) = $_f(d, x) |
| 113 | + $f(d::DiscreteLocationScale, x::Integer) = $_f(d, x) |
| 114 | + $_f(d::LocationScale, x::Real) = $f(d.ρ, (x - d.μ) / d.σ) |
| 115 | + end |
| 116 | +end |
| 117 | + |
| 118 | +quantile(d::LocationScale,q::Real) = d.μ + d.σ * quantile(d.ρ,q) |
| 119 | + |
| 120 | +rand(rng::AbstractRNG, d::LocationScale) = d.μ + d.σ * rand(rng, d.ρ) |
| 121 | +cf(d::LocationScale, t::Real) = cf(d.ρ,t*d.σ) * exp(1im*t*d.μ) |
| 122 | +gradlogpdf(d::ContinuousLocationScale, x::Real) = gradlogpdf(d.ρ,(x-d.μ)/d.σ) / d.σ |
| 123 | + |
| 124 | +#### Syntactic sugar for simple transforms of distributions, e.g., d + x, d - x, and so on |
| 125 | + |
| 126 | +Base.:+(d::UnivariateDistribution, x::Real) = LocationScale(x, one(x), d) |
| 127 | +Base.:+(x::Real, d::UnivariateDistribution) = d + x |
| 128 | +Base.:*(x::Real, d::UnivariateDistribution) = LocationScale(zero(x), x, d) |
| 129 | +Base.:*(d::UnivariateDistribution, x::Real) = x * d |
| 130 | +Base.:-(d::UnivariateDistribution, x::Real) = d + -x |
| 131 | +Base.:/(d::UnivariateDistribution, x::Real) = inv(x) * d |
| 132 | + |
0 commit comments