diff --git a/src/SystemIdentification.jl b/src/SystemIdentification.jl index 8d47ab1f3785127c68cf2bdcd2aa70e22d0aaee8..0374b498313f580126f379be9c24c8fada9d281e 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 cb23ca164c2a4a9a3021af8e73d82bf601b35613..36477a72dd6ae235ee962e1582b9aab9ebe8ab4f 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 34f31026f55b2428e8ae5aec0e294248732bf7dd..323ed664abf2cfbe7fb9fdfeb3a7991f813efe9d 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