public void RunTest() { // Example from // http://www.cs.columbia.edu/4761/notes07/chapter4.3-HMM.pdf 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 }, }; 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 }, }; HiddenMarkovModel model = new HiddenMarkovModel(states: 2, symbols: 2); MaximumLikelihoodLearning target = new MaximumLikelihoodLearning(model); target.UseLaplaceRule = false; double logLikelihood = target.Run(observations, paths); var pi = Matrix.Exp(model.Probabilities); var A = Matrix.Exp(model.Transitions); var B = Matrix.Exp(model.Emissions); Assert.AreEqual(0.5, pi[0]); Assert.AreEqual(0.5, pi[1]); Assert.AreEqual(7 / 20.0, A[0, 0], 1e-5); Assert.AreEqual(13 / 20.0, A[0, 1], 1e-5); Assert.AreEqual(14 / 20.0, A[1, 0], 1e-5); Assert.AreEqual(6 / 20.0, A[1, 1], 1e-5); Assert.AreEqual(17 / 25.0, B[0, 0]); Assert.AreEqual(8 / 25.0, B[0, 1]); Assert.AreEqual(19 / 23.0, B[1, 0]); Assert.AreEqual(4 / 23.0, B[1, 1]); Assert.AreEqual(-1.1472359046136624, logLikelihood); }
public static void MaximumLikelihoodLearning() { 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 }, }; // Now those are the visible states associated with each observation in each // observation sequence above. Note that there is always one state assigned // to each observation, so the lengths of the sequence of observations and // the sequence of states must always match. 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 our Markov model with two states (0, 1) and two symbols (0, 1) HiddenMarkovModel model = new HiddenMarkovModel(state_count: 2, symbol_count: 2); // Now we can create our learning algorithm MaximumLikelihoodLearning teacher = new MaximumLikelihoodLearning(model) { // Set some options UseLaplaceRule = false }; // and finally learn a model using the algorithm double logLikelihood = teacher.Run(observations, paths); // To check what has been learned, we can extract the emission // and transition matrices, as well as the initial probability // vector from the HMM to compare against expected values: double[] pi = model.ProbabilityVector; // { 0.5, 0.5 } double[,] A = model.TransitionMatrix; // { { 7/20, 13/20 }, { 14/20, 6/20 } } double[,] B = model.EmissionMatrix; // { { 17/25, 8/25 }, { 19/23, 4/23 } } Console.WriteLine("pi: {{{0}}}", string.Join(", ", pi)); Console.WriteLine("A: {0}", ToString(A)); Console.WriteLine("B: {0}", ToString(B)); }
public ModelTrainer() { HMM = new HiddenMarkovModel( new Forward(Enum.GetValues(typeof(LabelEnum)).Length), Enum.GetValues(typeof(TagEnum)).Length); HMM.Algorithm = HiddenMarkovModelAlgorithm.Viterbi; Teacher = new MaximumLikelihoodLearning(HMM); }
public void chunking_dataset_markov() { Chunking chunking = new Chunking(path: Path.GetTempPath()); // Learn a mapping between each word to an integer class label: var wordMap = new Codification().Learn(chunking.Words); // Learn a mapping between each tag to an integer class labels: var tagMap = new Codification().Learn(chunking.Tags); // Convert the training and testing sets into integer labels: int[][] trainX = wordMap.Transform(chunking.Training.Item1); int[][] testX = wordMap.Transform(chunking.Testing.Item1); // Convert the training and testing tags into integer labels: int[][] trainY = tagMap.Transform(chunking.Training.Item2); int[][] testY = tagMap.Transform(chunking.Testing.Item2); // Learn one Markov model using the training data var teacher = new MaximumLikelihoodLearning() { UseLaplaceRule = true, UseWeights = true }; // Use the teacher to learn a Markov model var markov = teacher.Learn(trainX, trainY); // Use the model to predict instances: int[][] predY = markov.Decide(testX); // Check the accuracy of the model: var cm = new GeneralConfusionMatrix( predicted: predY.Concatenate(), expected: testY.Concatenate()); double acc = cm.Accuracy; #if NET35 Assert.AreEqual(0.51725520822339954d, acc, 1e-10); #else Assert.AreEqual(0.43987588914452158, acc, 1e-10); #endif }
private static void hmm(int[][] trainX, int[][] trainY, int[][] testX, int[][] testY) { // Learn one Markov model using the training data var teacher = new MaximumLikelihoodLearning() { UseLaplaceRule = true, UseWeights = true }; // Use the teacher to learn a Markov model var markov = teacher.Learn(trainX, trainY); // Use the model to predict instances: int[][] predY = markov.Decide(testX); // Check the accuracy of the model: var cm = new ConfusionMatrix(predicted: predY.Concatenate(), expected: testY.Concatenate()); }
public void learn_test() { #region doc_learn // Example from // http://www.cs.columbia.edu/4761/notes07/chapter4.3-HMM.pdf // 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 a hidden Markov model with 2 states for 2 symbols var model = new HiddenMarkovModel(states: 2, symbols: 2); // Create a Maximum Likelihood learning algorithm var target = new MaximumLikelihoodLearning(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.LogEmissions.Exp(); #endregion Assert.AreEqual(0.5, pi[0]); Assert.AreEqual(0.5, pi[1]); Assert.AreEqual(7 / 20.0, A[0][0], 1e-5); Assert.AreEqual(13 / 20.0, A[0][1], 1e-5); Assert.AreEqual(14 / 20.0, A[1][0], 1e-5); Assert.AreEqual(6 / 20.0, A[1][1], 1e-5); Assert.AreEqual(17 / 25.0, B[0][0], 1e-5); Assert.AreEqual(8 / 25.0, B[0][1], 1e-5); Assert.AreEqual(19 / 23.0, B[1][0], 1e-5); Assert.AreEqual(4 / 23.0, B[1][1], 1e-5); }
public ViterbiLearning(HiddenMarkovModel hmm) { mModel = hmm; mMaximumLikelihoodLearner = new MaximumLikelihoodLearning(hmm); }
public void sequence_parsing_test() { #region doc_learn_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 #endregion Assert.AreEqual(new[] { 0, 0 }, firstPerson); Assert.AreEqual(new[] { 0, 0, 1, 0 }, secondPerson); }
public void learnTest() { #region doc_learn // Example from // http://www.cs.columbia.edu/4761/notes07/chapter4.3-HMM.pdf // 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; #endregion Assert.AreEqual(0.5, pi[0]); Assert.AreEqual(0.5, pi[1]); Assert.AreEqual(7 / 20.0, A[0][0], 1e-5); Assert.AreEqual(13 / 20.0, A[0][1], 1e-5); Assert.AreEqual(14 / 20.0, A[1][0], 1e-5); Assert.AreEqual(6 / 20.0, A[1][1], 1e-5); Assert.AreEqual(17 / 25.0, B[0].ProbabilityMassFunction(0)); Assert.AreEqual(8 / 25.0, B[0].ProbabilityMassFunction(1)); Assert.AreEqual(19 / 23.0, B[1].ProbabilityMassFunction(0)); Assert.AreEqual(4 / 23.0, B[1].ProbabilityMassFunction(1)); }