11using AlphaStableDistributions
22using Test, Random, Distributions
33
4- @testset " AlphaStableDistributions.jl" begin
4+ @testset " cf" begin
5+ rng = MersenneTwister (1 )
6+ for _ in 1 : 100
7+ d = AlphaStable (
8+ α= rand (rng,Uniform (0 ,10 )),
9+ β= rand (rng,Uniform (- 1 ,1 )),
10+ scale= rand (rng,Uniform (0 ,10 )),
11+ location= rand (rng,Uniform (0 ,10 )),
12+ )
13+ @test cf (d,0 ) ≈ 1
14+ x = rand (rng,Uniform (- 10 ,10 ))
15+ @test abs (cf (d, x)) <= 1
16+
17+ d32 = AlphaStable (Float32 .(Distributions. params (d))... )
18+ @test cf (d32, Float32 (x)) isa Complex{Float32}
19+ @test cf (d32, Float32 (x)) ≈ cf (d,x) atol= 100 * eps (Float32)
20+ end
21+ xs = range (- 1 ,1 ,length= 100 )
22+ d1 = SymmetricAlphaStable (α= 2.0 , scale= 1 / sqrt (2 ), location= 0.0 )
23+ d2 = AlphaStable (d1)
24+ d_ref = Normal (0.0 ,1.0 )
25+ @test cf .(Ref (d1),xs) ≈ cf .(Ref (d_ref),xs)
26+ @test cf .(Ref (d2),xs) ≈ cf .(Ref (d_ref),xs)
27+
28+ xs = range (- 10 ,10 ,length= 100 )
29+ d1 = SymmetricAlphaStable (α= 1.0 , scale= 1.0 , location= 0.0 )
30+ d2 = AlphaStable (d1)
31+ d_ref = Cauchy (0.0 ,1.0 )
32+ @test cf .(Ref (d1),xs) ≈ cf .(Ref (d_ref),xs)
33+ @test cf .(Ref (d2),xs) ≈ cf .(Ref (d_ref),xs)
34+
35+ d1 = SymmetricAlphaStable (α= 1.0 , scale= 17.9 , location= 42.0 )
36+ d2 = AlphaStable (d1)
37+ d_ref = Cauchy (42.0 ,17.9 )
38+ @test cf .(Ref (d1),xs) ≈ cf .(Ref (d_ref),xs)
39+ @test cf .(Ref (d2),xs) ≈ cf .(Ref (d_ref),xs)
40+
41+ d1 = AlphaStable (α= 1 / 2 , β= 1.0 , scale= 12.0 , location= - 7.2 )
42+ d_ref = Levy (- 7.2 , 12.0 )
43+ @test cf .(Ref (d1),xs) ≈ cf .(Ref (d_ref),xs)
544
45+ @test @inferred (cf (AlphaStable (α = 1.0 ) , 1.0 )) isa Complex{Float64}
46+ @test @inferred (cf (AlphaStable (α = 1 ) , 1 )) isa Complex{Float64}
47+ @test @inferred (cf (AlphaStable (α = 1.0 ) , 1f0 )) isa Complex{Float64}
48+ @test @inferred (cf (AlphaStable (α = 1f0 ) , 1 )) isa Complex{Float32}
49+ @test @inferred (cf (AlphaStable (α = 1f0 ) , 1f0 )) isa Complex{Float32}
50+ end
51+
52+
53+ @testset " AlphaStableDistributions.jl" begin
54+ rng = MersenneTwister (0 )
655 sampletypes = [Float32,Float64]
756 stabletypes = [AlphaStable,SymmetricAlphaStable]
857 αs = [0.6 : 0.1 : 2 ,1 : 0.1 : 2 ]
958 for sampletype ∈ sampletypes
1059 for (i, stabletype) in enumerate (stabletypes)
1160 for α in αs[i]
1261 d1 = AlphaStable (α= sampletype (α))
13- s = rand (d1, 200000 )
62+ s = rand (rng, d1, 200000 )
1463 @test eltype (s) == sampletype
1564
1665 d2 = fit (stabletype, s)
@@ -22,14 +71,14 @@ using Test, Random, Distributions
2271 @test d1. location ≈ d2. location atol= 0.1
2372 end
2473
25- xnormal = rand (Normal (3.0 , 4.0 ), 96000 )
74+ xnormal = rand (rng, Normal (3.0 , 4.0 ), 96000 )
2675 d = fit (stabletype, xnormal)
2776 @test d. α ≈ 2 rtol= 0.2
2877 stabletype != SymmetricAlphaStable && @test d. β ≈ 0 atol= 0.2
2978 @test d. scale ≈ 4 /√ 2 rtol= 0.2
3079 @test d. location ≈ 3 rtol= 0.1
3180
32- xcauchy = rand (Cauchy (3.0 , 4.0 ), 96000 )
81+ xcauchy = rand (rng, Cauchy (3.0 , 4.0 ), 96000 )
3382 d = fit (stabletype, xcauchy)
3483 @test d. α ≈ 1 rtol= 0.2
3584 stabletype != SymmetricAlphaStable && @test d. β ≈ 0 atol= 0.2
@@ -40,9 +89,9 @@ using Test, Random, Distributions
4089
4190 for α in 1.1 : 0.1 : 1.9
4291 d = AlphaSubGaussian (α= α, n= 96000 )
43- x = rand (d)
92+ x = rand (rng, d)
4493 x2 = copy (x)
45- rand! (d, x2)
94+ rand! (rng, d, x2)
4695 @test x != x2
4796
4897 d3 = fit (AlphaStable, x)
@@ -54,13 +103,12 @@ using Test, Random, Distributions
54103
55104 d4 = AlphaSubGaussian (α= 1.5 , n= 96000 )
56105 m = size (d4. R, 1 ) - 1
57- x = rand (d4)
106+ x = rand (rng, d4)
58107 d5 = fit (AlphaSubGaussian, x, m, p= 1.0 )
59108 @test d4. α ≈ d5. α rtol= 0.1
60109 @test d4. R ≈ d5. R rtol= 0.1
61110
62111end
63-
64112# 362.499 ms (4620903 allocations: 227.64 MiB)
65113# 346.520 ms (4621052 allocations: 209.62 MiB) # StaticArrays in outer fun
66114# 345.925 ms (4225524 allocations: 167.66 MiB) # tempind to tuple
114162# d1 = AlphaStable(α=1.5)
115163# s = rand(d1, 100000)
116164# using ThreadsX
117- # @btime fit($AlphaStable, $s, $ThreadsX.MergeSort)
165+ # @btime fit($AlphaStable, $s, $ThreadsX.MergeSort)
0 commit comments