Skip to content

Commit feafb0b

Browse files
committed
Merge branch 'master' of github.com:org-arl/AlphaStableDistributions.jl
2 parents 99966d5 + 638f5cd commit feafb0b

File tree

5 files changed

+77
-30
lines changed

5 files changed

+77
-30
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
language: julia
33
os:
44
- linux
5-
- osx
65
julia:
76
- 1.3
87
- nightly

Project.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1515
ToeplitzMatrices = "c751599d-da0a-543b-9d20-d0a503d91d24"
1616

1717
[compat]
18-
Distributions = "0.21"
18+
Distributions = "0.21, 0.22"
1919
MAT = "0.7"
20-
StatsBase = "0.32"
21-
julia = "1.1"
22-
SpecialFunctions = "0.8, 0.9"
20+
SpecialFunctions = "0.8, 0.9, 0.10"
2321
StaticArrays = "0.12"
22+
StatsBase = "0.32"
2423
ToeplitzMatrices = "0.6"
24+
julia = "1.1"
2525

2626
[extras]
2727
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
This library is a port of functionality from [arlpy](https://github.com/org-arl/arlpy/blob/master/arlpy/stable.py). The two distributions supported are
77
- [alpha-stable distribution](https://en.wikipedia.org/wiki/Stable_distribution) (`rand` and `fit`)
8-
- [alpha sub-Gaussian distribution with memory](https://arl.nus.edu.sg/twiki6/pub/ARL/BibEntries/SigProc2016RandomVariate.pdf) (`rand`)
8+
- [alpha sub-Gaussian distribution with memory](https://arl.nus.edu.sg/twiki6/pub/ARL/BibEntries/SigProc2016RandomVariate.pdf) (`rand` and `fit`)
99

1010
## Installation
1111
```julia

src/AlphaStableDistributions.jl

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ function Distributions.fit(d::Type{<:AlphaStable}, x)
7676
else
7777
α = 0.
7878
j = findfirst(>=(an), _ena) # _np.where(an <= _ena[:,0])[0]
79-
if j !== nothing
80-
t = (an-_ena[j,1])/(_ena[j+1]-_ena[j])
81-
α = (21-j-t)/10
82-
end
79+
(j === nothing || j == length(_ena)) && (j = length(_ena)-1)
80+
t = (an-_ena[j])/(_ena[j+1]-_ena[j])
81+
α = (21-j-t)/10
82+
8383
end
8484
if α < 0.5
8585
α = 0.5
@@ -185,15 +185,15 @@ function subgausscondprobtabulate(α, x1, x2_ind, invRx1, invR, vjoint, nmin, nm
185185
if r<nmin
186186
grad = (vjoint[m+1, 1]-k2[2])/nmin
187187
cons = k2[2]
188-
vjointR = grad*r1+cons
188+
vjointR = grad*r+cons
189189
elseif r>nmax
190-
vjointR = α*k1[2]*(r^(-α-m))
190+
vjointR = α*k1[2]*(r^(-α-m-1))
191191
else
192192
ti = (log10(r)-log10(nmin))/step+1
193193
tempind = (floor(Int, ti), ceil(Int, ti))
194194
grad = (vjoint[m+1, tempind[1]]-vjoint[m+1, tempind[2]])/(rind[tempind[1]]-rind[tempind[2]])
195195
cons = vjoint[m+1, tempind[1]]-grad*rind[tempind[1]]
196-
vjointR = grad*r1+cons
196+
vjointR = grad*r+cons
197197
end
198198
(1/sqrt(kappa))*kmarg*vjointR/vjointR1
199199
end
@@ -256,4 +256,41 @@ end
256256

257257
Base.rand(rng::AbstractRNG, d::AlphaSubGaussian) = rand!(rng, d, zeros(d.n))
258258

259+
"""
260+
fit(d::Type{<:AlphaSubGaussian}, x, m; p=1.0)
261+
262+
Fit an aSGN(m) model to data via the covariation method.
263+
264+
The covariation method requires an additional parameter `p`. Ideally, 1 < p < α. In most practical impulsive scenarios p=1.0 is sufficient.
265+
`m` is the number of lags in the covariance matrix.
266+
267+
The implementation is based on https://github.com/ahmd-mahm/alpha-SGNm/blob/master/param_est/asgnfit.m
268+
269+
Reference:
270+
A. Mahmood and M. Chitre, "Generating random variates for stable sub-Gaussian processes
271+
with memory", Signal Processing, Volume 131, Pages 271-279, 2017.
272+
(https://doi.org/10.1016/j.sigpro.2016.08.016.)
273+
"""
274+
function Distributions.fit(d::Type{<:AlphaSubGaussian}, x::AbstractVector{T}, m::Integer; p=one(T)) where T
275+
d1 = fit(AlphaStable, x)
276+
α = d1.α; scale=d1.scale
277+
cov = zeros(T, m+1, m+1)
278+
xlen = length(x)
279+
c = ((sum(abs.(x).^p)/xlen)^(1/p))/scale
280+
for i in 1:m
281+
tempxlen = xlen-mod(xlen, i)
282+
xtemp = reshape(x[1:end-mod(xlen, i)], i, tempxlen÷i)
283+
if mod(tempxlen÷i, 2) != 0
284+
xtemp = xtemp[:, 1:end-1]
285+
tempxlen = size(xtemp, 1)*size(xtemp, 2)
286+
end
287+
xtemp = reshape(xtemp', 2, tempxlen÷2)
288+
r = (2/(c^p))*(scale^(2-p))*(xtemp[1, :]'*((sign.(xtemp[2, :]).*(abs.(xtemp[2, :]).^(p-1)))))/(tempxlen/2)
289+
cov[diagind(cov, i)] .+= r
290+
end
291+
cov = (cov+cov')+2*(scale^2)*I(m+1)
292+
cov ./= 2*scale^2
293+
AlphaSubGaussian=α, R=cov, n=length(x))
294+
end
295+
259296
end # module

test/runtests.jl

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,39 @@ using Test, Random
33

44
@testset "AlphaStableDistributions.jl" begin
55

6-
d1 = AlphaStable()
7-
s = rand(d1, 100000)
6+
for α in 0.5:0.1:2
7+
d1 = AlphaStable=α)
8+
s = rand(d1, 100000)
89

9-
d2 = fit(AlphaStable, s)
10+
d2 = fit(AlphaStable, s)
1011

11-
@test d1.α d2.α rtol=0.1
12-
@test d1.β d2.β rtol=0.1
13-
@test d1.scale d2.scale rtol=0.1
14-
@test d1.location d2.location atol=0.03
12+
@test d1.α d2.α rtol=0.1
13+
@test d1.β d2.β rtol=0.1
14+
@test d1.scale d2.scale rtol=0.1
15+
@test d1.location d2.location atol=0.03
16+
end
1517

18+
for α in 1.1:0.1:1.9
1619

17-
d = AlphaSubGaussian(n=96000)
18-
x = rand(d)
19-
x2 = copy(x)
20-
rand!(d, x2)
21-
@test x != x2
20+
d = AlphaSubGaussian(n=96000, α=α)
21+
x = rand(d)
22+
x2 = copy(x)
23+
rand!(d, x2)
24+
@test x != x2
2225

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
26+
d3 = fit(AlphaStable, x)
27+
@test d3.α α rtol=0.2
28+
@test d3.β == 0
29+
@test d3.scale 1 rtol=0.2
30+
@test d3.location 0 atol=0.03
31+
end
32+
33+
d4 = AlphaSubGaussian(n=96000)
34+
m = size(d4.R, 1)-1
35+
x = rand(d4)
36+
d5 = fit(AlphaSubGaussian, x, m, p=1.0)
37+
@test d4.α d5.α rtol=0.1
38+
@test d4.R d5.R rtol=0.1
2839

2940
end
3041
# 362.499 ms (4620903 allocations: 227.64 MiB)

0 commit comments

Comments
 (0)