| 1 | /* logsum.c |
| 2 | * EPN, Fri Sep 7 16:44:58 2007 |
| 3 | * |
| 4 | * The FLogsum() function used for scaled integer log sums |
| 5 | * in many Infernal dp functions. This was ripped out of HMMER3 |
| 6 | * development code and Infernalized. |
| 7 | * |
| 8 | * Sean's notes from HMMER 3's logsum.c: |
| 9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * Exegesis: |
| 11 | * |
| 12 | * Internally, HMMER3 profile scores are in nats: floating point |
| 13 | * log-odds probabilities, with the log odds taken relative to |
| 14 | * background residue frequencies, and the log to the base e. |
| 15 | * |
| 16 | * The Forward algorithm needs to calculate sums of probabilities. |
| 17 | * Given two log probabilities s1 and s2, where s1 = \log |
| 18 | * \frac{p_1}{f_1}, and s2 = \log \frac{p_2}{f_2}, we need to |
| 19 | * calculate s3 = \log \frac{p_1 + p_2}{f_3}. |
| 20 | * |
| 21 | * The Forward algorithm guarantees that f_1 = f_2 = f_3, because it |
| 22 | * is always concerned with summing terms that describe different |
| 23 | * parses of the same target sequence prefix, and the product of the |
| 24 | * background frequencies for the same sequence prefix is a constant. |
| 25 | * |
| 26 | * The naive solution is s3 = log(e^{s1} + e^{s2}). This requires |
| 27 | * expensive calls to log() and exp(). |
| 28 | * |
| 29 | * A better solution is s3 = s1 + log(1 + e^{s2-s1}). s1 should be the |
| 30 | * greater, so s2-s1 is negative. For sufficiently small s2 << s1, |
| 31 | * e^{s2-s1} becomes less than the machine's FLT_EPSILON, and s3 ~= |
| 32 | * s1. (This is at about s2-s1 < -15.9, for the typical FLT_EPSILON of |
| 33 | * 1.2e-7.) |
| 34 | * |
| 35 | * With some loss of accuracy, we can precalculate log(1 + e^{s2-s1}) |
| 36 | * for a discretized range of differences (s2-s1), and compute s3 = s1 |
| 37 | * + table_lookup(s2-s1). This is what HMMER's p7_FLogsum() function |
| 38 | * does. |
| 39 | * |
| 40 | * Contents: |
| 41 | * |
| 42 | * SRE, Wed Jul 11 11:00:57 2007 [Janelia] |
| 43 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 44 | * |
| 45 | * EPN, Fri Sep 7 17:01:49 2007 |
| 46 | * |
| 47 | * Following changes made for Infernal (which uses bits not nats): |
| 48 | * o p7_ prefixes dropped. |
| 49 | * o FLogsum() duplicated, and duplicate renamed to LogSum2() to match |
| 50 | * existing calls in Infernal |
| 51 | * o magic 15.7 number changed to 23 (e^-15.7 = 1.5e-7) (2^-23. = 1.2e-7), |
| 52 | * not sure if this is right b/c I thought epsilon was 1.2e-7 (which != 1.5e-7) |
| 53 | * o changed exp() calls to sreEXP2() calls. |
| 54 | * o changed p7_IMPOSSIBLE to -INFTY (both are same: -987654321) |
| 55 | * o changed p7_ILogsumInit to init_ilogsum() to match old func calls. |
| 56 | * |
| 57 | * NOTE: there is no FLogSumInit() function in old Infernal. |
| 58 | */ |
| 59 | #include <esl_config.h> |
| 60 | #include <p7_config.h> |
| 61 | #include "config.h" |
| 62 | |
| 63 | #include <math.h> |
| 64 | #include <assert.h> |
| 65 | |
| 66 | #include "easel.h" |
| 67 | |
| 68 | #include "hmmer.h" |
| 69 | |
| 70 | #include "infernal.h" |
| 71 | |
| 72 | static int ilogsum_lookup[LOGSUM_TBL]; |
| 73 | void |
| 74 | init_ilogsum(void) |
| 75 | { |
| 76 | static int firsttime = TRUE; |
| 77 | if (!firsttime) return; |
| 78 | firsttime = FALSE; |
| 79 | |
| 80 | int i; |
| 81 | for (i = 0; i < LOGSUM_TBL; i++) |
| 82 | ilogsum_lookup[i] = rint(INTSCALE * (sreLOG2(1.+sreEXP2((double) -i/INTSCALE)))); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | int |
| 87 | ILogsum(int s1, int s2) |
| 88 | { |
| 89 | const int max = ESL_MAX(-INFTY, ESL_MAX(s1, s2)); |
| 90 | const int min = ESL_MIN(s1, s2); |
| 91 | return (min <= -INFTY || (max-min) >= LOGSUM_TBL) ? max : max + ilogsum_lookup[max-min]; |
| 92 | } |
| 93 | |
| 94 | /* guaranteed s1 >= -INFTY, s2 >= -INFTY */ |
| 95 | int |
| 96 | ILogsumNI(int s1, int s2) |
| 97 | { |
| 98 | ESL_DASSERT1((s1 > -INFTY)); |
| 99 | ESL_DASSERT1((s2 > -INFTY)); |
| 100 | /*assert(s1 > -INFTY); |
| 101 | assert(s2 > -INFTY);*/ |
| 102 | |
| 103 | const int max = ESL_MAX(s1, s2); |
| 104 | const int min = ESL_MIN(s1, s2); |
| 105 | return ((max-min) >= LOGSUM_TBL) ? max : max + ilogsum_lookup[max-min]; |
| 106 | /* about 10% slower |
| 107 | if(s1 > s2) |
| 108 | return ((s1-s2) >= LOGSUM_TBL) ? s1 : s1 + ilogsum_lookup[s1-s2]; |
| 109 | else |
| 110 | return ((s2-s1) >= LOGSUM_TBL) ? s2 : s2 + ilogsum_lookup[s2-s1]; |
| 111 | */ |
| 112 | } |
| 113 | |
| 114 | /* guaranteed s1 >= -INFTY, s2 >= -INFTY */ |
| 115 | int |
| 116 | ILogsumNI_diff(int s1a, int s1b, int s2a, int s2b, int db) |
| 117 | { |
| 118 | /* db = s1b - s2b */ |
| 119 | ESL_DASSERT1((s1a > -INFTY)); |
| 120 | ESL_DASSERT1((s1b > -INFTY)); |
| 121 | ESL_DASSERT1((s2a > -INFTY)); |
| 122 | ESL_DASSERT1((s2b > -INFTY)); |
| 123 | /*const int d = s1a-s2a+db; |
| 124 | if (d >= LOGSUM_TBL) return s1a + s1b; |
| 125 | else if (d > 0) return s1a + s1b + ilogsum_lookup[d]; |
| 126 | else if (d <= -LOGSUM_TBL) return s2a + s2b; |
| 127 | else return s2a + s2b + ilogsum_lookup[-d];*/ |
| 128 | const int d = s1a-s2a+db; |
| 129 | if(d > 0) |
| 130 | return (d >= LOGSUM_TBL) ? s1a + s1b : s1a + s1b + ilogsum_lookup[d]; |
| 131 | else |
| 132 | return (d <= LOGSUM_TBL) ? s2a + s2b : s2a + s2b + ilogsum_lookup[-d]; |
| 133 | } |
| 134 | |
| 135 | static float flogsum_lookup[LOGSUM_TBL]; |
| 136 | |
| 137 | void |
| 138 | FLogsumInit(void) |
| 139 | { |
| 140 | static int firsttime = TRUE; |
| 141 | if (!firsttime) return; |
| 142 | firsttime = FALSE; |
| 143 | |
| 144 | int i; |
| 145 | for (i = 0; i < LOGSUM_TBL; i++) |
| 146 | flogsum_lookup[i] = sreLOG2(1. + sreEXP2((double) -i / INTSCALE)); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | float |
| 151 | LogSum2(float s1, float s2) |
| 152 | { |
| 153 | const float max = ESL_MAX(s1, s2); |
| 154 | const float min = ESL_MIN(s1, s2); |
| 155 | return (min == -eslINFINITY || (max-min) >= 23.f) ? max : max + flogsum_lookup[(int)((max-min)*INTSCALE)]; |
| 156 | } |
| 157 | |
| 158 | /* yes LogSum2 and FLogsum are identical, this is for backwards compatibility */ |
| 159 | float |
| 160 | FLogsum(float s1, float s2) |
| 161 | { |
| 162 | const float max = ESL_MAX(s1, s2); |
| 163 | const float min = ESL_MIN(s1, s2); |
| 164 | #if 0 |
| 165 | return (min == -eslINFINITY || (max-min) >= 23.f) ? max : max + sreLOG2(1.0 + sreEXP2(min-max)); /* EPN: While debugging. Replaces logsum table with analytical calculation. Remember to remove! */ |
| 166 | #endif |
| 167 | return (min == -eslINFINITY || (max-min) >= 23.f) ? max : max + flogsum_lookup[(int)((max-min)*INTSCALE)]; |
| 168 | } |
| 169 | |
| 170 | #if 0 |
| 171 | /********************************************************************************** |
| 172 | * OLD LOG SUM FUNCTIONS * |
| 173 | **********************************************************************************/ |
| 174 | /* Function: ILogsum() |
| 175 | * |
| 176 | * Purpose: Return the scaled integer log probability of |
| 177 | * the sum of two probabilities p1 and p2, where |
| 178 | * p1 and p2 are also given as scaled log probabilities. |
| 179 | * |
| 180 | * log(exp(p1)+exp(p2)) = p1 + log(1 + exp(p2-p1)) for p1 > p2 |
| 181 | * |
| 182 | * For speed, builds a lookup table the first time it's called. |
| 183 | * LOGSUM_TBL is set to 20000 by default, in config.h. |
| 184 | * |
| 185 | * Because of the one-time initialization, we have to |
| 186 | * be careful in a multithreaded implementation... hence |
| 187 | * the use of pthread_once(), which forces us to put |
| 188 | * the initialization routine and the lookup table outside |
| 189 | * ILogsum(). (Thanks to Henry Gabb at Intel for pointing |
| 190 | * out this problem.) |
| 191 | * |
| 192 | * Args: p1,p2 -- scaled integer log_2 probabilities to be summed |
| 193 | * in probability space. |
| 194 | * |
| 195 | * Return: scaled integer log_2 probability of the sum. |
| 196 | */ |
| 197 | |
| 198 | static int ilogsum_lookup[LOGSUM_TBL]; |
| 199 | static void |
| 200 | init_ilogsum(void) |
| 201 | { |
| 202 | static int firsttime = 1; |
| 203 | if (firsttime) return; |
| 204 | firsttime = FALSE; |
| 205 | |
| 206 | int i; |
| 207 | for (i = 0; i < LOGSUM_TBL; i++) |
| 208 | ilogsum_lookup[i] = (int) (INTSCALE * 1.44269504 * |
| 209 | (log(1.+exp(0.69314718 * (float) -i/INTSCALE)))); |
| 210 | } |
| 211 | int |
| 212 | ILogsum(int s1, int s2) |
| 213 | { |
| 214 | if(s1 == -INFTY) return s2; /* EPN */ |
| 215 | if(s2 == -INFTY) return s1; /* EPN */ |
| 216 | |
| 217 | const int diff = s1-s2; |
| 218 | if (diff >= LOGSUM_TBL) return s1; |
| 219 | else if (diff > 0) return s1 + ilogsum_lookup[diff]; |
| 220 | else if (diff <= -LOGSUM_TBL) return s2; |
| 221 | else return s2 + ilogsum_lookup[-diff]; |
| 222 | } |
| 223 | |
| 224 | /* guaranteed s1 >= -INFTY, p2 >= -INFTY */ |
| 225 | int |
| 226 | ILogsumNI(int s1, int s2) |
| 227 | { |
| 228 | ESL_DASSERT1((s1 >= -INFTY)); |
| 229 | ESL_DASSERT1((s2 >= -INFTY)); |
| 230 | const int diff = s1-s2; |
| 231 | if (diff >= LOGSUM_TBL) return s1; |
| 232 | else if (diff <= -LOGSUM_TBL) return s2; |
| 233 | else if (diff > 0) return s1 + ilogsum_lookup[diff]; |
| 234 | else return s2 + ilogsum_lookup[-diff]; |
| 235 | } |
| 236 | |
| 237 | /* Function: LogSum2() |
| 238 | * |
| 239 | * Purpose: Returns the log_2 of the sum of two log_2 probabilities. |
| 240 | * log(exp(p1)+exp(p2)) = p1 + log(1 + exp(p2-p1)) for p1 > p2 |
| 241 | * Note that this is in log_2 space. |
| 242 | */ |
| 243 | float |
| 244 | LogSum2(float p1, float p2) |
| 245 | { |
| 246 | if (p1 > p2) |
| 247 | return (p1-p2 > 50.) ? p1 : p1 + sreLOG2(1. + pow(2.,(p2-p1))); |
| 248 | else |
| 249 | return (p2-p1 > 50.) ? p2 : p2 + sreLOG2(1. + pow(2.,(p1-p2))); |
| 250 | } |
| 251 | |
| 252 | #endif /* #if 0 */ |
| 253 |