Commit 62456129 authored by Fredrik Bagge Carlson's avatar Fredrik Bagge Carlson
Browse files

update

parent ee9a1cfb
module LTVModelsBase module LTVModelsBase
# Interface exports # Interface exports
export AbstractModel, AbstractCost, ModelAndCost, cost, export AbstractModel, AbstractCost, ModelAndCost,f,
cost_final, dc,calculate_cost,calculate_final_cost, dc,calculate_cost,calculate_final_cost,
fit_model, predict, df,costfun, covariance, LTVStateSpaceModel, fit_model, predict, df,costfun, LTVStateSpaceModel,
SimpleLTVModel, KalmanModel, GMMModel SimpleLTVModel, covariance
export rms, sse, nrmse
rms(x) = sqrt(mean(x.^2)) rms(x) = sqrt(mean(x.^2))
...@@ -46,28 +48,6 @@ end ...@@ -46,28 +48,6 @@ end
SimpleLTVModel(At,Bt,extend::Bool) = SimpleLTVModel{eltype(At)}(At,Bt,extend) SimpleLTVModel(At,Bt,extend::Bool) = SimpleLTVModel{eltype(At)}(At,Bt,extend)
mutable struct KalmanModel{T} <: LTVStateSpaceModel
At::Array{T,3}
Bt::Array{T,3}
Pt::Array{T,3}
extended::Bool
function KalmanModel{T}(At::Array{T,3},Bt::Array{T,3},Pt::Array{T,3},extend::Bool)
if extend
At = cat(3,At,At[:,:,end])
Bt = cat(3,Bt,Bt[:,:,end])
Pt = cat(3,Pt,Pt[:,:,end])
end
return new(At,Bt,Pt,extend)
end
end
KalmanModel(At,Bt,Pt,extend::Bool=false) = KalmanModel{eltype(At)}(At,Bt,Pt,extend)
mutable struct GMMModel <: AbstractModel
M
dynamics
T
end
""" """
model = fit_model(::Type{AbstractModel}, x,u)::AbstractModel model = fit_model(::Type{AbstractModel}, x,u)::AbstractModel
...@@ -172,7 +152,7 @@ function f(modelcost::ModelAndCost, x, u, i) ...@@ -172,7 +152,7 @@ function f(modelcost::ModelAndCost, x, u, i)
predict(modelcost.model, x, u, i) predict(modelcost.model, x, u, i)
end end
function constfun(modelcost::ModelAndCost, x, u) function costfun(modelcost::ModelAndCost, x, u)
calculate_cost(modelcost.cost, x, u) calculate_cost(modelcost.cost, x, u)
end end
...@@ -188,4 +168,14 @@ function df(modelcost::ModelAndCost, x, u) ...@@ -188,4 +168,14 @@ function df(modelcost::ModelAndCost, x, u)
end end
function covariance(model::AbstractModel, x, u)
xhat = similar(x)
xhat[:,1] = x[:,1]
for i = 1:size(x,2)-1
xhat[:,i+1] = model.At[:,:,i]*x[:,i] + model.Bt[:,:,i]*u[:,i]
end
return cov(x-xhat, 2)
end
end # module end # module
Markdown is supported
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