Commit 9bdb256e authored by Fredrik Bagge Carlson's avatar Fredrik Bagge Carlson
Browse files

Added tests for ar arx and fixed bug in ar arx LS_reg

parent 40f9982f
......@@ -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
......
......@@ -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)
......
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment