diff --git a/src/ARIMA.jl b/src/ARIMA.jl index 7adb2e7..f1b9565 100644 --- a/src/ARIMA.jl +++ b/src/ARIMA.jl @@ -2,8 +2,8 @@ # vectors of AR and MA coefficients and a number of differences. # This implementation is based on Section 8.3 in Brockwell and Davis 2002, # "Introduction to Time Series and Forecasting," 2nd ed. -function arima_statespace{T, I<:Integer}(ar::Vector{T}, d::I, ma::Vector{T}, - sigma::T) +function arima_statespace(ar::Vector{T}, d::I, ma::Vector{T}, + sigma::T) where {T, I<:Integer} p = length(ar) q = length(ma) r = max(p, q + 1) @@ -30,7 +30,7 @@ function arima_statespace{T, I<:Integer}(ar::Vector{T}, d::I, ma::Vector{T}, StateSpaceModel(F, V, G, W, x0, P0) end -function arima{T, I<:Integer}(y::Vector{T}, p::I, d::I, q::I) +function arima(y::Vector{T}, p::I, d::I, q::I) where {T, I<:Integer} build(par::Vector{T}) = arima_statespace(par[1:p], par[p+1], par[p+2:end]) par0 = zeros(p + d + q) fit(y, build, par0) diff --git a/src/GARCH.jl b/src/GARCH.jl index 3615ed4..44c8281 100644 --- a/src/GARCH.jl +++ b/src/GARCH.jl @@ -2,7 +2,7 @@ # Copyright 2013 Andrey Kolev # Distributed under MIT license (see LICENSE.md) -type GarchFit +mutable struct GarchFit data::Vector params::Vector llh::Float64 diff --git a/src/kalman_filter.jl b/src/kalman_filter.jl index d4bc924..85f5def 100644 --- a/src/kalman_filter.jl +++ b/src/kalman_filter.jl @@ -1,4 +1,4 @@ -type KalmanFiltered{T} +mutable struct KalmanFiltered{T} filtered::Array{T} predicted::Array{T} error_cov::Array{T} @@ -9,7 +9,7 @@ type KalmanFiltered{T} loglik::T end -function Base.show{T}(io::IO, filt::KalmanFiltered{T}) +function Base.show(io::IO, filt::KalmanFiltered{T}) where T n = size(filt.y, 1) dx, dy = filt.model.nx, filt.model.ny println("KalmanFiltered{$T}") @@ -17,7 +17,7 @@ function Base.show{T}(io::IO, filt::KalmanFiltered{T}) println("Negative log-likelihood: $(filt.loglik)") end -function kalman_filter{T}(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=zeros(size(y,1), model.nu)) +function kalman_filter(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=zeros(size(y,1), model.nu)) where T @assert size(u,1) == size(y,1) @assert size(y,2) == model.ny @@ -72,7 +72,7 @@ function kalman_filter{T}(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=ze return KalmanFiltered(x_filt', x_pred', P_filt, P_pred, model, y', u', log_likelihood) end -function loglikelihood{T}(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=zeros(size(y,1), model.nu)) +function loglikelihood(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=zeros(size(y,1), model.nu)) where T y, u = y', u' diff --git a/src/kalman_smooth.jl b/src/kalman_smooth.jl index aa76725..d8664ab 100644 --- a/src/kalman_smooth.jl +++ b/src/kalman_smooth.jl @@ -1,4 +1,4 @@ -type KalmanSmoothed{T} +mutable struct KalmanSmoothed{T} predicted::Array{T} filtered::Array{T} smoothed::Array{T} @@ -9,7 +9,7 @@ type KalmanSmoothed{T} loglik::T end -type KalmanSmoothedMinimal{T} +mutable struct KalmanSmoothedMinimal{T} y::Array{T} smoothed::Array{T} error_cov::Union{Vector{Matrix{T}}, Vector{SparseMatrixCSC{T, Int}}} @@ -18,7 +18,7 @@ type KalmanSmoothedMinimal{T} loglik::T end -function show{T}(io::IO, smoothed::KalmanSmoothed{T}) +function show(io::IO, smoothed::KalmanSmoothed{T}) where T n = size(smoothed.y, 1) dx, dy = smoothed.model.nx, smoothed.model.ny println("KalmanSmoothed{$T}") @@ -26,7 +26,7 @@ function show{T}(io::IO, smoothed::KalmanSmoothed{T}) println("Negative log-likelihood: $(smoothed.loglik)") end -function show{T}(io::IO, smoothed::KalmanSmoothedMinimal{T}) +function show(io::IO, smoothed::KalmanSmoothedMinimal{T}) where T n = size(smoothed.y, 1) dx, dy = smoothed.model.nx, smoothed.model.ny println("KalmanSmoothedMinimal{$T}") @@ -35,7 +35,7 @@ function show{T}(io::IO, smoothed::KalmanSmoothedMinimal{T}) end # Durbin-Koopman Smoothing -function kalman_smooth{T}(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=zeros(size(y,1), model.nu)) +function kalman_smooth(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=zeros(size(y,1), model.nu)) where T Ksparse = issparse(model.A(1)) && issparse(model.V(1)) && issparse(model.C(1)) @@ -154,7 +154,7 @@ function kalman_smooth{T}(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=ze end #smooth -function lag1_smooth{T}(y::Array{T}, u::Array{T}, m::StateSpaceModel{T}) +function lag1_smooth(y::Array{T}, u::Array{T}, m::StateSpaceModel{T}) where T A_0, A_I = zeros(m.A(1)), eye(m.A(1)) B_0, V_0, C_0 = zeros(m.B(1)), zeros(m.V(1)), zeros(m.C(1)) diff --git a/src/parameter_estimation.jl b/src/parameter_estimation.jl index ebc7f2c..52c3e18 100644 --- a/src/parameter_estimation.jl +++ b/src/parameter_estimation.jl @@ -1,7 +1,7 @@ # ML parameter estimation with filter result log-likelihood (via Nelder-Mead) -function fit{T}(y::Matrix{T}, build::Function, theta0::Vector{T}; - u::Array{T}=zeros(size(y,1), build(theta0).nu)) +function fit(y::Matrix{T}, build::Function, theta0::Vector{T}; + u::Array{T}=zeros(size(y,1), build(theta0).nu)) where T objective(theta) = kalman_filter(y, build(theta), u=u).loglik kfit = Optim.optimize(objective, theta0) return (kfit.minimum, build(kfit.minimum), filter(y, build(kfit.minimum))) @@ -9,8 +9,8 @@ end #fit # Expectation-Maximization (EM) parameter estimation -function fit{T}(y::Array{T}, pmodel::ParametrizedSSM, params::SSMParameters; - u::Array{T}=zeros(size(y,1), pmodel.nu), eps::Float64=1e-6, niter::Int=typemax(Int)) +function fit(y::Array{T}, pmodel::ParametrizedSSM, params::SSMParameters; + u::Array{T}=zeros(size(y,1), pmodel.nu), eps::Float64=1e-6, niter::Int=typemax(Int)) where T n = size(y, 1) @assert n == size(u, 1) @@ -62,7 +62,7 @@ function fit{T}(y::Array{T}, pmodel::ParametrizedSSM, params::SSMParameters; estimate_A, estimate_B, estimate_Q, estimate_C, estimate_D, estimate_R, estimate_x1, estimate_S = map(x->x>0, [na, nb, nq, nc, nd, nr, nx1, ns]) - function em_kernel!{T}(params::SSMParameters{T}) + function em_kernel!(params::SSMParameters{T}) where T m = pmodel(params) @@ -337,8 +337,8 @@ function fit{T}(y::Array{T}, pmodel::ParametrizedSSM, params::SSMParameters; end #fit -function fit{T}(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=zeros(size(y,1), model.nu), - eps::Float64=1e-6, niter::Int=typemax(Int)) +function fit(y::Array{T}, model::StateSpaceModel{T}; u::Array{T}=zeros(size(y,1), model.nu), + eps::Float64=1e-6, niter::Int=typemax(Int)) where T # B, Z, x1 default to parametrized as fully unconstrained A, A_params = parametrize_full(model.A(1)) diff --git a/src/statespacemodel.jl b/src/statespacemodel.jl index df1d128..6d87954 100644 --- a/src/statespacemodel.jl +++ b/src/statespacemodel.jl @@ -1,6 +1,6 @@ abstract type AbstractStateSpaceModel{T<:Real} end -immutable StateSpaceModel{T} <: AbstractStateSpaceModel{T} +struct StateSpaceModel{T} <: AbstractStateSpaceModel{T} # Process transition matrix, control matrix, and noise covariance A::Function @@ -36,20 +36,20 @@ immutable StateSpaceModel{T} <: AbstractStateSpaceModel{T} end # Time-dependent constructor -StateSpaceModel{T}(A::Function, V::Function, C::Function, W::Function, - x1::Vector{T}, P1::Union{Matrix{T}, SparseMatrixCSC{T}}; - B::Function=_->zeros(size(V(1), 1), 1), - D::Function=_->zeros(size(W(1), 1), 1)) = +StateSpaceModel(A::Function, V::Function, C::Function, W::Function, + x1::Vector{T}, P1::Union{Matrix{T}, SparseMatrixCSC{T}}; + B::Function=_->zeros(size(V(1), 1), 1), + D::Function=_->zeros(size(W(1), 1), 1)) where {T} = StateSpaceModel{T}(A, B, V, C, D, W, x1, P1) # Time-independent constructor -StateSpaceModel{T}(A::Matrix{T}, V::Matrix{T}, C::Matrix{T}, W::Matrix{T}, - x1::Vector{T}, P1::Union{Matrix{T}, SparseMatrixCSC{T}}; - B::Matrix{T}=zeros(size(A, 1), 1), - D::Matrix{T}=zeros(size(C, 1), 1)) = +StateSpaceModel(A::Matrix{T}, V::Matrix{T}, C::Matrix{T}, W::Matrix{T}, + x1::Vector{T}, P1::Union{Matrix{T}, SparseMatrixCSC{T}}; + B::Matrix{T}=zeros(size(A, 1), 1), + D::Matrix{T}=zeros(size(C, 1), 1)) where {T} = StateSpaceModel{T}(_->A, _->B, _->V, _->C, _->D, _->W, x1, P1) -function show{T}(io::IO, mod::StateSpaceModel{T}) +function show(io::IO, mod::StateSpaceModel{T}) where T dx, dy = mod.nx, mod.ny println("StateSpaceModel{$T}, $dx-D process x $dy-D observations") println("Process evolution matrix A:") @@ -68,7 +68,7 @@ end # Time-independent parametrized matrix type # TODO: A more general ParametrizedArray (allowing vectors) could be preferable -immutable ParametrizedMatrix{T} +struct ParametrizedMatrix{T} f::AbstractVector{T} D::Union{Matrix{T}, SparseMatrixCSC{T}} @@ -84,13 +84,13 @@ immutable ParametrizedMatrix{T} end #ParametrizedMatrix -ParametrizedMatrix{T}(f::Vector{T}, D::Matrix{T}, dims::Tuple{Int, Int}) = ParametrizedMatrix{T}(f, D, dims) +ParametrizedMatrix(f::Vector{T}, D::Matrix{T}, dims::Tuple{Int, Int}) where {T} = ParametrizedMatrix{T}(f, D, dims) -function ParametrizedMatrix{T}(f::SparseVector{T}, D::SparseMatrixCSC{T}, dims::Tuple{Int, Int}) +function ParametrizedMatrix(f::SparseVector{T}, D::SparseMatrixCSC{T}, dims::Tuple{Int, Int}) where T ParametrizedMatrix{T}(f, D, dims) end #ParametrizedMatrix -function show{T}(io::IO, cpm::ParametrizedMatrix{T}) +function show(io::IO, cpm::ParametrizedMatrix{T}) where T function combinestrelems(a, b) if (a != "") & (b != "") @@ -137,10 +137,10 @@ function (pm::ParametrizedMatrix{T})(params::Vector{T}) where {T} reshape(pm.f + pm.D * sparse(params), pm.dims) end #call -parametrize_full{T}(X::Matrix{T}) = +parametrize_full(X::Matrix{T}) where {T} = ParametrizedMatrix{T}(zeros(length(X)), eye(length(X)), size(X)) -function parametrize_diag{T}(x::Vector{T}; sparse=false) +function parametrize_diag(x::Vector{T}; sparse=false) where T n = length(x) f = sparse ? spzeros(n^2, 1) : zeros(n^2) D = sparse ? spzeros(n^2, n) : zeros(n^2, n) @@ -148,14 +148,14 @@ function parametrize_diag{T}(x::Vector{T}; sparse=false) return ParametrizedMatrix{T}(f, D, (n,n)) end #parametrize_diag -parametrize_none{T}(X::Matrix{T}) = +parametrize_none(X::Matrix{T}) where {T} = ParametrizedMatrix{T}(vec(X), zeros(length(X), 0), size(X)) -parametrize_none{T}(X::SparseMatrixCSC{T}) = +parametrize_none(X::SparseMatrixCSC{T}) where {T} = ParametrizedMatrix{T}(vec(X), spzeros(length(X), 0), size(X)) # Time-independent parametrized state space model -immutable ParametrizedSSM{T} <: AbstractStateSpaceModel{T} +struct ParametrizedSSM{T} <: AbstractStateSpaceModel{T} # Transition equation and noise covariance @@ -207,21 +207,21 @@ immutable ParametrizedSSM{T} <: AbstractStateSpaceModel{T} end #ParametrizedSSM -ParametrizedSSM{T}(A2::ParametrizedMatrix{T}, Q::ParametrizedMatrix{T}, - C2::ParametrizedMatrix{T}, R::ParametrizedMatrix{T}, - x1::ParametrizedMatrix{T}, S::ParametrizedMatrix{T}; - A1::Function=_->speye(size(A2,1)), A3::Function=_->speye(size(A2,2)), - B1::Function=_->speye(size(x1,1)), - B2::ParametrizedMatrix{T}=parametrize_none(spzeros(size(B1(1),2), 1)), - G::Function=_->speye(size(x1,1)), - C1::Function=_->speye(size(C2, 1)), C3::Function=_->speye(size(C2,2)), - D1::Function=_->speye(size(C1(1),1)), - D2::ParametrizedMatrix{T}=parametrize_none(spzeros(size(C1(1),1), 1)), - H::Function=_->speye(size(C1(1),1)), J::AbstractMatrix=speye(size(x1, 1))) = +ParametrizedSSM(A2::ParametrizedMatrix{T}, Q::ParametrizedMatrix{T}, + C2::ParametrizedMatrix{T}, R::ParametrizedMatrix{T}, + x1::ParametrizedMatrix{T}, S::ParametrizedMatrix{T}; + A1::Function=_->speye(size(A2,1)), A3::Function=_->speye(size(A2,2)), + B1::Function=_->speye(size(x1,1)), + B2::ParametrizedMatrix{T}=parametrize_none(spzeros(size(B1(1),2), 1)), + G::Function=_->speye(size(x1,1)), + C1::Function=_->speye(size(C2, 1)), C3::Function=_->speye(size(C2,2)), + D1::Function=_->speye(size(C1(1),1)), + D2::ParametrizedMatrix{T}=parametrize_none(spzeros(size(C1(1),1), 1)), + H::Function=_->speye(size(C1(1),1)), J::AbstractMatrix=speye(size(x1, 1))) where {T} = ParametrizedSSM{T}(A1, A2, A3, B1, B2, G, Q, C1, C2, C3, D1, D2, H, R, x1, J, S) # State space model parameters -immutable SSMParameters{T} +struct SSMParameters{T} # Process transition and noise covariance A::Vector{T} @@ -239,9 +239,9 @@ immutable SSMParameters{T} end #SSMParameters -SSMParameters{T}(__::T; A::Vector{T}=T[], B::Vector{T}=T[], Q::Vector{T}=T[], - C::Vector{T}=T[], D::Vector{T}=T[], R::Vector{T}=T[], - x1::Vector{T}=T[], S::Vector{T}=T[]) = +SSMParameters(__::T; A::Vector{T}=T[], B::Vector{T}=T[], Q::Vector{T}=T[], + C::Vector{T}=T[], D::Vector{T}=T[], R::Vector{T}=T[], + x1::Vector{T}=T[], S::Vector{T}=T[]) where {T} = SSMParameters{T}(A, B, Q, C, D, R, x1, S) function (m::ParametrizedSSM{T})(p::SSMParameters{T}) where {T} @@ -281,13 +281,13 @@ function confirm_matrix_sizes(F::AbstractMatrix, B::AbstractMatrix, V::AbstractM end #confirm_matrix_sizes -function confirm_matrix_sizes{T}(A1::AbstractMatrix, A2::ParametrizedMatrix{T}, A3::AbstractMatrix, - B1::AbstractMatrix, B2::ParametrizedMatrix{T}, - G::AbstractMatrix, Q::ParametrizedMatrix{T}, - C1::AbstractMatrix, C2::ParametrizedMatrix{T}, C3::AbstractMatrix, - D1::AbstractMatrix, D2::ParametrizedMatrix{T}, - H::AbstractMatrix, R::ParametrizedMatrix{T}, - x1::ParametrizedMatrix{T}, J::AbstractMatrix, S::ParametrizedMatrix{T}) +function confirm_matrix_sizes(A1::AbstractMatrix, A2::ParametrizedMatrix{T}, A3::AbstractMatrix, + B1::AbstractMatrix, B2::ParametrizedMatrix{T}, + G::AbstractMatrix, Q::ParametrizedMatrix{T}, + C1::AbstractMatrix, C2::ParametrizedMatrix{T}, C3::AbstractMatrix, + D1::AbstractMatrix, D2::ParametrizedMatrix{T}, + H::AbstractMatrix, R::ParametrizedMatrix{T}, + x1::ParametrizedMatrix{T}, J::AbstractMatrix, S::ParametrizedMatrix{T}) where T @assert size(B2, 2) == size(D2, 2) @@ -345,7 +345,7 @@ Arguments: u : Array (optional) n x nu array of exogenous inputs " -function simulate{T}(model::StateSpaceModel{T}, n::Int; u::Array{T}=zeros(n, model.nu)) +function simulate(model::StateSpaceModel{T}, n::Int; u::Array{T}=zeros(n, model.nu)) where T @assert size(u, 1) == n @assert size(u, 2) == model.nu