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 #79: funciones.tol

File funciones.tol, 9.6 KB (added by Pedro Gea, 15 years ago)
Line 
1
2//////////////////////////////////////////////////////////////////////////////
3Real CheckLinCom( Set OutputInfo, Set InputsInfo, Set AllHierarchyInfo,
4  Text obs.node, Real vl_inf, Real vl_pri, Text logRoute)
5//////////////////////////////////////////////////////////////////////////////
6{
7  // We extract all parameters involved in any hierarchy for node obs.node
8  Set sHieNodeInfo = BinGroup("<<",EvalSet(AllHierarchyInfo,Set (Set s)
9  {
10    Set prev1 = Extract(s,2)|Extract(s,1);
11
12    Set prev2 = Select(prev1,Real (Set x)
13    {
14      Real If(x[1] == obs.node, True, False)
15    }) - [[ Empty ]];
16
17    Set BinGroup("<<",Extract(prev2,2))
18  }));
19
20
21  Real c = 0;
22
23  Set inputs = Select(OutputInfo::InputDB,Real (Anything x)
24  {
25    Real c := c+1;
26
27    // Real flag is a label that tell us if an input has not any prior, or
28    // has not any unknown value, or if it is involved in any hierarchy
29tree
30
31    Real flag =
32      If(
33        Name(InputsInfo[c]) <: sHieNodeInfo,
34        Real False,
35        If(
36          Not(And( BinEQ( ?, (InputsInfo[c])->prior_mu ),
37                   BinEQ( ?, (InputsInfo[c])->prior_sigma ))),
38          Real False,
39          If(
40            Grammar(x) == "Serie",
41            Real Not(MatSum(IsUnknown(SerMat(x)))),// HasUnknown is slower
42            If(
43              Grammar(x) == "Matrix",
44              Real Not(MatSum(IsUnknown(x))),// HasUnknown is slower
45              Real True
46            )
47          )
48        )
49      );
50
51    flag
52  });
53
54  Real If( Not(Card(inputs)),
55    Real True,
56    Real LinComWarning(OutputInfo,inputs,logRoute,id_model,obs.node)
57  )
58};
59//////////////////////////////////////////////////////////////////////////////
60PutDescription(
61"It checks a set of input series (or matrix) to find linear combinations
62between the observational node parameters. It needs an OutputInfo BSR set
63of
64input series, the observational node, the real variable vl_pri (True or
65False)
66and a Path to store a log file with the report of the parameters that has
67linear combinations between them.
68If exist linear combination, it writes in log file and kill TOL",
69CheckLinCom);
70//////////////////////////////////////////////////////////////////////////////
71
72
73//////////////////////////////////////////////////////////////////////////////
74Real LinComWarning(Set OutputInfo, Set inputs, Text logRoute, Text
75id_model,
76  Text node)
77//////////////////////////////////////////////////////////////////////////////
78{
79  Real matrix = If( id_dating == "Matrix",True,False);
80
81  Set lcw = If( Not(matrix), LinComWarningSerie( OutputInfo,inputs ),
82    LinComWarningMatrix( inputs ));
83
84  Real If( EQ(Card(lcw),0), True,
85  {
86    Text WriteLn("[LinComWarning] THERE ARE "+IntText(Card(lcw))+" LINEAR
87"+
88      "COMBINATIONS OF INPUT SERIES CHECK LOG DIRECTORY!!", Text "E");
89
90    Text txt2File =
91    "update "+TBsrParameter+" set
92co_active="+SqlFormatText("N",GesAct)+""+NL+
93    "
94"+NL+
95    "where co_node  = "+SqlFormatText(node,GesAct)+"
96"+NL+
97    "  and co_model = "+SqlFormatText(id_model,GesAct)+"
98"+NL+
99    "
100"+NL+
101    "and co_parameter in
102"+NL+
103    "(
104"+NL+
105    "
106"+NL+
107    TxtListItemQuote(lcw,","+NL)+"
108"+NL+
109    "
110"+NL+
111    ")";
112
113    Text WriteFile(logRoute,txt2File);
114
115    Real False
116  })
117};
118//////////////////////////////////////////////////////////////////////////////
119PutDescription("It takes a set of series and check them to find linear
120combinations. If thre are linear combinations, it gives an error message
121and
122writes a query in a log file in the route logRoute.",
123LinComWarning);
124//////////////////////////////////////////////////////////////////////////////
125
126
127//////////////////////////////////////////////////////////////////////////////
128Set LinComWarningSerie(Set OutputInfo, Set inputs)
129//////////////////////////////////////////////////////////////////////////////
130{
131  Set  series   = inputs;
132  Date TruncIni = OutputInfo::IniEstim;
133  Date TruncEnd = OutputInfo::EndEstim;
134
135  Polyn dif = SetProd(EvalSet(OutputInfo::Arima,Polyn (Set p){ p[4] }));
136
137  Set seriesTrunc = EvalSet(series,Serie (Serie ser)
138  {
139    Serie ser2 = SubSer(ser,TruncIni,TruncEnd);
140    Serie dif:(ser2/MaxS(Abs(ser2)))
141  });
142
143  Matrix A = MatSetSeries(seriesTrunc);
144
145  // SVD algorithm builds a set of matrix, the second matrix has the
146  // eigenvalues.
147  /* Set svd1 = SVDDecomposition(A*Tra(A)); */ // Luis -> SVD no es
148estable
149  Set svd1 = SVD(A*Tra(A));
150
151  // We transform de second matrix into a rounded matrix because we need
152  // to search eigenvalues = 0. The 10-10 edge is 1000 times the cholesky
153  // rounding error so i understand that something below this limit is
154negligible
155  Set svd2 = RoundSVD(svd1,False,0.5,10^(-10));
156
157  // we take de main diagonal of the eigenvalues matrix and we check if
158exist
159  // eigenvalues = 0
160  Matrix diag = SubDiag( svd2[2] ,0 );
161  Real   prod = MatSet(diag)[1][Card(MatSet(diag)[1])];
162
163  Set lincom = If(BinEQ(prod,?),
164  {
165    Set Lin.Com.Procedure(series,svd2)
166  },
167  {
168    Set If(prod,
169    {
170      Set Empty
171    },
172    {
173      Set Lin.Com.Procedure(series,svd2)
174    })
175  })
176};
177//////////////////////////////////////////////////////////////////////////////
178PutDescription("It takes a set of series and gives you the number of
179linear
180combinations that happens",
181LinComWarningSerie);
182//////////////////////////////////////////////////////////////////////////////
183
184//////////////////////////////////////////////////////////////////////////////
185Set LinComWarningMatrix(Set inputs)
186//////////////////////////////////////////////////////////////////////////////
187{
188  Set vectors = inputs;
189
190  Matrix A = Tra(BinGroup("|",vectors));
191
192  // SVD algorithm builds a set of matrix, the second matrix has the
193  // eigenvalues.
194  /* Set svd1 = SVDDecomposition(A*Tra(A)); */ // Luis -> SVD no es
195estable
196  Set svd1 = SVD(A*Tra(A));
197
198  // we transform de second matrix into a rounded matrix because we need
199  // to search eigenvalues = 0
200  Set svd2 = RoundSVD(svd1,False,0.5,10^(-10));
201
202  // we take de main diagonal of the eigenvalues matrix and we check if
203exist
204  // eigenvalues = 0
205  Matrix diag = SubDiag( svd2[2] ,0 );
206  Real   prod = MatSet(diag)[1][Card(MatSet(diag)[1])];
207
208  Set lincom = If(BinEQ(prod,?),
209  {
210    Set Lin.Com.Procedure(vectors,svd2)
211  },
212  {
213    Set If(prod,
214    {
215      Set Empty
216    },
217    {
218      Set Lin.Com.Procedure(vectors,svd2)
219    })
220  })
221};
222//////////////////////////////////////////////////////////////////////////////
223PutDescription("It takes a set of matrix and gives you the number of
224linear
225combinations that happens",
226LinComWarningMatrix);
227//////////////////////////////////////////////////////////////////////////////
228
229//////////////////////////////////////////////////////////////////////////////
230Set Lin.Com.Procedure(Set elements, Set svd)
231//////////////////////////////////////////////////////////////////////////////
232{
233  // If exist eigenvalues = 0, we rounded to zero the smallest values of
234the
235  // last column of the third SVD matrix, and then, the not zero values
236show
237  // the linear combinations.
238
239  Matrix  lastCol = SubCol(svd[3],[[ Columns(svd[3]) ]]);
240  Matrix  LogicLC = Not(Not(lastCol));
241  Real    maxDiag = MaxMatrix(SubDiag(svd[2],0));
242
243  // We take the linear combinatioNs and store them into a set
244  Set indices = BinGroup("<<",MatSet(LogicLC));
245
246  Real c = 0;
247
248  Set select  = Select(elements,Real (Anything ser)
249  {
250    Real c := c+1;
251    Real If(maxDiag,indices[c],True)
252  });
253
254  Set EvalSet(select,Text (Anything x)
255  {
256    Text Name(x)
257  })
258};
259//////////////////////////////////////////////////////////////////////////////
260PutDescription("It needs a set of elements (series or matrix) and the
261result
262of a svd algorithm, and gives you tha distinct variables of elements Set
263that
264does linear combinations between them",
265Lin.Com.Procedure);
266//////////////////////////////////////////////////////////////////////////////
267
268//////////////////////////////////////////////////////////////////////////////
269Set RoundSVD(Set svd,Real trasposed, Real minSparse,Real tolerance)
270//////////////////////////////////////////////////////////////////////////////
271{
272  Set EvalSet(svd,Matrix (Matrix matrix)
273  {
274    VMatrix vmatrix = Mat2VMat(
275      matrix,     // matrix to transform
276      trasposed,  // does it trasposed?
277      minSparse,  // minSparse
278      tolerance); // tolerance
279
280    Matrix VMat2Mat(vmatrix)
281  })
282};
283//////////////////////////////////////////////////////////////////////////////
284PutDescription("It rounds an exit of SVD function to avoid negligible
285values.
286It needs:
287  matrix     --> matrix to transform
288  trasposed  --> does it trasposed? (True or False)
289  minSparse  --> minSparse use 0.5 by default
290  tolerance  --> round use 10^(-10) recommended
291",
292RoundSVD);
293//////////////////////////////////////////////////////////////////////////////
294
295
296//////////////////////////////////////////////////////////////////////////////
297Set SVDDecomposition(Matrix m)
298//////////////////////////////////////////////////////////////////////////////
299{
300  VMatrix vm = Mat2VMat(m);
301
302  Real rows    = VRows(vm);
303  Real columns = VColumns(vm);
304  Real nonNull = VNonNullCells(vm);
305
306  Real density = nonNull/(rows*columns);
307
308 // Set If(density <= 0.05, SVD(m,"Sparse"), SVD(m,"Jacobi"))
309  Set If(density <= 0.01, SVD(m,"Sparse"), SVD(m))
310};
311//////////////////////////////////////////////////////////////////////////////
312PutDescription("It does the SVD decomposition choosing betwwen Sparse and
313Jacobi method according to the density of de matrix m.
314",
315SVDDecomposition);
316//////////////////////////////////////////////////////////////////////////////