Commit 39f96b24 authored by Fredrik Bagge Carlson's avatar Fredrik Bagge Carlson
Browse files

Implemented minimization of 1-norm for ar arx

parent b2dfad97
""" Performs PCA PCA(W,doplot=false)"""
function PCA(W,doplot=false)
""" Performs PCA PCA(W)"""
function PCA(W)
W0 = mean(W,1);
W = W-repmat(W0,size(W,1),1);
(score,latent,C) = svd(W)
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
end
using Convex
using SCS
"""`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)
n_points = size(A,1)
if normtype == 2
if λ == 0
w = A\y_train
else
w = (A'A + λ*eye(size(A,2)))\A'y_train
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
error = y_train - prediction
model = AR(w,na)
......@@ -16,14 +25,21 @@ end
ar(iddata::IdData, na; λ = 0) = ar(iddata.y, na; λ = 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)
n_points = size(A,1)
if normtype == 2
if λ == 0
w = A\y_train
else
w = (A'A + λ*eye(size(A,2)))\A'y_train
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
error = y_train - prediction
......
Supports Markdown
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