⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 神经网络源程序.txt

📁 神经网络源程序,你可以知道神经网络程序是怎样编出来的
💻 TXT
字号:
% 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -