Ejemplo n.º 1
0
        public void Sigma_RightLeftAndParametersAnnNotNormalized_SigmaCalculated()
        {
            var delta        = 3;
            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 11, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfStates = NumberOfStatesRightLeft, Delta = delta, Emissions = CreateEmissions(observations, NumberOfStatesRightLeft, NumberOfComponents)
            });                                                                                                                                                                                                                                                                       //new HiddenMarkovModelState<Mixture<IMultivariateDistribution>>(NumberOfStatesRightLeft, delta, CreateEmissions(observations, NumberOfStatesRightLeft, NumberOfComponents)) { LogNormalized = false };

            model.Normalized = false;
            var baseParameters = new BasicEstimationParameters <Mixture <IMultivariateDistribution> > {
                Model = model, Observations = Helper.Convert(observations), Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <Mixture <IMultivariateDistribution> >();
            var alpha          = alphaEstimator.Estimate(baseParameters);
            var betaEstimator  = new BetaEstimator <Mixture <IMultivariateDistribution> >();
            var beta           = betaEstimator.Estimate(baseParameters);

            var parameters            = new ParameterEstimations <Mixture <IMultivariateDistribution> >(model, Helper.Convert(observations), alpha, beta);
            var sigma                 = new MixtureSigmaEstimator <Mixture <IMultivariateDistribution> >();
            var mixtureGammaEstimator = new MixtureGammaEstimator <Mixture <IMultivariateDistribution> >();
            var mixtureMuEstimator    = new MixtureMuEstimator <Mixture <IMultivariateDistribution> >();
            var @params               = new MixtureSigmaEstimationParameters <Mixture <IMultivariateDistribution> >
            {
                Model        = model,
                Normalized   = model.Normalized,
                Alpha        = alpha,
                Beta         = beta,
                Observations = Helper.Convert(observations),
                L            = model.Emission[0].Components.Length
            };
            var gamma          = mixtureGammaEstimator.Estimate(@params as AdvancedEstimationParameters <Mixture <IMultivariateDistribution> >);
            var gammaComponens = mixtureGammaEstimator.Estimate(@params);

            @params.Gamma           = gamma;
            @params.GammaComponents = gammaComponens;
            @params.Mu = mixtureMuEstimator.Estimate(@params);

            for (int i = 0; i < NumberOfStatesRightLeft; i++)
            {
                for (int l = 0; l < NumberOfComponents; l++)
                {
                    for (int rows = 0; rows < parameters.Observation[0].Dimention; rows++)
                    {
                        for (int cols = 0; cols < parameters.Observation[0].Dimention; cols++)
                        {
                            Assert.IsTrue(sigma.Estimate(@params)[i, l][rows, cols] > 0, string.Format("Failed Sigma {0}", sigma.Estimate(@params)[i, l][rows, cols]));
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Mu_ErgodicAndLogNormalized_MuCalculated()
        {
            var util         = new TestDataUtils();
            var observations = util.GetSvcData(util.FTSEFilePath, new DateTime(2011, 11, 18), new DateTime(2011, 12, 18));
            var model        = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <Mixture <IMultivariateDistribution> >()
            {
                NumberOfStates = NumberOfStates, Emissions = CreateEmissions(observations, NumberOfStates, NumberOfComponents)
            });                                                                                                                                                                                                                                      //new HiddenMarkovModelState<Mixture<IMultivariateDistribution>>(NumberOfStates, CreateEmissions(observations, NumberOfStates, NumberOfComponents)) { LogNormalized = true };

            model.Normalized = true;
            var baseParameters = new BasicEstimationParameters <Mixture <IMultivariateDistribution> > {
                Model = model, Observations = Helper.Convert(observations), Normalized = model.Normalized
            };
            var alphaEstimator = new AlphaEstimator <Mixture <IMultivariateDistribution> >();
            var alpha          = alphaEstimator.Estimate(baseParameters);
            var betaEstimator  = new BetaEstimator <Mixture <IMultivariateDistribution> >();
            var beta           = betaEstimator.Estimate(baseParameters);

            var mu = new MixtureMuEstimator <Mixture <IMultivariateDistribution> >();
            var mixtureGammaEstimator = new MixtureGammaEstimator <Mixture <IMultivariateDistribution> >();
            var @params = new MixtureCoefficientEstimationParameters <Mixture <IMultivariateDistribution> >
            {
                Model        = model,
                Normalized   = model.Normalized,
                Alpha        = alpha,
                Beta         = beta,
                Observations = Helper.Convert(observations),
                L            = model.Emission[0].Components.Length
            };
            var gamma          = mixtureGammaEstimator.Estimate(@params as AdvancedEstimationParameters <Mixture <IMultivariateDistribution> >);
            var gammaComponens = mixtureGammaEstimator.Estimate(@params);

            @params.Gamma           = gamma;
            @params.GammaComponents = gammaComponens;

            for (int i = 0; i < NumberOfStates; i++)
            {
                for (int l = 0; l < NumberOfComponents; l++)
                {
                    for (int d = 0; d < observations[0].Length; d++)
                    {
                        Assert.IsTrue(mu.Estimate(@params)[i, l][d] > 0, string.Format("Failed Mu {0}", mu.Estimate(@params)[i, l][d]));
                    }
                }
            }
        }
        public IHiddenMarkovModel <Mixture <IMultivariateDistribution> > Run(int maxIterations, double likelihoodTolerance)
        {
            // Initialize responce object
            var forwardBackward = new ForwardBackward(Normalized);

            do
            {
                maxIterations--;
                if (!_estimatedModel.Likelihood.EqualsTo(0))
                {
                    _currentModel = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <Mixture <IMultivariateDistribution> > {
                        Pi = _estimatedPi, TransitionProbabilityMatrix = _estimatedTransitionProbabilityMatrix, Emissions = _estimatedEmissions
                    });                                                                                                                                                                                                                                                 //new HiddenMarkovModelState<Mixture<IMultivariateDistribution>>(_estimatedPi, _estimatedTransitionProbabilityMatrix, _estimatedEmissions) { LogNormalized = _estimatedModel.LogNormalized };
                    _currentModel.Normalized = Normalized;
                    _currentModel.Likelihood = _estimatedModel.Likelihood;
                }
                // Run Forward-Backward procedure
                forwardBackward.RunForward(_observations, _currentModel);
                forwardBackward.RunBackward(_observations, _currentModel);
                // Calculate Gamma and Xi
                var @params = new MixtureSigmaEstimationParameters <Mixture <IMultivariateDistribution> >
                {
                    Alpha              = forwardBackward.Alpha,
                    Beta               = forwardBackward.Beta,
                    Observations       = _observations,
                    Model              = _currentModel,
                    Normalized         = _currentModel.Normalized,
                    L                  = _currentModel.Emission[0].Components.Length,
                    ObservationWeights = _observationWeights
                };
                _gammaEstimator = new GammaEstimator <Mixture <IMultivariateDistribution> >();
                _ksiEstimator   = new KsiEstimator <Mixture <IMultivariateDistribution> >();
                var mixtureCoefficientsEstimator = new MixtureCoefficientsEstimator <Mixture <IMultivariateDistribution> >();
                var mixtureMuEstimator           = new MixtureMuEstimator <Mixture <IMultivariateDistribution> >();    // Mean
                var mixtureSigmaEstimator        = new MixtureSigmaEstimator <Mixture <IMultivariateDistribution> >(); // Covariance
                var mixtureGammaEstimator        = new MixtureGammaEstimator <Mixture <IMultivariateDistribution> >();
                @params.Gamma           = _gammaEstimator.Estimate(@params);
                @params.GammaComponents = mixtureGammaEstimator.Estimate(@params);


                EstimatePi(_gammaEstimator.Estimate(@params));
                // TODO : weights for A
                EstimateTransitionProbabilityMatrix(_gammaEstimator.Estimate(@params), _ksiEstimator.Estimate(@params), _observationWeights, _observations.Count);

                for (var n = 0; n < _currentModel.N; n++)
                {
                    var mixturesComponents = _currentModel.Emission[n].Coefficients.Length;
                    var distributions      = new IMultivariateDistribution[mixturesComponents];
                    // Calculate coefficients for state n
                    // TODO : weights for W
                    var coefficients = mixtureCoefficientsEstimator.Estimate(@params)[n];
                    if (Normalized)
                    {
                        mixtureCoefficientsEstimator.Denormalize();
                    }
                    // TODO : weights Mu
                    @params.Mu = mixtureMuEstimator.Estimate(@params);
                    for (var l = 0; l < mixturesComponents; l++)
                    {
                        // TODO : weights Sigma
                        distributions[l] = new NormalDistribution(mixtureMuEstimator.Estimate(@params)[n, l], mixtureSigmaEstimator.Estimate(@params)[n, l]);
                    }
                    _estimatedEmissions[n] = new Mixture <IMultivariateDistribution>(coefficients, distributions);
                }
                _estimatedModel = HiddenMarkovModelStateFactory.GetState(new ModelCreationParameters <Mixture <IMultivariateDistribution> > {
                    Pi = _estimatedPi, TransitionProbabilityMatrix = _estimatedTransitionProbabilityMatrix, Emissions = _estimatedEmissions
                });
                _estimatedModel.Normalized = Normalized;
                _estimatedModel.Likelihood = forwardBackward.RunForward(_observations, _estimatedModel);
                _likelihoodDelta           = Math.Abs(Math.Abs(_currentModel.Likelihood) - Math.Abs(_estimatedModel.Likelihood));
                Debug.WriteLine("Iteration {3} , Current {0}, Estimate {1} Likelihood delta {2}", _currentModel.Likelihood, _estimatedModel.Likelihood, _likelihoodDelta, maxIterations);
            }while (_currentModel != _estimatedModel && maxIterations > 0 && _likelihoodDelta > likelihoodTolerance);

            return(_estimatedModel);
        }
Ejemplo n.º 4
0
        public void MixtureSigmaEstimator_Parameters_MixtureSigmaEstimatorCreated()
        {
            var sigma = new MixtureMuEstimator <Mixture <IMultivariateDistribution> >();

            Assert.IsNotNull(sigma);
        }
Ejemplo n.º 5
0
        public void MixtureMuEstimator_MultivariateNormalDistributionParameters_MixtureMuEstimatorCreated()
        {
            var mu = new MixtureMuEstimator <Mixture <IMultivariateDistribution> >();

            Assert.IsNotNull(mu);
        }