SystemIdentification.jl 2.08 KB
Newer Older
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
1
module SystemIdentification
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
2
3
if !isdefined(:DEBUG); DEBUG = false; end
export
4
Model,LinearModel,NonLinearModel,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
5
Network,
6
Polynom,PolynomMatrix,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
7
8
9
10
AR,
ARX,
RBFARX,
FitStatistics,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
11
FitResult,
12
IdData,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
13
# Functions
14
15
ar,arx,getARregressor,getARXregressor,find_na,
toeplitz, kalman
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
16
17
18

## Fit Methods =================
:LS
19
:LS_reg
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
20
21
22
23
24
25
26
:LM

## Types =======================
abstract Model
abstract LinearModel <: Model
abstract NonLinearModel <: Model
abstract Network <: NonLinearModel
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
27
28
29
typealias Polynom{T<:Real} Array{T,1}
typealias PolynomMatrix Array{Polynom,1}

30

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
31
32
33
34
35
36
37

"""
`a::Vector{Float64}`: The polynomial coeffs A(z) (not including the first 1)\n
`na::Int`\n
`bias::Bool`\n
`λ::Float64`\n
"""
38
type AR <: LinearModel
39
    a::Polynom
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
40
41
42
43
44
45
46
47
48
49
50
51
52
    na::Int
    bias::Bool
    λ::Float64
end

"""
`a::Vector{Float64}`: The polynomial coeffs A(z) (not including the first 1)\n
`b::VecOrMat{Float64}`: The polynomial coeffs B(z)\n
`na::Int`\n
`nb::Vector{Int}`\n
`bias::Bool`\n
`λ::Float64`\n
"""
53
type ARX <: LinearModel
54
55
    a::Polynom
    b::PolynomMatrix
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    na::Int
    nb::Vector{Int}
    bias::Bool
    λ::Float64
end

type RBFARX <: Network
    na::Int
    n_centers::Int
    n_state::Int
    outputnet::Bool
    normalized::Bool
end

70
type FitStatistics
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
71
72
73
    RMS::Float64
    FIT::Float64
    AIC::Float64
74
    FitStatistics(y::Vector{Float64},yh::Vector{Float64},d) = new(rms(y-yh),fit(y,yh),aic(y-yh,d))
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
75
76
end

77
type FitResult
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
78
    error::VecOrMat{Float64}
79
    fit::FitStatistics
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
80
    method::Symbol
81
82
83
84
    function FitResult(y,yh,d::Int,method::Symbol)
        error = y-yh
        new(error, FitStatistics(y,yh, d),method)
    end
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
85
86
end

87
type IdData
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
88
89
90
    y::VecOrMat{Float64}
    u::VecOrMat{Float64}
    Ts::Float64
91
92
93
    IdData(y::VecOrMat{Float64}) = new(y,[],0)
    IdData(y::VecOrMat{Float64} ,Ts::Float64) = new(y,[],Ts)
    IdData(y::VecOrMat{Float64}, u::VecOrMat{Float64}) = new(y,u,0)
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
94
95
end

96
97
98
99
100
## 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)
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
101
102
103
104
105
106
107

include("armax.jl")
include("kalman.jl")
include("PCA.jl")
include("kernelPCA.jl")
include("toeplitz.jl")

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
108
end