| 1 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // FILE : _decision.tol |
|---|
| 3 | // AUTHOR : Luis Miguel Pérez Liberal (lmperez@bayesinf.com) |
|---|
| 4 | // PURPOSE : Standard functions for diagnosis procedures |
|---|
| 5 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 9 | Serie ErrorForecast.Clasico(Real lengthFor, Serie residuals, Ratio psiw) |
|---|
| 10 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 11 | { |
|---|
| 12 | TimeSet dtn = Dating(residuals); |
|---|
| 13 | Date origen = Last(residuals); |
|---|
| 14 | Real sigma = StDsS(residuals); |
|---|
| 15 | |
|---|
| 16 | Real sigma2 = sigma^2; |
|---|
| 17 | Date fsFor = Succ(origen, dtn, 1); |
|---|
| 18 | Date lsFor = Succ(origen, dtn, lengthFor+1); |
|---|
| 19 | Polyn psi = Expand(psiw, lengthFor); |
|---|
| 20 | Serie zero = CalInd(W,dtn); |
|---|
| 21 | Serie psiSer = SubSer(psi:(zero + Pulse(origen,dtn)), origen, lsFor); |
|---|
| 22 | Serie psiSer2 = psiSer^2; |
|---|
| 23 | Serie error2 = sigma2 * DifEq(1/(1-B), psiSer2, 0); |
|---|
| 24 | |
|---|
| 25 | Serie error = SqRt(error2) |
|---|
| 26 | }; |
|---|
| 27 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 28 | PutDescription("Devuelve la sigma en prevision de una serie de residuos. |
|---|
| 29 | Ejemplo: |
|---|
| 30 | |
|---|
| 31 | Serie ErrorForecast.Clasico( |
|---|
| 32 | 90, |
|---|
| 33 | GenResiduals.Heteroscedastic(500), |
|---|
| 34 | Ratio (1-0.75*B^7)/((1-B^7)*(1-0.10*B)) |
|---|
| 35 | ); |
|---|
| 36 | ", |
|---|
| 37 | ErrorForecast.Clasico); |
|---|
| 38 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 42 | Serie ErrorForecast.Het.Luis(Real lengthFor, Serie residuals, Ratio psiw) |
|---|
| 43 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 44 | { |
|---|
| 45 | TimeSet dtn = Dating(residuals); |
|---|
| 46 | |
|---|
| 47 | Set sTms = {For(1,7,Text (Real k){ |
|---|
| 48 | Text If(CountS(DatCh(residuals,WD(k),FirstS)),"WD("+IntText(k)+")","") |
|---|
| 49 | }) - [[ "" ]]}; |
|---|
| 50 | |
|---|
| 51 | Date origen = Last(residuals); |
|---|
| 52 | Date fsFor = Succ(origen, dtn, 1); |
|---|
| 53 | Date lsFor = Succ(origen, dtn, lengthFor+1); |
|---|
| 54 | Polyn psi = Expand(psiw, lengthFor); |
|---|
| 55 | Serie zero = CalInd(W,dtn); |
|---|
| 56 | Serie psiSer = SubSer(psi:(zero + Pulse(origen,dtn)), origen, lsFor); |
|---|
| 57 | Serie psiSer2 = psiSer^2; |
|---|
| 58 | Serie difEQ = DifEq(1/(1-B), psiSer2, 0); |
|---|
| 59 | |
|---|
| 60 | Serie error2 = SetSumC(EvalSet(sTms,Serie (Text tms) |
|---|
| 61 | { |
|---|
| 62 | TimeSet tms_WD = Eval(tms); |
|---|
| 63 | Real sigma_WD = StDsS(DatCh(residuals,tms_WD,FirstS)); |
|---|
| 64 | Real sigma2_WD = sigma_WD^2; |
|---|
| 65 | Serie psiSer2_WD = difEQ*sigma2_WD*CalInd(tms_WD,dtn); |
|---|
| 66 | Serie psiSer2_WD |
|---|
| 67 | })); |
|---|
| 68 | |
|---|
| 69 | Serie DatCh(SqRt(SubSer(error2,fsFor,lsFor)),Dating(residuals),FirstS) |
|---|
| 70 | }; |
|---|
| 71 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 72 | PutDescription("Devuelve la sigma en prevision de una serie de residuos en |
|---|
| 73 | fechado Diario considerando la heterocedasticidad de los mismos. |
|---|
| 74 | Esta función falla para modelos con parámetros arima regulares grandes. |
|---|
| 75 | Ejemplo: |
|---|
| 76 | |
|---|
| 77 | Serie ErrorForecast.Het.Luis( |
|---|
| 78 | 90, |
|---|
| 79 | GenResiduals.Heteroscedastic(500), |
|---|
| 80 | Ratio (1-0.75*B^7)/((1-B^7)*(1-0.10*B)) |
|---|
| 81 | );", |
|---|
| 82 | ErrorForecast.Het.Luis); |
|---|
| 83 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 87 | Serie ErrorForecast.Het.Pepe(Real lengthFor, Serie residuals, Serie noise, |
|---|
| 88 | Set arima) |
|---|
| 89 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 90 | { |
|---|
| 91 | TimeSet dtn = Dating(residuals); |
|---|
| 92 | Date origen = Last(residuals); |
|---|
| 93 | Date fsFor = Succ(origen, dtn, 1); |
|---|
| 94 | Date lsFor = Succ(origen, dtn, lengthFor+1); |
|---|
| 95 | |
|---|
| 96 | Set sAr = arima[2]; |
|---|
| 97 | Set sMa = arima[3]; |
|---|
| 98 | Polyn dif = SetProd(arima[4]); |
|---|
| 99 | Ratio psiw = ma/(dif*ar); |
|---|
| 100 | Real maDeg = Degree(SetProd(sMa)); |
|---|
| 101 | Real ariDeg = Degree(SetProd(sAr)*dif); |
|---|
| 102 | |
|---|
| 103 | Set sTms = {For(1,7,Text (Real k){ |
|---|
| 104 | Text If(CountS(DatCh(residuals,WD(k),FirstS)),"WD("+IntText(k)+")","") |
|---|
| 105 | }) - [[ "" ]]}; |
|---|
| 106 | |
|---|
| 107 | Serie sigmaK = SetSumC(EvalSet(sTms,Serie (Text tms){ |
|---|
| 108 | Serie CalInd(Eval(tms),dtn)*StDsS(DatCh(residuals,Eval(tms),FirstS)) |
|---|
| 109 | })); |
|---|
| 110 | |
|---|
| 111 | Real noDegree = If(GT(maDeg+ariDeg,CountS(noise)),1,0); |
|---|
| 112 | |
|---|
| 113 | Serie Case( |
|---|
| 114 | EQ(MaxS(sigmaK),0), ErrorForecast.Clasico(lengthFor,residuals,psiw), |
|---|
| 115 | EQ(noDegree,1), ErrorForecast.Het.Luis(lengthFor,residuals,psiw), |
|---|
| 116 | True, |
|---|
| 117 | { |
|---|
| 118 | Real Show(False,"ALL",True); |
|---|
| 119 | Set estimate = Estimate( |
|---|
| 120 | ModelDef( |
|---|
| 121 | noise/(sigmaK+Not(sigmaK)), |
|---|
| 122 | 1, |
|---|
| 123 | 0, |
|---|
| 124 | SetMax(arima[1]), |
|---|
| 125 | 0, |
|---|
| 126 | dif, |
|---|
| 127 | sAr, |
|---|
| 128 | sMa, |
|---|
| 129 | Empty, |
|---|
| 130 | Empty |
|---|
| 131 | ) |
|---|
| 132 | ); |
|---|
| 133 | Real Show(True,"ALL"); |
|---|
| 134 | |
|---|
| 135 | Polyn p.ar = SetProd(estimate::Definition::AR); |
|---|
| 136 | Polyn p.ma = SetProd(estimate::Definition::MA); |
|---|
| 137 | Polyn p.dif = estimate::Definition::Dif; |
|---|
| 138 | Ratio r.psiw = p.ma/(p.dif*p.ar); |
|---|
| 139 | Serie error = estimate::Series::Residuals; |
|---|
| 140 | |
|---|
| 141 | Serie error.Prevision = ErrorForecast.Clasico( |
|---|
| 142 | lengthFor, |
|---|
| 143 | error, |
|---|
| 144 | r.psiw |
|---|
| 145 | )/(StDsS(error)+Not(StDsS(error))); |
|---|
| 146 | |
|---|
| 147 | Serie DatCh( |
|---|
| 148 | SubSer(error.Prevision*sigmaK,fsFor,lsFor), |
|---|
| 149 | Dating(residuals), |
|---|
| 150 | FirstS |
|---|
| 151 | ) |
|---|
| 152 | } |
|---|
| 153 | ) |
|---|
| 154 | }; |
|---|
| 155 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 156 | PutDescription("Devuelve la sigma en prevision de una serie de residuos en |
|---|
| 157 | fechado Diario considerando la heterocedasticidad de los mismos. |
|---|
| 158 | Esta función es capaz de controlar la heterocedasticidad de cualquier modelo |
|---|
| 159 | arima; sin embargo, puede darnos problemas si tenemos pocos grados de libertad |
|---|
| 160 | y es mucho más costosa computacionalmente. |
|---|
| 161 | Ejemplo: |
|---|
| 162 | |
|---|
| 163 | Set sArima = GetArimaFromLabel(\"P1_7DIF0_1AR1_0MA0_7\"); |
|---|
| 164 | Polyn p.AR = SetProd(Traspose(sArima)[2]); |
|---|
| 165 | Polyn p.MA = SetProd(Traspose(sArima)[3]); |
|---|
| 166 | Polyn p.DIF = SetProd(Traspose(sArima)[4]); |
|---|
| 167 | Polyn ar = GetRandomPolyn(p.AR); |
|---|
| 168 | Polyn ma = GetRandomPolyn(p.MA); |
|---|
| 169 | Ratio psi = ma/(p.DIF*ar); |
|---|
| 170 | |
|---|
| 171 | Serie residuals = GenResiduals.Heteroscedastic(500); |
|---|
| 172 | Serie noise = DifEq(psi,residuals,100); |
|---|
| 173 | |
|---|
| 174 | Serie ErrorForecast.Het.Pepe( |
|---|
| 175 | 90, |
|---|
| 176 | residuals, |
|---|
| 177 | noise, |
|---|
| 178 | Traspose(sArima) |
|---|
| 179 | );", |
|---|
| 180 | ErrorForecast.Het.Pepe); |
|---|
| 181 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 185 | // END |
|---|
| 186 | ////////////////////////////////////////////////////////////////////////////// |
|---|