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 | | , |
| 61 | static 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"; |
| 68 | static 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 | //-------------------------------------------------------------------- |
| 108 | DeclareContensClass(BDat, BDatTemporary, |
| 109 | BDatQuotientOfWeightSumsFromLog); |
| 110 | DefMethod(1, BDatQuotientOfWeightSumsFromLog, |
| 111 | "QuotientOfWeightSumsFromLog", 2, 2, "Matrix Matrix", |
| 112 | "(Matrix logWeight_A, Matrix logWeight_B)", |
| 113 | QuotientOfWeightSumsFromLog_desc, |
74 | | //-------------------------------------------------------------------- |
75 | | |
76 | | //-------------------------------------------------------------------- |
| 115 | void 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 | //-------------------------------------------------------------------- |
| 126 | static 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"; |
| 201 | DeclareContensClass(BMat, BMatTemporary, |
| 202 | BMatRandPropLogWeight); |
| 203 | DefMethod(1, BMatRandPropLogWeight, |
| 204 | "RandPropLogWeight", 2, 2, "Matrix Real", |
| 205 | "(Matrix logWeight, Real m)", |
| 206 | RandPropLogWeight_desc, |
| 207 | BOperClassify::MatrixAlgebra_); |