Skip to content

Commit 707ba02

Browse files
authored
Merge branch 'master' into compathelper/new_version/2019-12-31-03-05-28-992-167003063
2 parents cd0bc38 + b895c8e commit 707ba02

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ToeplitzMatrices = "c751599d-da0a-543b-9d20-d0a503d91d24"
1616

1717
[compat]
1818
MAT = "0.7"
19+
StatsBase = "0.32"
1920
julia = "1.1"
2021

2122
[extras]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ julia> using AlphaStableDistributions
1919
julia> d1 = AlphaStable()
2020
AlphaStable{Float64}=1.5, β=0.0, scale=1.0, location=0.0)
2121

22-
julia> s = [rand(d1) for _ in 1:100000];
22+
julia> s = rand(d1, 100000);
2323

2424
julia> d2 = fit(AlphaStable, s)
2525

src/AlphaStableDistributions.jl

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,40 @@ function Base.rand(rng::AbstractRNG, d::AlphaStable)
121121
= π/2 + β*ϕ
122122
x = 2/π * (bϕ*tan(ϕ) - β*log/2*w*cosϕ/bϕ))
123123
α == 1 || (x += β * tan*α/2))
124-
124+
125125
return loc + scale*x
126126
end
127127

128128

129129
"""
130-
...
130+
131+
Generate alpha-sub-Gaussian (aSG) random numbers.
132+
133+
The implementation is based on https://github.com/ahmd-mahm/alpha-SGNm/blob/master/asgn.m
134+
135+
Reference:
136+
A. Mahmood and M. Chitre, "Generating random variates for stable sub-Gaussian processes
137+
with memory", Signal Processing, Volume 131, Pages 271-279, 2017.
138+
(https://doi.org/10.1016/j.sigpro.2016.08.016.)
139+
140+
131141
# Arguments
132-
- `alpha`: characteristic exponent associated with the aSGN(m) process. This is
142+
- `α`: characteristic exponent associated with the aSGN(m) process. This is
133143
a scalar input and should lie within `collect(1.1:0.01:1.98)`.
134144
- `R`: covariance matrix of any adjacent `m+1` samples in an aSGN(m) process.
135145
The dimension of `R` is equal to `m+1`. It should be a symmetric toeplitz matrix.
136146
The maximum acceptable size of `R` is `10x10`
137147
- `n`: number of samples required
138-
...
148+
149+
# Examples
150+
```jldoctest
151+
julia> x = rand(AlphaSubGaussian(n=1000))
152+
```
139153
"""
140154
Base.@kwdef struct AlphaSubGaussian{T,M<:AbstractMatrix} <: Distributions.ContinuousUnivariateDistribution
141155
α::T = 1.50
142156
R::M = SMatrix{5,5}(collect(SymmetricToeplitz([1.0000, 0.5804, 0.2140, 0.1444, -0.0135])))
143-
n::Int = 10000
157+
n::Int
144158
end
145159

146160
"""
@@ -184,23 +198,10 @@ function subgausscondprobtabulate(α, x1, x2_ind, invRx1, invR, vjoint, nmin, nm
184198
(1/sqrt(kappa))*kmarg*vjointR/vjointR1
185199
end
186200

187-
"""
188-
Generate alpha-sub-Gaussian (aSG) random numbers.
189-
190-
The implementation is based on https://github.com/ahmd-mahm/alpha-SGNm/blob/master/asgn.m
191201

192-
Reference:
193-
A. Mahmood and M. Chitre, "Generating random variates for stable sub-Gaussian processes
194-
with memory", Signal Processing, Volume 131, Pages 271-279, 2017.
195-
(https://doi.org/10.1016/j.sigpro.2016.08.016.)
196-
197-
# Examples
198-
```jldoctest
199-
julia> x = rand(AlphaSubGaussian(n=1000))
200-
```
201-
"""
202-
function Base.rand(rng::AbstractRNG, d::AlphaSubGaussian)
202+
function Random.rand!(rng::AbstractRNG, d::AlphaSubGaussian, x::AbstractArray)
203203
α=d.α; R=d.R; n=d.n
204+
length(x) >= n || throw(ArgumentError("length of x must be at least n"))
204205
α 1.10:0.01:1.98 || throw(DomainError(α, "α must lie within `1.10:0.01:1.98`"))
205206
m = size(R, 1)-1
206207
funk1 = x -> (2^α)*sin*α/2)*gamma((α+2)/2)*gamma((α+x)/2)/(gamma(x/2)*π*α/2)
@@ -226,13 +227,13 @@ function Base.rand(rng::AbstractRNG, d::AlphaSubGaussian)
226227
S = S/sqrt(sum(abs2,S))
227228
xtmp = ((sigrootx1*sqrt(A*T))*S)'
228229
if n<=m
229-
x = xtmp[1:n]
230+
copyto!(x, @view(xtmp[1:n]))
230231
else
231-
x = zeros(n)
232+
# x = zeros(n)
232233
x[onetom] = xtmp
233234
vstud = α+m
234235
norms = pdf(TDist(vstud), 0.0)
235-
for i = m+1:n
236+
@inbounds for i = m+1:n
236237
x1 = SVector{m,Float64}(view(x,i-m:i-1))
237238
mode = modefactor*x1
238239
norm1 = subgausscondprobtabulate(α, x1, mode, invRx1, invR, vjoint, nmin, nmax, step, rind, kappa, k1, k2, kmarg)
@@ -252,4 +253,7 @@ function Base.rand(rng::AbstractRNG, d::AlphaSubGaussian)
252253
x
253254
end
254255

256+
257+
Base.rand(rng::AbstractRNG, d::AlphaSubGaussian) = rand!(rng, d, zeros(d.n))
258+
255259
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)