/// <summary> /// Constructs a new Hidden Markov Model. /// </summary> /// <param name="symbols">The number of output symbols used for this model.</param> /// <param name="states">The number of states for this model.</param> /// <param name="type">The topology which should be used by this model.</param> public HiddenMarkovModel(int symbols, int states, HiddenMarkovModelType type) : this(null, null, states, type) { if (symbols <= 0) { throw new ArgumentOutOfRangeException("symbols", "Number of symbols should be higher than zero."); } this.symbols = symbols; this.B = new double[states, symbols]; // Initialize B with uniform probabilities for (int i = 0; i < states; i++) { for (int j = 0; j < symbols; j++) { B[i, j] = 1.0 / symbols; } } }
/// <summary> /// Constructs a new Hidden Markov Model Sequence Classifier. /// </summary> public MarkovSequenceClassifier(int classes, int symbols, int[] states, String[] names, HiddenMarkovModelType type) { models = new HiddenMarkovModel[classes]; // For each of the possible output classes for (int i = 0; i < classes; i++) { // Create a Hidden Markov Models with the given number of symbols and states models[i] = new HiddenMarkovModel(symbols, states[i], type); if (names != null) { models[i].Tag = names[i]; } } }
/// <summary> /// Constructs a new Hidden Markov Model. /// </summary> private HiddenMarkovModel(double[,] transitions, double[] probabilities, int?states, HiddenMarkovModelType type) { // Automatically determine missing parameters #region Number of states N if (states != null) { if (states <= 0) { throw new ArgumentOutOfRangeException("states", "Number of states should be higher than zero."); } } else { if (probabilities != null) { states = probabilities.Length; } else if (transitions != null) { states = transitions.GetLength(0); } else { throw new ArgumentException("Number of states could not be determined.", "states"); } } int n = states.Value; this.states = n; #endregion #region Transitions Matrix A if (transitions != null) { if (transitions.GetLength(0) != states) { throw new ArgumentException( "Transition matrix should have the same dimensions as the number of model states.", "transitions"); } if (transitions.GetLength(0) != transitions.GetLength(1)) { throw new ArgumentException("Transition matrix should be square.", "transitions"); } } else { if (type == HiddenMarkovModelType.Ergodic) { // Create A using uniform distribution transitions = new double[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { transitions[i, j] = 1.0 / n; } } } else { // Create A using uniform distribution, // without allowing backward transitions. transitions = new double[n, n]; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { transitions[i, j] = 1.0 / (n - i); } } } } this.A = transitions; #endregion #region Initial Probabilities pi if (probabilities != null) { if (probabilities.Length != n) { throw new ArgumentException( "Initial probabilities should have the same length as the number of model states.", "probabilities"); } } else { // Create pi as left-to-right probabilities = new double[n]; probabilities[0] = 1.0; } this.pi = probabilities; #endregion }
/// <summary> /// Constructs a new Hidden Markov Model. /// </summary> private HiddenMarkovModel(double[,] transitions, double[] probabilities, int? states, HiddenMarkovModelType type) { // Automatically determine missing parameters #region Number of states N if (states != null) { if (states <= 0) { throw new ArgumentOutOfRangeException("states", "Number of states should be higher than zero."); } } else { if (probabilities != null) { states = probabilities.Length; } else if (transitions != null) { states = transitions.GetLength(0); } else { throw new ArgumentException("Number of states could not be determined.", "states"); } } int n = states.Value; this.states = n; #endregion #region Transitions Matrix A if (transitions != null) { if (transitions.GetLength(0) != states) throw new ArgumentException( "Transition matrix should have the same dimensions as the number of model states.", "transitions"); if (transitions.GetLength(0) != transitions.GetLength(1)) throw new ArgumentException("Transition matrix should be square.", "transitions"); } else { if (type == HiddenMarkovModelType.Ergodic) { // Create A using uniform distribution transitions = new double[n, n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) transitions[i, j] = 1.0 / n; } else { // Create A using uniform distribution, // without allowing backward transitions. transitions = new double[n, n]; for (int i = 0; i < n; i++) for (int j = i; j < n; j++) transitions[i, j] = 1.0 / (n - i); } } this.A = transitions; #endregion #region Initial Probabilities pi if (probabilities != null) { if (probabilities.Length != n) throw new ArgumentException( "Initial probabilities should have the same length as the number of model states.", "probabilities"); } else { // Create pi as left-to-right probabilities = new double[n]; probabilities[0] = 1.0; } this.pi = probabilities; #endregion }
/// <summary> /// Constructs a new Hidden Markov Model. /// </summary> /// <param name="symbols">The number of output symbols used for this model.</param> /// <param name="states">The number of states for this model.</param> /// <param name="type">The topology which should be used by this model.</param> public HiddenMarkovModel(int symbols, int states, HiddenMarkovModelType type) : this(null, null, states, type) { if (symbols <= 0) { throw new ArgumentOutOfRangeException("symbols", "Number of symbols should be higher than zero."); } this.symbols = symbols; this.B = new double[states, symbols]; // Initialize B with uniform probabilities for (int i = 0; i < states; i++) for (int j = 0; j < symbols; j++) B[i, j] = 1.0 / symbols; }
/// <summary> /// Constructs a new Hidden Markov Model Sequence Classifier. /// </summary> public MarkovSequenceClassifier(int classes, int symbols, int[] states, String[] names, HiddenMarkovModelType type) { models = new HiddenMarkovModel[classes]; // For each of the possible output classes for (int i = 0; i < classes; i++) { // Create a Hidden Markov Models with the given number of symbols and states models[i] = new HiddenMarkovModel(symbols, states[i], type); if (names != null) models[i].Tag = names[i]; } }