public void Denormalized_NormalizedEstimator_SigmaDenormalized()
        {
            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 parameters            = new ParameterEstimations <Mixture <IMultivariateDistribution> >(model, Helper.Convert(observations), alpha, beta);
            var coefficients          = new MixtureCoefficientsEstimator <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++)
                {
                    Assert.IsTrue(coefficients.Estimate(@params)[i][l] < 0, string.Format("Failed Coefficients {0}", coefficients.Estimate(@params)[i][l]));
                }
            }
            coefficients.Denormalize();
            for (int i = 0; i < NumberOfStates; i++)
            {
                for (int l = 0; l < NumberOfComponents; l++)
                {
                    Assert.IsTrue(coefficients.Estimate(@params)[i][l] > 0 && coefficients.Estimate(@params)[i][l] < 1, string.Format("Failed Coefficients {0}", coefficients.Estimate(@params)[i][l]));
                }
            }
        }
Example #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]));
                    }
                }
            }
        }
Example #3
0
        public double[, ][] Estimate(MixtureCoefficientEstimationParameters <TDistribution> parameters)
        {
            if (_mu != null)
            {
                return(_mu);
            }

            try
            {
                _mu = new double[parameters.Model.N, parameters.L][];
                for (var i = 0; i < parameters.Model.N; i++)
                {
                    for (var l = 0; l < parameters.L; l++)
                    {
                        var denominator = 0.0d;
                        var nominator   = new double[parameters.Observations[0].Dimention];
                        for (var t = 0; t < parameters.Observations.Count; t++)
                        {
                            // TODO : weights here
                            var weight = GetWeightValue(t, parameters.ObservationWeights);
                            var x      = parameters.Observations[t].Value;
                            var gamma  = (parameters.Model.Normalized) ? LogExtention.eExp(parameters.GammaComponents[t][i, l])
                                                                          : parameters.GammaComponents[t][i, l];
                            denominator += weight * gamma;
                            x            = x.Product(gamma * weight);
                            nominator    = nominator.Add(x);
                        }
                        _mu[i, l] = nominator.Product(1 / denominator);
                    }
                }
            }
            catch (Exception)
            {
                for (var i = 0; i < parameters.Model.N; i++)
                {
                    for (var l = 0; l < parameters.L; l++)
                    {
                        Debug.WriteLine("Mixture Mu [{0},{1}] : {2}", i, l, new Vector(_mu[i, l]));
                    }
                }
                throw;
            }

            return(_mu);
        }
        public void Coefficients_RightLeftAndNotNormilized_EachEntryMatrixIsSummedToOne()
        {
            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 coefficients          = new MixtureCoefficientsEstimator <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++)
            {
                Assert.AreEqual(1.0d, Math.Round(coefficients.Estimate(@params)[i].Sum(), 5), string.Format("Failed Coefficients {0} at component {1}", new Vector(coefficients.Estimate(@params)[i]), i));
            }
        }
        public double[][] Estimate(MixtureCoefficientEstimationParameters <TDistribution> parameters)
        {
            _parameters = parameters;
            if (_coefficients != null)
            {
                return(_coefficients);
            }

            try
            {
                _coefficients = new double[parameters.Model.N][];
                _denominator  = new double[parameters.Model.N];
                for (var i = 0; i < parameters.Model.N; i++)
                {
                    _denominator[i] = (parameters.Model.Normalized) ? double.NaN : 0d;
                    for (var t = 0; t < parameters.Observations.Count; t++)
                    {
                        var weight = GetWeightValue(t, parameters.ObservationWeights);
                        if (parameters.Normalized)
                        {
                            _denominator[i] = LogExtention.eLnSum(LogExtention.eLnProduct(weight, parameters.Gamma[t][i]), _denominator[i]);
                        }
                        else
                        {
                            _denominator[i] += weight * parameters.Gamma[t][i];
                        }
                    }
                }
                for (var i = 0; i < parameters.Model.N; i++)
                {
                    _coefficients[i] = new double[parameters.L];
                    for (var l = 0; l < parameters.L; l++)
                    {
                        var nominator = (parameters.Model.Normalized) ? double.NaN : 0d;
                        for (var t = 0; t < parameters.Observations.Count; t++)
                        {
                            var weight = GetWeightValue(t, parameters.ObservationWeights);
                            if (parameters.Normalized)
                            {
                                nominator = LogExtention.eLnSum(LogExtention.eLnProduct(weight, parameters.GammaComponents[t][i, l]), nominator);
                            }
                            else
                            {
                                nominator += weight * parameters.GammaComponents[t][i, l];
                            }
                        }

                        if (parameters.Normalized)
                        {
                            _coefficients[i][l] = LogExtention.eLnProduct(nominator, -_denominator[i]);
                        }
                        else
                        {
                            _coefficients[i][l] = nominator / _denominator[i];
                        }

                        if (Math.Round(_coefficients[i].Sum(), 5) > 1)
                        {
                            Debug.WriteLine("Mixture Coeffiecients [{0},{1}] : {2}", i, l, new Vector(_coefficients[i]));
                            throw new ApplicationException(string.Format("Mixture Coeffiecients is greater than 1. [{0},{1}] : {2} : {3}", i, l, new Vector(_coefficients[i]), Math.Round(_coefficients[i].Sum(), 5)));
                        }
                    }
                }
            }
            catch (Exception)
            {
                for (var i = 0; i < parameters.Model.N; i++)
                {
                    for (var l = 0; l < parameters.L; l++)
                    {
                        Debug.WriteLine("Coeffiecients[{0}][{1}] : {2}", i, l, _coefficients[i][l]);
                        if (Math.Round(_coefficients[i].Sum(), 5) > 1)
                        {
                            Debug.WriteLine("Mixture Coeffiecients [{0},{1}] : {2}", i, l, new Vector(_coefficients[i]));
                            throw new ApplicationException(string.Format("Mixture Coeffiecients is greater than 1. [{0},{1}] : {2} : {3}", i, l, new Vector(_coefficients[i]), Math.Round(_coefficients[i].Sum(), 5)));
                        }
                    }
                }
                throw;
            }

            return(_coefficients);
        }