Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. % student's project 2006 on Provlepsis lesson
  2. % PREDICTION USING AN ---ANFIS--- MODEL :PROJECT 6
  3.  
  4. close all %clean the workspace
  5. clear 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. %load agrasf
  11. %mydata
  12.  
  13.  
  14. tic
  15. figure(1) % a view of data
  16. plot (mydata)
  17. xlabel('time'); ylabel('values')
  18. title('Actual values')
  19.  
  20.  
  21. % Estimation of an ANFIS model
  22. % prepare training data
  23. %input data
  24. tr=mydata(1:200) % TRAINING DATA
  25. % input (k-2)
  26. train=tr
  27. train(length(train))=[]% removes the last row
  28. train(length(train))=[]; %removes the second last row
  29. length(train)
  30.  
  31.  
  32. %input k-1
  33.  
  34. train1=tr % first input
  35. train1(length(train1))=[]% removes the last row
  36. train1(1)=[] % removes the first row
  37. length(train1)
  38.  
  39. % output k
  40. train2=tr %second input
  41. train2(1)=[] % removes the first row
  42. train2(1)=[] %removes the second row
  43. length(train2)
  44.  
  45.  
  46.  
  47. trn_data=[train train1 train2 ] %(k-2) (k-1) (k) training data
  48.  
  49.  
  50.  
  51. %%%%%% Ploting TRAINING data as a scater plot%%%%%%%
  52. figure(10)
  53. subplot(1,2,1)
  54. plot (train, train2, 'o')
  55. xlabel ('input (k-2) ')
  56. ylabel('ouput (k)')
  57. title('Training data')
  58. axis equal; axis square
  59.  
  60. subplot(1,2,2)
  61. plot (train1, train2, 'o')
  62. xlabel ('input (k-1) ')
  63. ylabel('ouput (k)')
  64. title('Training data')
  65. axis equal; axis square
  66.  
  67.  
  68.  
  69.  
  70. %preparing the evaluation(test) data
  71. ev=mydata(201:end)% TESTING DATA
  72.  
  73. % input (k-2)
  74. eval=ev
  75. eval(length(eval))=[]; eval(length(eval))=[];
  76. length(eval)
  77.  
  78. % input (k-1)
  79. eval1=ev
  80. eval1(length(eval))=[];
  81. eval1(1)=[];
  82. length(eval)
  83.  
  84. % input (k)
  85. eval2=ev
  86. eval2(1)=[]
  87. eval2(1)=[];
  88. length(eval2)
  89.  
  90.  
  91. evaldata=[eval eval1] %input (k-2) and (k-1)
  92.  
  93. y2=eval2 %(k) output data for testing
  94.  
  95.  
  96.  
  97.  
  98.  
  99. % generate FIS matrix
  100. epoch_n=400
  101. mf_n=[4 4];
  102.  
  103. %mf_type='gbellmf'; %type of membership function
  104. %mf_type='trimf'; %the parameter b>c
  105. %mf_type='gauss2mf'
  106. mf_type='gaussmf' %
  107. %mf_type='smf' % unsupported
  108. %mf_type='trapmf' % the parameter b>c
  109. %mf_type='zmf' %unsupported
  110. %mf_type='pimf' % run problem
  111.  
  112. in_fismat=genfis1(trn_data, mf_n, mf_type);
  113.  
  114.  
  115. % start training
  116. ss=0.1;
  117.  
  118. [m_anfis trn_error step_size ] = ...
  119. anfis(trn_data, in_fismat, [epoch_n nan ss nan nan], [1,1,1,1]);
  120.  
  121.  
  122.  
  123. figure('name', ['ANFIS: time series prediction'])
  124.  
  125. subplot(211);
  126. tmp=[trn_error ];
  127. plot(tmp);
  128. title('Error Curves');
  129. axis([0 epoch_n min(tmp(:)) max(tmp(:))]);
  130. xlabel('epochs')
  131. ylabel('RMSE')
  132. legend('Training Error');
  133.  
  134.  
  135. subplot(212);
  136. plot(step_size);
  137. xlabel('epochs')
  138. title('Step Size');
  139.  
  140.  
  141.  
  142. % plot the initial membership functons
  143. figure (20)
  144. subplot(2,1,1)
  145. [mfx, mfy]=plotmf(in_fismat, 'input', 1);
  146. plot(mfx, mfy);
  147. title('(a) Initial MFs on input')
  148. axis([-inf inf -inf inf]);
  149. subplot(2,1,2)
  150. [mfx, mfy]=plotmf(in_fismat, 'input', 2);
  151. plot(mfx, mfy);
  152. title('(b) Initial MFs on input')
  153. axis([-inf inf -inf inf]);
  154.  
  155.  
  156. % plot final MF's on x,y,z,u
  157.  
  158. figure (30)
  159. subplot(2,1,1)
  160. [mfx, mfy]=plotmf(m_anfis, 'input', 1);
  161. plot(mfx, mfy);
  162. title('(a) Final MFs on input')
  163. axis([-inf inf -inf inf]);
  164. subplot(2,1,2)
  165. [mfx, mfy]=plotmf(m_anfis, 'input', 2);
  166. plot(mfx, mfy);
  167. title('(b) Final MFs on input')
  168. axis([-inf inf -inf inf]);
  169.  
  170.  
  171.  
  172.  
  173. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%************
  174.  
  175.  
  176. figure(40)
  177. plotfis(in_fismat)
  178.  
  179. figure(50)
  180. plotfis(m_anfis)
  181.  
  182. showrule(m_anfis)
  183.  
  184.  
  185. anfisedit (m_anfis)
  186. surfview(m_anfis)
  187.  
  188.  
  189. %%%%%%%%%%%%%%%%%%%%%%%%%% IN SAMPLE EVALAUTION %%%%%%%%%%%%
  190. insample_data=trn_data(:,1:2)
  191. insaple_output=trn_data(:,3)
  192. yhat_anfis_insample=evalfis(insample_data, m_anfis);
  193.  
  194. figure (60)
  195. plot(insaple_output(end-30:end), 'b-s'), hold, plot(yhat_anfis_insample(end-30:end), 'r-x');
  196. legend('actual values','ANFIS prediction values')
  197. xlabel('time')
  198. ylabel('values')
  199. title('Actual values and ANFIS prediction in sample')
  200.  
  201.  
  202. figure (70)
  203. plot(insaple_output-yhat_anfis_insample)
  204. xlabel('time')
  205. ylabel('error')
  206. title('Prediction in sample errors')
  207.  
  208.  
  209.  
  210. %Root Mean Square Error (RMSE)
  211. RMSE_anfis_insample=sqrt(norm(insaple_output-yhat_anfis_insample)^2/length(insaple_output-yhat_anfis_insample))
  212.  
  213.  
  214.  
  215.  
  216.  
  217. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  218.  
  219. %prediction by ANFIS model out of sample
  220.  
  221. %%%%%%%%%%%%%%%%%%%%%%%%%%% ANFIS EVALUATION %%%%%%%%%%%%%%%%%
  222. input=evaldata
  223.  
  224.  
  225. %%%%%% Ploting EVALUATION data as a scater plot%%%%%%%
  226. figure(90)
  227. subplot(1,2,1)
  228. plot (eval, eval2, 'o')
  229. xlabel ('input (k-2)')
  230. ylabel('ouput (k)')
  231. title('Evaluating data')
  232. axis equal; axis square
  233.  
  234. subplot(1,2,2)
  235. plot (eval1, eval2, 'o')
  236. xlabel ('input (k-1)')
  237. ylabel('ouput (k)')
  238. title('Evaluating data')
  239. axis equal; axis square
  240.  
  241.  
  242.  
  243. %%%%%%%%% Evaluation of Anfis out of sample
  244. yhat_anfis=evalfis(input, m_anfis);
  245.  
  246.  
  247. adapt_input=y2;
  248. length(adapt_input);
  249. length(yhat_anfis);
  250. result=[adapt_input yhat_anfis (adapt_input-yhat_anfis)];
  251. er_anfis=adapt_input-yhat_anfis; %error
  252.  
  253.  
  254. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  255. figure (100)
  256. plot(adapt_input(end-30:end), 'b-s'), hold, plot(yhat_anfis(end-30:end), 'r-x');
  257. legend('actual values','ANFIS prediction values')
  258. xlabel('time')
  259. ylabel('values')
  260. title('Actual values and ANFIS out of sample prediction')
  261.  
  262.  
  263. figure (120)
  264. plot(adapt_input-yhat_anfis)
  265. xlabel('time')
  266. ylabel('error')
  267. title('Prediction errors')
  268.  
  269.  
  270.  
  271.  
  272. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  273. % error measures calculation
  274.  
  275.  
  276. MSE_anfis=(1/length(yhat_anfis))*norm(er_anfis)^2
  277.  
  278. %Root Mean Square Error (RMSE)
  279. RMSE_anfis=sqrt(norm(er_anfis)^2/length(er_anfis))
  280.  
  281. %Mean Absolute Error (MAE)
  282. MAE_anfis=(1/length(er_anfis))*sum(abs(er_anfis))
  283.  
  284. %Mean Absolute percentage Error (MAPE)
  285. MAPE_anfis=(100/length(er_anfis))*sum(abs(er_anfis)./abs(adapt_input))
  286.  
  287. toc % the calculation time in seconds
  288. runing_minute_time=toc/60
  289. %end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement