/// <summary> /// Returns a random index of the matrix state column using the given distribution. /// </summary> /// <param name="state">Current state.</param> /// <param name="matrix">Probability distribution matrix.</param> /// <returns>Random index of the matrix state column.</returns> private int randomExtraction(int state, MySparse2DMatrix matrix) { var rand = SystemRandomSource.Default; var extraction = rand.NextDouble(); for (int i = 0; i < Symbols; i++) { if (extraction < matrix.getValue(state, i)) { return(i); } extraction -= matrix.getValue(state, i); } return(-1); }
/// <summary> /// Determining the frequency of the transition-emission pair values /// and dividing it by the probability of the entire string. /// /// Using the scaled values, I don't need to divide it for the /// probability of the entire string anymore /// </summary> /// <param name="t">Current time.</param> /// <param name="nextObservation">Observation at time t + 1.</param> /// <returns>Probability of transition from each state to any other at time t</returns> private MySparse2DMatrix calcXi(int t, int nextObservation) { if (tempInstancts == null) { throw new ArgumentNullException("tempInstancts"); } if (tempInstancts[t].Alpha == null || tempInstancts[t].Beta == null) { throw new ArgumentNullException("Alpha and Beta aren't set"); } MySparse2DMatrix currentXi = new MySparse2DMatrix(); double s = 0; for (int i = 0; i < States; i++) { for (int j = 0; j < States; j++) { double temp = tempInstancts[t].Alpha[i] * Transitions.getValue(i, j) * Emissions.getValue(j, nextObservation) * tempInstancts[t + 1].Beta[j]; s += temp; if (temp != 0) { currentXi.setValue(i, j, temp); } } } if (s != 0) // Scaling { for (int i = 0; i < States; i++) { for (int j = 0; j < States; j++) { if (currentXi.getValue(i, j) != 0) { currentXi.setValue(i, j, currentXi.getValue(i, j) / s); } } } } return(currentXi); }