Commit 02370aae authored by Fredrik Bagge Carlson's avatar Fredrik Bagge Carlson
Browse files

Updates to type defs and ar,arx methods

parent cf53f92c
......@@ -11,10 +11,12 @@ FitStatistics,
FitResult,
IdData,
# Functions
ar,arx,getARregressor,getARXregressor,find_na
ar,arx,getARregressor,getARXregressor,find_na,
toeplitz, kalman
## Fit Methods =================
:LS
:LS_reg
:LM
## Types =======================
......@@ -22,8 +24,8 @@ abstract Model
abstract LinearModel <: Model
abstract NonLinearModel <: Model
abstract Network <: NonLinearModel
typealias Polynom Union(Real, Array{Real,1})
typealias PolynomMatrix Union(Array{Polynom,1},Polynom)
typealias Polynom{T<:Real} Union(T, Array{T,1})
typealias PolynomMatrix{T<:Real} Union(Array{Polynom,1},Polynom)
"""
......@@ -32,7 +34,7 @@ typealias PolynomMatrix Union(Array{Polynom,1},Polynom)
`bias::Bool`\n
`λ::Float64`\n
"""
type AR <: Model
type AR <: LinearModel
a::Polynom
na::Int
bias::Bool
......@@ -47,7 +49,7 @@ end
`bias::Bool`\n
`λ::Float64`\n
"""
type ARX <: Model
type ARX <: LinearModel
a::Polynom
b::PolynomMatrix
na::Int
......@@ -68,14 +70,17 @@ type FitStatistics
RMS::Float64
FIT::Float64
AIC::Float64
FitStatistics(e::Vector{Float64}) = new(rms(e),fit(e),aic(e))
FitStatistics(y::Vector{Float64},yh::Vector{Float64},d) = new(rms(y-yh),fit(y,yh),aic(y-yh,d))
end
type FitResult
error::VecOrMat{Float64}
fit::FitStatistics
method::Symbol
FitResult(error::VecOrMat{Float64},method::Symbol) = new(error, FitStatistics(error),method)
function FitResult(y,yh,d::Int,method::Symbol)
error = y-yh
new(error, FitStatistics(y,yh, d),method)
end
end
type IdData
......@@ -87,7 +92,11 @@ type IdData
IdData(y::VecOrMat{Float64}, u::VecOrMat{Float64}) = new(y,u,0)
end
## Helper functions
rms(x) = sqrt(mean(x.^2))
sse(x) = sum(x.^2)
fit(y,yh) = 100 * (1-rms(y-yh)./rms(y-mean(y)));
aic(x,d) = log(sse(x)) + 2d/length(x)
include("armax.jl")
include("kalman.jl")
......
......@@ -9,7 +9,10 @@ function ar(y::Vector{Float64}, na; λ = 0, bias=false)
end
prediction = A*w
error = y_train - prediction
return AR(w,na,bias,λ), FitResult(error,:LS)
model = AR(w,na,bias,λ)
na = bias ? na+1:na
result = FitResult(y_train,prediction,na, λ>0?(:LS_reg) :(:LS))
return model, result
end
ar(iddata::IdData, na; λ = 0, doplot=false, bias=false) = ar(iddata.y, na; λ = 0, bias=bias)
......@@ -24,8 +27,10 @@ function arx(y::Vector{Float64}, u::VecOrMat{Float64}, na, nb; λ = 0, doplot=fa
end
prediction = A*w
error = y_train - prediction
return AR(w,na,bias,λ), FitResult(error,:LS)
model = AR(w,na,bias,λ)
na = bias ? na+1:na
result = FitResult(y_train,prediction,na, λ>0?(:LS_reg) :(:LS))
return model, result
end
arx(iddata::IdData, na, nb; λ = 0, bias=false) = arx(iddata.y, iddata.u, na, nb; λ = 0, bias=bias)
......
using SystemIdentification
@assert isa(1,Polynom)
@assert isa(1.0,Polynom)
@assert isa([1.0; 1.0],Polynom)
@assert isa(1.0,PolynomMatrix)
@assert isa(1,PolynomMatrix)
ar(collect(1:5.0),1,bias=true)
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