| 173 | // ---------------------------------------------------------------------------------------- |
| 174 | |
| 175 | void sample_hmm ( |
| 176 | dlib::rand& rnd, |
| 177 | const matrix<double>& transition_probabilities, |
| 178 | const matrix<double>& emission_probabilities, |
| 179 | unsigned long previous_label, |
| 180 | unsigned long& next_label, |
| 181 | unsigned long& next_sample |
| 182 | ) |
| 183 | /*! |
| 184 | requires |
| 185 | - previous_label < transition_probabilities.nr() |
| 186 | - transition_probabilities.nr() == transition_probabilities.nc() |
| 187 | - transition_probabilities.nr() == emission_probabilities.nr() |
| 188 | - The rows of transition_probabilities and emission_probabilities must sum to 1. |
| 189 | (i.e. sum_cols(transition_probabilities) and sum_cols(emission_probabilities) |
| 190 | must evaluate to vectors of all 1s.) |
| 191 | ensures |
| 192 | - This function randomly samples the HMM defined by transition_probabilities |
| 193 | and emission_probabilities assuming that the previous hidden state |
| 194 | was previous_label. |
| 195 | - The HMM is defined by: |
| 196 | - P(next_label |previous_label) == transition_probabilities(previous_label, next_label) |
| 197 | - P(next_sample|next_label) == emission_probabilities (next_label, next_sample) |
| 198 | - #next_label == the sampled value of the hidden state |
| 199 | - #next_sample == the sampled value of the observed state |
| 200 | !*/ |
| 201 | { |
| 202 | // sample next_label |
| 203 | double p = rnd.get_random_double(); |
| 204 | for (long c = 0; p >= 0 && c < transition_probabilities.nc(); ++c) |
| 205 | { |
| 206 | next_label = c; |
| 207 | p -= transition_probabilities(previous_label, c); |
| 208 | } |
| 209 | |
| 210 | // now sample next_sample |
| 211 | p = rnd.get_random_double(); |
| 212 | for (long c = 0; p >= 0 && c < emission_probabilities.nc(); ++c) |
| 213 | { |
| 214 | next_sample = c; |
| 215 | p -= emission_probabilities(next_label, c); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // ---------------------------------------------------------------------------------------- |
| 220 |
no test coverage detected