67 | | int len = length(in.d); |
68 | | int p1 = in.posit1 - 1; |
69 | | |
70 | | double i1 = -0.5*in.nu *log(in.d(p1)) -0.5*sum(log(in.d.right(len - p1 -1))); |
71 | | double i0 = -0.5*in.nu0*log(in.d0(p1)) -0.5*sum(log(in.d0.right(len - p1 -1))); |
72 | | return i1-i0; |
73 | | //DEBUGGing print: |
74 | | //fprintf('SELOGLIK1: str=%s loglik=%g\n', strPrintstr(in), l);*/ |
75 | | |
76 | | } |
77 | | |
78 | | |
79 | | void sedydr(mat &r,mat &f,double &Dr,double &Df,int R/*,int jl,int jh ,mat &rout, mat &fout, double &Drout, double &Dfoutint &kr*/){ |
80 | | /*SEDYDR dyadic reduction, performs transformation of sum of 2 dyads |
81 | | % |
82 | | % [rout, fout, Drout, Dfout, kr] = sedydr(r,f,Dr,Df,R,jl,jh); |
83 | | % [rout, fout, Drout, Dfout] = sedydr(r,f,Dr,Df,R); |
84 | | % |
85 | | % Description: dyadic reduction, performs transformation of sum of |
86 | | % 2 dyads r*Dr*r'+ f*Df*f' so that the element of r pointed by R is zeroed |
87 | | % |
88 | | % r : column vector of reduced dyad |
89 | | % f : column vector of reducing dyad |
90 | | % Dr : scalar with weight of reduced dyad |
91 | | % Df : scalar with weight of reducing dyad |
92 | | % R : scalar number giving 1 based index to the element of r, |
93 | | % which is to be reduced to |
94 | | % zero; the corresponding element of f is assumed to be 1. |
95 | | % jl : lower index of the range within which the dyads are |
96 | | % modified (can be omitted, then everything is updated) |
97 | | % jh : upper index of the range within which the dyads are |
98 | | % modified (can be omitted then everything is updated) |
99 | | % rout,fout,Drout,dfout : resulting two dyads |
100 | | % kr : coefficient used in the transformation of r |
101 | | % rnew = r + kr*f |
102 | | % |
103 | | % Description: dyadic reduction, performs transformation of sum of |
104 | | % 2 dyads r*Dr*r'+ f*Df*f' so that the element of r indexed by R is zeroed |
105 | | % Remark1: Constant mzero means machine zero and should be modified |
106 | | % according to the precision of particular machine |
107 | | % Remark2: jl and jh are, in fact, obsolete. It takes longer time to |
108 | | % compute them compared to plain version. The reason is that we |
109 | | % are doing vector operations in m-file. Other reason is that |
110 | | % we need to copy whole vector anyway. It can save half of time for |
111 | | % c-file, if you use it correctly. (please do tests) |
112 | | % |
113 | | % Note: naming: |
114 | | % se = structure estimation |
115 | | % dydr = dyadic reduction |
116 | | % |
117 | | % Original Fortran design: V. Peterka 17-7-89 |
118 | | % Modified for c-language: probably R. Kulhavy |
119 | | % Modified for m-language: L. Tesar 2/2003 |
120 | | % Updated: Feb 2003 |
121 | | % Project: post-ProDaCTool |
122 | | % Reference: none*/ |
123 | | |
124 | | /*if nargin<6; |
125 | | update_whole=1; |
126 | | else |
127 | | update_whole=0; |
128 | | end;*/ |
129 | | |
130 | | double mzero = 1e-32; |
131 | | |
132 | | if (Dr<mzero){ |
133 | | Dr=0; |
134 | | } |
135 | | |
136 | | double r0 = r(R,0); |
137 | | double kD = Df; |
138 | | double kr = r0 * Dr; |
139 | | |
140 | | |
141 | | Df = kD + r0 * kr; |
142 | | |
143 | | if (Df > mzero){ |
144 | | kD = kD / Df; |
145 | | kr = kr / Df; |
146 | | }else{ |
147 | | kD = 1; |
148 | | kr = 0; |
149 | | } |
150 | | |
151 | | Dr = Dr * kD; |
152 | | |
| 67 | int len = length ( in.d ); |
| 68 | int p1 = in.posit1 - 1; |
| 69 | |
| 70 | double i1 = -0.5 * in.nu * log ( in.d ( p1 ) ) - 0.5 * sum ( log ( in.d.right ( len - p1 - 1 ) ) ); |
| 71 | double i0 = -0.5 * in.nu0 * log ( in.d0 ( p1 ) ) - 0.5 * sum ( log ( in.d0.right ( len - p1 - 1 ) ) ); |
| 72 | return i1 - i0; |
| 73 | //DEBUGGing print: |
| 74 | //fprintf('SELOGLIK1: str=%s loglik=%g\n', strPrintstr(in), l);*/ |
| 75 | |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void sedydr ( mat &r, mat &f, double &Dr, double &Df, int R/*,int jl,int jh ,mat &rout, mat &fout, double &Drout, double &Dfoutint &kr*/ ) { |
| 80 | /*SEDYDR dyadic reduction, performs transformation of sum of 2 dyads |
| 81 | % |
| 82 | % [rout, fout, Drout, Dfout, kr] = sedydr(r,f,Dr,Df,R,jl,jh); |
| 83 | % [rout, fout, Drout, Dfout] = sedydr(r,f,Dr,Df,R); |
| 84 | % |
| 85 | % Description: dyadic reduction, performs transformation of sum of |
| 86 | % 2 dyads r*Dr*r'+ f*Df*f' so that the element of r pointed by R is zeroed |
| 87 | % |
| 88 | % r : column vector of reduced dyad |
| 89 | % f : column vector of reducing dyad |
| 90 | % Dr : scalar with weight of reduced dyad |
| 91 | % Df : scalar with weight of reducing dyad |
| 92 | % R : scalar number giving 1 based index to the element of r, |
| 93 | % which is to be reduced to |
| 94 | % zero; the corresponding element of f is assumed to be 1. |
| 95 | % jl : lower index of the range within which the dyads are |
| 96 | % modified (can be omitted, then everything is updated) |
| 97 | % jh : upper index of the range within which the dyads are |
| 98 | % modified (can be omitted then everything is updated) |
| 99 | % rout,fout,Drout,dfout : resulting two dyads |
| 100 | % kr : coefficient used in the transformation of r |
| 101 | % rnew = r + kr*f |
| 102 | % |
| 103 | % Description: dyadic reduction, performs transformation of sum of |
| 104 | % 2 dyads r*Dr*r'+ f*Df*f' so that the element of r indexed by R is zeroed |
| 105 | % Remark1: Constant mzero means machine zero and should be modified |
| 106 | % according to the precision of particular machine |
| 107 | % Remark2: jl and jh are, in fact, obsolete. It takes longer time to |
| 108 | % compute them compared to plain version. The reason is that we |
| 109 | % are doing vector operations in m-file. Other reason is that |
| 110 | % we need to copy whole vector anyway. It can save half of time for |
| 111 | % c-file, if you use it correctly. (please do tests) |
| 112 | % |
| 113 | % Note: naming: |
| 114 | % se = structure estimation |
| 115 | % dydr = dyadic reduction |
| 116 | % |
| 117 | % Original Fortran design: V. Peterka 17-7-89 |
| 118 | % Modified for c-language: probably R. Kulhavy |
| 119 | % Modified for m-language: L. Tesar 2/2003 |
| 120 | % Updated: Feb 2003 |
| 121 | % Project: post-ProDaCTool |
| 122 | % Reference: none*/ |
| 123 | |
| 124 | /*if nargin<6; |
| 125 | update_whole=1; |
| 126 | else |
| 127 | update_whole=0; |
| 128 | end;*/ |
| 129 | |
| 130 | double mzero = 1e-32; |
| 131 | |
| 132 | if ( Dr < mzero ) { |
| 133 | Dr = 0; |
| 134 | } |
| 135 | |
| 136 | double r0 = r ( R, 0 ); |
| 137 | double kD = Df; |
| 138 | double kr = r0 * Dr; |
| 139 | |
| 140 | |
| 141 | Df = kD + r0 * kr; |
| 142 | |
| 143 | if ( Df > mzero ) { |
| 144 | kD = kD / Df; |
| 145 | kr = kr / Df; |
| 146 | } else { |
| 147 | kD = 1; |
| 148 | kr = 0; |
| 149 | } |
| 150 | |
| 151 | Dr = Dr * kD; |
| 152 | |
160 | | /*else; |
161 | | rout = r; |
162 | | fout = f; |
163 | | rout(jl:jh) = r(jl:jh) - r0 * f(jl:jh); |
164 | | rout(R) = 0; |
165 | | fout(jl:jh) = f(jl:jh) + kr * rout(jl:jh); |
166 | | end;*/ |
167 | | } |
168 | | |
169 | | |
170 | | |
171 | | /*mat*/ void seswapudl(mat &L, vec &d , int i/*, vec &dout*/){ |
172 | | /*%SESWAPUDL swaps information matrix in decomposition V=L^T diag(d) L |
173 | | % |
174 | | % [Lout, dout] = seswapudl(L,d,i); |
175 | | % |
176 | | % L : lower triangular matrix with 1's on diagonal of the decomposistion |
177 | | % d : diagonal vector of diagonal matrix of the decomposition |
178 | | % i : index of line to be swapped with the next one |
179 | | % Lout : output lower triangular matrix |
180 | | % dout : output diagional vector of diagonal matrix D |
181 | | % |
182 | | % Description: |
183 | | % Lout' * diag(dout) * Lout = P(i,i+1) * L' * diag(d) * L * P(i,i+1); |
184 | | % |
185 | | % Where permutation matrix P(i,j) permutates columns if applied from the |
186 | | % right and line if applied from the left. |
187 | | % |
188 | | % Note: naming: |
189 | | % se = structure estimation |
190 | | % lite = light, simple |
191 | | % udl = U*D*L, or more precisely, L'*D*L, also called as ld |
192 | | % |
193 | | % Design : L. Tesar |
194 | | % Updated : Feb 2003 |
195 | | % Project : post-ProDaCTool |
196 | | % Reference: sedydr*/ |
197 | | |
198 | | int j = i+1; |
199 | | |
200 | | double pomd = d(i); |
201 | | d(i) = d(j); |
202 | | d(j) = pomd; |
203 | | |
204 | | /*vec pomL = L.get_row(i); |
205 | | L.set_row(i, L.get_row(j)); |
206 | | L.set_row(j,pomL);*/ |
207 | | |
208 | | L.swap_rows(i,j); |
209 | | L.swap_cols(i,j); |
210 | | |
211 | | /*pomL = L.get_col(i); |
212 | | L.set_col(i, L.get_col(j)); |
213 | | L.set_col(j,pomL);*/ |
214 | | |
| 160 | /*else; |
| 161 | rout = r; |
| 162 | fout = f; |
| 163 | rout(jl:jh) = r(jl:jh) - r0 * f(jl:jh); |
| 164 | rout(R) = 0; |
| 165 | fout(jl:jh) = f(jl:jh) + kr * rout(jl:jh); |
| 166 | end;*/ |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | /*mat*/ void seswapudl ( mat &L, vec &d , int i/*, vec &dout*/ ) { |
| 172 | /*%SESWAPUDL swaps information matrix in decomposition V=L^T diag(d) L |
| 173 | % |
| 174 | % [Lout, dout] = seswapudl(L,d,i); |
| 175 | % |
| 176 | % L : lower triangular matrix with 1's on diagonal of the decomposistion |
| 177 | % d : diagonal vector of diagonal matrix of the decomposition |
| 178 | % i : index of line to be swapped with the next one |
| 179 | % Lout : output lower triangular matrix |
| 180 | % dout : output diagional vector of diagonal matrix D |
| 181 | % |
| 182 | % Description: |
| 183 | % Lout' * diag(dout) * Lout = P(i,i+1) * L' * diag(d) * L * P(i,i+1); |
| 184 | % |
| 185 | % Where permutation matrix P(i,j) permutates columns if applied from the |
| 186 | % right and line if applied from the left. |
| 187 | % |
| 188 | % Note: naming: |
| 189 | % se = structure estimation |
| 190 | % lite = light, simple |
| 191 | % udl = U*D*L, or more precisely, L'*D*L, also called as ld |
| 192 | % |
| 193 | % Design : L. Tesar |
| 194 | % Updated : Feb 2003 |
| 195 | % Project : post-ProDaCTool |
| 196 | % Reference: sedydr*/ |
| 197 | |
| 198 | int j = i + 1; |
| 199 | |
| 200 | double pomd = d ( i ); |
| 201 | d ( i ) = d ( j ); |
| 202 | d ( j ) = pomd; |
| 203 | |
| 204 | /*vec pomL = L.get_row(i); |
| 205 | L.set_row(i, L.get_row(j)); |
| 206 | L.set_row(j,pomL);*/ |
| 207 | |
| 208 | L.swap_rows ( i, j ); |
| 209 | L.swap_cols ( i, j ); |
| 210 | |
| 211 | /*pomL = L.get_col(i); |
| 212 | L.set_col(i, L.get_col(j)); |
| 213 | L.set_col(j,pomL);*/ |
| 214 | |
270 | | int n_strL = length(in.strL); |
271 | | str_aux out = in; |
272 | | for (int i = 0; i < removed_elements.length();i++){ |
273 | | |
274 | | int f = removed_elements(i); |
275 | | int posit1 = (find(out.strL==1))(0); |
276 | | int positf = (find(out.strL==f))(0); |
277 | | int pom_strL; |
278 | | for (int g = positf-1; g >posit1 -1; g--) { |
279 | | //% BEGIN: We are swapping g and g+1 NOW!!!! |
280 | | seswapudl(out.L, out.d, g); |
281 | | seswapudl(out.L0, out.d0, g); |
282 | | |
283 | | pom_strL = out.strL(g); |
284 | | out.strL(g)= out.strL(g+1); |
285 | | out.strL(g+1) = pom_strL; |
286 | | |
287 | | //% END |
288 | | } |
289 | | } |
290 | | out.posit1 = (find(out.strL==1))(0)+1; |
291 | | out.strRgr = out.strL.right(n_strL - out.posit1); |
292 | | out.strMis = out.strL.left(out.posit1-1); |
293 | | str_bitres(out.bitstr,removed_elements,out.nbits); |
294 | | out.loglik = seloglik1(out); |
295 | | |
296 | | return out; |
297 | | } |
298 | | |
299 | | |
300 | | ivec setdiff(ivec a, ivec b){ |
301 | | ivec pos; |
302 | | |
303 | | for (int i = 0; i < b.length(); i++){ |
304 | | pos = find(a==b(i)); |
305 | | for (int j = 0; j < pos.length(); j++){ |
306 | | a.del(pos(j)-j); |
307 | | } |
308 | | } |
309 | | return a; |
310 | | } |
311 | | |
312 | | |
313 | | |
| 270 | int n_strL = length ( in.strL ); |
| 271 | str_aux out = in; |
| 272 | for ( int i = 0; i < removed_elements.length(); i++ ) { |
| 273 | |
| 274 | int f = removed_elements ( i ); |
| 275 | int posit1 = ( find ( out.strL == 1 ) ) ( 0 ); |
| 276 | int positf = ( find ( out.strL == f ) ) ( 0 ); |
| 277 | int pom_strL; |
| 278 | for ( int g = positf - 1; g > posit1 - 1; g-- ) { |
| 279 | //% BEGIN: We are swapping g and g+1 NOW!!!! |
| 280 | seswapudl ( out.L, out.d, g ); |
| 281 | seswapudl ( out.L0, out.d0, g ); |
| 282 | |
| 283 | pom_strL = out.strL ( g ); |
| 284 | out.strL ( g ) = out.strL ( g + 1 ); |
| 285 | out.strL ( g + 1 ) = pom_strL; |
| 286 | |
| 287 | //% END |
| 288 | } |
| 289 | } |
| 290 | out.posit1 = ( find ( out.strL == 1 ) ) ( 0 ) + 1; |
| 291 | out.strRgr = out.strL.right ( n_strL - out.posit1 ); |
| 292 | out.strMis = out.strL.left ( out.posit1 - 1 ); |
| 293 | str_bitres ( out.bitstr, removed_elements, out.nbits ); |
| 294 | out.loglik = seloglik1 ( out ); |
| 295 | |
| 296 | return out; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | ivec setdiff ( ivec a, ivec b ) { |
| 301 | ivec pos; |
| 302 | |
| 303 | for ( int i = 0; i < b.length(); i++ ) { |
| 304 | pos = find ( a == b ( i ) ); |
| 305 | for ( int j = 0; j < pos.length(); j++ ) { |
| 306 | a.del ( pos ( j ) - j ); |
| 307 | } |
| 308 | } |
| 309 | return a; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | |
368 | | |
369 | | int addit, i = 0; |
370 | | if (global_best.length() >= nbest){ |
371 | | //logliks = [global_best.loglik]; |
372 | | |
373 | | |
374 | | for (int j = 1; j < global_best.length(); j++){ |
375 | | if (global_best(j).loglik < global_best(i).loglik) { |
376 | | i = j; |
377 | | } |
378 | | } |
379 | | |
380 | | if (global_best(i).loglik < newone.loglik){ |
381 | | // if ~any(logliks == new.loglik); |
382 | | addit=1; |
383 | | |
384 | | |
385 | | //???????????????????????????????????????????? |
386 | | // V MATLABU SE MISTO DVOU A VICE DOUBLU |
387 | | // POROVNAVA KAZDY ZVLAST, COZ DISKRIMINUJE |
388 | | // VICEROZMERNE (>52) MATICE.. KOD V MATLABU |
389 | | // JE ZREJME SPATNE .. TODO |
390 | | if (newone.bitstr.length() == 1) { |
391 | | for (int j = 0; j < global_best.length(); j++){ |
392 | | for(int i = 0; i < global_best(j).bitstr.length(); i++){ |
393 | | |
394 | | if (newone.bitstr(0) == global_best(j).bitstr(i)){ |
395 | | addit = 0; |
396 | | break; |
397 | | } |
398 | | } |
399 | | } |
400 | | } |
401 | | |
402 | | |
403 | | //????????????????????????????????????????????????? |
404 | | |
405 | | if (addit){ |
406 | | global_best(i) = newone; |
407 | | // DEBUGging print: |
408 | | // fprintf('ADDED structure, add_new: %s, loglik=%g\n', strPrintstr(new), new.loglik); |
409 | | } |
410 | | } |
411 | | } |
412 | | else |
413 | | global_best = concat(global_best, newone); |
414 | | |
415 | | } |
416 | | |
417 | | |
418 | | |
419 | | |
420 | | |
421 | | |
422 | | str_aux sestrinsert(str_aux in,ivec inserted_elements){ |
| 368 | |
| 369 | int addit, i = 0; |
| 370 | if ( global_best.length() >= nbest ) { |
| 371 | //logliks = [global_best.loglik]; |
| 372 | |
| 373 | |
| 374 | for ( int j = 1; j < global_best.length(); j++ ) { |
| 375 | if ( global_best ( j ).loglik < global_best ( i ).loglik ) { |
| 376 | i = j; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if ( global_best ( i ).loglik < newone.loglik ) { |
| 381 | // if ~any(logliks == new.loglik); |
| 382 | addit = 1; |
| 383 | |
| 384 | |
| 385 | //???????????????????????????????????????????? |
| 386 | if ( newone.bitstr.length() == 1 ) { |
| 387 | for ( int j = 0; j < global_best.length(); j++ ) { |
| 388 | for ( int i = 0; i < global_best ( j ).bitstr.length(); i++ ) { |
| 389 | |
| 390 | if ( newone.bitstr ( 0 ) == global_best ( j ).bitstr ( i ) ) { |
| 391 | addit = 0; |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
| 399 | //????????????????????????????????????????????????? |
| 400 | |
| 401 | if ( addit ) { |
| 402 | global_best ( i ) = newone; |
| 403 | // DEBUGging print: |
| 404 | // fprintf('ADDED structure, add_new: %s, loglik=%g\n', strPrintstr(new), new.loglik); |
| 405 | } |
| 406 | } |
| 407 | } else |
| 408 | global_best = concat ( global_best, newone ); |
| 409 | |
| 410 | } |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | str_aux sestrinsert ( str_aux in, ivec inserted_elements ) { |
424 | | int n_strL = in.strL.length(); |
425 | | str_aux out = in; |
426 | | for (int j = 0;j < inserted_elements.length(); j++){ |
427 | | int f = inserted_elements(j); |
428 | | int posit1 = (find(out.strL==1))(0); |
429 | | int positf = (find(out.strL==f))(0); |
430 | | for (int g = positf; g <= posit1-1; g++ ){ |
431 | | |
432 | | // BEGIN: We are swapping g and g+1 NOW!!!! |
433 | | seswapudl(out.L, out.d, g); |
434 | | seswapudl(out.L0, out.d0, g); |
435 | | |
436 | | |
437 | | int pom_strL = out.strL(g); |
438 | | out.strL(g)= out.strL(g+1); |
439 | | out.strL(g+1) = pom_strL; |
440 | | |
441 | | // END |
442 | | } |
443 | | } |
444 | | |
445 | | out.posit1 = (find(out.strL==1))(0)+1; |
446 | | out.strRgr = out.strL.right(n_strL - out.posit1); |
447 | | out.strMis = out.strL.left(out.posit1-1); |
448 | | str_bitset(out.bitstr,inserted_elements,out.nbits); |
449 | | |
450 | | out.loglik = seloglik1(out); |
451 | | |
452 | | |
453 | | |
454 | | return out; |
455 | | |
456 | | } |
457 | | |
458 | | double seloglik2(str_aux in){ |
| 419 | int n_strL = in.strL.length(); |
| 420 | str_aux out = in; |
| 421 | for ( int j = 0; j < inserted_elements.length(); j++ ) { |
| 422 | int f = inserted_elements ( j ); |
| 423 | int posit1 = ( find ( out.strL == 1 ) ) ( 0 ); |
| 424 | int positf = ( find ( out.strL == f ) ) ( 0 ); |
| 425 | for ( int g = positf; g <= posit1 - 1; g++ ) { |
| 426 | |
| 427 | // BEGIN: We are swapping g and g+1 NOW!!!! |
| 428 | seswapudl ( out.L, out.d, g ); |
| 429 | seswapudl ( out.L0, out.d0, g ); |
| 430 | |
| 431 | |
| 432 | int pom_strL = out.strL ( g ); |
| 433 | out.strL ( g ) = out.strL ( g + 1 ); |
| 434 | out.strL ( g + 1 ) = pom_strL; |
| 435 | |
| 436 | // END |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | out.posit1 = ( find ( out.strL == 1 ) ) ( 0 ) + 1; |
| 441 | out.strRgr = out.strL.right ( n_strL - out.posit1 ); |
| 442 | out.strMis = out.strL.left ( out.posit1 - 1 ); |
| 443 | str_bitset ( out.bitstr, inserted_elements, out.nbits ); |
| 444 | |
| 445 | out.loglik = seloglik1 ( out ); |
| 446 | |
| 447 | |
| 448 | |
| 449 | return out; |
| 450 | |
| 451 | } |
| 452 | |
| 453 | double seloglik2 ( str_aux in ) { |
475 | | const mat &L = Ld._L(); |
476 | | const vec &d = Ld._D(); |
477 | | |
478 | | const mat &L0 = Ld0._L(); |
479 | | const vec &d0 = Ld0._D(); |
480 | | |
481 | | int n_data = d.length(); |
482 | | |
483 | | ivec belief_out = find(belief==4)+2; // we are avoiding to put this into regressor |
484 | | ivec belief_in = find(belief==1)+2; // we are instantly keeping this in regressor |
485 | | |
486 | | |
487 | | str_aux full; |
488 | | |
489 | | full.d0 = d0; |
490 | | full.nu0 = nu0; |
491 | | full.L0 = L0; |
492 | | full.L = L; |
493 | | full.d = d; |
494 | | |
495 | | |
496 | | |
497 | | /*full.d0 = "0.012360650875200 0.975779169502626 1.209840558439000"; |
498 | | |
499 | | full.L0 = "1.0000 0 0;" |
500 | | "0.999427690134298 1.0000 0;" |
501 | | "0.546994424043659 0.534335486953833 1.0000"; |
502 | | |
503 | | |
504 | | full.L = "1.0000 0 0;" |
505 | | "0.364376353850780 1.0000 0;" |
506 | | "1.222141096674815 1.286534510946323 1.0000"; |
507 | | |
508 | | full.d = "0.001610356837691 3.497566869589465 3.236640487818002"; |
509 | | */ |
510 | | |
511 | | fstream F; |
512 | | |
513 | | |
514 | | |
515 | | |
516 | | F.open("soubor3.txt", ios::in); |
517 | | F << setiosflags(ios::scientific); |
518 | | F << setprecision(16); |
519 | | F.flags(0x1); |
520 | | |
521 | | |
522 | | for (int i = 0; i < n_data ; i++){ |
523 | | F >> full.d0(i); |
524 | | } |
525 | | |
526 | | |
527 | | |
528 | | for (int i =0; i < n_data; i++){ |
529 | | for (int j = 0 ; j < n_data ; j++){ |
530 | | F >> full.L0(j,i); |
531 | | } |
532 | | } |
533 | | |
534 | | for (int i = 0; i < n_data ; i++){ |
535 | | F >> full.d(i); |
536 | | } |
537 | | |
538 | | |
539 | | for (int i =0; i < n_data; i++){ |
540 | | for (int j = 0 ; j < n_data ; j++){ |
541 | | F >> full.L(j,i); |
542 | | } |
543 | | } |
544 | | |
545 | | |
546 | | full.nu0 = nu0; |
547 | | full.nu = nu; |
548 | | full.strL = linspace(1,n_data); |
549 | | full.strRgr = linspace(2,n_data); |
550 | | full.strMis = ivec(0); |
551 | | full.posit1 = 1; |
552 | | full.bitstr.set_size(n_data-1); |
553 | | full.bitstr.clear(); |
554 | | str_bitset(full.bitstr,full.strRgr,full.nbits); |
| 470 | const mat &L = Ld._L(); |
| 471 | const vec &d = Ld._D(); |
| 472 | |
| 473 | const mat &L0 = Ld0._L(); |
| 474 | const vec &d0 = Ld0._D(); |
| 475 | |
| 476 | int n_data = d.length(); |
| 477 | |
| 478 | ivec belief_out = find ( belief == 4 ) + 2; // we are avoiding to put this into regressor |
| 479 | ivec belief_in = find ( belief == 1 ) + 2; // we are instantly keeping this in regressor |
| 480 | |
| 481 | |
| 482 | str_aux full; |
| 483 | |
| 484 | full.d0 = d0; |
| 485 | full.nu0 = nu0; |
| 486 | full.L0 = L0; |
| 487 | full.L = L; |
| 488 | full.d = d; |
| 489 | |
| 490 | |
| 491 | |
| 492 | /*full.d0 = "0.012360650875200 0.975779169502626 1.209840558439000"; |
| 493 | |
| 494 | full.L0 = "1.0000 0 0;" |
| 495 | "0.999427690134298 1.0000 0;" |
| 496 | "0.546994424043659 0.534335486953833 1.0000"; |
| 497 | |
| 498 | |
| 499 | full.L = "1.0000 0 0;" |
| 500 | "0.364376353850780 1.0000 0;" |
| 501 | "1.222141096674815 1.286534510946323 1.0000"; |
| 502 | |
| 503 | full.d = "0.001610356837691 3.497566869589465 3.236640487818002"; |
| 504 | */ |
| 505 | |
| 506 | fstream F; |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | F.open ( "soubor3.txt", ios::in ); |
| 512 | F << setiosflags ( ios::scientific ); |
| 513 | F << setprecision ( 16 ); |
| 514 | F.flags ( 0x1 ); |
| 515 | |
| 516 | |
| 517 | for ( int i = 0; i < n_data ; i++ ) { |
| 518 | F >> full.d0 ( i ); |
| 519 | } |
| 520 | |
| 521 | |
| 522 | |
| 523 | for ( int i = 0; i < n_data; i++ ) { |
| 524 | for ( int j = 0 ; j < n_data ; j++ ) { |
| 525 | F >> full.L0 ( j, i ); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | for ( int i = 0; i < n_data ; i++ ) { |
| 530 | F >> full.d ( i ); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | for ( int i = 0; i < n_data; i++ ) { |
| 535 | for ( int j = 0 ; j < n_data ; j++ ) { |
| 536 | F >> full.L ( j, i ); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | |
| 541 | full.nu0 = nu0; |
| 542 | full.nu = nu; |
| 543 | full.strL = linspace ( 1, n_data ); |
| 544 | full.strRgr = linspace ( 2, n_data ); |
| 545 | full.strMis = ivec ( 0 ); |
| 546 | full.posit1 = 1; |
| 547 | full.bitstr.set_size ( n_data - 1 ); |
| 548 | full.bitstr.clear(); |
| 549 | str_bitset ( full.bitstr, full.strRgr, full.nbits ); |
609 | | str_aux best; |
610 | | for (int n_start = -1; n_start <= max_nrep; n_start++){ |
611 | | str_aux last,best; |
612 | | |
613 | | to = n_start+2; |
614 | | |
615 | | if (n_start == -1){ |
616 | | //% start from the full structure |
617 | | last = full; |
618 | | } |
619 | | else {if (n_start == 0) |
620 | | //% start from the empty structure |
621 | | last = empty; |
622 | | |
623 | | else{ |
624 | | //% start from random structure |
625 | | |
626 | | ivec last_str = find(to_bvec<int>(::concat<int>(0,floor_i(2*randu(n_data-1)))));// this creates random vector consisting of indexes, and sorted |
627 | | last = sestrremove(full,setdiff(all_str,::concat<int>(::concat<int>(1 ,last_str), empty.strRgr))); |
628 | | |
629 | | } |
630 | | } |
631 | | //% DEBUGging print: |
632 | | //%fprintf('STRUCTURE generated in loop %2i was %s\n', n_start, strPrintstr(last)); |
633 | | |
634 | | //% The loop is repeated until likelihood stops growing (break condition |
635 | | //% used at the end; |
636 | | |
637 | | |
638 | | while (1){ |
639 | | //% This structure is going to hold the best elements |
640 | | best = last; |
641 | | //% Nesting by removing elements (enpoorment) |
642 | | ivec removed_items = setdiff(last.strRgr,belief_in); |
643 | | |
644 | | ivec removed_item; |
645 | | str_aux newone; |
646 | | |
647 | | for (int i = 0; i < removed_items.length(); i++){ |
648 | | removed_item = vec_1(removed_items(i)); |
649 | | newone = sestrremove(last,removed_item); |
650 | | if (nbest>1){ |
651 | | add_new(global_best,newone,nbest); |
652 | | } |
653 | | if (newone.loglik>best.loglik){ |
654 | | best = newone; |
655 | | } |
656 | | } |
657 | | //% Nesting by adding elements (enrichment) |
658 | | ivec added_items = setdiff(last.strMis,belief_out); |
659 | | ivec added_item; |
660 | | |
661 | | for (int j = 0; j < added_items.length(); j++){ |
662 | | added_item = vec_1(added_items(j)); |
663 | | newone = sestrinsert(last,added_item); |
664 | | if (nbest>1){ |
665 | | add_new(global_best,newone,nbest); |
666 | | } |
667 | | if (newone.loglik>best.loglik){ |
668 | | best = newone; |
669 | | } |
670 | | } |
671 | | |
672 | | |
673 | | |
674 | | |
675 | | |
676 | | //% Break condition if likelihood does not change. |
677 | | if (best.loglik <= last.loglik) |
678 | | break; |
679 | | else |
680 | | //% Making best structure last structure. |
681 | | last = best; |
682 | | |
683 | | |
684 | | } |
685 | | |
686 | | |
687 | | |
688 | | |
689 | | |
690 | | // % DEBUGging print: |
691 | | //%fprintf('STRUCTURE found (local maxima) in loop %2i was %s randun_seed=%11lu randun_counter=%4lu\n', n_start, strPrintstr(best), randn('seed'), RANDUN_COUNTER); |
692 | | |
693 | | //% Collecting of the best structure in case we don't need the second parameter |
694 | | if (nbest<=1){ |
695 | | if (best.loglik > global_best(0).loglik){ |
696 | | global_best = best; |
697 | | } |
698 | | } |
699 | | |
700 | | //% uniqueness of the structure found |
701 | | int append = 1; |
702 | | |
703 | | |
704 | | for(int j = 0; j < local_max.rows() ; j++){ |
705 | | if (best.bitstr == local_max.get_row(j)){ |
706 | | append = 0; |
707 | | break; |
708 | | } |
709 | | } |
710 | | |
711 | | |
712 | | if (append){ |
713 | | local_max.append_row(best.bitstr); |
714 | | muto = muto + 1; |
715 | | } |
716 | | |
717 | | //% stopping rule: |
718 | | double maxmuto = (to-order_k-1)/lambda-to+1; |
719 | | if (to>2){ |
720 | | if (maxmuto>=muto){ |
721 | | //% fprintf('*'); |
722 | | break; |
723 | | } |
724 | | } |
725 | | |
726 | | // do statistics if necessary: |
727 | | //if (nargout>=3){ |
728 | | mutos(to-1) = muto; |
729 | | maxmutos(to-1) = maxmuto; |
730 | | //} |
731 | | } |
732 | | |
| 604 | str_aux best; |
| 605 | for ( int n_start = -1; n_start <= max_nrep; n_start++ ) { |
| 606 | str_aux last, best; |
| 607 | |
| 608 | to = n_start + 2; |
| 609 | |
| 610 | if ( n_start == -1 ) { |
| 611 | //% start from the full structure |
| 612 | last = full; |
| 613 | } else { |
| 614 | if ( n_start == 0 ) |
| 615 | //% start from the empty structure |
| 616 | last = empty; |
| 617 | |
| 618 | else { |
| 619 | //% start from random structure |
| 620 | |
| 621 | ivec last_str = find ( to_bvec<int> ( ::concat<int> ( 0, floor_i ( 2 * randu ( n_data - 1 ) ) ) ) );// this creates random vector consisting of indexes, and sorted |
| 622 | last = sestrremove ( full, setdiff ( all_str, ::concat<int> ( ::concat<int> ( 1 , last_str ), empty.strRgr ) ) ); |
| 623 | |
| 624 | } |
| 625 | } |
| 626 | //% DEBUGging print: |
| 627 | //%fprintf('STRUCTURE generated in loop %2i was %s\n', n_start, strPrintstr(last)); |
| 628 | |
| 629 | //% The loop is repeated until likelihood stops growing (break condition |
| 630 | //% used at the end; |
| 631 | |
| 632 | |
| 633 | while ( 1 ) { |
| 634 | //% This structure is going to hold the best elements |
| 635 | best = last; |
| 636 | //% Nesting by removing elements (enpoorment) |
| 637 | ivec removed_items = setdiff ( last.strRgr, belief_in ); |
| 638 | |
| 639 | ivec removed_item; |
| 640 | str_aux newone; |
| 641 | |
| 642 | for ( int i = 0; i < removed_items.length(); i++ ) { |
| 643 | removed_item = vec_1 ( removed_items ( i ) ); |
| 644 | newone = sestrremove ( last, removed_item ); |
| 645 | if ( nbest > 1 ) { |
| 646 | add_new ( global_best, newone, nbest ); |
| 647 | } |
| 648 | if ( newone.loglik > best.loglik ) { |
| 649 | best = newone; |
| 650 | } |
| 651 | } |
| 652 | //% Nesting by adding elements (enrichment) |
| 653 | ivec added_items = setdiff ( last.strMis, belief_out ); |
| 654 | ivec added_item; |
| 655 | |
| 656 | for ( int j = 0; j < added_items.length(); j++ ) { |
| 657 | added_item = vec_1 ( added_items ( j ) ); |
| 658 | newone = sestrinsert ( last, added_item ); |
| 659 | if ( nbest > 1 ) { |
| 660 | add_new ( global_best, newone, nbest ); |
| 661 | } |
| 662 | if ( newone.loglik > best.loglik ) { |
| 663 | best = newone; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | //% Break condition if likelihood does not change. |
| 672 | if ( best.loglik <= last.loglik ) |
| 673 | break; |
| 674 | else |
| 675 | //% Making best structure last structure. |
| 676 | last = best; |
| 677 | |
| 678 | |
| 679 | } |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | // % DEBUGging print: |
| 686 | //%fprintf('STRUCTURE found (local maxima) in loop %2i was %s randun_seed=%11lu randun_counter=%4lu\n', n_start, strPrintstr(best), randn('seed'), RANDUN_COUNTER); |
| 687 | |
| 688 | //% Collecting of the best structure in case we don't need the second parameter |
| 689 | if ( nbest <= 1 ) { |
| 690 | if ( best.loglik > global_best ( 0 ).loglik ) { |
| 691 | global_best = best; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | //% uniqueness of the structure found |
| 696 | int append = 1; |
| 697 | |
| 698 | |
| 699 | for ( int j = 0; j < local_max.rows() ; j++ ) { |
| 700 | if ( best.bitstr == local_max.get_row ( j ) ) { |
| 701 | append = 0; |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | |
| 707 | if ( append ) { |
| 708 | local_max.append_row ( best.bitstr ); |
| 709 | muto = muto + 1; |
| 710 | } |
| 711 | |
| 712 | //% stopping rule: |
| 713 | double maxmuto = ( to - order_k - 1 ) / lambda - to + 1; |
| 714 | if ( to > 2 ) { |
| 715 | if ( maxmuto >= muto ) { |
| 716 | //% fprintf('*'); |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | // do statistics if necessary: |
| 722 | //if (nargout>=3){ |
| 723 | mutos ( to - 1 ) = muto; |
| 724 | maxmutos ( to - 1 ) = maxmuto; |
| 725 | //} |
| 726 | } |
| 727 | |
894 | | % |
895 | | |
896 | | /*function l = seloglik1(in) |
897 | | % This is the loglikelihood (non-constant part) - this should be used in |
898 | | % frequent computation |
899 | | len = length(in.d); |
900 | | p1 = in.posit1; |
901 | | |
902 | | i1 = -0.5*in.nu *log(in.d (p1)) -0.5*sum(log(in.d ((p1+1):len))); |
903 | | i0 = -0.5*in.nu0*log(in.d0(p1)) -0.5*sum(log(in.d0((p1+1):len))); |
904 | | l = i1-i0; |
905 | | |
906 | | % DEBUGGing print: |
907 | | % fprintf('SELOGLIK1: str=%s loglik=%g\n', strPrintstr(in), l);*/ |
908 | | |
909 | | |
910 | | function l = seloglik2(in) |
911 | | % This is the loglikelihood (constant part) - this should be added to |
912 | | % everything at the end. It needs some computation, so it is useless to |
913 | | % make it for all the stuff |
914 | | logpi = log(pi); |
915 | | |
916 | | i1 = gammaln(in.nu /2) - 0.5*in.nu *logpi; |
917 | | i0 = gammaln(in.nu0/2) - 0.5*in.nu0*logpi; |
918 | | l = i1-i0; |
919 | | |
920 | | |
| 890 | % |
| 891 | |
| 892 | /*function l = seloglik1(in) |
| 893 | % This is the loglikelihood (non-constant part) - this should be used in |
| 894 | % frequent computation |
| 895 | len = length(in.d); |
| 896 | p1 = in.posit1; |
| 897 | |
| 898 | i1 = -0.5*in.nu *log(in.d (p1)) -0.5*sum(log(in.d ((p1+1):len))); |
| 899 | i0 = -0.5*in.nu0*log(in.d0(p1)) -0.5*sum(log(in.d0((p1+1):len))); |
| 900 | l = i1-i0; |
| 901 | |
| 902 | % DEBUGGing print: |
| 903 | % fprintf('SELOGLIK1: str=%s loglik=%g\n', strPrintstr(in), l);*/ |
| 904 | |
| 905 | |
| 906 | function l = seloglik2 ( in ) |
| 907 | % This is the loglikelihood ( constant part ) - this should be added to |
| 908 | % everything at the end. It needs some computation, so it is useless to |
| 909 | % make it for all the stuff |
| 910 | logpi = log ( pi ); |
| 911 | |
| 912 | i1 = gammaln ( in.nu / 2 ) - 0.5*in.nu *logpi; |
| 913 | i0 = gammaln ( in.nu0 / 2 ) - 0.5*in.nu0*logpi; |
| 914 | l = i1 - i0; |
| 915 | |
| 916 | |