声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1648|回复: 3

[共享资源] bp网络源代码

[复制链接]
发表于 2007-4-24 15:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
% 一个非常优秀的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 = [1; 1; 1];
net.inputConnect = [1; 1; 0];
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 = [0 0 1];
net.targetConnect = [0 0 1];
% 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 = [0 1 2 3 5 7 10 15 20 25 30 40 50 60 80]; %from input 1 to layer 1
net.inputWeights{2,1}.delays = [0 1 2 7 10 30]; %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',[7 inf]);
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;
[kPer, kPer_avg] = stochastics(prdLength1, prdLength2, prdLength3, closep, highp, lowp);
prdLength1=5;
prdLength2=10;
prdLength2=3;
[macd, signal] = 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 = [P2 P3 P4 P5 P6];
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
[net,a,e] = 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 = [P2 P3 P4 P5 P6];
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
回复
分享到:

使用道具 举报

 楼主| 发表于 2007-4-24 15:17 | 显示全部楼层
有没高手解答一下数据读入格式?
发表于 2007-4-25 00:50 | 显示全部楼层
帮你顶一下
发表于 2007-4-25 09:12 | 显示全部楼层
help fullfile.
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-12-30 01:35 , Processed in 0.061154 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表