Exemplo n.º 1
0
        public double[][] Estimate(KsiGammaTransitionProbabilityMatrixParameters <TDistribution> parameters)
        {
            if (_estimatedTransitionProbabilityMatrix != null)
            {
                return(_estimatedTransitionProbabilityMatrix);
            }
            _estimatedTransitionProbabilityMatrix = new double[parameters.Model.N][];

            for (var i = 0; i < parameters.Model.N; i++)
            {
                _estimatedTransitionProbabilityMatrix[i] = new double[parameters.Model.N];
                for (var j = 0; j < parameters.Model.N; j++)
                {
                    double denominator = (parameters.Model.Normalized) ? double.NaN : 0, nominator = (parameters.Model.Normalized) ? double.NaN : 0;
                    for (var t = 0; t < parameters.T - 1; t++)
                    {
                        if (parameters.Model.Normalized)
                        {
                            nominator   = LogExtention.eLnSum(nominator, parameters.Ksi[t][i, j]);
                            denominator = LogExtention.eLnSum(denominator, parameters.Gamma[t][i]);
                        }
                        else
                        {
                            nominator   += parameters.Ksi[t][i, j];
                            denominator += parameters.Gamma[t][i];
                        }
                    }
                    _estimatedTransitionProbabilityMatrix[i][j] = CalculateTransitionProbabilityMatrixEntry(nominator, denominator, parameters.Normalized);
                }
            }
            return(_estimatedTransitionProbabilityMatrix);
        }
        public void Estimate_KsiGammaParameters_TransitionProbabilityMatrixCalculatedAndReturned()
        {
            const int numberOfStates = 2;

            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2010, 12, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <NormalDistribution>()
            {
                NumberOfStates = numberOfStates, Emissions = CreateEmissions(observations, numberOfStates)
            });

            model.Normalized = true;
            var observationsList = new List <IObservation>();

            for (var i = 0; i < observations.Length; i++)
            {
                observationsList.Add(new Observation(observations[i], i.ToString()));
            }
            var baseEstimator = new BasicEstimationParameters <NormalDistribution> {
                Model = model, Observations = Helper.Convert(observations), Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <NormalDistribution>();
            var alpha          = alphaEstimator.Estimate(baseEstimator);
            var betaEstimator  = new BetaEstimator <NormalDistribution>();
            var beta           = betaEstimator.Estimate(baseEstimator);
            var @params        = new AdvancedEstimationParameters <NormalDistribution>
            {
                Alpha        = alpha,
                Beta         = beta,
                Observations = observationsList,
                Model        = model,
                Normalized   = model.Normalized
            };
            var gammaEstimator = new GammaEstimator <NormalDistribution>();
            var ksiEstimator   = new KsiEstimator <NormalDistribution>();
            var gamma          = gammaEstimator.Estimate(@params);
            var ksi            = ksiEstimator.Estimate(@params);
            var estimator      = new TransitionProbabilityEstimator <NormalDistribution>();
            var parameters     = new KsiGammaTransitionProbabilityMatrixParameters <NormalDistribution>
            {
                Model      = model,
                Ksi        = ksi,
                Gamma      = gamma,
                T          = observations.Length,
                Normalized = model.Normalized
            };

            var estimatedTransitionProbabilityMatrix = estimator.Estimate(parameters);

            Assert.AreEqual(1d, Math.Round(estimatedTransitionProbabilityMatrix[0][0] + estimatedTransitionProbabilityMatrix[0][1], 5));
            Assert.AreEqual(1d, Math.Round(estimatedTransitionProbabilityMatrix[1][0] + estimatedTransitionProbabilityMatrix[1][1], 5));
        }