1 | #Require MMS; |
---|
2 | Real PutRandomSeed(2143); |
---|
3 | |
---|
4 | Date begin = y2000; |
---|
5 | Date end = y2012m12; |
---|
6 | Serie noise = SubSer(Gaussian(0, 0.5, Monthly), begin, end); |
---|
7 | Serie input1_ = SubSer(Rand(-1, 1, Monthly), begin, end); |
---|
8 | Serie input2_ = SubSer(Floor(Rand(0, 2, Monthly)), begin, end); |
---|
9 | Real beta1 = 0.4; |
---|
10 | Real beta2 = -0.2; |
---|
11 | Serie output_ = noise + beta1*input1_ + beta2*input2_; |
---|
12 | |
---|
13 | Matrix m = ((1,1), (2, -1)); |
---|
14 | Matrix mn = Col(0, 1); |
---|
15 | Matrix mmI = GaussInverse(Tra(m)*m); |
---|
16 | Real ms = 0.1; |
---|
17 | Matrix mn2 = mmI * Tra(m) * mn; |
---|
18 | |
---|
19 | MMS::@DataSet dataSet = MMS::Container::ReplaceDataSet([[ |
---|
20 | Text _.name = "DataSet" |
---|
21 | ]]); |
---|
22 | Anything dataSet::CreateVariable_Data(Serie { |
---|
23 | Serie output = Copy(output_) |
---|
24 | }); |
---|
25 | Anything dataSet::CreateVariable_Data(Serie { |
---|
26 | Serie input1 = Copy(input1_) |
---|
27 | }); |
---|
28 | Anything dataSet::CreateVariable_Data(Serie { |
---|
29 | Serie input2 = Copy(input2_) |
---|
30 | }); |
---|
31 | |
---|
32 | ////////////////////////////////////////////////////////////////////////////// |
---|
33 | |
---|
34 | MMS::@Model model = MMS::Container::ReplaceModel([[ |
---|
35 | Text _.name = "Model1"; |
---|
36 | MMS::@DataSet _.dataSet = dataSet |
---|
37 | ]]); |
---|
38 | |
---|
39 | MMS::@Submodel submodel = model::CreateSubmodel([[ |
---|
40 | Text _.name = "Submodel"; |
---|
41 | NameBlock _.output = [[ |
---|
42 | Text _.name = "output"; |
---|
43 | Text _.variable = "output" |
---|
44 | ]]; |
---|
45 | NameBlock _.noise = [[ |
---|
46 | Text _.type = "Normal" |
---|
47 | ]] |
---|
48 | ]]); |
---|
49 | |
---|
50 | Anything submodel::CreateExpTerm_Coefficient([[ |
---|
51 | Text _.name = "ExpTerm1"; |
---|
52 | NameBlock _.input = [[ |
---|
53 | Text _.name = "input1"; |
---|
54 | Text _.variable = "input1" |
---|
55 | ]]; |
---|
56 | Real _.coefficient = 0.1 |
---|
57 | ]]); |
---|
58 | |
---|
59 | Anything submodel::CreateExpTerm_Coefficient([[ |
---|
60 | Text _.name = "ExpTerm2"; |
---|
61 | NameBlock _.input = [[ |
---|
62 | Text _.name = "input2"; |
---|
63 | Text _.variable = "input2" |
---|
64 | ]]; |
---|
65 | Real _.coefficient = -0.1 |
---|
66 | ]]); |
---|
67 | |
---|
68 | MMS::@Estimation estimation = MMS::Container::ReplaceEstimation([[ |
---|
69 | Text _.name = "Estimation1a"; |
---|
70 | MMS::@Model _.model = model; |
---|
71 | MMS::@SettingsBSR _.settings = [[ |
---|
72 | Real _.showTraces = False; |
---|
73 | Real mcmc.burnin = 100; |
---|
74 | Real mcmc.sampleLength = 500 |
---|
75 | ]] |
---|
76 | ]]); |
---|
77 | |
---|
78 | ////////////////////////////////////////////////////////////////////////////// |
---|
79 | |
---|
80 | MMS::@Model model2 = model::Duplicate(?); |
---|
81 | |
---|
82 | Anything model2::CreateMCombination([[ |
---|
83 | Text _.name = "Suma"; |
---|
84 | Set _.parameters = [[ |
---|
85 | "Submodel__ExpTerm1__Linear.0"; |
---|
86 | "Submodel__ExpTerm2__Linear.0" |
---|
87 | ]]; |
---|
88 | Set _.coefficients = [[MatDat(m,1,1), MatDat(m,1,2)]]; |
---|
89 | NameBlock _.prior = [[ |
---|
90 | Real _.mean = MatDat(mn,1,1); |
---|
91 | Real _.sigma = ms |
---|
92 | ]] |
---|
93 | ]]); |
---|
94 | Anything model2::CreateMCombination([[ |
---|
95 | Text _.name = "Diferencia"; |
---|
96 | Set _.parameters = [[ |
---|
97 | "Submodel__ExpTerm1__Linear.0"; |
---|
98 | "Submodel__ExpTerm2__Linear.0" |
---|
99 | ]]; |
---|
100 | Set _.coefficients = [[MatDat(m,2,1), MatDat(m,2,2)]]; |
---|
101 | NameBlock _.prior = [[ |
---|
102 | Real _.mean = MatDat(mn,2,1); |
---|
103 | Real _.sigma = ms |
---|
104 | ]] |
---|
105 | ]]); |
---|
106 | |
---|
107 | MMS::@Estimation estimation2 = MMS::Container::ReplaceEstimation([[ |
---|
108 | Text _.name = "Estimation1b"; |
---|
109 | MMS::@Model _.model = model2; |
---|
110 | MMS::@SettingsBSR _.settings = [[ |
---|
111 | Real _.showTraces = False; |
---|
112 | Real mcmc.burnin = 100; |
---|
113 | Real mcmc.sampleLength = 500 |
---|
114 | ]] |
---|
115 | ]]); |
---|
116 | |
---|
117 | ////////////////////////////////////////////////////////////////////////////// |
---|
118 | |
---|
119 | MMS::@Model model3 = model::Duplicate(?); |
---|
120 | |
---|
121 | MMS::@Hierarchy hierarchy = model3::CreateHierarchy([[ |
---|
122 | Text _.name = "Hierarchy"; |
---|
123 | Set _.mElements = [[ |
---|
124 | "Submodel__ExpTerm1__Linear.0"; |
---|
125 | "Submodel__ExpTerm2__Linear.0" |
---|
126 | ]]; |
---|
127 | NameBlock _.noise = [[ |
---|
128 | Text _.type = "Normal"; |
---|
129 | Real _.sigma = ms; |
---|
130 | Real _.sigmaFixed = 1; |
---|
131 | Matrix _.relativeCovariance = mmI |
---|
132 | ]] |
---|
133 | ]]); |
---|
134 | Anything hierarchy::CreateHierarchyTerm([[ |
---|
135 | Text _.name = "Term"; |
---|
136 | Real _.initialParameter = 1; |
---|
137 | Real _.isFixed = 1; |
---|
138 | Set _.coefficients = [[ MatDat(mn2,1,1), MatDat(mn2,2,1) ]] |
---|
139 | ]]); |
---|
140 | |
---|
141 | MMS::@Estimation estimation3 = MMS::Container::ReplaceEstimation([[ |
---|
142 | Text _.name = "Estimation1c"; |
---|
143 | MMS::@Model _.model = model3; |
---|
144 | MMS::@SettingsBSR _.settings = [[ |
---|
145 | Real _.showTraces = False; |
---|
146 | Real mcmc.burnin = 100; |
---|
147 | Real mcmc.sampleLength = 500 |
---|
148 | ]] |
---|
149 | ]]); |
---|
150 | |
---|
151 | ////////////////////////////////////////////////////////////////////////////// |
---|
152 | |
---|
153 | MMS::@Model model4 = model::Duplicate(?); |
---|
154 | |
---|
155 | Anything model4::CreateMCombination([[ |
---|
156 | Text _.name = "Suma"; |
---|
157 | Set _.parameters = [[ |
---|
158 | "Submodel__ExpTerm1__Linear.0"; |
---|
159 | "Submodel__ExpTerm2__Linear.0" |
---|
160 | ]]; |
---|
161 | Set _.coefficients = [[MatDat(m,1,1), MatDat(m,1,2)]] |
---|
162 | ]]); |
---|
163 | Anything model4::CreateMCombination([[ |
---|
164 | Text _.name = "Diferencia"; |
---|
165 | Set _.parameters = [[ |
---|
166 | "Submodel__ExpTerm1__Linear.0"; |
---|
167 | "Submodel__ExpTerm2__Linear.0" |
---|
168 | ]]; |
---|
169 | Set _.coefficients = [[MatDat(m,2,1), MatDat(m,2,2)]] |
---|
170 | ]]); |
---|
171 | |
---|
172 | MMS::@Hierarchy hierarchy2 = model4::CreateHierarchy([[ |
---|
173 | Text _.name = "Hierarchy"; |
---|
174 | Set _.mElements = [[ |
---|
175 | "Suma"; |
---|
176 | "Diferencia" |
---|
177 | ]]; |
---|
178 | NameBlock _.noise = [[ |
---|
179 | Text _.type = "Normal"; |
---|
180 | Real _.sigma = ms; |
---|
181 | Real _.sigmaFixed = 1 |
---|
182 | ]] |
---|
183 | ]]); |
---|
184 | Anything hierarchy2::CreateHierarchyTerm([[ |
---|
185 | Text _.name = "Term"; |
---|
186 | Real _.initialParameter = 1; |
---|
187 | Real _.isFixed = 1; |
---|
188 | Set _.coefficients = [[ MatDat(mn,1,1), MatDat(mn,2,1) ]] |
---|
189 | ]]); |
---|
190 | |
---|
191 | MMS::@Estimation estimation4 = MMS::Container::ReplaceEstimation([[ |
---|
192 | Text _.name = "Estimation1d"; |
---|
193 | MMS::@Model _.model = model4; |
---|
194 | MMS::@SettingsBSR _.settings = [[ |
---|
195 | Real _.showTraces = False; |
---|
196 | Real mcmc.burnin = 100; |
---|
197 | Real mcmc.sampleLength = 500 |
---|
198 | ]] |
---|
199 | ]]); |
---|
200 | |
---|
201 | ////////////////////////////////////////////////////////////////////////////// |
---|
202 | |
---|
203 | Real ShowResults(MMS::@Estimation est) { |
---|
204 | Set EvalSet(est::GetParameters(?), Real (Anything r) { |
---|
205 | If(Grammar(r)=="Real", |
---|
206 | WriteLn(Name(r)<<" "<<r), |
---|
207 | WriteLn(Name(r)<<" "<<r::GetMean(?)) ); |
---|
208 | 1}); |
---|
209 | 1}; |
---|
210 | |
---|
211 | |
---|
212 | Real PutRandomSeed(Time); |
---|
213 | |
---|
214 | Real PutRandomSeed(2143); |
---|
215 | Real estimation::Execute(?); |
---|
216 | Real PutRandomSeed(2143); |
---|
217 | Real estimation2::Execute(?); |
---|
218 | Real PutRandomSeed(2143); |
---|
219 | Real estimation3::Execute(?); |
---|
220 | Real PutRandomSeed(2143); |
---|
221 | Real estimation4::Execute(?); |
---|
222 | |
---|
223 | Real ShowResults(estimation); |
---|
224 | Real ShowResults(estimation2); |
---|
225 | Real ShowResults(estimation3); |
---|
226 | Real ShowResults(estimation4); |
---|
227 | |
---|