1 | ////////////////////////////////////////////////////////////////////////////// |
---|
2 | // FILE : _preEstimation.tol |
---|
3 | // AUTHOR : Luis Miguel Pérez Liberal (lmperez@bayesinf.com) |
---|
4 | // PURPOSE : Pre-Estimation diagnosis functions |
---|
5 | ////////////////////////////////////////////////////////////////////////////// |
---|
6 | |
---|
7 | |
---|
8 | ////////////////////////////////////////////////////////////////////////////// |
---|
9 | Matrix DinamicAutoCor(Serie serie, TimeSet tms, Real times, Real interval) |
---|
10 | ////////////////////////////////////////////////////////////////////////////// |
---|
11 | { |
---|
12 | BinGroup("|",For(1,times,Matrix (Real j) |
---|
13 | { |
---|
14 | Serie s = SubSer(serie,Succ(Last(serie),tms,-interval*j),Last(serie)); |
---|
15 | Matrix SubCol(AutoCor(s,times),[[ 2 ]]) |
---|
16 | })) |
---|
17 | }; |
---|
18 | ////////////////////////////////////////////////////////////////////////////// |
---|
19 | PutDescription("It gives the autocorrelation function for different history |
---|
20 | momments, for instance: |
---|
21 | |
---|
22 | Matrix DinamicAutoCor( |
---|
23 | GenSerie.Normal(500,\"P1DIF1AR0MA1\"), |
---|
24 | Diario, |
---|
25 | 30, |
---|
26 | 10 |
---|
27 | );", |
---|
28 | DinamicAutoCor); |
---|
29 | ////////////////////////////////////////////////////////////////////////////// |
---|
30 | |
---|
31 | |
---|
32 | ////////////////////////////////////////////////////////////////////////////// |
---|
33 | Matrix DinamicPartAutoCor(Serie serie, TimeSet tms, Real times, Real interval) |
---|
34 | ////////////////////////////////////////////////////////////////////////////// |
---|
35 | { |
---|
36 | BinGroup("|",For(1,times,Matrix (Real j) |
---|
37 | { |
---|
38 | Serie s = SubSer(serie,Succ(Last(serie),tms,-interval*j),Last(serie)); |
---|
39 | Matrix SubCol(PartAutoCor(s,times),[[ 2 ]]) |
---|
40 | })) |
---|
41 | }; |
---|
42 | ////////////////////////////////////////////////////////////////////////////// |
---|
43 | PutDescription("It gives the partial autocorrelation function for different |
---|
44 | history momments, for instance: |
---|
45 | |
---|
46 | Matrix DinamicPartAutoCor( |
---|
47 | GenSerie.Normal(500,\"P1DIF1AR0MA1\"), |
---|
48 | Diario, |
---|
49 | 30, |
---|
50 | 10 |
---|
51 | );", |
---|
52 | DinamicPartAutoCor); |
---|
53 | ////////////////////////////////////////////////////////////////////////////// |
---|
54 | |
---|
55 | |
---|
56 | ////////////////////////////////////////////////////////////////////////////// |
---|
57 | Set GetDistACF(Serie serie, Real seasonality, TimeSet tms, Real times, |
---|
58 | Real interval) |
---|
59 | ////////////////////////////////////////////////////////////////////////////// |
---|
60 | { |
---|
61 | Matrix m1 = DinamicAutoCor( serie,tms,times,interval); |
---|
62 | Matrix m2 = DinamicPartAutoCor(serie,tms,times,interval); |
---|
63 | |
---|
64 | Set AR1 = SetHistCon(MatSet(SubRow(m1, [[ 1 ]]))[1],20); |
---|
65 | Set AR2 = SetHistCon(MatSet(SubRow(m1, [[ 2 ]]))[1],20); |
---|
66 | Set MAP = SetHistCon(MatSet(SubRow(m2, [[ seasonality ]]))[1],20); |
---|
67 | |
---|
68 | Set Eval("Set GetDistACF."+Name(serie)+" = [[ |
---|
69 | AR1, |
---|
70 | AR2, |
---|
71 | Set Eval(\"Set MA"+IntText(seasonality)+" = MAP;\") |
---|
72 | ]];") |
---|
73 | }; |
---|
74 | ////////////////////////////////////////////////////////////////////////////// |
---|
75 | PutDescription("Calcula las funciones de distribución de las funciones de |
---|
76 | autocorrelación simple y parcial dinámicamente en el tiempo. |
---|
77 | Es muy útil para detectar outputs con estructura y raíces arima variantes en |
---|
78 | el tiempo.", |
---|
79 | GetDistACF); |
---|
80 | ////////////////////////////////////////////////////////////////////////////// |
---|
81 | |
---|
82 | |
---|
83 | ////////////////////////////////////////////////////////////////////////////// |
---|
84 | Real Densidad(Serie serie, Date ini, Date fin) |
---|
85 | ////////////////////////////////////////////////////////////////////////////// |
---|
86 | { |
---|
87 | Real datos = Card(DatesInd(serie)); |
---|
88 | Real fechas = Card(Dates(Dating(serie),ini,fin)); |
---|
89 | Real datos/fechas |
---|
90 | }; |
---|
91 | ////////////////////////////////////////////////////////////////////////////// |
---|
92 | PutDescription("Devuelve la densidad de una serie entre dos fechas, sin tener |
---|
93 | en cuenta a los ceros como datos", |
---|
94 | Densidad); |
---|
95 | ////////////////////////////////////////////////////////////////////////////// |
---|
96 | |
---|
97 | |
---|
98 | ////////////////////////////////////////////////////////////////////////////// |
---|
99 | Real Densidad.0(Serie serie, Date ini, Date fin) |
---|
100 | ////////////////////////////////////////////////////////////////////////////// |
---|
101 | { |
---|
102 | Real datos = Card(DatesIndComplet(serie)); |
---|
103 | Real fechas = Card(Dates(Dating(serie),ini,fin)); |
---|
104 | Real datos/fechas |
---|
105 | }; |
---|
106 | ////////////////////////////////////////////////////////////////////////////// |
---|
107 | PutDescription("Devuelve la densidad de una serie entre dos fechas, teniendo |
---|
108 | en cuenta a los ceros como datos", |
---|
109 | Densidad.0); |
---|
110 | ////////////////////////////////////////////////////////////////////////////// |
---|
111 | |
---|
112 | |
---|
113 | ////////////////////////////////////////////////////////////////////////////// |
---|
114 | Set Analisis.Densidad(Serie serie, Date ini, Date fin) |
---|
115 | ////////////////////////////////////////////////////////////////////////////// |
---|
116 | { |
---|
117 | Real densidad = Densidad( serie,ini,fin); |
---|
118 | Real densidad.0 = Densidad.0(serie,ini,fin); |
---|
119 | |
---|
120 | Set Eval("Set _.densidad."+Name(serie)+" = [[ densidad, densidad.0 ]];") |
---|
121 | }; |
---|
122 | ////////////////////////////////////////////////////////////////////////////// |
---|
123 | PutDescription("Analiza las densidades de un set de series", |
---|
124 | Analisis.Densidad); |
---|
125 | ////////////////////////////////////////////////////////////////////////////// |
---|
126 | |
---|
127 | |
---|
128 | ////////////////////////////////////////////////////////////////////////////// |
---|
129 | Set Densidad.Dif(Serie serie, Date ini, Date fin, Set diferencias) |
---|
130 | ////////////////////////////////////////////////////////////////////////////// |
---|
131 | {//Set diferencias = For(1,14,Polyn (Real j){ 1-B^j }); |
---|
132 | |
---|
133 | Set EvalSet(diferencias,Real (Polyn p) |
---|
134 | { |
---|
135 | Real If(LE(Card(DatesIndComplet(serie)),Degree(p)), |
---|
136 | { |
---|
137 | Real densDif = Densidad(serie, ini, fin); |
---|
138 | Real Eval("Real densidadDif."+IntText(Degree(p))+" = densDif;") |
---|
139 | }, |
---|
140 | { |
---|
141 | Real densDif = Densidad(p:serie, ini, fin); |
---|
142 | Real Eval("Real densidadDif."+IntText(Degree(p))+" = densDif;") |
---|
143 | }) |
---|
144 | }) |
---|
145 | }; |
---|
146 | ////////////////////////////////////////////////////////////////////////////// |
---|
147 | PutDescription("Devuelve un set con las densidades de las diferencias aplica"+ |
---|
148 | "das. Ejemplo: |
---|
149 | |
---|
150 | Serie serie = GenSerie.Normal(500,\"P1DIF1AR0MA1\"); |
---|
151 | Set Analisis.Densidad.Dif( |
---|
152 | serie, |
---|
153 | First(serie), |
---|
154 | Last(serie), |
---|
155 | Set For(1,14,Polyn (Real j){ Polyn 1-B^j }) |
---|
156 | );", |
---|
157 | Densidad.Dif); |
---|
158 | ////////////////////////////////////////////////////////////////////////////// |
---|
159 | |
---|
160 | |
---|
161 | ////////////////////////////////////////////////////////////////////////////// |
---|
162 | Set Analisis.Densidad.Dif(Serie serie, Date ini, Date fin, Set diferencias) |
---|
163 | ////////////////////////////////////////////////////////////////////////////// |
---|
164 | { |
---|
165 | Set densidades = Densidad.Dif(serie, ini, fin, diferencias); |
---|
166 | // Miro si alguna densidad es cero |
---|
167 | Real fix = If(SetProd(densidades),False,True); |
---|
168 | |
---|
169 | Real con = 0; |
---|
170 | // Si alguna densidad es cero, saco cual es |
---|
171 | Real pol = If(Not(fix),0, |
---|
172 | Real Select(densidades,Real (Real r) |
---|
173 | { |
---|
174 | Real con := con + 1; |
---|
175 | Real If(r,False,con) |
---|
176 | })[1] |
---|
177 | ); |
---|
178 | |
---|
179 | Real min = MinS(serie); |
---|
180 | Real max = MaxS(serie); |
---|
181 | |
---|
182 | Real m = If(fix,pol,Round(SetAvr(densidades),4)); |
---|
183 | Real s = If(fix,?, Round(SetStDs(densidades),4)); |
---|
184 | Real k = If(fix,?, Round(SetKurtosis(densidades),4)); |
---|
185 | |
---|
186 | Set Eval("Set _.densidad.Dif."+Name(serie)+" = [[ min,max,m,s,k ]];") |
---|
187 | }; |
---|
188 | ////////////////////////////////////////////////////////////////////////////// |
---|
189 | PutDescription("Analiza las densidades de las diferencias aplicadas a un Set |
---|
190 | de series y devuelve para cada serie: minimo, máximo, numero de datos, media |
---|
191 | de las distintas densidades de direncia, sigma y kurtosis. Si alguna densidad |
---|
192 | de diferencia es cero, entonces devuelve como media el orden de la diferencia |
---|
193 | que anula a la serie. |
---|
194 | |
---|
195 | Es útil para detectar sevicios fijos y veitar errores de Marquardt en series |
---|
196 | planas", |
---|
197 | Analisis.Densidad.Dif); |
---|
198 | ////////////////////////////////////////////////////////////////////////////// |
---|
199 | |
---|
200 | |
---|
201 | ////////////////////////////////////////////////////////////////////////////// |
---|
202 | Real Analisis.Nulo(Serie serie, Date ini, Date fin) |
---|
203 | ////////////////////////////////////////////////////////////////////////////// |
---|
204 | { |
---|
205 | Serie serie2 = SubSer(serie,ini,fin); |
---|
206 | Real count = Card(DatesIndComplet(serie2)); |
---|
207 | Real sNula = If(EQ(count,0),True,False); |
---|
208 | |
---|
209 | Real Eval("Real _.Nulo."+Name(serie)+" = sNula;") |
---|
210 | }; |
---|
211 | ////////////////////////////////////////////////////////////////////////////// |
---|
212 | PutDescription("Analiza las series que son enteras de nulos y devuelve |
---|
213 | verdadero en el caso que lo sean", |
---|
214 | Analisis.Nulo); |
---|
215 | ////////////////////////////////////////////////////////////////////////////// |
---|
216 | |
---|
217 | |
---|
218 | ////////////////////////////////////////////////////////////////////////////// |
---|
219 | Set Analisis.Omitidos(Serie serie, Date ini, Date fin) |
---|
220 | ////////////////////////////////////////////////////////////////////////////// |
---|
221 | { |
---|
222 | Serie serie2 = SubSer(serie,ini,fin); |
---|
223 | Real nDias = Card(Dates(Dating(serie),ini,fin)); |
---|
224 | Real nDatos = Card(DatesIndComplet(serie2)); |
---|
225 | Real nOmitidos = nDias - nDatos; |
---|
226 | |
---|
227 | Date ini2 = Max(First(serie2),Succ(fin,Dating(serie),-7*4)); |
---|
228 | // para que dé 1 la densidad de omitidos. Es para evitar el error cuando |
---|
229 | // ambas fechas coinciden |
---|
230 | Real k = If(ini2>=Last(serie2),1,0); |
---|
231 | |
---|
232 | Serie serie3 = SubSer(serie2,ini2,fin); |
---|
233 | |
---|
234 | Real nDiasFin = If(k,1,Card(Dates(Dating(serie),First(serie3),fin))); |
---|
235 | Real nDatosFin = If(k,0,Card(DatesIndComplet(serie3))); |
---|
236 | Real nOmitidosFin = If(k,1,nDiasFin - nDatosFin); |
---|
237 | |
---|
238 | Set Eval("Set _.Omitidos."+Name(serie)+" = [[ |
---|
239 | nOmitidos, |
---|
240 | nDias, |
---|
241 | nOmitidosFin, |
---|
242 | nDiasFin |
---|
243 | ]];") |
---|
244 | }; |
---|
245 | ////////////////////////////////////////////////////////////////////////////// |
---|
246 | PutDescription("Analiza el número de omitidos de un set de series", |
---|
247 | Analisis.Omitidos); |
---|
248 | ////////////////////////////////////////////////////////////////////////////// |
---|
249 | |
---|
250 | |
---|
251 | ////////////////////////////////////////////////////////////////////////////// |
---|
252 | Set Analisis.NivelMed(Serie serie, Date ini, Date fin) |
---|
253 | ////////////////////////////////////////////////////////////////////////////// |
---|
254 | { |
---|
255 | Serie serie2 = SubSer(serie,ini,fin); |
---|
256 | |
---|
257 | Real venta = SumS(serie2); |
---|
258 | Real cont = Card(Dates(Dating(serie),ini,fin)); |
---|
259 | Real datos = Card(DatesIndComplet(serie2)); |
---|
260 | |
---|
261 | Real medTot = Round(venta/cont,4); |
---|
262 | Real medDat = Round(venta/datos,4); |
---|
263 | |
---|
264 | Set Eval("Set _.NivelMed."+Name(serie)+" = [[ medDat,medTot ]];") |
---|
265 | }; |
---|
266 | ////////////////////////////////////////////////////////////////////////////// |
---|
267 | PutDescription("Analiza la venta media de un set de series entre dos fechas. |
---|
268 | Devuelve primero la media en su fechado no nulo y segundo la media en el |
---|
269 | total del fechado", |
---|
270 | Analisis.NivelMed); |
---|
271 | ////////////////////////////////////////////////////////////////////////////// |
---|
272 | |
---|
273 | |
---|
274 | ////////////////////////////////////////////////////////////////////////////// |
---|
275 | // END |
---|
276 | ////////////////////////////////////////////////////////////////////////////// |
---|