コード例 #1
0
ファイル: ViterbiTest.cs プロジェクト: xg86/HmmDotNet
        public void Run_3ObservationsAnd2StatesAnd123Model_PathCount3()
        {
            var algo = new Viterbi(false);

            var observations = new List <IObservation>
            {
                new Observation(new double[] { 0 }, "Red"),
                new Observation(new double[] { 1 }, "Blue"),
                new Observation(new double[] { 0 }, "Red")
            };

            var startDistribution = new [] { 1 / 3d, 1 / 3d, 1 / 3d };

            var states = new List <IState> {
                new State(0, "One"), new State(1, "Two"), new State(2, "Three")
            };

            var tpm = new double[][] { new[] { 0.3, 0.6, 0.1 }, new[] { 0.5, 0.2, 0.3 }, new[] { 0.4, 0.1, 0.5 } };

            var distributions = new List <IDistribution>
            {
                new FirstDistribution(), new SecondDistribution(), new ThirdDistribution()
            };

            var path = algo.Run(observations, states, startDistribution, tpm, distributions);

            Assert.AreEqual(path.Count, 3);
            Assert.AreEqual("One", path[0].Description);
            Assert.AreEqual("Two", path[1].Description);
            Assert.AreEqual("One", path[2].Description);
        }
コード例 #2
0
ファイル: ViterbiTest.cs プロジェクト: xg86/HmmDotNet
        public void Run_3ObservationsAnd2StatesAndRainySunnyModel_PathCount3()
        {
            var algo = new Viterbi(false);

            var observations = new List <IObservation>
            {
                new Observation(new double[] { 0 }, "walk"),
                new Observation(new double[] { 1 }, "shop"),
                new Observation(new double[] { 2 }, "clean")
            };

            var startDistribution = new [] { 0.6, 0.4 };

            var states = new List <IState> {
                new State(0, "Rainy"), new State(1, "Sunny")
            };

            var tpm = new double[][] { new[] { 0.7, 0.3 }, new[] { 0.4, 0.6 } };

            var distributions = new List <IDistribution> {
                new RainyDistribution(), new SunnyDistribution()
            };

            var path = algo.Run(observations, states, startDistribution, tpm, distributions);

            Assert.AreEqual(path.Count, 3);
            Assert.AreEqual("Sunny", path[0].Description);
            Assert.AreEqual("Rainy", path[1].Description);
            Assert.AreEqual("Rainy", path[2].Description);
        }
コード例 #3
0
ファイル: ViterbiTest.cs プロジェクト: xg86/HmmDotNet
        public void Run_4ObservationsAnd2StatesAndSTModel_PathCount4()
        {
            var observations = new List <IObservation>
            {
                new Observation(new double[] { 0 }, "A"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 1 }, "B"),
                new Observation(new double[] { 0 }, "A")
            };

            var startDistribution = new[] { 0.85, 0.15 };

            var states = new List <IState> {
                new State(0, "s"), new State(1, "t")
            };

            var tpm = new double[2][];

            tpm[0] = new[] { 0.3, 0.7 };
            tpm[1] = new[] { 0.1, 0.9 };

            var distributions = new List <IDistribution> {
                new HealthyDistribution(), new OkDistribution(), new SickDistribution()
            };

            var algo = new Viterbi(false);
            var path = algo.Run(observations, states, startDistribution, tpm, distributions);

            Assert.AreEqual(path.Count, 4);
            Assert.AreEqual("s", path[0].Description);
            Assert.AreEqual("t", path[1].Description);
            Assert.AreEqual("t", path[2].Description);
            Assert.AreEqual("t", path[3].Description);
        }
コード例 #4
0
ファイル: ViterbiTest.cs プロジェクト: xg86/HmmDotNet
        public void Run_Normalized5ObservationsAnd3StatesAndHealthySickModel_PathCount5()
        {
            var algo = new Viterbi(true);

            var observations = new List <IObservation>
            {
                new Observation(new double[] { 0 }, "high"),
                new Observation(new double[] { 1 }, "average"),
                new Observation(new double[] { 1 }, "average"),
                new Observation(new double[] { 2 }, "low"),
                new Observation(new double[] { 2 }, "low")
            };

            var startDistribution = new[] { 1 / 3d, 1 / 3d, 1 / 3d };

            var states = new List <IState> {
                new State(0, "Healthy"), new State(1, "OK"), new State(2, "Sick")
            };

            var tpm = new double[][] { new[] { 0.4, 0.3, 0.3 }, new[] { 0.2, 0.6, 0.2 }, new[] { 0, 0.4, 0.6 } };

            var distributions = new List <IDistribution> {
                new HealthyDistribution(), new OkDistribution(), new SickDistribution()
            };

            var path = algo.Run(observations, states, startDistribution, tpm, distributions);

            Assert.AreEqual(path.Count, 5);
            Assert.AreEqual("Healthy", path[0].Description);
            Assert.AreEqual("OK", path[1].Description);
            Assert.AreEqual("OK", path[2].Description);
            Assert.AreEqual("Sick", path[3].Description);
            Assert.AreEqual("Sick", path[4].Description);
        }
