When I upgrade to Accord Framework 3.2.0 and run software I get 2 'obsolete' errors:
Warning 1 'HiddenMarkovClassifier'<'MultivariateNormalDistribution'>'' is obsolete: 'Please use HiddenMarkovClassifier'<'TDistribution, TObservation'>' instead.'
Warning 14 'BaumWelchLearning'<'MultivariateNormalDistribution'>' is obsolete: 'Please use BaumWelchLearning'<'TDistribution, TObservation'>' instead.'
There is no apparent API for the BaumWelchLearning'<'TDistribution, TObservation'>'. When I go to the new API for HiddenMarkovClassifier'<'TDistribution'>' and the relatively minor changes reflected on that documentation it does not fix the 2 issues above but does improve analysis run time.
// Create continuous sequences. In the sequences below, there
// seems to be two states, one for values between 0 and 1 and
// another for values between 5 and 7. The states seems to be
// switched on every observation.
double[][] sequences = new double[][]
{
new double[] { 0.1, 5.2, 0.3, 6.7, 0.1, 6.0 },
new double[] { 0.2, 6.2, 0.3, 6.3, 0.1, 5.0 },
new double[] { 0.1, 7.0, 0.1, 7.0, 0.2, 5.6 },
};
// Specify a initial normal distribution for the samples.
var density = new NormalDistribution();
// Creates a continuous hidden Markov Model with two states organized in a forward
// topology and an underlying univariate Normal distribution as probability density.
var model = new HiddenMarkovModel<NormalDistribution, double>(new Ergodic(2), density);
// Configure the learning algorithms to train the sequence classifier until the
// difference in the average log-likelihood changes only by as little as 0.0001
var teacher = new BaumWelchLearning<NormalDistribution, double>(model)
{
Tolerance = 0.0001,
Iterations = 0,
};
// Fit the model
teacher.Learn(sequences);
// See the log-probability of the sequences learned
double a1 = model.Evaluate(new[] { 0.1, 5.2, 0.3, 6.7, 0.1, 6.0 }); // -0.12799388666109757
double a2 = model.Evaluate(new[] { 0.2, 6.2, 0.3, 6.3, 0.1, 5.0 }); // 0.01171157434400194
// See the probability of an unrelated sequence
double a3 = model.Evaluate(new[] { 1.1, 2.2, 1.3, 3.2, 4.2, 1.0 }); // -298.7465244473417double likelihood = Math.Exp(logLikelihood);
a1 = Math.Exp(a1); // 0.879
a2 = Math.Exp(a2); // 1.011
a3 = Math.Exp(a3); // 0.000
// We can also ask the model to decode one of the sequences. After
// this step the resulting sequence will be: { 0, 1, 0, 1, 0, 1 }
int[] states = model.Decode(new[] { 0.1, 5.2, 0.3, 6.7, 0.1, 6.0 });
var density = new MultivariateNormalDistribution(3);
var model = new HiddenMarkovModel<MultivariateNormalDistribution, double[]>(new Forward(5), density);
var learning = new BaumWelchLearning<MultivariateNormalDistribution, double[]>(model)
{
Tolerance = 0.0001,
Iterations = 0,
FittingOptions = new NormalOptions() { Regularization = 0.0001 }
};
learning.Learn(observations);
int[] y = hmm.Decide(sequence_of_observations); // will provide a class label for each observation in the sequence
double ll = hmm.LogLikelihood(sequence_of_observations); // will provide the log-likelihood of the sequence
// Create a Continuous density Hidden Markov Model Sequence Classifier
// to detect a univariate sequence and the same sequence backwards.
double[][] sequences = new double[][]
{
new double[] { 0,1,2,3,4 }, // This is the first sequence with label = 0
new double[] { 4,3,2,1,0 }, // This is the second sequence with label = 1
};
// Labels for the sequences
int[] labels = { 0, 1 };
// Creates a sequence classifier containing 2 hidden Markov Models
// with 2 states and an underlying Normal distribution as density.
var density = new NormalDistribution();
var classifier = new HiddenMarkovClassifier<NormalDistribution, double>(2, new Ergodic(2), density);
// Configure the learning algorithms to train the sequence classifier
var teacher = new HiddenMarkovClassifierLearning<NormalDistribution, double>(classifier)
{
// Train each model until the log-likelihood changes less than 0.001
Learner = modelIndex => new BaumWelchLearning<NormalDistribution, double>(classifier.Models[modelIndex])
{
Tolerance = 0.0001,
Iterations = 0
}
};
// Train the sequence classifier using the algorithm
teacher.Learn(sequences, labels);
double logLikelihood = teacher.LogLikelihood;
// Calculate the probability that the given
// sequences originated from the model
double likelihood1, likelihood2;
// Try to classify the first sequence (output should be 0)
int c1 = classifier.Decide(sequences[0]);
likelihood1 = classifier.Probability(sequences[0]);
// Try to classify the second sequence (output should be 1)
int c2 = classifier.Decide(sequences[1]);
likelihood2 = classifier.Probability(sequences[1]);
Thank you for creating & supporting this wonderful library. I am working with "multivariate" model and have gotten to a point where I can train an HMM and use "Decode" to identify states and "Decide" to classify given the observations. Now I am trying to use predict (to predict next set of observations) but hitting a wall saying "System.NotSupportedException" occurred in Accord.Statistics.dll.....I am sure I am missing something here, by any chance do you have any insights or an example of usage for predict?
Dim density = New MultivariateNormalDistribution(10)
Dim model = New HiddenMarkovModel(Of MultivariateNormalDistribution, Double())(New Ergodic(4, True), density)
Dim learning = New BaumWelchLearning(Of MultivariateNormalDistribution, Double())(model) With { _
.Tolerance = 0.000001, _
.Iterations = 0, _
.FittingOptions = New NormalOptions() With { _
.Regularization = 0.000001 _
} _
}
' Fit the model
learning.Learn(sequences)
'test(0)(10)(10) <-last 10 observations with 10 variables.....
Dim test_states As Integer() = model.Decide(tests(0)) -> tests(0)
nextObservation = model.Predict(tests(0)) -> this is where I get error
Thanks,
Manish