📄 lard.m
字号:
% MATLAB implementation of local AR models built through the clustered data
% vectors.
%
% Based on the prediction method described in the following paper:
%
% Lehtokangas et al. (1996). "A network of autoregressive processing units
% for time series modelling", Applied Mathematics and Computation,
% 75:151-165.
%
% Authors: Guilherme A. Barreto
% Date: September 21st 2006
clear; clc; close all;
%--------------- Organize the training data ------------
load train_series_data.dat; % Load the time series to be clustered
%% Building the input vectors from an univariate time series
p=5; % Dimension of the input vector (length of the time window)
lap=p-1; % Amount of overlapping between consecutive input vectors
Dw=buffer(train_series_data,p,lap); % Build the data vectors
if lap>0,
Dw=Dw(:,p:end)'; % Eliminate the first 'p-1' vectors with zeros)
else Dw=Dw';
end
Dw=fliplr(Dw);
Dw=Dw+0.01*randn(size(Dw)); % Add some gaussian noise to the data
%-------------------------------------------------------
Mx = 10; % Number of neurons in the X-dimension
My = 1; % Number of neurons in the Y-dimension
msize = [Mx My]; % Size of 2-D SOM map
sMap = som_randinit(Dw, 'msize', msize,'lattice','rect','shape','sheet'); % Random weight initialization
% Train SOM
sMap = som_seqtrain(sMap,Dw,'radius',[8 0],'sample_order','random','neigh','gaussian','trainlen',50);
% Divide the training vectors into clusters based on learned prototypes
[V I] = som_divide(sMap,Dw);
% Build local AR models from data
WW=[];
for i=1:Mx*My,
y=V{i}(:,1); % Prediction vector
X=[ones(size(y)) V{i}(:,2:end)]; % Regression matrix
w=X\y; % Estimated coefficient vector w=inv(X'*X)*X'*y
WW=[WW; w']; % Store the coefficient vectors
end
%--------------- Organize the testing data ------------
load test_series_data.dat; % Load testing time series
Dw=buffer(test_series_data,p,lap); % Build input data vectors
if lap>0,
Dw=Dw(:,p:end)'; % Eliminate the first 'p-1' vectors containing zeros
else Dw=Dw';
end
Dw=fliplr(Dw);
Dw=Dw+0.01*randn(size(Dw)); % Add some gaussian noise to the data
Ytrue=Dw(2:end,1); % Desired output values
%------------------------------------------------------
[LEN_DATA DIM_INPUT]=size(Dw); % Data matrix size (1 input vector per row)
for t=1:LEN_DATA-1,
win = som_bmus(sMap,Dw(t,:),1); % Find the winning neuron (bmu)
Yhat(t) = dot(WW(win,:),Dw(t,:)); % Predicted value of neuron 'win'
error(t)=Ytrue(t)-Yhat(t); % Corresponding prediction error
end
% Normalized mean squared error
NMSE=var(error)/var(Ytrue)
% Plot target and predicted time series
figure; plot(Ytrue,'r-'); hold on; plot(Yhat,'b-'); hold off
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -