Skip to content

Commit 8d0a8ab

Browse files
committed
ASG inplace
1 parent d76a234 commit 8d0a8ab

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/AlphaStableDistributions.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,13 @@ function Random.rand!(rng::AbstractRNG, d::AlphaSubGaussian, x::AbstractArray)
227227
S = S/sqrt(sum(abs2,S))
228228
xtmp = ((sigrootx1*sqrt(A*T))*S)'
229229
if n<=m
230-
x = xtmp[1:n]
230+
copyto!(x, @view(xtmp[1:n]))
231231
else
232-
x = zeros(n)
232+
# x = zeros(n)
233233
x[onetom] = xtmp
234234
vstud = α+m
235235
norms = pdf(TDist(vstud), 0.0)
236-
for i = m+1:n
236+
@inbounds for i = m+1:n
237237
x1 = SVector{m,Float64}(view(x,i-m:i-1))
238238
mode = modefactor*x1
239239
norm1 = subgausscondprobtabulate(α, x1, mode, invRx1, invR, vjoint, nmin, nmax, step, rind, kappa, k1, k2, kmarg)
@@ -253,4 +253,7 @@ function Random.rand!(rng::AbstractRNG, d::AlphaSubGaussian, x::AbstractArray)
253253
x
254254
end
255255

256+
257+
Base.rand(rng::AbstractRNG, d::AlphaSubGaussian) = rand!(rng, d, zeros(d.n))
258+
256259
end # module

test/runtests.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ using Test
44
@testset "AlphaStableDistributions.jl" begin
55

66
d1 = AlphaStable()
7-
s = [rand(d1) for _ in 1:100000]
7+
s = rand(d1, 100000)
8+
89
d2 = fit(AlphaStable, s)
910

1011
@test d1.α d2.α rtol=0.1
@@ -13,8 +14,17 @@ d2 = fit(AlphaStable, s)
1314
@test d1.location d2.location atol=0.03
1415

1516

16-
x = rand(AlphaSubGaussian(n=96000))
17+
d = AlphaSubGaussian(n=96000)
18+
x = rand(d)
19+
x2 = copy(x)
20+
rand!(d, x2)
21+
@test x != x2
1722

23+
d3 = fit(AlphaStable, x)
24+
@test d3.α 1.5 rtol=0.2
25+
@test d3.β == 0
26+
@test d3.scale 1 rtol=0.2
27+
@test d3.location 0 atol=0.03
1828

1929
end
2030
# 362.499 ms (4620903 allocations: 227.64 MiB)

0 commit comments

Comments
 (0)