s-broda / ARCHModels.jl
Showing 2 of 4 files from the diff.
Other files ignored by Codecov

@@ -142,14 +142,14 @@
 142 142 end 143 143 144 144 """ 145 - StdT(v) 145 + StdT(ν) 146 146 147 - Create a standardized t distribution with `v` degrees of freedom. `ν`` can be passed 147 + Create a standardized t distribution with `ν` degrees of freedom. `ν`` can be passed 148 148 as a scalar or vector. 149 149 """ 150 150 StdT(ν) = StdT([ν]) 151 151 StdT(ν::Integer) = StdT(float(ν)) 152 - StdT(v::Vector{T}) where {T} = StdT{T}(v) 152 + StdT(ν::Vector{T}) where {T} = StdT{T}(ν) 153 153 (rand(d::StdT{T})::T) where {T} = (ν=d.coefs[1]; tdistrand(ν)*sqrt((ν-2)/ν)) 154 154 @inline kernelinvariants(::Type{<:StdT}, coefs) = (1/ (coefs[1]-2),) 155 155 @inline logkernel(::Type{<:StdT}, x, coefs, iv) = (-(coefs[1] + 1) / 2) * log1p(abs2(x) *iv)
@@ -184,8 +184,8 @@
 184 184 end 185 185 186 186 function quantile(dist::StdT, q::Real) 187 - v = dist.coefs[1] 188 - tdistinvcdf(v, q)*sqrt((v-2)/v) 187 + ν = dist.coefs[1] 188 + tdistinvcdf(ν, q)*sqrt((ν-2)/ν) 189 189 end 190 190 191 191 ################################################################################
@@ -253,3 +253,68 @@
 253 253 qq = 2*q-1 254 254 return sign(qq) * (gammainvcdf(ip, 1., abs(qq)))^ip/kernelinvariants(StdGED, [p])[1] 255 255 end 256 + 257 + ################################################################################ 258 + #Hansen's SKT-Distribution 259 + 260 + """ 261 + StdSkewT{T} <: StandardizedDistribution{T} 262 + 263 + Hansen's standardized (mean zero, variance one) Skewed Student's t distribution. 264 + """ 265 + struct StdSkewT{T} <: StandardizedDistribution{T} 266 + coefs::Vector{T} 267 + function StdSkewT{T}(coefs::Vector) where {T} 268 + length(coefs) == 2 || throw(NumParamError(2, length(coefs))) 269 + new{T}(coefs) 270 + end 271 + end 272 + 273 + """ 274 + StdSkewT(v,λ) 275 + 276 + Create a standardized skewed t distribution with `v` degrees of freedom and `λ` shape parameter. `ν,λ`` can be passed 277 + as scalars or vectors. 278 + """ 279 + StdSkewT(ν,λ) = StdSkewT([float(ν), float(λ)]) 280 + StdSkewT(coefs::Vector{T}) where {T} = StdSkewT{T}(coefs) 281 + 282 + (rand(d::StdSkewT{T}) where {T} = (quantile(d, rand()))) 283 + 284 + @inline a(d::Type{<:StdSkewT}, coefs) = (ν=coefs[1];λ=coefs[2]; 4λ*c(d,coefs) * ((ν-2)/(ν-1))) 285 + @inline b(d::Type{<:StdSkewT}, coefs) = (ν=coefs[1];λ=coefs[2]; sqrt(1+3λ^2-a(d,coefs)^2)) 286 + @inline c(d::Type{<:StdSkewT}, coefs) = (ν=coefs[1];λ=coefs[2]; gamma((ν+1)/2) / (sqrt(π*(ν-2)) * gamma(ν/2))) 287 + 288 + @inline kernelinvariants(::Type{<:StdSkewT}, coefs) = (1/ (coefs[1]-2),) 289 + @inline function logkernel(d::Type{<:StdSkewT}, x, coefs, iv) 290 + ν=coefs[1] 291 + λ=coefs[2] 292 + c = gamma((ν+1)/2) / (sqrt(π*(ν-2)) * gamma(ν/2)) 293 + a = 4λ * c * ((ν-2)/(ν-1)) 294 + b = sqrt(1 + 3λ^2 -a^2) 295 + λsign = x < (-a/b) ? -1 : 1 296 + (-(ν + 1) / 2) * log1p(1/abs2(1+λ*λsign) * abs2(b*x + a) *iv) 297 + end 298 + @inline logconst(d::Type{<:StdSkewT}, coefs) = (log(b(d,coefs))+(log(c(d,coefs)))) 299 + 300 + nparams(::Type{<:StdSkewT}) = 2 301 + coefnames(::Type{<:StdSkewT}) = ["ν", "λ"] 302 + distname(::Type{<:StdSkewT}) = "Hansen's Skewed t" 303 + 304 + function constraints(::Type{<:StdSkewT}, ::Type{T}) where {T} 305 + lower = T[20/10, -one(T)] 306 + upper = T[Inf,one(T)] 307 + return lower, upper 308 + end 309 + 310 + startingvals(::Type{<:StdSkewT}, data::Array{T}) where {T<:AbstractFloat} = [startingvals(StdT, data)..., zero(T)] 311 + 312 + function quantile(d::StdSkewT{T}, q::T) where T 313 + ν = d.coefs[1] 314 + λ = d.coefs[2] 315 + a_val = a(typeof(d),d.coefs) 316 + b_val = b(typeof(d),d.coefs) 317 + λconst = q < (1 - λ)/2 ? (1 - λ) : (1 + λ) 318 + quant_numer = q < (1 - λ)/2 ? q : (q + λ) 319 + 1/b_val * ((λconst) * sqrt((ν-2)/ν) * tdistinvcdf(ν, quant_numer/λconst) - a_val) 320 + end

@@ -49,7 +49,7 @@