using FFTW, ARCHModels
abstract type CepstralCoeffModel end
struct RealCepstral <: CepstralCoeffModel end
function cc(
model::Type{<:CepstralCoeffModel},
tseries::AbstractVector,
p::Integer,
n::Integer
)
series = tseries
normalize && begin
series = copy(tseries)
normalizer!(series)
end
α = fit_arima(series, p)
coefs = cepscoef(model, α, p, n)
return real.(coefs)
end
normalizer!(series::AbstractVector) = series .= (series .- mean(series)) ./ std(series)
function fit_arima(series::AbstractVector, p::Integer)
model = fit(ARMA{p, 0}, series)
return model.meanspec.coefs[2:end]
end
function cepscoef(::Type{RealCepstral}, series::AbstractVector, p::Integer, n::Int)
p≥n || ArgumentError("`p` must be equal to or greater than `n` when using `RealCepstral`. \
Passed $p and $n.") |> throw
res = series |> fft .|> abs .|> log |> ifft
return res[1:n]
end
Sorry for any inconvenience, @nsajko, @cjdoris.
The complete script is provided above.
Thank you for the great advice. I found out there is an internal problem with ARCHModels.jl.
The root of the problem derives from model = fit(ARMA{p, 0}, series) in my code. The Cthulhu shows:
The problem that I mentioned in the original post:
Handled by using type assertions in the definition of the fit_arima function. The former version of the function:
The new version:
function fit_arima(series::AbstractVector{T}, p::Integer) where T
model = fit(ARMA{p, 0}, series)
return model.meanspec.coefs[2:end]::Vector{T}
end
And the problem gets solved. Credits belong to my friend, @eliascarv. Thank you so much
P.S.: The problem with the type instability of the ARCHModels.jl still exists. There are several technical issues with the source code that I will hopefully manage to fix through some PRs.