%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Convolution neural network
%%%%% for time series prediction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
clf;
close all hidden;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% MATLAB %%%%%%%%%%%%%%%%%%%%%%%%%%%%
MATLAB=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CONV=0; %%%%% Convolution Network 0 1 2 (0: all)
LinearPred=1; %%%%% Initialize by linear prediction 0 1
RIDGE=0; %%%%% Ridge 0 1
LASSO=0; %%%%% Lasso 0 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% Training Cycles %%%%%%%%%%%%%%%%
CYCLE=2000;
MODCYC=10;
Err0=zeros(1,CYCLE/MODCYC);
Err1=zeros(1,CYCLE/MODCYC);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%% DATA READING %%%%%%%%%%%%%%
AA=dlmread('hakusai.txt');
%%% Hakusai.txt は
%%%「政府統計の総合窓口」のデータを使用しています。
%%% http://www.e-stat.go.jp/SG1/estat/eStatTopPortal.do
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[T1, T2]=size(AA);
AAA=AA(:,2);
meanval=mean(AAA);
maxval=max(abs(AAA-meanval));
AAA=0.49*(AAA-meanval)/maxval+0.5;
ZZZ=27;
alldata=T1-(ZZZ+1);
ntrain=floor(alldata/2);
ntest=alldata-ntrain;
for i=1:1:alldata
ydata(1,i)=AAA(i+ZZZ);
xdata(:,i)=AAA(i:1:(i+ZZZ-1));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% RIDGE, LASSO, Network %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
HYPERPARAMETER1=0.000004; %%%%% RIDGE Hyperparameter %%%%%%%%%%
HYPERPARAMETER2=0.000002; %%%%% LASSO Hyperparameter %%%%%%%%%%
if(RIDGE==1)
fff=@(a)(HYPERPARAMETER1*a);
elseif(LASSO==1)
fff=@(a)(HYPERPARAMETER2*sign(a));
else
fff=@(a)(0);
end
%%%%%%%%%%%%%%%%% Neural Network Architecture %%%
%%%%% M=H3 -> H2 -> H1 -> H0=N
N=1;
H0=1;
H1=3;
H2=9;
H3=ZZZ;
%%%%%%%%%%%%%%%%%%%%%%%% Training Paramaters %%%%%%%%%%%%%%%%%
ETA0=0.5;
ALPHA=0.1;
EPSILON=0.01;
%%%%%%%%%%%%%%%%%%%%%%%% Neural Network Calculation %%%%%%%%%%
sig=@(t)(1./(1+exp(-t)));
out=@(w,t,h)(sig(w*h+t));
%%%%%%%%%%%%%%%%%%%% input, hidden, output %%%%%%%%%%%%%%
h0=zeros(H0,1);
h1=zeros(H1,1);
h2=zeros(H2,1);
%%%%%%%%%%%%%%%%%%%%%% Training Initialization %%%%%%%%
w0=0.1*randn(H0,H1);
w1=0.1*randn(H1,H2);
w2=0.1*randn(H2,H3);
th0=0.01*randn(H0,1);
th1=0.01*randn(H1,1);
th2=0.01*randn(H2,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% Initial weight is determined by linear prediction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(LinearPred==1)
xtr=xdata(:,1:1:ntrain);
ytr=ydata(1,1:1:ntrain);
xtest=xdata(:,(ntrain+1):1:alldata);
ytest=ydata(1,(ntrain+1):1:alldata);
syx=xtr*ytr';
sxx=xtr*xtr';
wlinear=inv(sxx)*syx;
for i=1:1:H2
for j=1:1:H3
if(floor((j+2)/3)==i)
w2(i,j)=tanh(5*wlinear(j,1));
end
end
end
Tlinear=sum((ytr-wlinear'*xtr).^2);
Glinear=sum((ytest-wlinear'*xtest).^2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Convolution network
%%% CONV==0 --> all weights are used. Not convolution
%%% CONV==1 --> Convoltion (-1,0,1)
%%% CONV==2 --> Convolution (-2,-1,0,1,2)
if(CONV==0)
wmask1=ones(H1,H2);
wmask2=ones(H2,H3);
else
wmask1=zeros(H1,H2);
wmask2=zeros(H2,H3);
for i=1:1:H1
for j=1:1:H2
if(abs(3*i-j-1)0)
if(w0(1,j)>0)
plot(px,py,'r-','Linewidth',bold);
else
plot(px,py,'b-','Linewidth',bold);
end
end
end
prp(1)=plot(px(1),Y0,'ko');
py(1)=Y1;
py(2)=Y2;
for i=1:1:H1
px(1)=H2*(i-0.5);
for j=1:1:H2
px(2)=H1*(j-0.5);
bold=floor(abs(w1(i,j))+0.9);
if(bold>0)
if(w1(i,j)>0)
plot(px,py,'r-','Linewidth',bold);
else
plot(px,py,'b-','Linewidth',bold);
end
end
end
prp(1+i)=plot(px(1),2,'ko');
end
py(1)=Y2;
py(2)=Y3;
for i=1:1:H2
px(1)=H1*(i-0.5);
for j=1:1:H3
px(2)=(j-0.5);
bold=floor(abs(w2(i,j))+0.9);
if(bold>0)
if(w2(i,j)>0)
plot(px,py,'r-','Linewidth',bold);
else
plot(px,py,'b-','Linewidth',bold);
end
end
end
prp(1+H1+i)=plot(px(1),Y2,'ko');
end
for i=1:1:H3
px(1)=(i-0.5);
prp(1+H1+H2+i)=plot(px(1),Y3,'ko');
end
axis off;
set(prp,'Color', [0,0,0], 'MarkerFaceColor', [1,1,1],'MarkerSize',8, 'LineWidth', 2);
title('Trained Network. Input: 27 months ago ---> Last month');
hold off;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%