Skip to content

Commit

Permalink
Merge pull request #67 from JuliaStats/fbot/deps
Browse files Browse the repository at this point in the history
Fix deprecations
  • Loading branch information
andreasnoack committed Oct 23, 2017
2 parents 976d52c + 93cf2c8 commit 648b529
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 63 deletions.
6 changes: 3 additions & 3 deletions src/ARIMA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/GARCH.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/kalman_filter.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type KalmanFiltered{T}
mutable struct KalmanFiltered{T}
filtered::Array{T}
predicted::Array{T}
error_cov::Array{T}
Expand All @@ -9,15 +9,15 @@ 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}")
println("$n observations, $dx-D process x $dy-D observations")
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
Expand Down Expand Up @@ -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'
Expand Down
12 changes: 6 additions & 6 deletions src/kalman_smooth.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type KalmanSmoothed{T}
mutable struct KalmanSmoothed{T}
predicted::Array{T}
filtered::Array{T}
smoothed::Array{T}
Expand All @@ -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}}}
Expand All @@ -18,15 +18,15 @@ 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}")
println("$n observations, $dx-D process x $dy-D observations")
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}")
Expand All @@ -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))

Expand Down Expand Up @@ -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))
Expand Down
14 changes: 7 additions & 7 deletions src/parameter_estimation.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# 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)))
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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down
84 changes: 42 additions & 42 deletions src/statespacemodel.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:")
Expand All @@ -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}}
Expand All @@ -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 != "")
Expand Down Expand Up @@ -137,25 +137,25 @@ 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)
D[[1 + (i-1)*(n^2 + n + 1) for i in 1:n]] = 1
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

Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 648b529

Please sign in to comment.