Changeset 2652

Show
Ignore:
Timestamp:
11/08/10 15:32:23 (3 years ago)
Author:
vdebuen
Message:

Nuevo método QuotientOfWeightSumsFromLog

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • tolp/OfficialTolArchiveNetwork/BysSampler/CppTools/source/CppTools.cpp

    r2647 r2652  
    5959 
    6060//-------------------------------------------------------------------- 
    61 DeclareContensClass(BMat, BMatTemporary,  
    62   BMatRandPropLogWeight); 
    63 DefMethod(1, BMatRandPropLogWeight,  
    64   "RandPropLogWeight",  
    65   2, 2, "Matrix Real", 
    66   "(Matrix logWeight, Real m)", 
    67   "Dado un vector de n pesos logarítmicos ln(W[k]) con =1...n] devuelve " 
    68   "m muestras aleatorias de la distribución de probabilidad \n" 
    69   "\n" 
    70   " Pr[X=k] = W[k] / Sum(W[j]; j=1...n) \n" 
    71   "\n" 
    72   , 
     61static BText QuotientOfWeightSumsFromLog_desc =  
     62"Dado dos vectores columna de pesos logarítmicos \n" 
     63"  logWeight_A = ( ln(W_A[1]), ..., ln(W_A[n_A]) ) \n" 
     64"  logWeight_B = ( ln(W_B[1]), ..., ln(W_B[n_B]) ) \n" 
     65"devuelve el cociente de las sumas de los pesos:\n" 
     66" Sum(W_A[j]; j=1...n_A) / Sum(W_B[j]; j=1...n_B)\n" 
     67"\n"; 
     68static double QuotientOfWeightSumsFromLog( 
     69  const DMat& logWeight_A, 
     70  const DMat& logWeight_B) 
     71//-------------------------------------------------------------------- 
     72{ 
     73  double max = BDat::NegInf(); 
     74  int j; 
     75  int n_A = logWeight_A.Rows(); 
     76  int n_B = logWeight_B.Rows(); 
     77  double sum_A=0, sum_B=0; 
     78  for(j=0; j<n_A; j++) 
     79  { 
     80    double& lwj = logWeight_A(j,0); 
     81    if(lwj>max) { max = lwj; } 
     82  } 
     83  for(j=0; j<n_B; j++) 
     84  { 
     85    double& lwj = logWeight_B(j,0); 
     86    if(lwj>max) { max = lwj; } 
     87  } 
     88  for(j=0; j<n_A; j++) 
     89  { 
     90    double& lwj = logWeight_A(j,0); 
     91    if((lwj!=BDat::NegInf()) && !IS_NAN(lwj))  
     92    { 
     93      sum_A += exp(lwj-max); 
     94    } 
     95  } 
     96  for(j=0; j<n_B; j++) 
     97  { 
     98    double& lwj = logWeight_B(j,0); 
     99    if((lwj!=BDat::NegInf()) && !IS_NAN(lwj))  
     100    { 
     101      sum_B += exp(lwj-max); 
     102    } 
     103  } 
     104  return(sum_A / sum_B); 
     105} 
     106 
     107//-------------------------------------------------------------------- 
     108DeclareContensClass(BDat, BDatTemporary,  
     109  BDatQuotientOfWeightSumsFromLog); 
     110DefMethod(1, BDatQuotientOfWeightSumsFromLog,  
     111  "QuotientOfWeightSumsFromLog", 2, 2, "Matrix Matrix", 
     112  "(Matrix logWeight_A, Matrix logWeight_B)", 
     113  QuotientOfWeightSumsFromLog_desc, 
    73114  BOperClassify::MatrixAlgebra_); 
    74 //-------------------------------------------------------------------- 
    75  
    76 //-------------------------------------------------------------------- 
     115void BDatQuotientOfWeightSumsFromLog::CalcContens() 
     116//-------------------------------------------------------------------- 
     117{ 
     118  static BText _MID = "[BysSampler::CppTools::QuotientOfWeightSumsFromLog] "; 
     119  DMat& lw_A = (DMat&)Mat(Arg(1)); 
     120  DMat& lw_B = (DMat&)Mat(Arg(2)); 
     121  int m = (int)Real(Arg(2)); 
     122  contens_ = QuotientOfWeightSumsFromLog(lw_A, lw_B); 
     123}; 
     124 
     125//-------------------------------------------------------------------- 
     126static BText RandPropLogWeight_desc =  
     127"Dado un vector columna de n pesos logarítmicos \n" 
     128" logWeight = ( ln(W[1]), ..., ln(W[n])) \n" 
     129"devuelve un vector columna con m muestras aleatorias de la " 
     130"distribución de probabilidad \n" 
     131"\n" 
     132" Pr[X=k] = W[k] / Sum(W[j]; j=1...n) \n" 
     133"\n"; 
    77134static void RandPropLogWeight( 
    78135   const DMat& logWeight, 
     
    94151    double& lwj = logWeight(j,0); 
    95152    double& wj = weight(j,0); 
    96     if((lwj==BDat::NegInf()) || (IS_NAN(lwj)))  
     153    if((lwj==BDat::NegInf()) || IS_NAN(lwj)) 
    97154    { 
    98155      wj = 0.0; 
     
    142199 
    143200//-------------------------------------------------------------------- 
     201DeclareContensClass(BMat, BMatTemporary,  
     202  BMatRandPropLogWeight); 
     203DefMethod(1, BMatRandPropLogWeight,  
     204  "RandPropLogWeight", 2, 2, "Matrix Real", 
     205  "(Matrix logWeight, Real m)", 
     206  RandPropLogWeight_desc, 
     207  BOperClassify::MatrixAlgebra_); 
    144208void BMatRandPropLogWeight::CalcContens() 
    145209//--------------------------------------------------------------------