Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 10.08 KB | None | 0 0
  1. %Retirar os valores
  2.  
  3. x = datasetATDPL1;
  4.  
  5. data_labels= x(2:length(x), 1); %datas
  6. valores = [x{2:length(x), 2}]; %valores referentes a cada data
  7.  
  8. N = length(valores);
  9. t = (0: N-1);
  10. tt = (0:2*N-1); %escala temporal para a previsao
  11.  
  12. %figure(1)
  13. %plot (t, valores, '-+')
  14.  
  15. haNaN = any(isnan(valores));  %Ha NaN por colunas?
  16. colNaN = find(isnan(valores)); % Colunas com NaN
  17.  
  18. %Elimina linhas com NaN e reconstroi
  19. %Sugestao:
  20. % Reconstruir as linhas eliminadas usando extrapolacao
  21.  
  22. valores_1 = valores;
  23.  
  24. if any(isnan(valores))
  25.     ind = find(isnan(valores));
  26.     for k = 1 : length(ind)
  27.         tt = t(ind(k) - 4 : ind(k) - 1);
  28.         xx = valores_1(ind(k) - 4 : ind(k) - 1);
  29.         aux = interp1(tt,xx,t(ind(k)),'pchip','extrap');
  30.         if aux < 0
  31.             valores_1(ind(k)) = interp1(tt,xx,t(ind(k)),'linear','extrap');
  32.         else
  33.             valores_1(ind(k)) = aux;
  34.         end
  35.     end
  36. end
  37.  
  38. %figure(1)
  39. %plot (t, valores_1, '-+')
  40.  
  41.  
  42. %Media e desvio padrao de cada grupo de 31
  43. mu = zeros(12,1);
  44. aux = 1;
  45. auxMax = 31;
  46.  
  47. for k = 1 : 12
  48.     mu(k,1) = mean(valores_1(aux:auxMax));
  49.     auxMax = min(auxMax + 31, length(valores_1));
  50.     aux = aux + 31;
  51.    
  52. end  
  53.  
  54. sigma = zeros(12,1);
  55. aux_1 = 1;
  56. auxMax_1 = 31;
  57. for k = 1 : 12
  58.     vetor_aux = valores_1(aux_1:auxMax_1);
  59.     sigma(k,1) = std(vetor_aux);
  60.     auxMax_1 = min(auxMax_1 + 31, length(valores_1));
  61.     aux_1 = aux_1 + 31;
  62.    
  63. end
  64.  
  65. %outliers
  66.  
  67. aux_2 = 1;
  68. auxMax_2 = 31;
  69. valores_s_outliers = valores_1; %Substituicao dos outliers
  70.  
  71. for k = 1 : 12
  72.     vetor_aux = valores_1(aux_2:auxMax_2);
  73.     N = length(vetor_aux);
  74.     MeanMat = repmat(mu(k),N,1); %Repete a media (mu) em N linhas
  75.     SigmaMat = repmat(sigma(k),N,1); %Repete a media (sigma) em N linhas
  76.     outliers = find(abs(vetor_aux - MeanMat) > 3 * SigmaMat); %identifica os outliers
  77.     nout = length(outliers); %numero de outliers
  78.     if nout
  79.         for x = aux_2: auxMax_2
  80.             disp(k);
  81.            
  82.             if valores_s_outliers(x) - mu(k) > 3* sigma(k)
  83.                 disp(k);
  84.                valores_s_outliers(x) = mu(k) + 2.5*sigma(k);
  85.             elseif mu(k) - valores_s_outliers(x) > 3*sigma(k)
  86.                valores_s_outliers(x) = mu(k) - 2.5*sigma(k);
  87.            end
  88.         end
  89.     end
  90.     auxMax_2 = min(auxMax_2 + 31, length(valores_1));
  91.     aux_2 = aux_2 + 31;
  92. end
  93.  
  94. %disp(valores_s_outliers);
  95. %figure(1)
  96. %plot (t, valores_1, '-+',t,valores_s_outliers,'-*')
  97.  
  98.  
  99. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  100. %ficha 4 e pergunta 2 do mini projeto
  101.  
  102. %2.2 e 2.3 MINIPROJETO
  103.  
  104. %---Estimar tendencia parametrica (polinomial)
  105. %trend de ordem 0
  106. valores_s_outliers_t0 = detrend(valores_s_outliers,'constant'); %media da serie
  107. tr1_0 = valores_s_outliers - valores_s_outliers_t0;
  108. %trend de ordem 1
  109. valores_s_outliers_t1 = detrend(valores_s_outliers,'linear'); %serie sem a tendencia
  110. tr1_1 = valores_s_outliers - valores_s_outliers_t1;
  111.  
  112. %2.4 Rpresentar graficamente
  113.  
  114. %figure(2) %Para ver mudar numero
  115. %subplot(211)
  116. %plot(t,valores_s_outliers,'-+',t,tr1_1,'-',t,valores_s_outliers_t1,'-');
  117. %legend('Serie 1 regularizada', 'componente da tendência','Série temporal sem a tendência de grau 1');
  118. t = t';
  119. %vai se usar o t' e nao o t para que t e valores_s_outliers tenham as mesmas dimensoes
  120. p = polyfit(t',valores_s_outliers,1); %coeficiente dos polinomios
  121. tr1_2 = polyval(p,t); %valores resultantes
  122. valores_s_tend = valores_s_outliers - tr1_1;
  123.  
  124. %2.5 Representacao grafica
  125.  
  126. %figure(1)
  127. %subplot(211)
  128. %plot(t,valores_s_outliers,'-+',t,tr1_1,'-*');
  129. %title('Serie 1 (+) e trend(*) de grau 2')
  130. %xlabel('t [h]');
  131.  
  132. %subplot(212)
  133. %plot(t,valores_s_tend,'-o');
  134. %title('Serie 1 sem (o) trend de grau 2')
  135. %xlabel('t [h]');
  136.  
  137. %2.6 e %2.7
  138. %N = length(valores);
  139. %Estimar sazonalidade
  140.  
  141. h0 = repmat((1:91)', 4, 1); %trimestre
  142. %h0 = cat(1, h0, h0(1:4));
  143. sX = dummyvar(h0); %gera matriz dummy, matriz de 0's e 1's
  144. Bs1 = sX\valores_s_outliers_t1'; %Obtem valores para cada trimestre %valores_s_outliers_t1??????
  145. st1 = sX * Bs1; %componente sazonal
  146.  
  147. valores_s_sazonalidade = valores_s_outliers' - st1;
  148.  
  149. %2.8 Representacao grafica
  150.  
  151. %figure(10)
  152. %subplot(211)
  153. %plot(t,valores_s_outliers,'-+',t,valores_s_sazonalidade','-o');
  154. %subplot(212)
  155. %plot(t,st1,'-+');
  156. %title('Sazonalidade da Serie 1')
  157. %xlabel('t [h]');
  158.  
  159. %2.9
  160.  
  161. %-- Estimar a componente irregular
  162. irreg = valores_s_outliers' - tr1_2 - st1;
  163. %--- serie temporal sem a componente irregular
  164. valores_s_irreg = valores_s_outliers' - irreg;
  165.  
  166. %2.10 Representacao grafica
  167.  
  168. %figure(1)
  169. %subplot(211)
  170. %plot(t,valores_s_outliers,'-+',t,valores_s_irreg','-o');
  171. %title('Serie 1 com (+) e sem (o) componente irregular')
  172. %subplot(212)
  173. %plot(t,irreg,'-*');
  174. %title('Componente irregular da serie 1')
  175. %xlabel('t [h]');
  176.  
  177.  
  178. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  179. %ficha 5 e pergunta 3 do mini projeto
  180.  
  181. %3.1
  182. %3.2
  183.  
  184. adftest(valores_s_outliers) %teste de estacionaridade da serie regularizada
  185. adftest(st1) %teste de estacionaridade da componente sazonal
  186.  
  187. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  188.  
  189. %figure(1); %representacao do FAC e do FACP
  190.  
  191. y1 = st1(1:91);
  192.  
  193. %subplot(211)
  194. %autocorr(y1) %indicador para a ordem de nc (modelo MA)
  195.  
  196. %subplot(212)
  197. %parcorr(y1) %indicador para a ordem de na (modelo RA)
  198.  
  199. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  200.  
  201. id_y1 = iddata(y1,[],1,'TimeUnit','day');
  202.  
  203. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.5%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  204.  
  205. opt1_AR = arOptions('Approach','ls'); %Opçoes do modelo AR
  206. na1_AR = 30; %historico da variavel
  207. model1_AR = ar(id_y1,na1_AR,opt1_AR); %modelo AR
  208. pcoef1_AR = polydata(model1_AR); %parametros do modelo AR
  209.  
  210. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.6%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  211.  
  212. %Simulaçao do modelo AR
  213. y1_AR = y1(1:na1_AR);
  214. for k = na1_AR + 1 : 91
  215.     y1_AR(k) = sum(-pcoef1_AR(2:end)'.*flip(y1_AR(k-na1_AR : k-1)));
  216. end
  217. y1_AR2 = repmat(y1_AR,4,1);
  218.  
  219. %Simulacao do modelo AR com forecast
  220. y1_ARf = forecast(model1_AR,y1(1:na1_AR),91-na1_AR);
  221. y1_ARf2 = repmat([y1(1:na1_AR); y1_ARf],4,1);
  222.  
  223. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.7%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  224. %Representacoes graficas
  225.  
  226. %t=(1:182)';
  227. %st1_=st1(1:182);
  228.  
  229. %figure(1) %compara a componente sazonal com a sua estimacao
  230. %plot(t,st1,'+-',1:size(y1_AR2+tr1_2),y1_AR2+tr1_2,'-o');
  231.  
  232. %figure(2) %compara a componente sazonal com a sua estimacao
  233. %plot(t,st1,'+-',t,y1_AR2,'-o',t,y1_ARf2,'-*');
  234. %xlabel('t [h]');
  235. %title('Componente sazonal 1 (-+) e Estimacao com o model AR (-o)');
  236.  
  237. %figure(3)
  238. %plot(t,valores_s_outliers,'-+',t,y1_AR2+tr1_2,'-o');
  239. %xlabel('t [h]');
  240. %title('Serie 1 (-+) e estimaçao com o modelo AR (-o)');
  241.  
  242. %metrica para analise
  243. E1_AR = sum((valores_s_outliers-(y1_AR2+tr1_2)).^2);
  244.  
  245. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.8%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  246.  
  247. tt = (0:364*2-1);
  248.  
  249. %tentativa de encontrar uma tendencia cos()
  250. t1_prev = polyval(p, tt); %tendencia adaptada para 730 (linear) %calcula a endencia para 2N
  251. media_G = mean(valores_s_outliers);
  252. valores_normal = valores_s_outliers - media_G;
  253. %plot(t, valores_s_outliers, '-+', t, valores_normal, 'r-+');
  254. amplitude = 20;
  255. periodo = 235;
  256. acerto = -20;
  257. tend_cos = amplitude * cos((2*pi/periodo)*tt - acerto) + t1_prev;
  258. %plot(tt, tend_cos, '-+', t, valores_s_outliers, 'r-+');
  259.  
  260. figure(4) %Faz a previsao para 2N
  261. aux3 = repmat(y1_AR2,2,1);
  262. plot(t,valores_s_outliers,'-+',tt,aux3+tend_cos', '-o');
  263. xlabel('t [h]');
  264. title('Serie 1 (-+) e Previsao com o modelo AR (-o)')
  265.  
  266. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.9%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  267.  
  268. opt1_ARMAX = armaxOptions('SearchMethod','auto'); %Opçoes do modelo ARMAX
  269. na1_ARMA = 5; %numero de parcelas %mais valores
  270. nc1_ARMA = 1; %ruido
  271. model1_ARMA = armax(id_y1,[na1_ARMA nc1_ARMA],opt1_ARMAX);
  272.  
  273. [pa1_ARMA,pb1_ARMA,pc1_ARMA] = polydata(model1_ARMA);
  274.  
  275. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.10%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  276.  
  277. e = randn(91,1); %ruido branco
  278.  
  279. y1_ARMA = y1(1:na1_ARMA); %Simulacao do Modelo ARMA
  280. for k = na1_ARMA + 1 : 91
  281.     y1_ARMA(k) = sum(-pa1_ARMA(2:end)'.*flip(y1_ARMA(k - na1_ARMA : k - 1))) + sum(pc1_ARMA'.*flip(e(k - nc1_ARMA : k)));
  282. end
  283. y1_ARMA2 = repmat(y1_ARMA,4,1); %que valor aqui?????????
  284.  
  285. %Simulacao do Modelo ARMA com forecast
  286.  
  287. y1_ARMAf = forecast(model1_ARMA,y1(1:na1_ARMA), 91 - na1_ARMA); %que valor aqui?????????
  288. y1_ARMAf2 = repmat([y1(1:na1_ARMA); y1_ARMAf],4,1); %que valor aqui?????????
  289.  
  290.  
  291. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.11%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  292.  
  293. %figure(1) %compara a componente sazonal com a sua estimacao
  294. %plot(t,st1,'+-',t,y1_ARMA2,'-o',t,y1_ARMAf2,'-*');
  295. %xlabel('t [h]');
  296. %title('Componente sazonal 1 (-+) e Estimacao com o model AR (-o)');
  297.  
  298. %figure(1)
  299. %plot(t,valores_s_outliers,'-+',t,y1_ARMA2+tr1_2,'-o');
  300. %xlabel('t [h]');
  301. %title('Serie 1 (-+) e estimaçao com o modelo AR (-o)');
  302.  
  303. %metrica para analise
  304. E1_AR = sum((valores_s_outliers-(y1_ARMA2+tr1_2)).^2);
  305.  
  306.  
  307. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.12%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  308.  
  309. %provavelmente esta mal, ver o que e !!!!!!!!!!!!!
  310.  
  311. %figure(1) %Faz a previsao para 2N
  312. %plot(t,valores_s_outliers,'-+',tt,repmat(y1_ARMA2,4,1) + tr1_2_2, '-o');
  313. %xlabel('t [h]');
  314. %title('Serie 1 (-+) e Previsao com o modelo ARMA (-o)')
  315.  
  316. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.13%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  317.  
  318. %Estimcao de um modelo ARIMA
  319.  
  320. %Parametros do modelo ARIMA
  321. p1_ARIMA = 3;  %?????
  322. D1_ARIMA = 1; %?????
  323. q1_ARIMA = 1; %?????
  324.  
  325. %Obtem a estrutura do modelo ARIMA
  326. Md1 = arima(p1_ARIMA,D1_ARIMA,q1_ARIMA);
  327.  
  328. %Estimacao do modelo ARIMA ?????????????????
  329. %EstMd1 = estimate(Md1,valores_s_outliers(1:N) , 'Y0', valores_s_outliers(1:p1_ARIMA +1));
  330.  
  331. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.14%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  332.  
  333. %Simulacao do modelo ARIMA
  334. %y1_ARIMA = simulate(EstMd1,N);
  335.  
  336. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%3.15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  337.  
  338. %figure(1)
  339. %plot(t,valores_s_outliers,'-+',t,y1_ARIMA,'-o');
  340. %xlabel('t [h]');
  341. %title('Serie 1 (-+) e Estimacao com o modelo ARIMA (-o)')
  342.  
  343. %Metrica para analise ARIMA
  344. %E1_ARIMA = sum((valores_s_outliers - y1_ARIMA(1:N)).^2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement