% Listing 18.1 for FIgure 18.7 of the book 
% "Principles of System Identification: Theory and Practice"

% Arun K. Tangirala
% December 06, 2015
% Freely distributed under no warranty

%% Data 

% Load data
load liqlevel_ident

zk = iddata(yk,uk,1);
datatrain = zk(1:1500);

% Remove trends
[dataset,Tr] = detrend(datatrain,0);

%% Build OE and ARX models
mod_oe = oe(dataset,[1 1 1]);
mod_arx = arx(dataset,[5 5 1]);

%% Compare one-step ahead predictions
predone_oe = predict(mod_oe,dataset,1);
predone_arx = predict(mod_arx,dataset,1);
plot((1:250),predone_oe.y(1:250),'-',(1:250),predone_arx.y(1:250),'k--',(1:250),dataset.y(1:250),'r.');  % Or use 'compare'

%% Compute infinite-step ahead predictions on a fresh data
datatest = detrend(zk(1501:end),Tr);
compare(datatest(1:250),mod_oe,mod_arx,inf);