public void FitTest() { double[] coefficients = { 0.50, 0.50 }; NormalDistribution[] components = new NormalDistribution[2]; components[0] = new NormalDistribution(2, 1); components[1] = new NormalDistribution(5, 1); var target = new Mixture<NormalDistribution>(coefficients, components); double[] values = { 0, 1, 1, 0, 1, 6, 6, 5, 7, 5 }; double[] part1 = values.Submatrix(0, 4); double[] part2 = values.Submatrix(5, 9); MixtureOptions options = new MixtureOptions() { Threshold = 1e-10 }; target.Fit(values, options); var actual = target; var mean1 = Accord.Statistics.Tools.Mean(part1); var var1 = Accord.Statistics.Tools.Variance(part1); Assert.AreEqual(mean1, actual.Components[0].Mean, 1e-6); Assert.AreEqual(var1, actual.Components[0].Variance, 1e-6); var mean2 = Accord.Statistics.Tools.Mean(part2); var var2 = Accord.Statistics.Tools.Variance(part2); Assert.AreEqual(mean2, actual.Components[1].Mean, 1e-6); Assert.AreEqual(var2, actual.Components[1].Variance, 1e-5); var expectedMean = Accord.Statistics.Tools.Mean(values); var actualMean = actual.Mean; Assert.AreEqual(expectedMean, actualMean, 1e-7); var expectedVar = Accord.Statistics.Tools.Variance(values, false); var actualVar = actual.Variance; Assert.AreEqual(expectedVar, actualVar, 0.15); }
public void FitTest2() { double[] coefficients = { 0.50, 0.50 }; NormalDistribution[] components = new NormalDistribution[2]; components[0] = new NormalDistribution(2, 1); components[1] = new NormalDistribution(5, 1); var target = new Mixture<NormalDistribution>(coefficients, components); double[] values = { 12512, 1, 1, 0, 1, 6, 6, 5, 7, 5 }; double[] weights = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; weights = weights.Divide(weights.Sum()); double[] part1 = values.Submatrix(1, 4); double[] part2 = values.Submatrix(5, 9); MixtureOptions opt = new MixtureOptions() { Threshold = 0.000001 }; target.Fit(values, weights, opt); var mean1 = Accord.Statistics.Tools.Mean(part1); var var1 = Accord.Statistics.Tools.Variance(part1); Assert.AreEqual(mean1, target.Components[0].Mean, 1e-5); Assert.AreEqual(var1, target.Components[0].Variance, 1e-5); var mean2 = Accord.Statistics.Tools.Mean(part2); var var2 = Accord.Statistics.Tools.Variance(part2); Assert.AreEqual(mean2, target.Components[1].Mean, 1e-5); Assert.AreEqual(var2, target.Components[1].Variance, 1e-5); var expectedMean = Accord.Statistics.Tools.WeightedMean(values, weights); var actualMean = target.Mean; Assert.AreEqual(expectedMean, actualMean, 1e-5); }
/// <summary> /// Creates a Baum-Welch with default configurations for /// hidden Markov models with normal mixture densities. /// </summary> /// public static BaumWelchLearning<MultivariateMixture<MultivariateNormalDistribution>> FromMixtureModel( HiddenMarkovModel<MultivariateMixture<MultivariateNormalDistribution>> model, NormalOptions options) { MixtureOptions mixOptions = new MixtureOptions() { Iterations = 1, InnerOptions = options }; return new BaumWelchLearning<MultivariateMixture<MultivariateNormalDistribution>>(model) { FittingOptions = mixOptions }; }