コード例 #5
0
ファイル: ViterbiMixtureTest.cs プロジェクト: xg86/HmmDotNet
        public void Run_100LengthObservations2MixtureDistribution_PathCount100()
        {
            var algo = new Viterbi(false);

            var path = algo.Run(_observations, _states, _startDistribution, _tpm, _mixtures);

            Assert.AreEqual(path.Count, 100);
        }
コード例 #6
0
        private double[] PredictNextValue <TDistribution>(IHiddenMarkovModel <TDistribution> model, double[][] trainingSet) where TDistribution : IDistribution
        {
            var alg = new Viterbi(model.Normalized);
            var mpp = alg.Run(Helper.Convert(trainingSet), model.GetStates(), model.Pi, model.TransitionProbabilityMatrix, model.GetEmissions());

            var emission   = model.Emission[mpp[trainingSet.Length - 1].Index];
            var prediction = CalculatePredictionValue(emission, trainingSet);

            return(prediction);
        }
コード例 #7
0
        private ViterbiResult RunViterbi(FilterResult filterResult, string fileSha1)
        {
            // Load any user-defined state machines.
            List <UserState> userStates;

            try
            {
                userStates = Loader.LoadUserStates(MainForm.Program.UserStatesEnabled,
                                                   MainForm.Program.UserStatesPath);
            }
            catch (Exception ex)
            {
                DisplayExceptionMessages(ex, "User-Defined States");
                return(null);
            }
            ViterbiResult viterbiResultFields = null;

            try
            {
                write("Running Viterbi on fields");
                DateTime dt = DateTime.Now;
#if _GENERALPARSE
                Viterbi viterbi = new Viterbi(RunType.GeneralParse, false);
                viterbiResultFields = viterbi.Run(filterResult.UnfilteredBlocks, this.filePath);
#else
#if SKIPREALWORK
                viterbiResultFields        = new ViterbiResult();
                viterbiResultFields.Fields = new List <ViterbiField>();
#else
                if (filterResult.UnfilteredBlocks.Count > 0)
                {
                    ThreadedViterbi tv = new ThreadedViterbi(filterResult.UnfilteredBlocks, RunType.GeneralParse, userStates, this.filePath, this.fileSha1);
                    viterbiResultFields = tv.RunThreadedViterbi();
                    TimeSpan ts = DateTime.Now.Subtract(dt);
                    write("Time elapsed for Viterbi fields: {0}", ts.ToString("c"));
                    write("Field count: {0}", viterbiResultFields.Fields.Count);
                }
#endif
#endif
                filterResult.UnfilteredBlocks.Clear(); // Allow gc to clean things up
            }
            catch (ThreadAbortException)
            {
                return(null);
            }
            catch (Exception ex)
            {
                DisplayExceptionMessages(ex, "Viterbi Fields");
                return(null);
            }
            return(viterbiResultFields);
        }
コード例 #8
0
ファイル: ViterbiTest.cs プロジェクト: xg86/HmmDotNet
        public void Run_Normalized9ObservationsAnd2StatesAndHLModel_PathCount9()
        {
            var algo = new Viterbi(true);

            var observations = new List <IObservation>
            {
                new Observation(new double[] { 2 }, "G"),
                new Observation(new double[] { 2 }, "G"),
                new Observation(new double[] { 1 }, "C"),
                new Observation(new double[] { 0 }, "A"),
                new Observation(new double[] { 1 }, "C"),
                new Observation(new double[] { 3 }, "T"),
                new Observation(new double[] { 2 }, "G"),
                new Observation(new double[] { 0 }, "A"),
                new Observation(new double[] { 0 }, "A")
            };

            var startDistribution = new[] { 0.5, 0.5 };

            var states = new List <IState> {
                new State(0, "H"), new State(1, "L")
            };

            var tpm = new double[][] { new[] { 0.5, 0.5 }, new[] { 0.4, 0.6 } };

            var distributions = new List <IDistribution> {
                new HDistribution(), new LDistribution()
            };

            var path = algo.Run(observations, states, startDistribution, tpm, distributions);

            Assert.AreEqual(path.Count, 9);
            Assert.AreEqual("H", path[0].Description);
            Assert.AreEqual("H", path[1].Description);
            Assert.AreEqual("H", path[2].Description);
            Assert.AreEqual("L", path[3].Description);
            Assert.AreEqual("L", path[4].Description);
            Assert.AreEqual("L", path[5].Description);
            Assert.AreEqual("L", path[6].Description);
            Assert.AreEqual("L", path[7].Description);
            Assert.AreEqual("L", path[8].Description);
        }