Skip to main content
Homepage
Explore
Search or go to…
/
Register
Sign in
Explore
Primary navigation
Project
SystemIdentification
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Redmine
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Collapse sidebar
Snippets
Groups
Projects
Show more breadcrumbs
Fredrik Bagge Carlson
SystemIdentification
Commits
40f9982f
Commit
40f9982f
authored
Sep 5, 2015
by
Fredrik Bagge Carlson
Browse files
Options
Downloads
Patches
Plain Diff
Updated types and removed bias from ar arx
parent
c7a5d448
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/SystemIdentification.jl
+3
-7
3 additions, 7 deletions
src/SystemIdentification.jl
src/armax.jl
+18
-25
18 additions, 25 deletions
src/armax.jl
test/tests.jl
+4
-4
4 additions, 4 deletions
test/tests.jl
with
25 additions
and
36 deletions
src/SystemIdentification.jl
+
3
−
7
View file @
40f9982f
...
...
@@ -24,21 +24,19 @@ abstract Model
abstract
LinearModel
<:
Model
abstract
NonLinearModel
<:
Model
abstract
Network
<:
NonLinearModel
typealias
Polynom
{
T
<:
Real
}
Array
{
T
,
1
}
typealias
PolynomMatrix
Array
{
Polynom
,
1
}
typealias
Polynom
{
T
<:
Real
}
Union
(
Array
{
T
,
1
}
,
T
)
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
`bias::Bool`
\n
`λ::Float64`
\n
"""
type
AR
<:
LinearModel
a
::
Polynom
na
::
Int
bias
::
Bool
λ
::
Float64
end
...
...
@@ -47,15 +45,13 @@ end
`b::VecOrMat{Float64}`: The polynomial coeffs B(z)
\n
`na::Int`
\n
`nb::Vector{Int}`
\n
`bias::Bool`
\n
`λ::Float64`
\n
"""
type
ARX
<:
LinearModel
a
::
Polynom
b
::
PolynomMatrix
na
::
Int
nb
::
Vector
{
Int
}
bias
::
Bool
nb
::
Polynom
{
Int
}
λ
::
Float64
end
...
...
...
...
This diff is collapsed.
Click to expand it.
src/armax.jl
+
18
−
25
View file @
40f9982f
"""`ar(y, na; λ = 0, doplot=false
, bias=false
)`"""
function
ar
(
y
::
Vector
{
Float64
},
na
;
λ
=
0
,
bias
=
false
)
y_train
,
A
=
getARregressor
(
y
,
na
;
bias
=
bias
)
"""`ar(y, na; λ = 0, doplot=false)`"""
function
ar
(
y
::
Vector
{
Float64
},
na
;
λ
=
0
)
y_train
,
A
=
getARregressor
(
y
,
na
)
n_points
=
size
(
A
,
1
)
if
λ
==
0
w
=
A
\
y_train
...
...
@@ -9,16 +9,15 @@ function ar(y::Vector{Float64}, na; λ = 0, bias=false)
end
prediction
=
A
*
w
error
=
y_train
-
prediction
model
=
AR
(
w
,
na
,
bias
,
λ
)
na
=
bias
?
na
+
1
:
na
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
,
bias
=
false
)
=
ar
(
iddata
.
y
,
na
;
λ
=
0
,
bias
=
bias
)
ar
(
iddata
::
IdData
,
na
;
λ
=
0
,
doplot
=
false
)
=
ar
(
iddata
.
y
,
na
;
λ
=
0
)
"""arx(y, u, na, nb; λ = 0, doplot=false
, bias=false
)"""
function
arx
(
y
::
Vector
{
Float64
},
u
::
VecOrMat
{
Float64
},
na
,
nb
;
λ
=
0
,
doplot
=
false
,
bias
=
false
)
y_train
,
A
=
getARXregressor
(
y
,
u
,
na
,
nb
;
bias
=
bias
)
"""arx(y, u, na, nb; λ = 0, doplot=false)"""
function
arx
(
y
::
Vector
{
Float64
},
u
::
VecOrMat
{
Float64
},
na
,
nb
;
λ
=
0
,
doplot
=
false
)
y_train
,
A
=
getARXregressor
(
y
,
u
,
na
,
nb
)
n_points
=
size
(
A
,
1
)
if
λ
==
0
w
=
A
\
y_train
...
...
@@ -30,33 +29,29 @@ function arx(y::Vector{Float64}, u::VecOrMat{Float64}, na, nb; λ = 0, doplot=fa
si
=
na
+
1
b
=
Polynom
[
Polynom
(
w
[
si
:
si
+
nb
[
1
]
-
1
]
)
]
b
=
Polynom
[
w
[
si
:
si
+
nb
[
1
]
-
1
]]
si
+=
nb
[
1
]
for
i
=
2
:
length
(
nb
)
push!
(
b
,
Polynom
(
w
[
si
:
si
+
nb
[
i
]
-
1
])
)
push!
(
b
,
w
[
si
:
si
+
nb
[
i
]
-
1
])
si
+=
nb
[
i
]
end
model
=
ARX
(
w
,
na
,
nb
,
bias
,
λ
)
na
=
bias
?
na
+
1
:
na
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
,
bias
=
false
)
=
arx
(
iddata
.
y
,
iddata
.
u
,
na
,
nb
;
λ
=
0
,
bias
=
bias
)
arx
(
iddata
::
IdData
,
na
,
nb
;
λ
=
0
)
=
arx
(
iddata
.
y
,
iddata
.
u
,
na
,
nb
;
λ
=
0
)
function
getARregressor
(
y
::
Vector
{
Float64
},
na
;
bias
=
false
)
function
getARregressor
(
y
::
Vector
{
Float64
},
na
)
A
=
toeplitz
(
y
[
na
+
1
:
end
],
y
[
na
+
1
:-
1
:
1
])
y
=
copy
(
A
[
:
,
1
])
A
=
A
[
:
,
2
:
end
]
n_points
=
size
(
A
,
1
)
if
bias
A
=
[
A
ones
(
n_points
)]
end
return
y
,
A
end
getARregressor
(
iddata
::
IdData
,
na
,
bias
=
false
)
=
getARXregressor
(
iddata
.
y
,
na
,
bias
=
bias
)
getARregressor
(
iddata
::
IdData
,
na
)
=
getARXregressor
(
iddata
.
y
,
na
)
function
getARXregressor
(
y
::
Vector
{
Float64
},
u
::
VecOrMat
{
Float64
},
na
,
nb
;
bias
=
false
)
function
getARXregressor
(
y
::
Vector
{
Float64
},
u
::
VecOrMat
{
Float64
},
na
,
nb
)
assert
(
length
(
nb
)
==
size
(
u
,
2
))
m
=
max
(
na
+
1
,
maximum
(
nb
))
n
=
length
(
y
)
-
m
+
1
...
...
@@ -70,12 +65,10 @@ function getARXregressor(y::Vector{Float64},u::VecOrMat{Float64}, na, nb; bias=f
A
=
[
A
toeplitz
(
u
[
nb
[
i
]
+
offs
:
n
+
nb
[
i
]
+
offs
-
1
,
i
],
u
[
nb
[
i
]
+
offs
:-
1
:
1
+
offs
,
i
])]
end
if
bias
A
=
[
A
ones
(
n
)]
end
return
y
,
A
end
getARXregressor
(
iddata
::
IdData
,
na
,
nb
;
bias
=
false
)
=
getARXregressor
(
iddata
.
y
,
iddata
.
u
,
na
,
nb
,
bias
=
bias
)
getARXregressor
(
iddata
::
IdData
,
na
,
nb
)
=
getARXregressor
(
iddata
.
y
,
iddata
.
u
,
na
,
nb
)
"""Plots the RMSE and AIC for model orders up to `n`. Useful for model selection"""
...
...
@@ -84,7 +77,7 @@ function find_na(y::Vector{Float64},n::Int)
for
i
=
1
:
n
w
,
e
=
ar
(
y
,
i
)
error
[
i
,
1
]
=
rms
(
e
)
error
[
i
,
2
]
=
aic
(
e
,
i
+
1
)
error
[
i
,
2
]
=
aic
(
e
,
i
)
print
(
i
,
", "
)
end
println
(
"Done"
)
...
...
...
...
This diff is collapsed.
Click to expand it.
test/tests.jl
+
4
−
4
View file @
40f9982f
...
...
@@ -51,24 +51,24 @@ function run_tests()
end
@test
"AR "
begin
model
,
result
=
ar
(
collect
(
1
:
5.0
),
1
,
bias
=
true
)
model
,
result
=
ar
(
collect
(
1
:
5.0
),
2
)
fit
=
result
.
fit
@tassert
isa
(
model
,
AR
)
@tassert
isa
(
result
,
FitResult
)
@tassert
isa
(
fit
,
FitStatistics
)
@tassert
model
.
a
≈
[
1
;
1
]
@tassert
model
.
a
≈
[
2
;
-
1
]
@tassert
fit
.
FIT
≈
100.0
@tassert
isapprox
(
fit
.
RMS
,
0.0
,
atol
=
1e-10
)
@tassert
result
.
method
==
:
LS
end
@test
"ARX "
begin
model
,
result
=
arx
(
collect
(
1
:
5.0
),
collect
(
1
:
5.0
),
1
,
1
,
bias
=
true
)
model
,
result
=
arx
(
collect
(
1
:
5.0
),
collect
(
1
:
5.0
),
1
,
1
)
fit
=
result
.
fit
@tassert
isa
(
model
,
ARX
)
@tassert
isa
(
result
,
FitResult
)
@tassert
isa
(
fit
,
FitStatistics
)
@tassert
model
.
a
≈
[
1
;
1
]
@tassert
model
.
a
≈
[
2
;
-
1
]
@tassert
fit
.
FIT
≈
100.0
@tassert
isapprox
(
fit
.
RMS
,
0.0
,
atol
=
1e-10
)
@tassert
result
.
method
==
:
LS
...
...
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment