close Warning: Can't synchronize with repository "(default)" (/var/svn/mms does not appear to be a Subversion repository.). Look in the Trac log for more information.

Ticket #167: ej_cov.tol

File ej_cov.tol, 5.9 KB (added by Pedro Gea, 12 years ago)
Line 
1#Require MMS;
2Real PutRandomSeed(2143);
3
4Date begin = y2000;
5Date end = y2012m12;
6Serie noise = SubSer(Gaussian(0, 0.5, Monthly), begin, end);
7Serie input1_ = SubSer(Rand(-1, 1, Monthly), begin, end);
8Serie input2_ = SubSer(Floor(Rand(0, 2, Monthly)), begin, end);
9Real beta1 = 0.4;
10Real beta2 = -0.2;
11Serie output_ = noise + beta1*input1_ + beta2*input2_;
12
13Matrix m = ((1,1), (2, -1));
14Matrix mn = Col(0, 1);
15Matrix mmI =  GaussInverse(Tra(m)*m);
16Real ms = 0.1;
17Matrix mn2 = mmI * Tra(m) * mn;
18
19MMS::@DataSet dataSet = MMS::Container::ReplaceDataSet([[
20  Text _.name = "DataSet"
21]]);
22Anything dataSet::CreateVariable_Data(Serie {
23  Serie output = Copy(output_)
24});
25Anything dataSet::CreateVariable_Data(Serie {
26  Serie input1 = Copy(input1_)
27});
28Anything dataSet::CreateVariable_Data(Serie {
29  Serie input2 = Copy(input2_)
30});
31
32//////////////////////////////////////////////////////////////////////////////
33
34MMS::@Model model = MMS::Container::ReplaceModel([[
35  Text _.name = "Model1";
36  MMS::@DataSet _.dataSet = dataSet
37]]);
38
39MMS::@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   
50Anything 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
59Anything 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
68MMS::@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
80MMS::@Model model2 =  model::Duplicate(?);
81
82Anything 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]]);
94Anything 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
107MMS::@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
119MMS::@Model model3 =  model::Duplicate(?);
120
121MMS::@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]]);
134Anything 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
141MMS::@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
153MMS::@Model model4 =  model::Duplicate(?);
154
155Anything 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]]);
163Anything 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
172MMS::@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]]);
184Anything 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
191MMS::@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
203Real 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});
2091};
210
211
212Real PutRandomSeed(Time);
213
214Real PutRandomSeed(2143);
215Real estimation::Execute(?);
216Real PutRandomSeed(2143);
217Real estimation2::Execute(?);
218Real PutRandomSeed(2143);
219Real estimation3::Execute(?);
220Real PutRandomSeed(2143);
221Real estimation4::Execute(?);
222
223Real ShowResults(estimation);
224Real ShowResults(estimation2);
225Real ShowResults(estimation3);
226Real ShowResults(estimation4);
227