| 305 | // ---------------------------------------------------------------------------------------- |
| 306 | |
| 307 | void sample_hmm ( |
| 308 | dlib::rand& rnd, |
| 309 | const matrix<double>& transition_probabilities, |
| 310 | const matrix<double>& emission_probabilities, |
| 311 | unsigned long previous_label, |
| 312 | unsigned long& next_label, |
| 313 | unsigned long& next_sample |
| 314 | ) |
| 315 | /*! |
| 316 | requires |
| 317 | - previous_label < transition_probabilities.nr() |
| 318 | - transition_probabilities.nr() == transition_probabilities.nc() |
| 319 | - transition_probabilities.nr() == emission_probabilities.nr() |
| 320 | - The rows of transition_probabilities and emission_probabilities must sum to 1. |
| 321 | (i.e. sum_cols(transition_probabilities) and sum_cols(emission_probabilities) |
| 322 | must evaluate to vectors of all 1s.) |
| 323 | ensures |
| 324 | - This function randomly samples the HMM defined by transition_probabilities |
| 325 | and emission_probabilities assuming that the previous hidden state |
| 326 | was previous_label. |
| 327 | - The HMM is defined by: |
| 328 | - P(next_label |previous_label) == transition_probabilities(previous_label, next_label) |
| 329 | - P(next_sample|next_label) == emission_probabilities (next_label, next_sample) |
| 330 | - #next_label == the sampled value of the hidden state |
| 331 | - #next_sample == the sampled value of the observed state |
| 332 | !*/ |
| 333 | { |
| 334 | // sample next_label |
| 335 | double p = rnd.get_random_double(); |
| 336 | for (long c = 0; p >= 0 && c < transition_probabilities.nc(); ++c) |
| 337 | { |
| 338 | next_label = c; |
| 339 | p -= transition_probabilities(previous_label, c); |
| 340 | } |
| 341 | |
| 342 | // now sample next_sample |
| 343 | p = rnd.get_random_double(); |
| 344 | for (long c = 0; p >= 0 && c < emission_probabilities.nc(); ++c) |
| 345 | { |
| 346 | next_sample = c; |
| 347 | p -= emission_probabilities(next_label, c); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // ---------------------------------------------------------------------------------------- |
| 352 |
no test coverage detected