Click or drag to resize
Accord.NET (logo)

HiddenMarkovModelPredict Method (Int32, Int32, Double, Double)

Predicts the next observations occurring after a given observation sequence.

Namespace:  Accord.Statistics.Models.Markov
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.8.0
Syntax
public int[] Predict(
	int[] observations,
	int next,
	out double logLikelihood,
	out double[][] logLikelihoods
)
Request Example View Source

Parameters

observations
Type: SystemInt32
A sequence of observations. Predictions will be made regarding the next observations that should be coming after the last observation in this sequence.
next
Type: SystemInt32
The number of observations to be predicted. Default is 1.
logLikelihood
Type: SystemDouble
The log-likelihood of the given sequence, plus the predicted next observations. Exponentiate this value (use the System.Math.Exp function) to obtain a likelihood value.
logLikelihoods
Type: SystemDouble
The log-likelihood of the different symbols for each predicted next observations. In order to convert those values to probabilities, exponentiate the values in the vectors (using the Exp function) and divide each value by their vector's sum.

Return Value

Type: Int32
Examples
// We will try to create a Hidden Markov Model which
// can recognize (and predict) the following sequences:
int[][] sequences =
{
    new[] { 1, 3, 5, 7, 9, 11, 13 },
    new[] { 1, 3, 5, 7, 9, 11 },
    new[] { 1, 3, 5, 7, 9, 11, 13 },
    new[] { 1, 3, 3, 7, 7, 9, 11, 11, 13, 13 },
    new[] { 1, 3, 7, 9, 11, 13 },
};

// Create a Baum-Welch HMM algorithm:
var teacher = new BaumWelchLearning()
{
    // Let's creates a left-to-right (forward)
    // Hidden Markov Model with 7 hidden states
    Topology = new Forward(7),

    // We'll try to fit the model to the data until the difference in
    // the average log-likelihood changes only by as little as 0.0001
    Tolerance = 0.0001,
    Iterations = 0 // do not impose a limit on the number of iterations
};

// Use the algorithm to learn a new Markov model:
HiddenMarkovModel hmm = teacher.Learn(sequences);

// Now, we will try to predict the next 1 observation in a base symbol sequence
int[] prediction = hmm.Predict(observations: new[] { 1, 3, 5, 7, 9 }, next: 1);

// At this point, prediction should be int[] { 11 }
int nextSymbol = prediction[0]; // should be 11.

// We can try to predict further, but this might not work very 
// well due the Markov assumption between the transition states:
int[] nextSymbols = hmm.Predict(observations: new[] { 1, 3, 5, 7 }, next: 2);

// At this point, nextSymbols should be int[] { 9, 11 }
int nextSymbol1 = nextSymbols[0]; // 9
int nextSymbol2 = nextSymbols[1]; // 11
See Also