Skip to content

Commit 963e623

Browse files
added tanh and hardtanh mapping options for pacf's
1 parent 94455d7 commit 963e623

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

src/ARIMAUtils.jl

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,38 @@ function ARMAacf(; ϕ = [], θ = [], maxlag = maximum(length(ϕ), length(θ)+1),
2020
return ARMAacf
2121
end
2222

23+
"""
24+
hardtanh(x, ε = 0.0001)
2325
26+
Hard tanh function that maps x linearly to the interval (-1 + ε, 1 - ε).
27+
28+
# Examples
29+
```julia-repl
30+
julia> hardtanh(0.4, 0.0001)
31+
0.4
32+
julia> hardtanh(1, 0.0001)
33+
0.9999
34+
julia> hardtanh(-1.1, 0.0001)
35+
-0.9999
36+
```
37+
"""
38+
function hardtanh(x, ε = 0.0001)
39+
return (x <= -1) ? -(1-ε) : (x>= 1) ? (1-ε) : x
40+
end
2441

2542
"""
2643
arma_reparam(x::Vector [, pacf_map = "monahan", negative_signs = true])
2744
2845
Takes a p-dim vector `x` and returns parameters restricted to the have roots outside the unit circle, i.e. stationary(AR) or invertible (MA). The mapping is performed via the partial autocorrelations, P as: x -> P -> ϕ.
2946
30-
If `pacf_map == "sigmoid"`, then the partial autocorrelations are parameterized as
31-
P = (exp(x) - 1) / (exp(x) + 1 )
47+
If `pacf_map == "tanh"`, then the partial autocorrelations are parameterized as
48+
P = tanh(x)
3249
3350
If `pacf_map == "monahan"`, then the partial autocorrelations are parameterized as
3451
P = x/√(1 + x²)
3552
53+
If `pacf_map == "hardtanh"`, then the partial autocorrelations are parameterized as hardtanh
54+
3655
If `negative_signs == true`
3756
coefficients are for polynomial 1 - ϕ₁B - ϕ₂B² - .... which is typically used for AR\\
3857
If `negative_signs == false`
@@ -49,22 +68,19 @@ julia> check_stationarity(ϕ)[1] # second element would return the eigenvalues
4968
true
5069
```
5170
"""
52-
function arma_reparam(x; pacf_map = "monahan", threshold = nothing, negative_signs = true)
71+
function arma_reparam(x; pacf_map = "monahan", negative_signs = true)
5372

5473
p = length(x)
55-
if pacf_map == "sigmoid"
56-
P = (exp.(x) .- 1) ./ (exp.(x) .+ 1 )
57-
P[x .>= 700] = (1 .- exp.(-x[x .>= 700])) ./ (1 .+ exp.(-x[x .>= 700]))
74+
if pacf_map == "tanh"
75+
P = tanh.(x)
5876
elseif pacf_map == "monahan"
5977
P = x./sqrt.(1 .+ x.^2)
78+
elseif pacf_map == "hardtanh"
79+
P = hardtanh.(x)
6080
elseif pacf_map == "linear" # No transformation
6181
return x, NaN
6282
else
63-
error("pacf_map must be either 'sigmoid' or 'monahan'")
64-
end
65-
66-
if !isnothing(threshold)
67-
P = clamp.(P, -threshold, threshold)
83+
error("pacf_map must be either 'tanh', 'monahan', 'relu' or 'linear'")
6884
end
6985

7086
if negative_signs
@@ -130,13 +146,13 @@ function check_stationarity(ϕ::Vector)
130146
end
131147

132148
"""
133-
sarma_reparam(θ::Vector, Θ::Vector, s, activeLags; pacf_map = "monahan", threshold = nothing, negative_signs = true)
149+
sarma_reparam(θ::Vector, Θ::Vector, s, activeLags; pacf_map = "monahan", negative_signs = true)
134150
135151
Takes a p-dim vector `θ` with regular AR/MA coefficients and a P-dim vector with seasonal AR/MA coefficients `Θ` (with season `s`) and returns the *non-zero* coefficients in the product polynomial
136152
(1 - ϕ₁B - ϕ₂B² - ....)(1 - Φ₁B - Φ₂B² - ....) = (1 - ψ₁B - Ψ₂B² - ....)
137153
where both sets of parameters (ϕ and Φ) are restricted to the have roots outside the unit circle, i.e. stationary(AR) or invertible (MA). The mapping is performed via the partial autocorrelations, P as: θ -> P₁ -> ϕ and Θ -> P₂ -> Φ.
138154
139-
If `pacf_map == "sigmoid"`, then the partial autocorrelations are parameterized as
155+
If `pacf_map == "tanh"`, then the partial autocorrelations are parameterized as
140156
P = (exp(x) - 1) / (exp(x) + 1 )
141157
142158
If `pacf_map == "monahan"`, then the partial autocorrelations are parameterized as
@@ -149,18 +165,15 @@ coefficients are for polynomial
149165
1 + ϕ₁B + ϕ₂B² + .... which is typically used for MA
150166
151167
"""
152-
function sarma_reparam(θ, Θ, s, activeLags = nothing; pacf_map = "monahan",
153-
threshold = nothing, negative_signs = true)
168+
function sarma_reparam(θ, Θ, s, activeLags = nothing; pacf_map = "monahan", negative_signs = true)
154169

155170
p = length(θ)
156171
P = length(Θ)
157172
if isnothing(activeLags)
158173
activeLags = FindActiveLagsSAR(p, P, s)
159174
end
160-
ϕ = arma_reparam(θ; pacf_map = pacf_map, threshold = threshold,
161-
negative_signs = negative_signs)[1] .+ eps()
162-
Φ = arma_reparam(Θ; pacf_map = pacf_map, threshold = threshold,
163-
negative_signs = negative_signs)[1] .+ eps()
175+
ϕ = arma_reparam(θ; pacf_map = pacf_map, negative_signs = negative_signs)[1] .+ eps()
176+
Φ = arma_reparam(Θ; pacf_map = pacf_map, negative_signs = negative_signs)[1] .+ eps()
164177
ARpoly = Polynomial([1;-ϕ], :z)
165178
ARseasonpolymat = [zeros(s-1,P);-Φ'];
166179
ARseasonpoly = Polynomial([1;ARseasonpolymat[:]], :z)

0 commit comments

Comments
 (0)