| 1 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // File: ejemplo1.tol |
|---|
| 3 | // Purpuse: ejemplo inventado de un modelo mixto |
|---|
| 4 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 5 | |
|---|
| 6 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 7 | #Require MMS; |
|---|
| 8 | |
|---|
| 9 | // Real PutRandomSeed(190463); |
|---|
| 10 | // Real PutRandomSeed(514809833); |
|---|
| 11 | Real PutRandomSeed(1528068215); |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | // Definimos las series de las observaciones y las series que entraran |
|---|
| 15 | // tanto como inputs aditivos como multiplicativos. |
|---|
| 16 | |
|---|
| 17 | Date begin = y2000m01; |
|---|
| 18 | Date end = y2012m08; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | Polyn ma = 1-0.3*B^12; |
|---|
| 22 | Polyn ar = 1-0.6*B; |
|---|
| 23 | Polyn dif = (1-B^12); |
|---|
| 24 | Real initLength = 100*Max(Degree(ar)+ Degree(dif), Degree(ma)); |
|---|
| 25 | Date begin_ = Succ(begin, Monthly, -initLength); |
|---|
| 26 | Serie R0 = SubSer(Gaussian(0, 0.2, Monthly), begin_, end); |
|---|
| 27 | Serie N0 = DifEq(ma/(dif*ar), R0); |
|---|
| 28 | Serie R = SubSer(R0, begin, end); |
|---|
| 29 | Serie N = SubSer(N0, begin, end)+4; |
|---|
| 30 | |
|---|
| 31 | // X1 ~ N(10, 2) |
|---|
| 32 | Real beta1 = 2; |
|---|
| 33 | Serie X1 = SubSer(Gaussian(3, 1, Monthly), begin, end); |
|---|
| 34 | |
|---|
| 35 | // X2 ~ N(5, 4) |
|---|
| 36 | Real beta2 = 0.2; |
|---|
| 37 | Serie X2_ = SubSer(Gaussian(5, 0.4, Monthly), begin, end); |
|---|
| 38 | Serie X2 = DifEq((1)/(1-0.3*B), X2_, N0); |
|---|
| 39 | |
|---|
| 40 | // X3 ~ Festivos |
|---|
| 41 | TimeSet CtFes.es = |
|---|
| 42 | M(1)*D(1) + // Año nuevo |
|---|
| 43 | M(1)*D(6) + // Epifania (no siempre es FasNac) |
|---|
| 44 | Succ(Easter, -2, C) + // Viernes Santo |
|---|
| 45 | M(5)*D(1) + // Dia del trabajador |
|---|
| 46 | M(8)*D(15) + // Virgen de Agosto |
|---|
| 47 | M(10)*D(12) + // Dia del Pilar |
|---|
| 48 | M(11)*D(1) + // Todos los Santos |
|---|
| 49 | M(12)*D(6) + // Constitucion |
|---|
| 50 | M(12)*D(8) + // Inmaculada |
|---|
| 51 | M(12)*D(25) + // Navidad |
|---|
| 52 | WD(7) + WD(6) |
|---|
| 53 | ; |
|---|
| 54 | |
|---|
| 55 | Real beta3 = 0.20; |
|---|
| 56 | Serie X3 = SubSer(CalInd(CtFes.es, Monthly), begin, end); |
|---|
| 57 | |
|---|
| 58 | Serie additive = beta1*X1; |
|---|
| 59 | Serie multiplicative = N + beta2*X2 + beta3*X3; |
|---|
| 60 | Serie observation = Exp(multiplicative)+additive; |
|---|
| 61 | |
|---|
| 62 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 67 | // DATASET: Conjunto de variables |
|---|
| 68 | |
|---|
| 69 | MMS::@DataSet DS = MMS::Container::ReplaceDataSet([[ |
|---|
| 70 | Text _.name = "Mixto_DataSet" |
|---|
| 71 | ]]); |
|---|
| 72 | |
|---|
| 73 | // ReplaceVariable |
|---|
| 74 | |
|---|
| 75 | Anything DS::ReplaceVariable([[ |
|---|
| 76 | Text _.name = "Observation"; |
|---|
| 77 | Text _.description = "Variable de las observaciones"; |
|---|
| 78 | Text _.expression = "Serie observation"; |
|---|
| 79 | Set _.tags = SetOfText ("Output") |
|---|
| 80 | ]]); |
|---|
| 81 | |
|---|
| 82 | Anything DS::ReplaceVariable([[ |
|---|
| 83 | Text _.name = "Input1"; |
|---|
| 84 | Text _.description = "Variable Input1"; |
|---|
| 85 | Text _.expression = "Serie X1"; |
|---|
| 86 | Set _.tags = SetOfText ("Input", "additive") |
|---|
| 87 | ]]); |
|---|
| 88 | |
|---|
| 89 | Anything DS::ReplaceVariable([[ |
|---|
| 90 | Text _.name = "Input2"; |
|---|
| 91 | Text _.description = "Variable Input2"; |
|---|
| 92 | Text _.expression = "Serie X2"; |
|---|
| 93 | Set _.tags = SetOfText ("Input", "multiplicative") |
|---|
| 94 | ]]); |
|---|
| 95 | |
|---|
| 96 | Anything DS::ReplaceVariable([[ |
|---|
| 97 | Text _.name = "Input3"; |
|---|
| 98 | Text _.description = "Variable Input3"; |
|---|
| 99 | Text _.expression = "Serie X3"; |
|---|
| 100 | Set _.tags = SetOfText ("Input", "multiplicative") |
|---|
| 101 | ]]); |
|---|
| 102 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 107 | // MODEL: Modelo y Submodelo: TerminosExplicativos+ Noise |
|---|
| 108 | |
|---|
| 109 | // CreateModel |
|---|
| 110 | MMS::@Model model = MMS::Container::ReplaceModel([[ |
|---|
| 111 | Text _.name = "Mixto_Model"; |
|---|
| 112 | Text _.description = "Modelo mixto: aditivo y multiplicativo"; |
|---|
| 113 | Set _.dataSets = [[ "Mixto_DataSet" ]] |
|---|
| 114 | ]]); |
|---|
| 115 | |
|---|
| 116 | // CreateSubmodel |
|---|
| 117 | MMS::@Submodel submodel = model::CreateSubmodel([[ |
|---|
| 118 | Text _.name = "Mixto_Submodel"; |
|---|
| 119 | Text _.descripton = "Submodelo del ejemplo de modelo mixto"; |
|---|
| 120 | NameBlock _.output = [[ |
|---|
| 121 | Text _.name = "Output"; |
|---|
| 122 | Text _.variable = "Observation"; |
|---|
| 123 | Text _.transformationLabel = "BoxCox_0_0" |
|---|
| 124 | ]]; |
|---|
| 125 | NameBlock _.noise = [[ |
|---|
| 126 | Text _.type = "ARIMA"; |
|---|
| 127 | Text _.arimaLabel = "P1_12DIF0_1AR1_0MA0_12"; |
|---|
| 128 | Real _.sigma = 1 |
|---|
| 129 | ]] |
|---|
| 130 | ]]); |
|---|
| 131 | |
|---|
| 132 | // Para crear el input aditivo, simplemente tenemos que poner el |
|---|
| 133 | // atributo de la clase término explicativo: |
|---|
| 134 | // |
|---|
| 135 | // Real _.isAdditive = 1 (True) |
|---|
| 136 | // |
|---|
| 137 | // Por defecto, este atributo está con valor 0 (False) |
|---|
| 138 | // Observacion |
|---|
| 139 | // el polinomio de la función de transferencia no podemos introducir un 0 |
|---|
| 140 | // porque no lo toma como polinomio constante 0, lo toma como que no |
|---|
| 141 | // existe polinomio. |
|---|
| 142 | |
|---|
| 143 | // CreateExpTerm_TransferFunction |
|---|
| 144 | Set EvalSet(Select( |
|---|
| 145 | model::GetDataSet(?)::GetVariables(?), Real(NameBlock var){ |
|---|
| 146 | "Input" <: var::GetTags(?) |
|---|
| 147 | }), |
|---|
| 148 | Anything (NameBlock varInput){ |
|---|
| 149 | submodel::CreateExpTerm_TransferFunction([[ |
|---|
| 150 | Text _.name = varInput::GetName(?); |
|---|
| 151 | Real _.isAdditive = If("additive" <: varInput::GetTags(?), 1, 0); |
|---|
| 152 | NameBlock _.input = [[ |
|---|
| 153 | Text _.name = varInput::GetName(?); |
|---|
| 154 | Text _.variable = varInput::GetName(?) |
|---|
| 155 | ]]; |
|---|
| 156 | Polyn _.transferFunction = 0.1 |
|---|
| 157 | ]]) |
|---|
| 158 | }); |
|---|
| 159 | |
|---|
| 160 | // Restricción del término aditivo |
|---|
| 161 | /* |
|---|
| 162 | Real model::GetParameter("Mixto_Submodel__Input1__Linear.0") |
|---|
| 163 | ::SetPrior([[ |
|---|
| 164 | Real _.mean = 2; |
|---|
| 165 | Real _.sigma = 0.1 |
|---|
| 166 | ]]); |
|---|
| 167 | */ |
|---|
| 168 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 173 | // ESTIMATION: Estimación MLE |
|---|
| 174 | MMS::@Estimation estimationMLE= MMS::Container::ReplaceEstimation([[ |
|---|
| 175 | Text _.name = "Mixto_Estimate"; |
|---|
| 176 | MMS::@Model _.model = MMS::Container::GetModel("Mixto_Model"); |
|---|
| 177 | MMS::@SettingsMultiMLE _.settings = [[ |
|---|
| 178 | Real _.showTraces = False // Para que no muestre la traza |
|---|
| 179 | ]] |
|---|
| 180 | ]]); |
|---|
| 181 | |
|---|
| 182 | // Ejecución de la estimación: |
|---|
| 183 | Real estimationMLE::Execute(?); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | // ESTIMATION: Estimación BSR |
|---|
| 187 | MMS::@Estimation estimationBSR = MMS::Container::ReplaceEstimation([[ |
|---|
| 188 | Text _.name = "Mixto_EstimateBSR"; |
|---|
| 189 | MMS::@Model _.model = MMS::Container::GetModel("Mixto_Model"); |
|---|
| 190 | MMS::@SettingsBSR _.settings = [[ |
|---|
| 191 | Real mcmc.sampleLength = 500; // tamaño de la muestra |
|---|
| 192 | Real do.report = True |
|---|
| 193 | ]] |
|---|
| 194 | ]]); |
|---|
| 195 | |
|---|
| 196 | // Ejecución de la estimación: |
|---|
| 197 | Real estimationBSR::Execute(?); |
|---|
| 198 | |
|---|
| 199 | // ESTIMATION: Estimación BSR |
|---|
| 200 | MMS::@Estimation estimationBSR2 = MMS::Container::ReplaceEstimation([[ |
|---|
| 201 | Text _.name = "Mixto_EstimateBSR2"; |
|---|
| 202 | MMS::@Model _.model = MMS::Container::GetModel("Mixto_Model"); |
|---|
| 203 | MMS::@SettingsBSR _.settings = [[ |
|---|
| 204 | Real _.additiveApproximation = 0; |
|---|
| 205 | Real mcmc.sampleLength = 500; // tamaño de la muestra |
|---|
| 206 | Real do.report = True |
|---|
| 207 | ]] |
|---|
| 208 | ]]); |
|---|
| 209 | |
|---|
| 210 | // Ejecución de la estimación: |
|---|
| 211 | Real estimationBSR2::Execute(?); |
|---|
| 212 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 213 | |
|---|