Click or drag to resize
Accord.NET (logo)

HiddenMarkovModelPredict Method (Int32, Double)

Predicts the next observation 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,
	out double[] logLikelihoods
)
Request Example View Source

Parameters

observations
Type: SystemInt32
A sequence of observations. Predictions will be made regarding the next observation that should be coming after the last observation in this sequence.
logLikelihoods
Type: SystemDouble
The log-likelihood of the different symbols for the next observation. In order to convert those values to probabilities, exponentiate the values in the vector (using the Exp function) and divide each value by the vector sum. This will give the probability of each next possible symbol to be the next observation in the sequence.

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