From 9bdb256e3c826274e64ae25b01bd52ee817d4b64 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlsson <cont-frb@ulund.org> Date: Sat, 5 Sep 2015 14:23:20 +0200 Subject: [PATCH] Added tests for ar arx and fixed bug in ar arx LS_reg --- src/SystemIdentification.jl | 9 +++------ src/armax.jl | 12 ++++++------ test/tests.jl | 13 ++++++++++++- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/SystemIdentification.jl b/src/SystemIdentification.jl index 8d47ab1..0374b49 100644 --- a/src/SystemIdentification.jl +++ b/src/SystemIdentification.jl @@ -32,12 +32,10 @@ typealias PolynomMatrix{T} Union(Array{Polynom{T},1},Polynom{T}, T) """ `a::Vector{Float64}`: The polynomial coeffs A(z) (not including the first 1)\n `na::Int`\n -`λ::Float64`\n """ type AR <: LinearModel a::Polynom na::Int - λ::Float64 end """ @@ -45,14 +43,12 @@ end `b::VecOrMat{Float64}`: The polynomial coeffs B(z)\n `na::Int`\n `nb::Vector{Int}`\n -`λ::Float64`\n """ type ARX <: LinearModel a::Polynom b::PolynomMatrix na::Int nb::Polynom{Int} - λ::Float64 end type RBFARX <: Network @@ -74,9 +70,10 @@ type FitResult error::VecOrMat{Float64} fit::FitStatistics method::Symbol - function FitResult(y,yh,d::Int,method::Symbol) + λ::Float64 + function FitResult(y,yh,d::Int,method::Symbol, λ = 0) error = y-yh - new(error, FitStatistics(y,yh, d),method) + new(error, FitStatistics(y,yh, d),method, λ) end end diff --git a/src/armax.jl b/src/armax.jl index cb23ca1..36477a7 100644 --- a/src/armax.jl +++ b/src/armax.jl @@ -5,12 +5,12 @@ function ar(y::Vector{Float64}, na; λ = 0) if λ == 0 w = A\y_train else - w = (A'A + λeye(size(A,2)))\A'y_train + w = (A'A + λ*eye(size(A,2)))\A'y_train end prediction = A*w error = y_train - prediction - model = AR(w,na,λ) - result = FitResult(y_train,prediction,na, λ>0?(:LS_reg) :(:LS)) + model = AR(w,na) + result = FitResult(y_train,prediction,na, λ>0?(:LS_reg) :(:LS),λ) return model, result end ar(iddata::IdData, na; λ = 0, doplot=false) = ar(iddata.y, na; λ = 0) @@ -22,7 +22,7 @@ function arx(y::Vector{Float64}, u::VecOrMat{Float64}, na, nb; λ = 0, doplot=fa if λ == 0 w = A\y_train else - w = (A'A + λeye(size(A,2)))\A'y_train + w = (A'A + λ*eye(size(A,2)))\A'y_train end prediction = A*w error = y_train - prediction @@ -35,8 +35,8 @@ function arx(y::Vector{Float64}, u::VecOrMat{Float64}, na, nb; λ = 0, doplot=fa push!(b,w[si:si+nb[i]-1]) si += nb[i] end - model = ARX(w[1:na],b,na,nb,λ) - result = FitResult(y_train,prediction,na, λ>0?(:LS_reg) :(:LS)) + model = ARX(w[1:na],b,na,nb) + result = FitResult(y_train,prediction,na, λ>0?(:LS_reg) :(:LS), λ) return model, result end arx(iddata::IdData, na, nb; λ = 0) = arx(iddata.y, iddata.u, na, nb; λ = 0) diff --git a/test/tests.jl b/test/tests.jl index 34f3102..323ed66 100644 --- a/test/tests.jl +++ b/test/tests.jl @@ -60,6 +60,12 @@ function run_tests() @tassert fit.FIT ≈ 100.0 @tassert isapprox(fit.RMS, 0.0, atol=1e-10) @tassert result.method == :LS + @tassert model.na == 2 + @tassert result.λ == 0 + model, result = ar(collect(1:5.0),2,λ = 1) + @tassert result.method == :LS_reg + @tassert result.λ == 1 + end @test "ARX " begin @@ -68,10 +74,15 @@ function run_tests() @tassert isa(model,ARX) @tassert isa(result,FitResult) @tassert isa(fit,FitStatistics) - @tassert model.a ≈ [2;-1] @tassert fit.FIT ≈ 100.0 @tassert isapprox(fit.RMS, 0.0, atol=1e-10) @tassert result.method == :LS + @tassert model.nb == 1 + @tassert model.na == 1 + @tassert result.λ == 0 + model, result = arx(collect(1:5.0),collect(1:5.0),1,1,λ = 1) + @tassert result.method == :LS_reg + @tassert result.λ == 1 end end -- GitLab