-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathar.jl
61 lines (54 loc) · 1.29 KB
/
ar.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using BandedMatrices
using ToeplitzMatrices
using LinearAlgebra
function find_ρ(ϕ::Array{T}) where {T}
p = length(ϕ)
A = zeros(T, p+1,p+1)
A[1,1] = 1;
for i in 1:p
for j in 1:p
idx = abs(i-j)
(idx <= p) && (A[i+1,idx+1] += ϕ[j])
end
end
# Get the eigen vector associated with the largest eigen value.
# This should correspond to the fix-point of ρ = A ρ
r = eigvecs(A)[:,p+1];
r /= r[1]
# Get rid of the useless 1
r[2:(p+1)]
end
function gen_ρ(n, ϕ::Array{T}) where {T}
p = length(ϕ)
ρ = zeros(T, n)
ρ[1:p] = find_ρ(ϕ)
for i in (p+1):n
for j in 1:p
ρ[i] += ϕ[j]*ρ[i-j]
end
end
return ρ
end
function Γ_ar(n, ϕ::Array{T}) where {T}
ρ = gen_ρ(n, ϕ)
Toeplitz(ρ, ρ)
end
function W(n, ϕ::Array{T}, stationary=true) where {T}
p = length(ϕ);
# Actually, it will be a lower-band matrix with band-with equal to p
M = BandedMatrix(zeros(T, n, n), (p,0));
for i in 1:n
M[i,i] = 1;
for j in 1:p
((i-j) > 0) && (M[i,i-j] = -ϕ[j])
end
end
if stationary
E = Symmetric(BandedMatrix(M * Γ_ar(n, ϕ) * transpose(M), (p,p)));
E = cholesky(E)
# Uhh, very inefficient Gauss-algorithm and waste of mem/gc action.
m = Matrix(M);
M = BandedMatrix(E.L \ m, (p,0));
end
return M
end