Skip to content

Commit 9716006

Browse files
committed
fix: type parameters
1 parent ec8e84c commit 9716006

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

src/AlphaStableDistributions.jl

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ skewness parameter, scale parameter (dispersion^1/α) and location parameter res
188188
189189
α, β, c and δ are computed based on McCulloch (1986) fractile.
190190
"""
191-
function Distributions.fit(::Type{<:AlphaStable}, x, alg=QuickSort)
191+
function Distributions.fit(::Type{<:AlphaStable}, x::AbstractArray{T}, alg=QuickSort) where {T}
192192
sx = sort(x, alg=alg)
193193
p = quantile.(Ref(sx), (0.05, 0.25, 0.28, 0.5, 0.72, 0.75, 0.95), sorted=true)
194194
να = (p[7]-p[1]) / (p[6]-p[2])
@@ -212,7 +212,7 @@ function Distributions.fit(::Type{<:AlphaStable}, x, alg=QuickSort)
212212
else
213213
δ = ζ - β * c * tan*α/2)
214214
end
215-
return AlphaStable=α, β=β, scale=c, location=oftype(α, δ))
215+
return AlphaStable=T(α), β=T(β), scale=T(c), location=T(δ))
216216
end
217217

218218
Base.@kwdef struct SymmetricAlphaStable{T} <: Distributions.ContinuousUnivariateDistribution
@@ -235,12 +235,12 @@ returns `SymmetricAlphaStable`
235235
scale is computed based on Fama & Roll (1971) fractile.
236236
location is the 50% trimmed mean of the sample.
237237
"""
238-
function Distributions.fit(::Type{<:SymmetricAlphaStable}, x, alg=QuickSort)
238+
function Distributions.fit(::Type{<:SymmetricAlphaStable}, x::AbstractArray{T}, alg=QuickSort) where {T}
239239
sx = sort(x, alg=alg)
240240
δ = mean(@view(sx[end÷4:(3*end)÷4]))
241241
p = quantile.(Ref(sx), (0.05, 0.25, 0.28, 0.72, 0.75, 0.95), sorted=true)
242-
c = (p[4]-p[3])/1.654
243-
an = (p[6]-p[1])/(p[5]-p[2])
242+
c = (p[4]-p[3]) / 1.654
243+
an = (p[6]-p[1]) / (p[5]-p[2])
244244
if an < 2.4388
245245
α = 2.
246246
else
@@ -253,7 +253,7 @@ function Distributions.fit(::Type{<:SymmetricAlphaStable}, x, alg=QuickSort)
253253
if α < 0.5
254254
α = 0.5
255255
end
256-
return SymmetricAlphaStable=α, scale=c, location=oftype(α, δ))
256+
return SymmetricAlphaStable=T(α), scale=T(c), location=T(δ))
257257
end
258258

259259
"""
@@ -322,16 +322,12 @@ The maximum acceptable size of `R` is `10x10`
322322
julia> x = rand(AlphaSubGaussian(n=1000))
323323
```
324324
"""
325-
Base.@kwdef struct AlphaSubGaussian{T<:AbstractFloat} <: Distributions.ContinuousUnivariateDistribution
325+
Base.@kwdef struct AlphaSubGaussian{T<:AbstractFloat,M<:AbstractMatrix} <: Distributions.ContinuousUnivariateDistribution
326326
α::T = 1.50
327-
R::AbstractMatrix{T} = SMatrix{5,5}(collect(SymmetricToeplitz([1.0000, 0.5804, 0.2140, 0.1444, -0.0135])))
327+
R::M = SMatrix{5,5}(collect(SymmetricToeplitz([1.0000, 0.5804, 0.2140, 0.1444, -0.0135])))
328328
n::Int
329329
end
330330

