Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. % FORECASTING 20/11/2013 - ATSALAKIS GEORGE -
  2. %ONE-STEP AHEAD PREDICTION % By Neural Network
  3.  
  4. clear all
  5. close all
  6. clc
  7.  
  8. mydata=xlsread ('C:\Users\User\Desktop\Ziogas.xlsx', 'e1:e252'); % retrieves the E column data of range 1 to 252 from the data file 'atermondata'
  9.  
  10. time=mydata
  11.  
  12. %output (k)
  13. data_for_output= xlsread ('C:\Users\User\Desktop\Ziogas.xlsx','e3:e252')
  14. %input one step delay (k-1)
  15. dataDealyed_for_input=xlsread ('C:\Users\User\Desktop\Ziogas.xlsx', 'e2:e251')
  16.  
  17. data=[dataDealyed_for_input data_for_output]
  18.  
  19.  
  20. % prepare training data
  21. % input (k-1)
  22. N2=length(data_for_output)
  23. N1=floor((N2/5)*4) % 80%
  24.  
  25. %training data
  26. train_data_input=dataDealyed_for_input(1:N1); %training data-input
  27. train_data_output=data_for_output(1:N1); %training data-input
  28.  
  29. %testing data
  30. test_data_input=dataDealyed_for_input(N1+1:N2); %testing data
  31. test_data_output=data_for_output(N1+1:N2); %testing data
  32.  
  33. x=train_data_input
  34. y=train_data_output
  35.  
  36. [x y]
  37.  
  38. trainX =x
  39. trainY = y;
  40.  
  41. % Create test set
  42. testX = test_data_input;
  43. testY = test_data_output;
  44.  
  45. t=1:(length(x))
  46.  
  47. % figure of data
  48. figure('name', ['Training data']);
  49. subplot(211); plot(t, x,'-', t, x, 'go');
  50. xlabel('Time'); ylabel('x'); axis([-inf inf -inf inf]);
  51. title('Training data x')
  52.  
  53. subplot(212); plot(t, y, '-', t, y, 'go');
  54. xlabel('Time'); ylabel('y'); axis([-inf inf -inf inf]);
  55. title('Training data y')
  56.  
  57.  
  58. %%%%%%PLOTING TRAINING DATA AS A SCATTER PLOT%%%%%%%%
  59. figure('name', ['TRAINING DATA AS A SCATTER PLOT 2D']);
  60. plot (x, y, 'o')
  61. xlabel ('x')
  62. ylabel('y')
  63. title('Training data')
  64. axis equal; axis square
  65.  
  66.  
  67. figure('name', ['TRAINING DATA AS A SCATTER PLOT 3D'])
  68. plot(x, y, 'o');
  69. axis([-inf inf -inf inf -inf inf]);
  70. set(gca, 'box', 'on');
  71. xlabel('x'); ylabel('y');% zlabel('y(k+1)'); title('Training Data');
  72.  
  73. net = newff(trainX', trainY', 20); %create a feed-forward backprobagation network
  74. %net = newfit(trainX', trainY', 20); %create a fiting network
  75.  
  76. net.performFcn = 'mae'; % calculates the MAE (mean absolute eror)
  77. net = train(net, trainX', trainY');
  78.  
  79.  
  80.  
  81.  
  82. %% Forecast using Neural Network Model
  83. % Once the model is built, perform a forecast on the independent test set.
  84.  
  85. forecastLoad = sim(net, testX')';
  86.  
  87. %% Compare Forecast Load and Actual Load
  88. % Create a plot to compare the actual load and the predicted load as well
  89. % as compute the forecast error. In addition to the visualization, quantify
  90. % the performance of the forecaster using metrics such as mean absolute
  91. % error (MAE), mean absolute percent error (MAPE) and daily peak forecast
  92. % error.
  93.  
  94. err = testY-forecastLoad;
  95. %fitPlot(testDates, [testY forecastLoad], err);
  96.  
  97. errpct = abs(err)./testY*100;
  98.  
  99. %fL = reshape(forecastLoad, 24, length(forecastLoad)/24)'; %μετατρέπει τις γραμμες σε στήλες
  100. %tY = reshape(testY, 24, length(testY)/24)';
  101. %peakerrpct = abs(max(tY,[],2) - max(fL,[],2))./max(tY,[],2) * 100;
  102.  
  103. MAE = mean(abs(err));
  104. MAPE = mean(errpct(~isinf(errpct)));
  105.  
  106. %fprintf('Mean Absolute Percent Error (MAPE): %0.2f%% \nMean Absolute Error (MAE): %0.2f MWh\nDaily Peak MAPE: %0.2f%%\n',...
  107. % MAPE, MAE, mean(peakerrpct))
  108.  
  109. MSE_NN=(1/length(forecastLoad))*norm(err)^2
  110.  
  111. %Root Mean Square Error (RMSE)
  112. RMSE_NN=sqrt(norm(err)^2/length(err))
  113.  
  114. %Mean Absolute Error (MAE)
  115. MAE_NN=(1/length(err))*sum(abs(err))
  116.  
  117. %Mean Absolute percentage Error (MAPE)
  118. MAPE_NN=(100/length(err))*sum(abs(err)./abs(testY))
  119.  
  120.  
  121. %% Examine Distribution of Errors
  122. % In addition to reporting scalar error metrics such as MAE and MAPE, the
  123. % plot of the distribution of the error and absolute error can help build
  124. % intuition around the performance of the forecaster
  125.  
  126. figure (3);
  127. subplot(3,1,1); hist(err,100); title('Error distribution');
  128. subplot(3,1,2); hist(abs(err),100); title('Absolute error distribution');
  129. line([MAE MAE], ylim); legend('Errors', 'MAE');
  130. subplot(3,1,3); hist(errpct,100); title('Absolute percent error distribution');
  131. line([MAPE MAPE], ylim); legend('Errors', 'MAPE');
  132.  
  133.  
  134.  
  135.  
  136. figure (200)
  137. plot(forecastLoad(end-40:end), 'b-s'), hold, plot(testY(end-40:end), 'r-x');
  138. legend('actual values','NN forecasted values')
  139. xlabel('time')
  140. ylabel('values')
  141. title('Actual values and NN forecasts')
  142.  
  143. % four erros
  144. MAPE_NN
  145. MAE_NN
  146. MSE_NN
  147. RMSE_NN
  148.  
  149. fprintf('RMSE = %d\n', RMSE_NN)
  150. fprintf('MAE = %d\n', MAE_NN)
  151. fprintf('MAPE = %d\n', MAPE_NN)
  152. fprintf('MSE = %d\n', MSE_NN)
  153.  
  154.  
  155. %end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement