bp网络源代码
% 一个非常优秀的BP神经网络源程序,好不容易找到的% Construct the network object
net = network;
% Specify the input and layer size
net.numInputs = 1;
net.numLayers = 3;
% Set connections among all parts
net.biasConnect = ;
net.inputConnect = ;
net.layerConnect = [0 0 0; %(1, j): connect to layer 1 from layer j
1 0 0; %(2, j): connect to layer 2 from layer j
0 1 0]; %(3, j): connect to layer 3 from layer j
net.outputConnect = ;
net.targetConnect = ;
% Set input range
net.inputs{1}.range = [-2 2; -1 1; -2 2; -1 1; -1 1];
% Set neural number for layer 1
net.layers{1}.size = 4; %8, 10, 15
% Set transfer functions for layer 1
net.layers{1}.transferFcn = 'purelin';
% Set initial function for layer 2
net.layers{1}.initFcn = 'initnw';
% Set neural number for layer 2
net.layers{2}.size = 3; %4, 10, 15
% Set transfer functions for layer 2
net.layers{2}.transferFcn = 'tansig';
% Set initial function for layer 2
net.layers{2}.initFcn = 'initnw';
% Set neural number for layer 3
net.layers{3}.size = 1;
% Set transfer functions for layer 3
net.layers{3}.transferFcn = 'purelin';
% Set initial function for layer 3
net.layers{3}.initFcn = 'initnw';
% Set input weight delays for layer 1
net.inputWeights{1,1}.delays = ; %from input 1 to layer 1
net.inputWeights{2,1}.delays = ; %from input 1 to layer 2
net.initFcn = 'initlay';
net.performFcn = 'mse';
% Choose the train method
net.adaptFcn = 'trainlm';
% Initialize the optimized parameters in net works
net = init(net);
% Set data file: Loading orignal data
fNameIn1=testFileName;
fileName1 = fullfile('C:', 'stockeys', 'stockDatabase', 'NYStock60', fNameIn1{ii});
fid = fopen(fileName1);
data = fscanf(fid,'%d, %d, %g, %g, %g, %g, %g',);
data=data';
fclose(fid);
date=data(:,1);
time=data(:,2);
openp=data(:,3);
highp=data(:,4);
lowp=data(:,5);
closep=data(:,6);
volume=data(:,7);
endLine_wholwFile=size(time);
file_Length=endLine_wholwFile;
% Set input data and target data
startLine0 = 200;
Nprd = 14;
startLine = startLine0 + Nprd;
trainLength = 300;
endLine = startLine + trainLength;
priceRate=rateClosePrice(closep);
prdLength1=4;
prdLength2=2;
prdLength3=3;
= stochastics(prdLength1, prdLength2, prdLength3, closep, highp, lowp);
prdLength1=5;
prdLength2=10;
prdLength2=3;
= macDivergence(prdLength1, prdLength2, prdLength3, closep);
prdLength=6;
dpo = DetrendedPriceOscillator(prdLength, closep);
prdLength1=5;
prdLength2=3;
cmf = CMFlow(prdLength1, prdLength2, closep, highp, lowp, volume);
prdLength1=5;
prdLength2=3;
RPer = WilliamsRPer(prdLength1, prdLength2, closep, highp, lowp);
P2 = priceRate(startLine:endLine-1);
P3 = kPer(startLine:endLine-1);
P4 = macd(startLine:endLine-1);
P5 = dpo(startLine:endLine-1);
P6 = cmf(startLine:endLine-1);
P2=P2';
P3=P3';
P4=P4';
P5=P5';
P6=P6';
Pi2 = ;
P = con2seq(Pi2');
% Set price change rate to be target
T = priceRate(startLine+1:endLine);
T = con2seq(T);
% Set neural network parameters
net.adaptParam.show = 50;
net.adaptParam.goal = 1.e-12;
net.adaptParam.epochs = 150;
lr=0.00319;
% Call adapt(...) to train the network
= adapt(net,P,T);
% Set initial data for simulate other stock data
predictLength = 15;
endLinePr = endLine + predictLength;
% Input data
P2 = priceRate(startLine:endLinePr-1);
P3 = kPer(startLine:endLinePr-1);
P4 = macd(startLine:endLinePr-1);
P5 = dpo(startLine:endLinePr-1);
P6 = cmf(startLine:endLinePr-1);
P2=P2';
P3=P3';
P4=P4';
P5=P5';
P6=P6';
Pi2 = ;
P = con2seq(Pi2');
% Target data
Target = priceRate(startLine+1:endLinePr);
Target=Target';
% Simulate the results
Y = sim(net,P);
Y = Y'; %Y or Y' size is endLinePr-startLine. From 1 to endLinePr-startLine
%simulateRate;
% Calculate sign between original price rate and predicted price rate
% If sign is +, correct; if sign is -, wrong; if sign is 0, and both original
% price rate and predicted price rate are zeros, correct; otherwise, wrong.
for i = 1:endLinePr-startLine
i0(i) = i;
Predict_Solution(i) = Y{i};
Input(i) = P2(i);
end
for i = 1:endLinePr-startLine
signD(i) = Predict_Solution(i) * Target(i);
error(i) = Predict_Solution(i) - Target(i);
end
squaredError = 0;
for i = 1:endLine-startLine
squaredError = squaredError + error(i)*error(i);
end
MSEt=sqrt(squaredError/(endLine-startLine+1));
%MSEt
% Calculate the correct rate of the whole learning points
iCount = 0;
for i = 1:endLine-startLine
if signD(i) > 0
iCount = iCount + 1;
signD(i) = 1;
elseif signD(i) == 0
if (Predict_Solution(i) == 0) & (Target(i) == 0)
iCount = iCount + 1;
signD(i) = 1;
end
else
signD(i) = -1;
end
end
LearningCorrectRateTotal = iCount/(endLine-startLine);
% Calculate the correct rate of the learning of last 100 points
iCount = 0;
for i = endLine-startLine-99:endLine-startLine
if signD(i) > 0
iCount = iCount + 1;
signD(i) = 1;
elseif signD(i) == 0
if (Predict_Solution(i) == 0) & (Target(i) == 0)
iCount = iCount + 1;
signD(i) = 1;
end
else
signD(i) = -1;
end
end
LearningCorrectRateL100 = iCount/100;
% Calculate the correct rate of the learning of last 30 points
iCount = 0;
for i = endLine-startLine-29:endLine-startLine
if signD(i) > 0
iCount = iCount + 1;
signD(i) = 1;
elseif signD(i) == 0
if (Predict_Solution(i) == 0) & (Target(i) == 0)
iCount = iCount + 1;
signD(i) = 1;
end
else
signD(i) = -1;
end
end
LearningCorrectRateL30 = iCount/30;
% Calculate the correct rate of the learning of last 10 points
iCount = 0;
for i = endLine-startLine-9:endLine-startLine
if signD(i) > 0
iCount = iCount + 1;
signD(i) = 1;
elseif signD(i) == 0
if (Predict_Solution(i) == 0) & (Target(i) == 0)
iCount = iCount + 1;
signD(i) = 1;
end
else
signD(i) = -1;
end
end
LearningCorrectRateL10 = iCount/10;
% Calculate the correct rate of the prediction for 15 points
iCount = 0;
for i = endLine-startLine+1:endLine-startLine+15
if signD(i) > 0
iCount = iCount + 1;
signD(i) = 1;
elseif signD(i) == 0
if (Predict_Solution(i) == 0) & (Target(i) == 0)
iCount = iCount + 1;
signD(i) = 1;
end
else
signD(i) = -1;
end
end
PredictCorrectRate15 = iCount/15;
LearningCorrectRateTotal
LearningCorrectRateL100
LearningCorrectRateL30
LearningCorrectRateL10
PredictCorrectRate15
% Record file including input, target, output and error
out=[i0(1:endLinePr-startLine)', Input(1:endLinePr-startLine)', ...
Predict_Solution(1:endLinePr-startLine)', Target(1:endLinePr-startLine), signD(1:endLinePr-startLine)'];
switch(calc_Time)
case 1
fileName2=fullfile('C:', 'stockeys', 'intraday_1step', 'temp', 'temp_file1');
fid=fopen(fileName2,'w');
fprintf(fid,'%12.6f %12.6f %12.6f %12.6f %12.6f \n', out');
fclose(fid);
case 2
fileName2=fullfile('C:', 'stockeys', 'intraday_1step', 'temp', 'temp_file2');
fid=fopen(fileName2,'w');
fprintf(fid,'%12.6f %12.6f %12.6f %12.6f %12.6f \n', out');
fclose(fid);
case 3
fileName2=fullfile('C:', 'stockeys', 'intraday_1step', 'temp', 'temp_file3');
fid=fopen(fileName2,'w');
fprintf(fid,'%12.6f %12.6f %12.6f %12.6f %12.6f \n', out');
fclose(fid);
case 4
fileName2=fullfile('C:', 'stockeys', 'intraday_1step', 'temp', 'temp_file4');
fid=fopen(fileName2,'w');
fprintf(fid,'%12.6f %12.6f %12.6f %12.6f %12.6f \n', out');
fclose(fid);
case 5
fileName2=fullfile('C:', 'stockeys', 'intraday_1step', 'temp', 'temp_file5');
fid=fopen(fileName2,'w');
fprintf(fid,'%12.6f %12.6f %12.6f %12.6f %12.6f \n', out');
fclose(fid);
end 有没高手解答一下数据读入格式? 帮你顶一下 help fullfile.
页:
[1]