Click or drag to resize
Accord.NET (logo)

MaximumLikelihoodLearningTDistribution, TObservation Class

Maximum Likelihood learning algorithm for discrete-density Hidden Markov Models.
Inheritance Hierarchy
  Accord.Statistics.Models.Markov.LearningBaseMaximumLikelihoodLearningHiddenMarkovModelTDistribution, TObservation, TDistribution, TObservation, IFittingOptions
    Accord.Statistics.Models.Markov.LearningMaximumLikelihoodLearningTDistribution, TObservation

Namespace:  Accord.Statistics.Models.Markov.Learning
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.8.0
public class MaximumLikelihoodLearning<TDistribution, TObservation> : BaseMaximumLikelihoodLearning<HiddenMarkovModel<TDistribution, TObservation>, TDistribution, TObservation, IFittingOptions>, 
	ISupervisedLearning<HiddenMarkovModel<TDistribution, TObservation>, TObservation[], int[]>
where TDistribution : Object, IFittableDistribution<TObservation>
Request Example View Source

Type Parameters


The MaximumLikelihoodLearningTDistribution, TObservation type exposes the following members.

Public propertyEmissions
Gets or sets the function that initializes the emission distributions in the hidden Markov Models.
(Inherited from BaseMaximumLikelihoodLearningTModel, TDistribution, TObservation, TOptions.)
Public propertyFittingOptions
Gets or sets the distribution fitting options to use when estimating distribution densities during learning.
(Inherited from BaseMaximumLikelihoodLearningTModel, TDistribution, TObservation, TOptions.)
Public propertyModel
Gets the model being trained.
(Inherited from BaseMaximumLikelihoodLearningTModel, TDistribution, TObservation, TOptions.)
Public propertyToken
Gets or sets a cancellation token that can be used to stop the learning algorithm while it is running.
(Inherited from BaseMaximumLikelihoodLearningTModel, TDistribution, TObservation, TOptions.)
Public propertyUseLaplaceRule
Gets or sets whether to use Laplace's rule of succession to avoid zero probabilities.
(Inherited from BaseMaximumLikelihoodLearningTModel, TDistribution, TObservation, TOptions.)
Public propertyUseWeights
Gets or sets whether the emission fitting algorithm should present weighted samples or simply the clustered samples to the density estimation methods.
(Inherited from BaseMaximumLikelihoodLearningTModel, TDistribution, TObservation, TOptions.)
Protected methodCreate
Creates an instance of the model to be learned. Inheritors of this abstract class must define this method so new models can be created from the training data.
(Overrides BaseMaximumLikelihoodLearningTModel, TDistribution, TObservation, TOptionsCreate(TObservation, Int32).)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLearn
Learns a model that can map the given inputs to the given outputs.
(Inherited from BaseMaximumLikelihoodLearningTModel, TDistribution, TObservation, TOptions.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Extension Methods
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)

The maximum likelihood estimate is a supervised learning algorithm. It considers both the sequence of observations as well as the sequence of states in the Markov model are visible and thus during training.

Often, the Maximum Likelihood Estimate can be used to give a starting point to a unsupervised algorithm, making possible to use semi-supervised techniques with HMMs. It is possible, for example, to use MLE to guess initial values for an HMM given a small set of manually labeled labels, and then further estimate this model using the Viterbi learning algorithm.


The following example comes from Prof. Yechiam Yemini slides on Hidden Markov Models, available at In this example, we will be specifying both the sequence of observations and the sequence of states assigned to each observation in each sequence to learn our Markov model.

// Example from

// Inputs
int[][] observations =
    new int[] { 0,0,0,1,0,0 },
    new int[] { 1,0,0,1,0,0 },
    new int[] { 0,0,1,0,0,0 },
    new int[] { 0,0,0,0,1,0 },
    new int[] { 1,0,0,0,1,0 },
    new int[] { 0,0,0,1,1,0 },
    new int[] { 1,0,0,0,0,0 },
    new int[] { 1,0,1,0,0,0 },

// Outputs
int[][] paths =
    new int[] { 0,0,1,0,1,0 },
    new int[] { 1,0,1,0,1,0 },
    new int[] { 1,0,0,1,1,0 },
    new int[] { 1,0,1,1,1,0 },
    new int[] { 1,0,0,1,0,1 },
    new int[] { 0,0,1,0,0,1 },
    new int[] { 0,0,1,1,0,1 },
    new int[] { 0,1,1,1,0,0 },

// Create the initial discrete distributions for 2 symbols
var initial = new GeneralDiscreteDistribution(symbols: 2);

