Neural Network Time Series Tool
训练时仅需要目标数据即可。
这种模型可以用来预测股票或债券的未来价值,基于这些经济变量,如失业率、GDP等。它也可以用于系统识别,在这些模型中,模型被开发出来代表动态系统,如化学过程、制造系统、机器人、航天汽车等。
首先选择time series App
我们可以看到里面有三种类型的神经元模式,第一种可以应用于股票预测,可以在输出中看到他们的表达形式,第一种有输入和输出共同影响输出;第二中仅有输出;第三种仅有输入影响输出。选择第一种进行讲解,其他的可以试一试。
我们可以通过使用example,进行测试;
选择好以后,选择各种参数,最后进行训练,和以前的操作一致。
最后也可以通过操作生成程序;
选择next
继续next
最后一次next
左上角simple script和advanced script,可以生成指令。
如下:
X = phInputs;
T = phTargets;
trainFcn = ‘trainlm’; % Levenberg-Marquardt
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 10;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,‘open’,trainFcn);
net.inputs{1}.processFcns = {‘removeconstantrows’,‘mapminmax’};
net.inputs{2}.processFcns = {‘removeconstantrows’,‘mapminmax’};
[x,xi,ai,t] = preparets(net,X,{},T);
net.divideFcn = ‘dividerand’; % Divide data randomly
net.divideMode = ‘value’; % Divide up every value
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
net.plotFcns = {‘plotperform’,‘plottrainstate’,‘plotresponse’, …
‘ploterrcorr’, ‘plotinerrcorr’};
% Train the Network
[net,tr] = train(net,x,t,xi,ai);
% Test the Network
y = net(x,xi,ai);
e = gsubtract(t,y);
performance = perform(net,t,y)
% Recalculate Training, Validation and Test Performance
trainTargets = gmultiply(t,tr.trainMask);
valTargets = gmultiply(t,tr.valMask);
testTargets = gmultiply(t,tr.testMask);
trainPerformance = perform(net,trainTargets,y)
valPerformance = perform(net,valTargets,y)
testPerformance = perform(net,testTargets,y)
% View the Network
view(net)
netc = closeloop(net);
netc.name = [net.name ’ - Closed Loop’];
view(netc)
[xc,xic,aic,tc] = preparets(netc,X,{},T);
yc = netc(xc,xic,aic);
closedLoopPerformance = perform(netc,tc,yc)
numTimesteps = size(x,2);
knownOutputTimesteps = 1:(numTimesteps-5);
predictOutputTimesteps = (numTimesteps-4):numTimesteps;
X1 = X(:,knownOutputTimesteps);
T1 = T(:,knownOutputTimesteps);
[x1,xio,aio] = preparets(net,X1,{},T1);
[y1,xfo,afo] = net(x1,xio,aio);
x2 = X(1,predictOutputTimesteps);
[netc,xic,aic] = closeloop(net,xfo,afo);
[y2,xfc,afc] = netc(x2,xic,aic);
multiStepPerformance = perform(net,T(1,predictOutputTimesteps),y2)
nets = removedelay(net);
nets.name = [net.name ’ - Predict One Step Ahead’];
view(nets)
[xs,xis,ais,ts] = preparets(nets,X,{},T);
ys = nets(xs,xis,ais);
stepAheadPerformance = perform(nets,ts,ys)
if (false)
genFunction(net,‘myNeuralNetworkFunction’);
y = myNeuralNetworkFunction(x,xi,ai);
end
if (false)
genFunction(net,‘myNeuralNetworkFunction’,‘MatrixOnly’,‘yes’);
x1 = cell2mat(x(1,:));
x2 = cell2mat(x(2,:));
xi1 = cell2mat(xi(1,:));
xi2 = cell2mat(xi(2,:));
y = myNeuralNetworkFunction(x1,x2,xi1,xi2);
end
if (false)
gensim(net);
end