| 1 | |
|---|
| 2 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 3 | Real 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 |
|---|
| 29 | tree |
|---|
| 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 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 60 | PutDescription( |
|---|
| 61 | "It checks a set of input series (or matrix) to find linear combinations |
|---|
| 62 | between the observational node parameters. It needs an OutputInfo BSR set |
|---|
| 63 | of |
|---|
| 64 | input series, the observational node, the real variable vl_pri (True or |
|---|
| 65 | False) |
|---|
| 66 | and a Path to store a log file with the report of the parameters that has |
|---|
| 67 | linear combinations between them. |
|---|
| 68 | If exist linear combination, it writes in log file and kill TOL", |
|---|
| 69 | CheckLinCom); |
|---|
| 70 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 74 | Real LinComWarning(Set OutputInfo, Set inputs, Text logRoute, Text |
|---|
| 75 | id_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 |
|---|
| 92 | co_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 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 119 | PutDescription("It takes a set of series and check them to find linear |
|---|
| 120 | combinations. If thre are linear combinations, it gives an error message |
|---|
| 121 | and |
|---|
| 122 | writes a query in a log file in the route logRoute.", |
|---|
| 123 | LinComWarning); |
|---|
| 124 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 128 | Set 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 |
|---|
| 148 | estable |
|---|
| 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 |
|---|
| 154 | negligible |
|---|
| 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 |
|---|
| 158 | exist |
|---|
| 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 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 178 | PutDescription("It takes a set of series and gives you the number of |
|---|
| 179 | linear |
|---|
| 180 | combinations that happens", |
|---|
| 181 | LinComWarningSerie); |
|---|
| 182 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 183 | |
|---|
| 184 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 185 | Set 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 |
|---|
| 195 | estable |
|---|
| 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 |
|---|
| 203 | exist |
|---|
| 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 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 223 | PutDescription("It takes a set of matrix and gives you the number of |
|---|
| 224 | linear |
|---|
| 225 | combinations that happens", |
|---|
| 226 | LinComWarningMatrix); |
|---|
| 227 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 228 | |
|---|
| 229 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 230 | Set Lin.Com.Procedure(Set elements, Set svd) |
|---|
| 231 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 232 | { |
|---|
| 233 | // If exist eigenvalues = 0, we rounded to zero the smallest values of |
|---|
| 234 | the |
|---|
| 235 | // last column of the third SVD matrix, and then, the not zero values |
|---|
| 236 | show |
|---|
| 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 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 260 | PutDescription("It needs a set of elements (series or matrix) and the |
|---|
| 261 | result |
|---|
| 262 | of a svd algorithm, and gives you tha distinct variables of elements Set |
|---|
| 263 | that |
|---|
| 264 | does linear combinations between them", |
|---|
| 265 | Lin.Com.Procedure); |
|---|
| 266 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 267 | |
|---|
| 268 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 269 | Set 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 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 284 | PutDescription("It rounds an exit of SVD function to avoid negligible |
|---|
| 285 | values. |
|---|
| 286 | It 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 | ", |
|---|
| 292 | RoundSVD); |
|---|
| 293 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 297 | Set 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 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 312 | PutDescription("It does the SVD decomposition choosing betwwen Sparse and |
|---|
| 313 | Jacobi method according to the density of de matrix m. |
|---|
| 314 | ", |
|---|
| 315 | SVDDecomposition); |
|---|
| 316 | ////////////////////////////////////////////////////////////////////////////// |
|---|