// Create a generic hidden Markov model based on the initial distribution with 2 states
var model = new HiddenMarkovModel<GeneralDiscreteDistribution, int>(states: 2, emissions: initial);

// Create a new (fully supervised) Maximum Likelihood learning algorithm for HMMs
var target = new MaximumLikelihoodLearning<GeneralDiscreteDistribution, int>(model)
    UseLaplaceRule = false // don't use Laplace smoothing (to reproduce the example)

// Learn the Markov model
target.Learn(observations, paths);

// Recover the learned parameters
var pi = model.LogInitial.Exp();
var A = model.LogTransitions.Exp();
var B = model.Emissions;

The following example shows how hidden Markov models trained using Maximum Likelihood Learning can be used in the context of fraud analysis.

// Ensure results are reproducible
Accord.Math.Random.Generator.Seed = 0;

// Let's say we have the following data about credit card transactions,
// where the data is organized in order of transaction, per credit card 
// holder. Everytime the "Time" column starts at zero, denotes that the
// sequence of observations follow will correspond to transactions of the
// same person:

double[,] data =
    // "Time", "V1",   "V2",  "V3", "V4", "V5", "Amount",  "Fraud"
    {      0,   0.521, 0.124, 0.622, 15.2, 25.6,   2.70,      0 }, // first person, ok
    {      1,   0.121, 0.124, 0.822, 12.2, 25.6,   42.0,      0 }, // first person, ok

    {      0,   0.551, 0.124, 0.422, 17.5, 25.6,   20.0,      0 }, // second person, ok
    {      1,   0.136, 0.154, 0.322, 15.3, 25.6,   50.0,      0 }, // second person, ok
    {      2,   0.721, 0.240, 0.422, 12.2, 25.6,   100.0,     1 }, // second person, fraud!
    {      3,   0.222, 0.126, 0.722, 18.1, 25.8,   10.0,      0 }, // second person, ok

// Transform the above data into a jagged matrix
double[][][] input;
int[][] states;
transform(data, out input, out states);

// Determine here the number of dimensions in the observations (in this case, 6)
int observationDimensions = 6; // 6 columns: "V1", "V2", "V3", "V4", "V5", "Amount"

// Create some prior distributions to help initialize our parameters
var priorC = new WishartDistribution(dimension: observationDimensions, degreesOfFreedom: 10); // this 10 is just some random number, you might have to tune as if it was a hyperparameter
var priorM = new MultivariateNormalDistribution(dimension: observationDimensions);

// Configure the learning algorithms to train the sequence classifier
var teacher = new MaximumLikelihoodLearning<MultivariateNormalDistribution, double[]>()
    // Their emissions will be multivariate Normal distributions initialized using the prior distributions
    Emissions = (j) => new MultivariateNormalDistribution(mean: priorM.Generate(), covariance: priorC.Generate()),

    // We will prevent our covariance matrices from becoming degenerate by adding a small 
    // regularization value to their diagonal until they become positive-definite again:
    FittingOptions = new NormalOptions()
        Regularization = 1e-6

// Use the teacher to learn a new HMM 
var hmm = teacher.Learn(input, states);

// Use the HMM to predict whether the transations were fradulent or not:
int[] firstPerson = hmm.Decide(input[0]); // predict the first person, output should be: 0, 0

int[] secondPerson = hmm.Decide(input[1]); // predict the second person, output should be: 0, 0, 1, 0

Where the transform function is defined as:

private static void transform(double[,] data, out double[][][] input, out int[][] states)
    var sequences = new List<double[][]>();
    var classLabels = new List<int[]>();

    List<double[]> currentSequence = null;
    List<int> currentLabels = null;
    for (int i = 0; i < data.Rows(); i++)
        // Check if the first column contains a zero, this would be an indication
        // that a new sequence (for a different person) is beginning:
        if (data[i, 0] == 0)
            // Yes, this is a new sequence. Check if we were building
            // a sequence before, and if yes, save it to the list:
            if (currentSequence != null)
                // Save the sequence we had so far 

                currentSequence = null;
                currentLabels = null;

            // We will be starting a new sequence
            currentSequence = new List<double[]>();
            currentLabels = new List<int>();

        double[] features = data.GetRow(i).Get(1, 7); // Get values in columns from 1 (inclusive) to 7 (exclusive), meaning "V1", "V2", "V3", "V4", "V5", and "Amount"
        int classLabel = (int)data[i, 7]; // The seventh index corresponds to the class label column ("Class")

        // Save this information:

    // Check if there are any sequences and labels that we haven't saved yet:
    if (currentSequence != null)
        // Yes there are: save them

    input = sequences.ToArray();
    states = classLabels.ToArray();
See Also