331-
AlphaSubGaussian::T, n::Int) where {T<:AbstractFloat} = AlphaSubGaussian=α,
332-
R=SMatrix{5,5}(T.(collect(SymmetricToeplitz([1.0000, 0.5804, 0.2140, 0.1444, -0.0135])))),
333-
n=n)
334-
335331
"""
336332
Generates the conditional probability f(X2|X1) if [X1, X2] is a sub-Gaussian
337333
stable random vector such that X1(i)~X2~S(alpha,delta) and rho is the correlation
@@ -377,7 +373,7 @@ end
377373
function Random.rand!(rng::AbstractRNG, d::AlphaSubGaussian{T}, x::AbstractArray{T}) where {T<:Real}
378374
α=d.α; R=d.R; n=d.n
379375
length(x) >= n || throw(ArgumentError("length of x must be at least n"))
380-
α T.(1.10:0.01:1.98) || throw(DomainError(α, "α must lie within `1.10:0.01:1.98`"))
376+
α 1.10:0.01:1.98 || throw(DomainError(α, "α must lie within `1.10:0.01:1.98`"))
381377
m = size(R, 1)-1
382378
funk1 = x -> (2^α)*sin*α/2)*gamma((α+2)/2)*gamma((α+x)/2)/(gamma(x/2)*π*α/2)
383379
funk2 = x -> 4*gamma(x/α)/((α*2^2)*gamma(x/2)^2)
@@ -430,7 +426,7 @@ end
430426

431427

432428
Base.rand(rng::AbstractRNG, d::AlphaSubGaussian) = rand!(rng, d, zeros(eltype(d), d.n))
433-
Base.eltype(::Type{<:AlphaSubGaussian{T}}) where {T} = T
429+
Base.eltype(::Type{<:AlphaSubGaussian}) = Float64
434430

435431
"""
436432
fit(d::Type{<:AlphaSubGaussian}, x, m; p=1.0)

test/runtests.jl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ using Test, Random, Distributions
1010
for (i, stabletype) in enumerate(stabletypes)
1111
for α in αs[i]
1212
d1 = AlphaStable=sampletype(α))
13-
s = rand(d1, 100000)
13+
s = rand(d1, 200000)
1414
@test eltype(s) == sampletype
1515

1616
d2 = fit(stabletype, s)
17+
@test typeof(d2.α) == sampletype
1718

1819
@test d1.α d2.α rtol=0.1
1920
stabletype != SymmetricAlphaStable && @test d1.β d2.β atol=0.2
@@ -35,32 +36,31 @@ using Test, Random, Distributions
3536
@test d.scale 4 rtol=0.2
3637
@test d.location 3 rtol=0.1
3738
end
39+
end
3840

39-
for α in 1.1:0.1:1.9
40-
d = AlphaSubGaussian(sampletype(α), 96000)
41-
x = rand(d)
42-
@test eltype(x) == sampletype
43-
x2 = copy(x)
44-
rand!(d, x2)
45-
@test x != x2
41+
for α in 1.1:0.1:1.9
42+
d = AlphaSubGaussian=α, n=96000)
43+
x = rand(d)
44+
x2 = copy(x)
45+
rand!(d, x2)
46+
@test x != x2
4647

47-
d3 = fit(AlphaStable, x)
48-
@test d3.α α rtol=0.2
49-
@test d3.β 0 atol=0.2
50-
@test d3.scale 1 rtol=0.2
51-
@test d3.location 0 atol=0.1
52-
end
48+
d3 = fit(AlphaStable, x)
49+
@test d3.α α rtol=0.2
50+
@test d3.β 0 atol=0.2
51+
@test d3.scale 1 rtol=0.2
52+
@test d3.location 0 atol=0.1
53+
end
5354

54-
d4 = AlphaSubGaussian(sampletype(1.5), 96000)
55-
m = size(d4.R, 1) - 1
56-
x = rand(d4)
57-
@test eltype(x) == sampletype
58-
d5 = fit(AlphaSubGaussian, x, m, p=1.0)
59-
@test d4.α d5.α rtol=0.1
60-
@test d4.R d5.R rtol=0.1
55+
d4 = AlphaSubGaussian=1.5, n=96000)
56+
m = size(d4.R, 1) - 1
57+
x = rand(d4)
58+
d5 = fit(AlphaSubGaussian, x, m, p=1.0)
59+
@test d4.α d5.α rtol=0.1
60+
@test d4.R d5.R rtol=0.1
6161

62-
end
6362
end
63+
6464
# 362.499 ms (4620903 allocations: 227.64 MiB)
6565
# 346.520 ms (4621052 allocations: 209.62 MiB) # StaticArrays in outer fun
6666
# 345.925 ms (4225524 allocations: 167.66 MiB) # tempind to tuple

0 commit comments

Comments
 (0)