SystemIdentification.jl 2.36 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
TFdata,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
8
9
10
11
AR,
ARX,
RBFARX,
FitStatistics,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
12
FitResult,
13
IdData,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
14
# Functions
15
ar,arx,getARregressor,getARXregressor,find_na,
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
16
toeplitz, kalman, kalman_smoother, forward_kalman, PCA, plotmodel
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
17
18
19

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

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

32

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

"""
`a::Vector{Float64}`: The polynomial coeffs A(z) (not including the first 1)\n
`na::Int`\n
"""
38
type AR <: LinearModel
39
    a::Polynom
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
40
41
    na::Int
end
42
43
44
45
46
47
48
function Base.show(m::AR)
    print("A(z) = 1")
    for i = 1:length(m.a)
        print(m.a[i], " + z^-$i")
    end
    print("\n")
end
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
49
50
51
52
53
54
55

"""
`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
"""
56
type ARX <: LinearModel
57
58
    a::Polynom
    b::PolynomMatrix
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
59
    na::Int
60
    nb::Polynom{Int}
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
61
62
end

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
63
type TFdata <: LinearModel
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
64
65
66
67
    P
    F
end

Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
68
69
70
71
72
73
74
75
type RBFARX <: Network
    na::Int
    n_centers::Int
    n_state::Int
    outputnet::Bool
    normalized::Bool
end

76
type FitStatistics
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
77
78
79
    RMS::Float64
    FIT::Float64
    AIC::Float64
80
    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
81
82
end

83
type FitResult
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
84
    error::VecOrMat{Float64}
85
    fit::FitStatistics
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
86
    method::Symbol
87
88
    λ::Float64
    function FitResult(y,yh,d::Int,method::Symbol, λ = 0)
89
        error = y-yh
90
        new(error, FitStatistics(y,yh, d),method, λ)
91
    end
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
92
93
end

94
type IdData
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
95
96
97
    y::VecOrMat{Float64}
    u::VecOrMat{Float64}
    Ts::Float64
98
99
100
    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
101
102
end

103
104
105
106
107
## 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
108

109
110
111
112
##
Base.show(fit::FitStatistics) = println("Fit RMS:$(fit.RMS), FIT:$(fit.FIT), AIC:$(fit.AIC)")


Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
113
114
115
include("armax.jl")
include("kalman.jl")
include("PCA.jl")
116
# include("kernelPCA.jl")
Fredrik Bagge Carlson's avatar
Fredrik Bagge Carlson committed
117
118
include("toeplitz.jl")

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