Changeset 646

Show
Ignore:
Timestamp:
10/01/09 17:55:03 (15 years ago)
Author:
mido
Message:

arx_straux od sarky

Location:
library/bdm/estim
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/estim/arx_straux.cpp

    r607 r646  
     1 
     2 
     3#include <limits> 
    14#include "arx.h" 
    25 
     6 
     7#include <fstream> 
     8#include<iostream> 
     9#include<iomanip> 
     10 
    311namespace bdm { 
    412 
    5   struct str_aux { 
     13   
     14 
     15         
     16/*       
     17struct str_aux { 
    618        vec d0; 
    719        double nu0; 
     
    1527        int posit1;                // regressand position 
    1628        int nbits;                                 // number of bits available in double 
    17         int bitstr;  
     29        bvec bitstr;  
    1830        double loglik;          // loglikelihood 
    1931  }; 
    2032   
    21 ivec straux1(ldmat Ld, double nu, ldmat Ld0, double nu0, ivec belief, int nbest, int max_nrep, double lambda, int order_k, ivec &rgrsout){ 
     33 */ 
     34 
     35   
     36 /* bvec str_bitset( bvec in,ivec ns,int nbits){ 
     37      //int index, bitindex,n; 
     38          bvec out = in; 
     39          int n; 
     40           
     41         for (int i = 0; i < ns.length(); i++){ 
     42          n = ns(i); 
     43          out(n-2) = 1; 
     44          cout << out;  
     45          
     46         } 
     47         
     48        return out;  
     49   
     50  }*/ 
     51  
     52   
     53  void str_bitset( bvec &out,ivec ns,int nbits){ 
     54     for (int i = 0; i < ns.length(); i++){ 
     55          out(ns(i)-2) = 1; 
     56         } 
     57} 
     58 
     59 
     60 
     61 
     62 
     63 
     64double seloglik1(str_aux in){ 
     65// This is the loglikelihood (non-constant part) - this should be used in 
     66// frequent computation 
     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 
     79void 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; 
     126else 
     127   update_whole=0; 
     128end;*/ 
     129  
     130double mzero = 1e-32; 
     131  
     132if (Dr<mzero){ 
     133   Dr=0; 
     134} 
     135  
     136double r0   = r(R,0); 
     137double kD   = Df; 
     138double kr   = r0 * Dr; 
     139 
     140 
     141Df   = kD + r0 * kr; 
     142  
     143if (Df > mzero){ 
     144    kD = kD / Df; 
     145    kr = kr / Df; 
     146}else{ 
     147    kD = 1; 
     148    kr = 0; 
     149} 
     150  
     151Dr = Dr * kD; 
     152  
     153// Try to uncomment marked stuff (*) if in numerical problems, but I don't 
     154// think it can make any difference for normal healthy floating-point unit 
     155//if update_whole; 
     156   r = r - r0*f; 
     157//   rout(R) = 0;   // * could be needed for some nonsense cases(or numeric reasons?), normally not 
     158   f = f + kr*r; 
     159//   fout(R) = 1;   // * could be needed for some nonsense cases(or numeric reasons?), normally not 
     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); 
     166end;*/ 
     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  
     198int j = i+1; 
     199  
     200double pomd = d(i); 
     201d(i) = d(j); 
     202d(j) = pomd; 
     203  
     204/*vec pomL   = L.get_row(i); 
     205L.set_row(i, L.get_row(j));    
     206L.set_row(j,pomL);*/ 
     207  
     208L.swap_rows(i,j); 
     209L.swap_cols(i,j); 
     210 
     211/*pomL   = L.get_col(i); 
     212L.set_col(i, L.get_col(j)); 
     213L.set_col(j,pomL);*/ 
     214  
     215//% We must be working with LINES of matrix L ! 
     216  
     217 
     218 
     219mat r = L.get_row(i); 
     220r = r.transpose(); 
     221r = r.transpose(); 
     222//???????????????? 
     223mat f = L.get_row(j); 
     224f = f.transpose(); 
     225f = f.transpose(); 
     226 
     227 
     228 
     229 
     230double Dr = d(i); 
     231double Df = d(j); 
     232  
     233sedydr(r, f, Dr, Df, j); 
     234  
     235 
     236 
     237double r0 = r(i,0); 
     238Dr = Dr*r0*r0; 
     239r  = r/r0; 
     240 
     241 
     242 
     243mat pom_mat = r.transpose(); 
     244L.set_row(i, pom_mat.get_row(0));  
     245pom_mat = f.transpose(); 
     246L.set_row(j, pom_mat.get_row(0));  
     247 
     248d(i)   = Dr; 
     249d(j)   = Df; 
     250  
     251L(i,i) = 1; 
     252L(j,j) = 1; 
     253  
     254   
     255  } 
     256   
     257 
     258 void str_bitres(bvec &out,ivec ns,int nbits){ 
     259     
     260         
     261        for (int i = 0; i < ns.length(); i++){ 
     262          out(ns(i)-2) = 0; 
     263   } 
     264      
     265   
     266  } 
     267   
     268str_aux sestrremove(str_aux in,ivec removed_elements){ 
     269//% Removes elements from regressor 
     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   
     314/* 
     315   
     316  Array<str_aux> add_new(Array<str_aux> global_best,str_aux newone,int nbest){ 
     317// Eventually add to global best, but do not go over nbest values 
     318// Also avoids repeating things, which makes this function awfully slow 
     319           
     320          Array<str_aux> global_best_out; 
     321          if (global_best.length() >= nbest){ 
     322      //logliks = [global_best.loglik]; 
     323       
     324          vec logliks(1);  
     325          logliks(0) =  global_best(0).loglik; 
     326          for (int j = 1; j < global_best.length(); j++) 
     327          logliks = concat(logliks, global_best(j).loglik); 
     328           
     329          int i, addit; 
     330          double loglik = min(logliks, i); 
     331      global_best_out = global_best; 
     332          if (loglik < newone.loglik){ 
     333         //         if ~any(logliks == new.loglik); 
     334          addit=1; 
     335                  
     336                  
     337          
     338                  if (newone.bitstr.length() == 1) { 
     339          for (int j = 0; j < global_best.length(); j++){ 
     340                  for(int i = 0; i < global_best(j).bitstr.length(); i++){ 
     341 
     342                          if (newone.bitstr(0) == global_best(j).bitstr(i)){ 
     343               addit = 0; 
     344               break; 
     345                          } 
     346                          }    
     347          }    
     348                  } 
     349                 if (addit){ 
     350            global_best_out(i) = newone; 
     351            // DEBUGging print: 
     352            // fprintf('ADDED structure, add_new: %s, loglik=%g\n', strPrintstr(new), new.loglik); 
     353             }         
     354         }   
     355} 
     356   else 
     357       global_best_out = concat(global_best, newone); 
     358 
     359  return global_best_out; 
     360   
     361  } 
     362   
     363*/ 
     364    
     365void add_new(Array<str_aux> &global_best,str_aux newone,int nbest){ 
     366// Eventually add to global best, but do not go over nbest values 
     367// Also avoids repeating things, which makes this function awfully slow 
     368           
     369          int  addit, i = 0; 
     370if (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){ 
     423// Moves elements into regressor 
     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){ 
     459// This is the loglikelihood (constant part) - this should be added to 
     460// everything at the end. It needs some computation, so it is useless to 
     461// make it for all the stuff 
     462   double logpi = log(pi); 
     463  
     464   double i1 = lgamma(in.nu /2) - 0.5*in.nu *logpi; 
     465   double i0 = lgamma(in.nu0/2) - 0.5*in.nu0*logpi; 
     466   return i1-i0; 
     467  } 
     468 
     469  
     470 
     471   
     472  ivec straux1(ldmat Ld, double nu, ldmat Ld0, double nu0, ivec belief, int nbest, int max_nrep, double lambda, int order_k, Array<str_aux> &rgrsout/*, stat &statistics*/){ 
    22473// see utia_legacy/ticket_12/ implementation and str_test.m 
    23474 
     
    30481int n_data = d.length(); 
    31482 
    32 ivec belief_out = find(belief==4)+1; // we are avoiding to put this into regressor 
    33 ivec belief_in  = find(belief==1)+1; // we are instantly keeping this in regressor 
     483ivec belief_out = find(belief==4)+2; // we are avoiding to put this into regressor 
     484ivec belief_in  = find(belief==1)+2; // we are instantly keeping this in regressor 
     485 
    34486 
    35487str_aux full; 
     
    40492full.L   = L; 
    41493full.d   = d; 
     494 
     495 
     496 
     497/*full.d0  = "0.012360650875200       0.975779169502626        1.209840558439000"; 
     498          
     499full.L0  = "1.0000         0         0;" 
     500           "0.999427690134298    1.0000         0;" 
     501           "0.546994424043659   0.534335486953833    1.0000"; 
     502 
     503 
     504full.L   = "1.0000         0         0;" 
     505           "0.364376353850780    1.0000         0;" 
     506           "1.222141096674815   1.286534510946323    1.0000"; 
     507 
     508full.d   =  "0.001610356837691          3.497566869589465       3.236640487818002"; 
     509*/ 
     510 
     511fstream F; 
     512 
     513 
     514 
     515 
     516F.open("soubor3.txt", ios::in); 
     517F << setiosflags(ios::scientific); 
     518F << setprecision(16); 
     519F.flags(0x1); 
     520 
     521 
     522for (int i = 0; i < n_data ; i++){ 
     523F >> full.d0(i); 
     524} 
     525 
     526 
     527 
     528for (int i =0; i < n_data; i++){ 
     529        for (int j = 0 ; j < n_data  ; j++){ 
     530F >> full.L0(j,i); 
     531} 
     532} 
     533 
     534for (int i = 0; i < n_data ; i++){ 
     535F >> full.d(i); 
     536} 
     537 
     538 
     539for (int i =0; i < n_data; i++){ 
     540        for (int j = 0 ; j < n_data  ; j++){ 
     541F >> full.L(j,i); 
     542} 
     543} 
     544 
     545 
     546full.nu0 = nu0; 
    42547full.nu  = nu; 
    43548full.strL = linspace(1,n_data); 
    44549full.strRgr = linspace(2,n_data); 
    45550full.strMis = ivec(0);                      
    46 full.posit1 = 1;    
    47 //full.nbits  = floor(log2(bitmax))-1;  //!!!!!!! 
    48  
    49 return ivec(0); //  
    50 } 
    51  
    52  
    53  
    54 } 
     551full.posit1 = 1; 
     552full.bitstr.set_size(n_data-1); 
     553full.bitstr.clear(); 
     554str_bitset(full.bitstr,full.strRgr,full.nbits); 
     555//full.nbits  = std::numeric_lim its<double>::digits-1;  // number of bits available in double 
     556/*bvec in(n_data-1); 
     557in.clear(); 
     558full.bitstr = str_bitset(in,full.strRgr,full.nbits);*/ 
     559 
     560 
     561 
     562 
     563full.loglik = seloglik1(full);       // % loglikelihood 
     564    
     565 
     566 
     567 
     568 
     569//% construct full and empty structure 
     570full = sestrremove(full,belief_out); 
     571str_aux empty = sestrremove(full,setdiff(full.strRgr,belief_in)); 
     572  
     573//% stopping rule calculation: 
     574 
     575 
     576 
     577 
     578bmat local_max(0,0); 
     579int to, muto = 0; 
     580  
     581//% statistics: 
     582//double cputime0 = cputime;  
     583//if nargout>=3; 
     584    
     585 CPU_Timer timer; 
     586 timer.start();  
     587  
     588   ivec mutos(max_nrep+2); 
     589   vec maxmutos(max_nrep+2); 
     590   mutos.zeros(); 
     591   maxmutos.zeros(); 
     592 
     593 
     594//end; 
     595//% ---------------------- 
     596  
     597//% For stopping-rule calculation 
     598//%so       = 2^(n_data -1-length(belief_in)- length(belief_out)); % do we use this ? 
     599//% ---------------------- 
     600  
     601ivec all_str = linspace(1,n_data); 
     602  
     603Array<str_aux> global_best(1);  
     604global_best(0) = full; 
     605 
     606 
     607//% MAIN LOOP is here. 
     608 
     609str_aux   best; 
     610for (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           
     644ivec 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  
     733//% Aftermath: The best structure was in: global_best 
     734  
     735//% Updating loglikelihoods: we have to add the constant stuff 
     736 
     737 
     738 
     739for (int f=0 ; f <global_best.length(); f++){ 
     740   global_best(f).loglik = global_best(f).loglik + seloglik2(global_best(f)); 
     741} 
     742  
     743/*for f=1:length(global_best); 
     744   global_best(f).loglik = global_best(f).loglik + seloglik2(global_best(f)); 
     745end;*/ 
     746 
     747 
     748//% Making first output parameter: 
     749 
     750int max_i = 0; 
     751for (int j = 1; j < global_best.length(); j++) 
     752  if  (global_best(max_i).loglik < (global_best(j).loglik)) max_i = j; 
     753 
     754best = global_best(max_i); 
     755 
     756//% Making the second output parameter 
     757 
     758vec logliks(global_best.length()); 
     759for (int j = 0; j < logliks.length(); j++) 
     760 logliks(j) = global_best(j).loglik; 
     761 
     762ivec i = sort_index(logliks); 
     763rgrsout.set_length(global_best.length()); 
     764 
     765for (int j = global_best.length() - 1; j >= 0; j--) 
     766rgrsout(j) = global_best(i(j)); 
     767  
     768//if (nargout>=3); 
     769    
     770    
     771str_statistics statistics; 
     772 
     773   statistics.allstrs = 2^(n_data -1-length(belief_in) - length(belief_out)); 
     774   statistics.nrand   = to-2; 
     775   statistics.unique  = muto; 
     776   statistics.to  = to; 
     777   statistics.cputime_seconds = timer.get_time(); 
     778   statistics.itemspeed       = statistics.to / statistics.cputime_seconds; 
     779   statistics.muto = muto; 
     780   statistics.mutos = mutos; 
     781   statistics.maxmutos = maxmutos; 
     782//end; 
     783 
     784return best.strRgr; 
     785 
     786}  
     787 
     788#ifdef LADIM 
     789//% randun seed stuff: 
     790//%randn('seed',SEED); 
     791  
     792//% --------------------- END of MAIN program -------------------- 
     793  
     794% This is needed for bitstr manipulations 
     795/*function out = str_bitset(in,ns,nbits) 
     796   out = in; 
     797   for n = ns; 
     798      index = 1+floor((n-2)/nbits); 
     799      bitindex = 1+rem(n-2,nbits); 
     800      out(index) = bitset(out(index),bitindex); 
     801   end;   
     802function out = str_bitres(in,ns,nbits) 
     803   out = in; 
     804   for n = ns; 
     805      index = 1+floor((n-2)/nbits); 
     806      bitindex = 1+rem(n-2,nbits); 
     807      mask = bitset(0,bitindex); 
     808      out(index) = bitxor(bitor(out(index),mask),mask); 
     809   end;*/ 
     810  
     811function out = strPrintstr(in) 
     812   out = '0'; 
     813   nbits = in.nbits; 
     814   for f = 2:length(in.d0); 
     815      index = 1+floor((f-2)/nbits); 
     816      bitindex = 1+rem(f-2,nbits); 
     817      if bitget(in.bitstr(index),bitindex); 
     818          out(f) = '1'; 
     819      else; 
     820          out(f) = '0'; 
     821      end; 
     822   end; 
     823  
     824/*function global_best_out = add_new(global_best,new,nbest) 
     825% Eventually add to global best, but do not go over nbest values 
     826% Also avoids repeating things, which makes this function awfully slow 
     827   if length(global_best)>=nbest; 
     828      logliks = [global_best.loglik]; 
     829      [loglik i] = min(logliks); 
     830      global_best_out = global_best; 
     831      if loglik<new.loglik; 
     832         %         if ~any(logliks == new.loglik); 
     833         addit=1; 
     834         for f = [global_best.bitstr]; 
     835            if f == new.bitstr; 
     836               addit = 0; 
     837               break; 
     838            end; 
     839         end;          
     840         if addit; 
     841            global_best_out(i) = new; 
     842            % DEBUGging print: 
     843            % fprintf('ADDED structure, add_new: %s, loglik=%g\n', strPrintstr(new), new.loglik); 
     844         end;          
     845      end; 
     846   else; 
     847      global_best_out = [global_best new]; 
     848   end;*/ 
     849  
     850/*function out = sestrremove(in,removed_elements); 
     851% Removes elements from regressor 
     852   n_strL = length(in.strL); 
     853   out = in; 
     854   for f=removed_elements; 
     855      posit1 = find(out.strL==1); 
     856      positf = find(out.strL==f); 
     857      for g=(positf-1):-1:posit1; 
     858         % BEGIN: We are swapping g and g+1 NOW!!!! 
     859         [out.L, out.d]   = seswapudl(out.L, out.d, g); 
     860         [out.L0, out.d0]   = seswapudl(out.L0, out.d0, g); 
     861         out.strL([g g+1]) = [out.strL(g+1) out.strL(g)]; 
     862         % END 
     863      end; 
     864   end; 
     865   out.posit1 = find(out.strL==1); 
     866   out.strRgr = out.strL((out.posit1+1):n_strL); 
     867   out.strMis = out.strL(1:(out.posit1-1)); 
     868   out.bitstr = str_bitres(out.bitstr,removed_elements,out.nbits); 
     869   out.loglik = seloglik1(out);*/ 
     870    
     871/*function out = sestrinsert(in,inserted_elements); 
     872% Moves elements into regressor 
     873   n_strL = length(in.strL); 
     874   out = in; 
     875   for f=inserted_elements; 
     876      posit1 = find(out.strL==1); 
     877      positf = find(out.strL==f); 
     878      for g=positf:(posit1-1); 
     879          % BEGIN: We are swapping g and g+1 NOW!!!! 
     880          [out.L,  out.d]   = seswapudl(out.L,  out.d,  g); 
     881          [out.L0, out.d0]  = seswapudl(out.L0, out.d0, g); 
     882          out.strL([g g+1]) = [out.strL(g+1) out.strL(g)]; 
     883          % END 
     884      end; 
     885   end;          
     886   out.posit1 = find(out.strL==1); 
     887   out.strRgr = out.strL((out.posit1+1):n_strL); 
     888   out.strMis = out.strL(1:(out.posit1-1)); 
     889   out.bitstr = str_bitset(out.bitstr,inserted_elements,out.nbits); 
     890   out.loglik = seloglik1(out);*/ 
     891  
     892% 
     893% seloglik_real = seloglik1 + seloglik2 
     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  
     910function 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  
     921/*function [Lout, dout] = seswapudl(L,d,i); 
     922%SESWAPUDL swaps information matrix in decomposition V=L^T diag(d) L 
     923% 
     924%  [Lout, dout] = seswapudl(L,d,i); 
     925% 
     926% L : lower triangular matrix with 1's on diagonal of the decomposistion 
     927% d : diagonal vector of diagonal matrix of the decomposition 
     928% i : index of line to be swapped with the next one  
     929% Lout : output lower triangular matrix 
     930% dout : output diagional vector of diagonal matrix D 
     931% 
     932% Description: 
     933%  Lout' * diag(dout) * Lout = P(i,i+1) * L' * diag(d) * L * P(i,i+1); 
     934%  
     935%  Where permutation matrix P(i,j) permutates columns if applied from the 
     936%  right and line if applied from the left. 
     937%    
     938% Note: naming: 
     939%       se = structure estimation 
     940%       lite = light, simple 
     941%       udl = U*D*L, or more precisely, L'*D*L, also called as ld 
     942%    
     943% Design  : L. Tesar 
     944% Updated : Feb 2003 
     945% Project : post-ProDaCTool 
     946% Reference: sedydr 
     947    
     948j = i+1; 
     949  
     950pomd = d(i); 
     951d(i) = d(j); 
     952d(j) = pomd; 
     953  
     954pomL   = L(i,:); 
     955L(i,:) = L(j,:); 
     956L(j,:) = pomL; 
     957  
     958pomL   = L(:,i); 
     959L(:,i) = L(:,j); 
     960L(:,j) = pomL; 
     961  
     962% We must be working with LINES of matrix L ! 
     963  
     964r  = L(i,:)';  
     965f  = L(j,:)'; 
     966Dr = d(i); 
     967Df = d(j); 
     968  
     969[r, f, Dr, Df] = sedydr(r, f, Dr, Df, j); 
     970  
     971r0 = r(i); 
     972Dr = Dr*r0*r0; 
     973r  = r/r0; 
     974  
     975L(i,:) = r';  
     976L(j,:) = f'; 
     977d(i)   = Dr; 
     978d(j)   = Df; 
     979  
     980L(i,i) = 1; 
     981L(j,j) = 1; 
     982  
     983Lout = L; 
     984dout = d;*/ 
     985  
     986/*function [rout, fout, Drout, Dfout, kr] = sedydr(r,f,Dr,Df,R,jl,jh); 
     987%SEDYDR dyadic reduction, performs transformation of sum of 2 dyads 
     988% 
     989% [rout, fout, Drout, Dfout, kr] = sedydr(r,f,Dr,Df,R,jl,jh); 
     990% [rout, fout, Drout, Dfout] = sedydr(r,f,Dr,Df,R); 
     991% 
     992% Description: dyadic reduction, performs transformation of sum of  
     993%  2 dyads r*Dr*r'+ f*Df*f' so that the element of r pointed by R is zeroed 
     994% 
     995%     r    : column vector of reduced dyad 
     996%     f    : column vector of reducing dyad 
     997%     Dr   : scalar with weight of reduced dyad 
     998%     Df   : scalar with weight of reducing dyad 
     999%     R    : scalar number giving 1 based index to the element of r, 
     1000%            which is to be reduced to  
     1001%            zero; the corresponding element of f is assumed to be 1. 
     1002%     jl   : lower index of the range within which the dyads are  
     1003%            modified (can be omitted, then everything is updated) 
     1004%     jh   : upper index of the range within which the dyads are  
     1005%            modified (can be omitted then everything is updated) 
     1006%     rout,fout,Drout,dfout : resulting two dyads 
     1007%     kr   : coefficient used in the transformation of r 
     1008%            rnew = r + kr*f 
     1009% 
     1010% Description: dyadic reduction, performs transformation of sum of  
     1011%            2 dyads r*Dr*r'+ f*Df*f' so that the element of r indexed by R is zeroed 
     1012% Remark1: Constant mzero means machine zero and should be modified 
     1013%           according to the precision of particular machine 
     1014% Remark2: jl and jh are, in fact, obsolete. It takes longer time to 
     1015%           compute them compared to plain version. The reason is that we 
     1016%           are doing vector operations in m-file. Other reason is that 
     1017%           we need to copy whole vector anyway. It can save half of time for 
     1018%           c-file, if you use it correctly. (please do tests) 
     1019% 
     1020% Note: naming: 
     1021%       se = structure estimation 
     1022%       dydr = dyadic reduction 
     1023% 
     1024% Original Fortran design: V. Peterka 17-7-89 
     1025% Modified for c-language: probably R. Kulhavy 
     1026% Modified for m-language: L. Tesar 2/2003 
     1027% Updated: Feb 2003 
     1028% Project: post-ProDaCTool 
     1029% Reference: none 
     1030    
     1031if nargin<6; 
     1032   update_whole=1; 
     1033else 
     1034   update_whole=0; 
     1035end; 
     1036  
     1037mzero = 1e-32; 
     1038  
     1039if Dr<mzero; 
     1040   Dr=0; 
     1041end; 
     1042  
     1043r0   = r(R); 
     1044kD   = Df; 
     1045kr   = r0 * Dr; 
     1046Dfout   = kD + r0 * kr; 
     1047  
     1048if Dfout > mzero; 
     1049    kD = kD / Dfout; 
     1050    kr = kr / Dfout; 
     1051else; 
     1052    kD = 1; 
     1053    kr = 0; 
     1054end; 
     1055  
     1056Drout = Dr * kD; 
     1057  
     1058% Try to uncomment marked stuff (*) if in numerical problems, but I don't 
     1059% think it can make any difference for normal healthy floating-point unit 
     1060if update_whole; 
     1061   rout = r - r0*f; 
     1062%   rout(R) = 0;   % * could be needed for some nonsense cases(or numeric reasons?), normally not 
     1063   fout = f + kr*rout; 
     1064%   fout(R) = 1;   % * could be needed for some nonsense cases(or numeric reasons?), normally not 
     1065else;   
     1066   rout = r; 
     1067   fout = f; 
     1068   rout(jl:jh) = r(jl:jh) - r0 * f(jl:jh); 
     1069   rout(R) = 0;    
     1070   fout(jl:jh) = f(jl:jh) + kr * rout(jl:jh); 
     1071end;*/ 
     1072  
     1073  
     1074  
     1075#endif 
     1076  
     1077  
     1078} 
     1079 
  • library/bdm/estim/arx_straux.h

    r606 r646  
    1616namespace bdm { 
    1717 
     18         
     19struct str_aux { 
     20        vec d0; 
     21        double nu0; 
     22        mat L0; 
     23        mat L; 
     24        vec d; 
     25        double nu; 
     26        ivec strL;                 // Current structure of L and d 
     27        ivec strRgr;               // Structure elements currently inside regressor (after regressand) 
     28        ivec strMis;               // structure elements, that are currently outside regressor (before regressand) 
     29        int posit1;                // regressand position 
     30        int nbits;                                 // number of bits available in double 
     31        bvec bitstr;  
     32        double loglik;          // loglikelihood 
     33  }; 
     34   
     35 
     36 
     37struct str_statistics { 
     38 long long int allstrs; 
     39 int nrand; 
     40 int unique; 
     41 int to; 
     42 double cputime_seconds; 
     43 double itemspeed; 
     44 int muto; 
     45 ivec mutos; 
     46 vec maxmutos; 
     47}; 
     48 
     49 
    1850        //! Rplication of Ludvik Tesar original straux1 from mixtools straux1 
    19 ivec straux1(ldmat Ld, double nu, ldmat Ld0, double nu0, ivec belief, int nbest, int max_nrep, double lambda, int order_k, ivec &rgrsout); 
     51ivec straux1(ldmat Ld, double nu, ldmat Ld0, double nu0, ivec belief, int nbest, int max_nrep, double lambda, int order_k, Array<str_aux> &rgrsout); 
    2052 
    2153}