第一篇:用小波神经网络来对时间序列进行预测
/* Note:Your choice is C IDE */ #include “stdio.h” void main(){
}/*用小波神经网络来对时间序列进行预测 */ /*%File name : nprogram.m %Description : This file reads the data from %its source into their respective matrices prior to % performing wavelet decomposition.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Clear command screen and variables */ clc;clear;
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % user desired resolution level(Tested: resolution = 2 is best)*/ level = menu('Enter desired resolution level: ', '1',...'2(Select this for testing)', '3', '4');switch level case 1, resolution = 1;case 2, resolution = 2;case 3, resolution = 3;case 4, resolution = 4;end
msg = ['Resolution level to be used is ', num2str(resolution)];disp(msg);
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % user desired amount of data to use */ data = menu('Choose amount of data to use: ', '1 day', '2 days', '3 days', '4 days',...'5 days', '6 days', '1 week(Select this for testing)');switch data case 1, dataPoints = 48;/*%1 day = 48 points */ case 2, dataPoints = 96;/* %2 days = 96 points */ case 3, dataPoints = 144;/*%3 days = 144 points */
case 4, dataPoints = 192;/*%4 days = 192 points */ case 5, dataPoints = 240;/* %5 days = 240 points */ case 6, dataPoints = 288;/* %6 days = 288 points */ case 7, dataPoints = 336;/*%1 weeks = 336 points */
end
msg = ['No.of data points to be used is ', num2str(dataPoints)];disp(msg);
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Menu for data set selection */ select = menu('Use QLD data of: ', 'Jan02',...'Feb02', 'Mar02(Select this for testing)', 'Apr02', 'May02');switch select case 1, demandFile = 'DATA200601_QLD1';
case 2, demandFile = 'DATA200602_QLD1';
case 3, demandFile = 'DATA200603_QLD1';
case 4, demandFile = 'DATA200604_QLD1';
case 5, demandFile = 'DATA200605_QLD1';end
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Reading the historical DEMAND data into tDemandArray */ selectedDemandFile=[demandFile,'.csv'];[regionArray, sDateArray, tDemandArray, rrpArray, pTypeArray]...= textread(selectedDemandFile, '%s %q %f %f %s', 'headerlines', 1, 'delimiter', ',');
/*%Display no.of points in the selected time series demand data */ [demandDataPoints, y] = size(tDemandArray);msg = ['The no.of points in the selected Demand data is ', num2str(demandDataPoints)];disp(msg);
/*%Decompose historical demand data signal */ [dD, l] = swtmat(tDemandArray, resolution, 'db2');approx = dD(resolution, :);
/*%Plot the original demand data signal */ figure(1);subplot(resolution + 2, 1, 1);plot(tDemandArray(1: dataPoints))legend('Demand original');title('QLD Demand Data Signal');
/*%Plot the approximation demand data signal */ for i = 1 : resolution subplot(resolution + 2, 1, i + 1);plot(approx(1: dataPoints))legend('Demand Approximation');end
/*%After displaying approximation signal, display detail x */ for i = 1: resolution if(i > 1)detail(i, :)= dD(i-1, :)-dD(i, :);else
detail(i, :)= tDemandArray'i + 3);plot(detail(i, 1: dataPoints))legendName = ['Demand Detail ', num2str(i)];legend(legendName);
else
subplot(resolution + 2, 1, resolutiondP(1, :);end
if i == 1 [B,A]=butter(1,0.65,'low');result =filter(B,A, detailP(i, 1: dataPoints));
subplot(resolution + 3, 1, resolutioni + 3);plot(detailP(i, 2: dataPoints))legendName = ['Price Detail ', num2str(i)];legend(legendName);
else
subplot(resolution + 3, 1, resolution1)];end disp(neuralNetwork);disp(' ');
/*%Set no.of input nodes and hidden neurons for the %respective demand and price coefficient signal */ numOfInputs = 2;inputValue = ['Number of neural network INPUT units is set at ', num2str(numOfInputs)];disp(inputValue);disp(' ');numOfOutput = 1;outValue = ['Output is set to ', num2str(numOfOutput)];disp(outValue);disp(' ');numOfHiddens = input('Enter the no.of HIDDEN units for the NN hidden : ');hiddenValue = ['Number of neural network HIDDEN units is set at ', num2str(numOfHiddens)];disp(hiddenValue);disp(' ');/*%Setting no.of training examples */ trainingLength = dataPoints;
/* %Set target outputs of the training examples */ if(x == 1)targetDemand = normDemand(targetStartAt: 1 + trainingLength);else
targetDemand = normDemandDetail(x1, y + 1);inputs(2, y + 1)= normPriceDetail(x1, y + 1);propData(2, y + 1)= normPriceDetail(x1)predicted(1: pointsAhead))./ actualDemand;msg = ['Mean Absolute Error = ', num2str(mean(AbsError(1: pointsAhead))), '!'];disp(' ');disp(msg);
/*%Plot actual time series against predicted result */ figure(3)actualWithPredicted(:, 1)= actualDemand;actualWithPredicted(:, 2)= predicted(1: pointsAhead);plot(actualWithPredicted);graph = ['Mean Absolute Error = ', num2str(mean(AbsError))];title(graph);legend('Actual', 'Forecasted');
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%File name : nretrain.m %Description : This file loads the existing NNs and trains them again.*/ clc;/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Prompt for the starting point for training */ disp('Program will now RETRAIN the Neural Networks ')disp('with the SAME intial data series again...');disp(' ');disp('To capture the pattern of the signal, the model is ')disp('set to accept dataPoints x 2 sets of training examples;');disp('1 set of demand + 1 sets of price.');disp(' ');disp('The normalised demand data , is to be taken as the ')disp('output value for the first iteration of training examples.');disp(' ');msg = ['Data points to be used for reTraining the NNs is from 1 to ',...num2str(dataPoints)];disp(msg);disp(' ');disp('Press ENTER key to continue...');pause;
/*%Clear command screen */ clc;
/*%Prompt for no.of training cycles %For current program, 1 cycle = user set no.of iterations(ie: dataPoints)*/ cycle = input('Input number of cycles to retrain the NNs: ');
numOfTimes = resolution + 1;/*%Loading existing NNs for training */ for x = 1: numOfTimes
/*%Re-initialising variables */ clear targetDemand;clear inputs;clear output;clc;
/*%Loading NN for the respective demand and temperature coefficient signals */
filename = ['nn', num2str(x)];clear nn load(filename);
/*%Getting the size of NN*/ numOfInputs = nn.nin;numOfHiddens = nn.nhidden;numOfOutput = 1;/*%Setting length of reTraining examples and target outputs */ reTrainLength = dataPoints;targetLength = reTrainLength;
targetStartAt = 2;
/*%Set target outputs of the training examples */
if(x == 1)targetDemand = normDemand(targetStartAt: 1 + targetLength);else
targetDemand = normDemandDetail(x1, y + 1);inputs(2, y + 1)= normPriceDetail(x-1, y + 1);
end
output(y + 1, :)= targetDemand(y + 1);
y = y + 1;end
inputs =(inputs');
/*%Setting no.of training cycles */
[ni, np] = size(targetDemand);/*% <== [ni, np] tells the NN how long is 1 cycle;*/ size(targetDemand)/* %With reference to line 106 */
/*%NN options */ options = zeros(1, 18);options(1)= 1;%Provides display of error values options(14)= cycle * ni * np;
/*%Training the neural network %netopt(net, options, x, t, alg);*/
nn = netopt(nn, options, inputs, output, 'scg');
/*%Save the neural network */ filename = ['nn', num2str(x)];save(filename, 'nn');
disp(' ');msg = ['Neural network => ', filename, ' <= successfully RETRAINED and saved!'];
disp(msg);
if(x < 3)disp(' ');disp('Press ENTER key to continue training the next NN...');else
disp(' ');disp('Model is now ready to forecast again!');disp(' ');disp('Press ENTER key to continue...');end
pause;end
第二篇:基于库存的小波神经网络煤炭期货价格预测分析
基于库存的小波神经网络煤炭期货价格预测分析
【摘 要】本文以Matlab为工具,利用小波神经网络,对基于库存的动力煤期货价格进行模型分析和预测,结果表明其具有高度相关性,并具有研究价值。这对探究中国煤炭期货未来的价格变动趋势,为煤炭期货市场提供基础资料和决策支持。
【关键词】煤炭期货;MATLAB;神经网络;模型预测
0 引言
随着经济全球化的推进,我国的基础能源越来越依赖国际煤炭资源,掌握煤炭未来市场价格的话语权对我国经济发展、资源供给都具有重要意义。在现货市场不景气的情况下,期货这种能够规避市场风险的工具便具有了更大的发展空间。小波分析具有较好的描述能力,而且预测的精度较高、预测结果的可靠性较大,作为一种信息和信号处理工具在许多方面得到应用。煤炭期货的上市
动力煤期货上市后,其价格将成为动力煤市场的风向标,自身也将成为电力企业锁定成本的保值工具。因此,动力煤期货上市,对于动力煤市场乃至主要的国民经济发展带来重大积极作用。
一是有利于完善市场体系、稳健企业经营、优化资源配置。上市动力煤期货通过形成公正、透明、权威的中远期价格体系,能够为煤炭、电力等上下游企业签订长、短期协议提供有效参考,是动力煤市场化改革的重要配套措施和煤炭市场体系进一步完善的重要内容。
二是有利于缓解“煤电联动”压力。“煤电联动”是指在某一约定时间周期内,如果煤炭价格上涨超过某一既定比例,那么国家便对电价做出相应调整。推出动力煤期货后,电力等下游企业通过期货市场进行套期保值交易,实现现货、期货“两条腿走路”,有效规避煤炭价格市场波动风险。
三是有利于“煤电一体化”的发展方向。一方面,上市动力煤期货通过提供有效规避市场风险的金融工具,有助于为进一步推动煤电一体化夯实基础;另一方面,动力煤期货能够为煤炭定价提供有益借鉴,煤电合营企业可以在期货价格的基础上,依据自身情况,制定结算价格,为煤电一体化消除消极因素。模型与算法
2.1 小波神经网络
小波神经网络方法与传统的参数模型方法最大的不同在于它是数驱动的自适应技术,不需要对问题的模型做出任何假设。在解决问题的内部规律未知或难以描述的情况下,神经网络可以通过对样本数据的学习训练,以获取数据之间隐藏的函数关系,是一种多变量输入的非线性、非参数的统计方法。
利用神经网络预测时间序列时,利用时间序列模型不需要知道影响预测变量的因果关系,而且有足够多的数据可以用来构成一个合理长度的时间序列的特点,通过把输出层单元的误差逐层向输入层“分摊”给各层单元,从而获得参考误差,并调整各边连接权,直到小于允许误差。输入信号从输入节点依次传过各隐藏节点,然后传到输出层,每一层的节点的输出只影响下一层节点的输出。在学习过程中,为使网络输出层的误差平方和达到最小,通过连续不断地在相对于误差函数斜率下降的方向上计算网络权值和偏差的变化而逐渐逼近目标。每一次权值和偏差的变化都与网络误差的影响成正比,并以反向传播的方式传递到每一层。
2.2 数据准备
本文模型中使用的是郑商所CZCE动力煤自2013年9月上市一年内的收盘价格,以交易日为单位(节假日停盘,故省略)。
库存数据方面,模型使用的是我国主要港口煤炭库存总量,数据来源于鲁中煤炭交易中心。选取区间与动力煤期货价格的选取区间相同,都为2013年9月动力煤在郑商所上市之后的一年时间。动力煤期货价格与库存有存在一定的相关性,动力煤期货价格有较大波动的时段都伴随着库存的反向波动。
(5)记录已学习过的样本数p。如果p
(6)确定隐含层层数。隐含层节点数太多会导致学习时间过长;而隐含层节点太少,容错性差,识别未经学习的样本能力低,所以必须综合多方面的因素进行设计。利用逐步增长或逐步修剪来确定。根据试算比较,最终确定隐含层节点数为10。
(7)按权值修正公式修正各层的权职和阈值;
(9)利用测试样本检验训练好的网络模型,并对未来进行预测。对训练、测试以及预测输出的结果进行反归一化处理,即将输出值还原为原量纲值。预测结果评价方法
通过以2013年9月26日到2014年9月5日的煤炭期货价格与港口库存数据进行分析和单步预测,得到如下图
并对预测的整体效果进行比较,采用平均值误差平方和MSE(Mean Suare Error)、平均绝对误差MAE(Mean Absolute Error)、平均绝对百分误差MAPE(Mean Absolute Percentage Error)等三项预测误差评价指标对其进行评价。误差结果如下(部分)
从图4及表1可以看出,基于神经网络模型预测的误差评价指标都非常小,预测表明,在无重大利好消息出台或者政策变化的情况下,在短期内,国内煤炭期货价格仍处于在低位保持震荡的趋势。结果与讨论
通过对煤炭期货价格交易的分析预测得出,在动力煤期货上市之后,煤炭下游行业对煤炭消费的需求,更加直观的在期货价格上得以提现,而通过市场形成的期货价格,虽然影响因素众多,不能完全准确的预测价格波动,但本文模拟出的结果是比较满意的。也说明煤炭期货价格变动具有研究价值。
【参考文献】
[1]江梦,盛宇华.基于波动理论的煤炭期货市场运行规律研究[J].中国煤炭,2013,39(7):20-23.[2]刘硕,何永秀,陶卫君,杨丽芳,张宇.遗传BP神经网络的煤价预测与煤价风险规避策略[J].华北电力大学学报,2009,36(6):75-80.[3]张智勇,吴晓东,杨鹏,李东武.动力煤期货指数定价模式的思路探讨[J].中国煤炭,2014,40(10).[责任编辑:杨玉洁]