📄 larw.m
字号:
% MATLAB implementation of local AR models built through the centroids
% (weight vectors)
%
% Based on the prediction method described in the following paper:
%
% Barreto, G.A., Mota, J.C.M., Souza, L.G.M., Frota, R.A. (2004).
% "Nonstationary time series prediction using local models
% based on competitive neural networks", Lecture Notes in Computer Science
% vol. 3029, pages 1146-1155.
%
% Author: Guilherme A. Barreto
% Date: September 28th 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 = 25; % 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);
%--------------- 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)'; % get rid of 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
Ytrue=Dw(2:end,1); % Desired output values (for comparison purposes)
%------------------------------------------------------
[LEN_DATA DIM_INPUT]=size(Dw); % Data matrix size (1 input vector per row)
K=p+1; % Number of selected winners per input vector
for t=1:LEN_DATA-1,
win = som_bmus(sMap,Dw(t,:),[1:K]); % Find current K winners
y=sMap.codebook(win,1); % Prediction vector extracted from weights
X=[ones(size(y)) sMap.codebook(win,2:end)]; % Regression matrix from weights
w=X\y; % Estimated coefficient vector w=inv(X'*X)*X'*y
Yhat(t) = dot(w,Dw(t,:)); % predicted value
error(t)=Ytrue(t)-Yhat(t); % prediction error
end
% Plot target and predicted time series
figure; plot(Ytrue,'r-'); hold on; plot(Yhat,'b-'); hold off
% Normalized mean squared error
NMSE=var(error)/var(Ytrue)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -