Skip to content
Snippets Groups Projects
Commit 39f96b24 authored by Fredrik Bagge Carlson's avatar Fredrik Bagge Carlson
Browse files

Implemented minimization of 1-norm for ar arx

parent b2dfad97
No related branches found
No related tags found
No related merge requests found
""" Performs PCA PCA(W,doplot=false)""" """ Performs PCA PCA(W)"""
function PCA(W,doplot=false) function PCA(W)
W0 = mean(W,1); W0 = mean(W,1);
W = W-repmat(W0,size(W,1),1); W = W-repmat(W0,size(W,1),1);
(score,latent,C) = svd(W) (score,latent,C) = svd(W)
score = score*diagm(latent) score = score*diagm(latent)
if doplot
newplot(cumsum(latent)./sum(latent),"o")
title("Fraction of variance explained")
ylim([0,1])
end
C,score,latent,W0 C,score,latent,W0
end end
using Convex
using SCS
"""`ar(y, na; λ = 0)`""" """`ar(y, na; λ = 0)`"""
function ar(y::Vector{Float64}, na; λ = 0) function ar(y::Vector{Float64}, na; λ = 0, normtype=2, verbose=false)
y_train, A = getARregressor(y,na) y_train, A = getARregressor(y,na)
n_points = size(A,1) n_points = size(A,1)
if normtype == 2
if λ == 0 if λ == 0
w = A\y_train w = A\y_train
else else
w = (A'A + λ*eye(size(A,2)))\A'y_train w = (A'A + λ*eye(size(A,2)))\A'y_train
end end
elseif normtype == 1
w = Variable(size(A,2),1)
problem = minimize(sum(abs(A*w-y_train )) + λ*norm(w))
solve!(problem, SCSSolver(verbose=Int(verbose)))
w = w.value[:]
end
prediction = A*w prediction = A*w
error = y_train - prediction error = y_train - prediction
model = AR(w,na) model = AR(w,na)
...@@ -16,14 +25,21 @@ end ...@@ -16,14 +25,21 @@ end
ar(iddata::IdData, na; λ = 0) = ar(iddata.y, na; λ = 0) ar(iddata::IdData, na; λ = 0) = ar(iddata.y, na; λ = 0)
"""arx(y, u, na, nb; λ = 0)""" """arx(y, u, na, nb; λ = 0)"""
function arx(y::Vector{Float64}, u::VecOrMat{Float64}, na, nb; λ = 0) function arx(y::Vector{Float64}, u::VecOrMat{Float64}, na, nb; λ = 0, normtype=2, verbose=false)
y_train, A = getARXregressor(y,u, na, nb) y_train, A = getARXregressor(y,u, na, nb)
n_points = size(A,1) n_points = size(A,1)
if normtype == 2
if λ == 0 if λ == 0
w = A\y_train w = A\y_train
else else
w = (A'A + λ*eye(size(A,2)))\A'y_train w = (A'A + λ*eye(size(A,2)))\A'y_train
end end
elseif normtype == 1
w = Variable(size(A,2),1)
problem = minimize(sum(abs(A*w-y_train )) + λ*norm(w))
solve!(problem, SCSSolver(verbose=Int(verbose)))
w = w.value[:]
end
prediction = A*w prediction = A*w
error = y_train - prediction error = y_train - prediction
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment