| 1 | #Require MMS; |
|---|
| 2 | Real PutRandomSeed(2143); |
|---|
| 3 | |
|---|
| 4 | Date begin0 = y1999m12; |
|---|
| 5 | Date begin = y2000; |
|---|
| 6 | Date end = y2012m12; |
|---|
| 7 | Real phi = 0.5; |
|---|
| 8 | Serie residuals = SubSer(Gaussian(0, 0.5, Monthly), begin0, end); |
|---|
| 9 | Serie noise = (1-phi*B):residuals; |
|---|
| 10 | Serie input1_ = SubSer(Rand(-1, 1, Monthly), begin, end); |
|---|
| 11 | Serie input2_ = SubSer(Floor(Rand(0, 2, Monthly)), begin, end); |
|---|
| 12 | Real beta1 = 0.4; |
|---|
| 13 | Real beta2 = -0.2; |
|---|
| 14 | Serie output_ = noise + beta1*input1_ + beta2*input2_; |
|---|
| 15 | |
|---|
| 16 | MMS::@DataSet dataSet = MMS::Container::ReplaceDataSet([[ |
|---|
| 17 | Text _.name = "DataSet" |
|---|
| 18 | ]]); |
|---|
| 19 | Anything dataSet::CreateVariable_Data(Serie { |
|---|
| 20 | Serie output = Copy(output_) |
|---|
| 21 | }); |
|---|
| 22 | Anything dataSet::CreateVariable_Data(Serie { |
|---|
| 23 | Serie input1 = Copy(input1_) |
|---|
| 24 | }); |
|---|
| 25 | Anything dataSet::CreateVariable_Data(Serie { |
|---|
| 26 | Serie input2 = Copy(input2_) |
|---|
| 27 | }); |
|---|
| 28 | |
|---|
| 29 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 30 | |
|---|
| 31 | MMS::@Model model = MMS::Container::ReplaceModel([[ |
|---|
| 32 | Text _.name = "Model1"; |
|---|
| 33 | MMS::@DataSet _.dataSet = dataSet |
|---|
| 34 | ]]); |
|---|
| 35 | |
|---|
| 36 | MMS::@Submodel submodel = model::CreateSubmodel([[ |
|---|
| 37 | Text _.name = "Submodel"; |
|---|
| 38 | NameBlock _.output = [[ |
|---|
| 39 | Text _.name = "output"; |
|---|
| 40 | Text _.variable = "output" |
|---|
| 41 | ]]; |
|---|
| 42 | NameBlock _.noise = [[ |
|---|
| 43 | Text _.type = "Normal" |
|---|
| 44 | ]] |
|---|
| 45 | ]]); |
|---|
| 46 | |
|---|
| 47 | Anything submodel::CreateExpTerm_Coefficient([[ |
|---|
| 48 | Text _.name = "ExpTerm1"; |
|---|
| 49 | NameBlock _.input = [[ |
|---|
| 50 | Text _.name = "input1"; |
|---|
| 51 | Text _.variable = "input1" |
|---|
| 52 | ]]; |
|---|
| 53 | Real _.coefficient = 0.1 |
|---|
| 54 | ]]); |
|---|
| 55 | |
|---|
| 56 | Anything submodel::CreateExpTerm_Coefficient([[ |
|---|
| 57 | Text _.name = "ExpTerm2"; |
|---|
| 58 | NameBlock _.input = [[ |
|---|
| 59 | Text _.name = "input2"; |
|---|
| 60 | Text _.variable = "input2" |
|---|
| 61 | ]]; |
|---|
| 62 | Real _.coefficient = -0.1 |
|---|
| 63 | ]]); |
|---|
| 64 | |
|---|
| 65 | MMS::@Estimation estimation = MMS::Container::ReplaceEstimation([[ |
|---|
| 66 | Text _.name = "Estimation1a"; |
|---|
| 67 | MMS::@Model _.model = model; |
|---|
| 68 | MMS::@SettingsBSR _.settings = [[ |
|---|
| 69 | Real _.showTraces = False; |
|---|
| 70 | Real mcmc.burnin = 100; |
|---|
| 71 | Real mcmc.sampleLength = 500 |
|---|
| 72 | ]] |
|---|
| 73 | ]]); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 77 | |
|---|
| 78 | MMS::@Model model2 = model::Duplicate(?); |
|---|
| 79 | |
|---|
| 80 | Real model2::GetSubmodel(1)::SetNoise([[ |
|---|
| 81 | Text _.type = "ARIMA"; |
|---|
| 82 | Text _.arimaLabel = "P1DIF0AR0MA1" |
|---|
| 83 | ]]); |
|---|
| 84 | |
|---|
| 85 | MMS::@Noise noise2 = model2::GetSubmodel(1)::GetNoise(?); |
|---|
| 86 | |
|---|
| 87 | MMS::@Parameter ma = noise2::GetARIMABlock(1)::GetParameterARIMA(1); |
|---|
| 88 | Real ma::SetInitialValue(0.5); |
|---|
| 89 | Real ma::SetIsFixed(1); |
|---|
| 90 | |
|---|
| 91 | MMS::@Estimation estimation2 = MMS::Container::ReplaceEstimation([[ |
|---|
| 92 | Text _.name = "Estimation1b"; |
|---|
| 93 | MMS::@Model _.model = model2; |
|---|
| 94 | MMS::@SettingsBSR _.settings = [[ |
|---|
| 95 | Real _.showTraces = False; |
|---|
| 96 | Real mcmc.burnin = 100; |
|---|
| 97 | Real mcmc.sampleLength = 500 |
|---|
| 98 | ]] |
|---|
| 99 | ]]); |
|---|
| 100 | |
|---|
| 101 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 102 | |
|---|
| 103 | MMS::@Model model3 = model::Duplicate(?); |
|---|
| 104 | |
|---|
| 105 | Real size = CountS(output_); |
|---|
| 106 | Polyn pMa = 1-phi*B; |
|---|
| 107 | Matrix cov1 = Tra(PolMat(pMa,size,size)) * PolMat(pMa,size,size); |
|---|
| 108 | Matrix cov2 = PolMat(pMa,size,size) * Tra(PolMat(pMa,size,size)); |
|---|
| 109 | Matrix cov3 = Tra(PolMat(pMa,size+1,size)) * PolMat(pMa,size+1,size); |
|---|
| 110 | |
|---|
| 111 | Real model3::GetSubmodel(1)::SetNoise([[ |
|---|
| 112 | Text _.type = "Normal"; |
|---|
| 113 | Matrix _.relativeCovariance = cov3 |
|---|
| 114 | ]]); |
|---|
| 115 | |
|---|
| 116 | MMS::@Estimation estimation3 = MMS::Container::ReplaceEstimation([[ |
|---|
| 117 | Text _.name = "Estimation1c"; |
|---|
| 118 | MMS::@Model _.model = model3; |
|---|
| 119 | MMS::@SettingsBSR _.settings = [[ |
|---|
| 120 | Real _.showTraces = False; |
|---|
| 121 | Real mcmc.burnin = 100; |
|---|
| 122 | Real mcmc.sampleLength = 500 |
|---|
| 123 | ]] |
|---|
| 124 | ]]); |
|---|
| 125 | |
|---|
| 126 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 127 | |
|---|
| 128 | Real ShowResults(MMS::@Estimation est) { |
|---|
| 129 | Set EvalSet(est::GetParameters(?), Real (Anything r) { |
|---|
| 130 | If(Grammar(r)=="Real", |
|---|
| 131 | WriteLn(Name(r)<<" "<<r), |
|---|
| 132 | WriteLn(Name(r)<<" "<<r::GetMean(?)) ); |
|---|
| 133 | 1}); |
|---|
| 134 | 1}; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | Real PutRandomSeed(Time); |
|---|
| 138 | |
|---|
| 139 | Real PutRandomSeed(2143); |
|---|
| 140 | Real estimation::Execute(?); |
|---|
| 141 | Real PutRandomSeed(2143); |
|---|
| 142 | Real estimation2::Execute(?); |
|---|
| 143 | Real PutRandomSeed(2143); |
|---|
| 144 | Real estimation3::Execute(?); |
|---|
| 145 | |
|---|
| 146 | Real ShowResults(estimation); |
|---|
| 147 | Real ShowResults(estimation2); |
|---|
| 148 | Real ShowResults(estimation3); |
|---|
| 149 | |
|---|