| 1 | #Require MMS; |
|---|
| 2 | Real PutRandomSeed(2143); |
|---|
| 3 | |
|---|
| 4 | Date begin0 = y1989m12; |
|---|
| 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 = SubSer(DifEq(1/(1-phi*B), residuals, 0), begin, end); |
|---|
| 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 | Real _.sigma = 0.5; |
|---|
| 45 | Real _.sigmaFixed = 1 |
|---|
| 46 | ]] |
|---|
| 47 | ]]); |
|---|
| 48 | |
|---|
| 49 | Anything submodel::CreateExpTerm_Coefficient([[ |
|---|
| 50 | Text _.name = "ExpTerm1"; |
|---|
| 51 | NameBlock _.input = [[ |
|---|
| 52 | Text _.name = "input1"; |
|---|
| 53 | Text _.variable = "input1" |
|---|
| 54 | ]]; |
|---|
| 55 | Real _.coefficient = 0.1 |
|---|
| 56 | ]]); |
|---|
| 57 | |
|---|
| 58 | Anything submodel::CreateExpTerm_Coefficient([[ |
|---|
| 59 | Text _.name = "ExpTerm2"; |
|---|
| 60 | NameBlock _.input = [[ |
|---|
| 61 | Text _.name = "input2"; |
|---|
| 62 | Text _.variable = "input2" |
|---|
| 63 | ]]; |
|---|
| 64 | Real _.coefficient = -0.1 |
|---|
| 65 | ]]); |
|---|
| 66 | |
|---|
| 67 | MMS::@Estimation estimation = MMS::Container::ReplaceEstimation([[ |
|---|
| 68 | Text _.name = "Estimation1a"; |
|---|
| 69 | MMS::@Model _.model = model; |
|---|
| 70 | MMS::@SettingsBSR _.settings = [[ |
|---|
| 71 | Real _.showTraces = False; |
|---|
| 72 | Real mcmc.burnin = 100; |
|---|
| 73 | Real mcmc.sampleLength = 500 |
|---|
| 74 | ]] |
|---|
| 75 | ]]); |
|---|
| 76 | |
|---|
| 77 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 78 | |
|---|
| 79 | MMS::@Model model2 = model::Duplicate(?); |
|---|
| 80 | |
|---|
| 81 | Real model2::GetSubmodel(1)::SetNoise([[ |
|---|
| 82 | Text _.type = "ARIMA"; |
|---|
| 83 | Text _.arimaLabel = "P1DIF0AR1MA0"; |
|---|
| 84 | Real _.sigma = 0.5; |
|---|
| 85 | Real _.sigmaFixed = 1 |
|---|
| 86 | ]]); |
|---|
| 87 | |
|---|
| 88 | MMS::@Noise noise2 = model2::GetSubmodel(1)::GetNoise(?); |
|---|
| 89 | |
|---|
| 90 | MMS::@Parameter ar = noise2::GetARIMABlock(1)::GetParameterARIMA(1); |
|---|
| 91 | Real ar::SetInitialValue(0.5); |
|---|
| 92 | Real ar::SetIsFixed(1); |
|---|
| 93 | |
|---|
| 94 | MMS::@Estimation estimation2 = MMS::Container::ReplaceEstimation([[ |
|---|
| 95 | Text _.name = "Estimation1b"; |
|---|
| 96 | MMS::@Model _.model = model2; |
|---|
| 97 | MMS::@SettingsBSR _.settings = [[ |
|---|
| 98 | Real _.showTraces = False; |
|---|
| 99 | Real mcmc.burnin = 100; |
|---|
| 100 | Real mcmc.sampleLength = 500 |
|---|
| 101 | ]] |
|---|
| 102 | ]]); |
|---|
| 103 | |
|---|
| 104 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 105 | |
|---|
| 106 | MMS::@Model model3 = model::Duplicate(?); |
|---|
| 107 | |
|---|
| 108 | Real size = CountS(output_); |
|---|
| 109 | Polyn pAr = Expand(1/(1-phi*B), size+100); |
|---|
| 110 | Matrix cov1 = Tra(PolMat(pAr,size,size)) * PolMat(pAr,size,size); |
|---|
| 111 | Matrix cov2 = PolMat(pAr,size,size) * Tra(PolMat(pAr,size,size)); |
|---|
| 112 | Matrix cov3 = Tra(PolMat(pAr,size+100,size)) * PolMat(pAr,size+100,size); |
|---|
| 113 | |
|---|
| 114 | Real model3::GetSubmodel(1)::SetNoise([[ |
|---|
| 115 | Text _.type = "Normal"; |
|---|
| 116 | Matrix _.relativeCovariance = cov3; |
|---|
| 117 | Real _.sigma = 0.5; |
|---|
| 118 | Real _.sigmaFixed = 1 |
|---|
| 119 | ]]); |
|---|
| 120 | |
|---|
| 121 | MMS::@Estimation estimation3 = MMS::Container::ReplaceEstimation([[ |
|---|
| 122 | Text _.name = "Estimation1c"; |
|---|
| 123 | MMS::@Model _.model = model3; |
|---|
| 124 | MMS::@SettingsBSR _.settings = [[ |
|---|
| 125 | Real _.showTraces = False; |
|---|
| 126 | Real mcmc.burnin = 100; |
|---|
| 127 | Real mcmc.sampleLength = 500 |
|---|
| 128 | ]] |
|---|
| 129 | ]]); |
|---|
| 130 | |
|---|
| 131 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 132 | |
|---|
| 133 | Real ShowResults(MMS::@Estimation est) { |
|---|
| 134 | Set EvalSet(est::GetParameters(?), Real (Anything r) { |
|---|
| 135 | If(Grammar(r)=="Real", |
|---|
| 136 | WriteLn(Name(r)<<" "<<r), |
|---|
| 137 | WriteLn(Name(r)<<" "<<r::GetMean(?)) ); |
|---|
| 138 | 1}); |
|---|
| 139 | 1}; |
|---|
| 140 | |
|---|
| 141 | Real PutRandomSeed(Time); |
|---|
| 142 | |
|---|
| 143 | Real PutRandomSeed(2143); |
|---|
| 144 | Real estimation::Execute(?); |
|---|
| 145 | Real PutRandomSeed(2143); |
|---|
| 146 | Real estimation2::Execute(?); |
|---|
| 147 | Real PutRandomSeed(2143); |
|---|
| 148 | Real estimation3::Execute(?); |
|---|
| 149 | |
|---|
| 150 | Real ShowResults(estimation); |
|---|
| 151 | Real ShowResults(estimation2); |
|---|
| 152 | Real ShowResults(estimation3); |
|---|
| 153 | |
|---|
| 154 | |
|---|