Exemplo n.º 1
0
        public double[][] Estimate(BasicEstimationParameters <TDistribution> parameters)
        {
            if (_beta != null)
            {
                return(_beta);
            }
            var T = parameters.Observations.Count;

            try
            {
                _beta        = new double[T][];
                _beta[T - 1] = new double[parameters.Model.N];

                for (var i = 0; i < parameters.Model.N; i++)
                {
                    _beta[T - 1][i] = (parameters.Normalized) ? 0d : 1d;
                }

                for (var t = T - 2; t >= 0; t--)
                {
                    _beta[t] = new double[parameters.Model.N];
                    for (var i = 0; i < parameters.Model.N; i++)
                    {
                        _beta[t][i] = (parameters.Normalized) ? double.NaN : 0d;
                        for (var j = 0; j < parameters.Model.N; j++)
                        {
                            var o = EstimatorUtilities.GetProbability(parameters.Model.Emission[j], parameters.Observations, t + 1);
                            if (parameters.Normalized)
                            {
                                _beta[t][i] = LogExtention.eLnSum(_beta[t][i],
                                                                  LogExtention.eLnProduct(LogExtention.eLn(parameters.Model.TransitionProbabilityMatrix[i][j]),
                                                                                          LogExtention.eLnProduct(LogExtention.eLn(o), _beta[t + 1][j])));
                            }
                            else
                            {
                                _beta[t][i] += parameters.Model.TransitionProbabilityMatrix[i][j] * o * _beta[t + 1][j];
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                for (var t = T - 2; t >= 0; t--)
                {
                    for (var i = 0; i < parameters.Model.N; i++)
                    {
                        for (var j = 0; j < parameters.Model.N; j++)
                        {
                            Debug.WriteLine("[{0}][{1}] : beta : {2}", t, i, _beta[t][i]);
                        }
                    }
                }
                throw;
            }

            return(_beta);
        }
Exemplo n.º 2
0
        public double[][,] Estimate(AdvancedEstimationParameters <TDistribution> parameters)
        {
            if (_ksi != null)
            {
                return(_ksi);
            }
            var denominator = new double[parameters.Observations.Count];

            for (var t = 0; t < parameters.Observations.Count - 1; t++)
            {
                denominator[t] = (parameters.Normalized) ? double.NaN : 0d;
                for (var i = 0; i < parameters.Model.N; i++)
                {
                    for (var j = 0; j < parameters.Model.N; j++)
                    {
                        var o = EstimatorUtilities.GetProbability(parameters.Model.Emission[j], parameters.Observations, t + 1);
                        if (parameters.Normalized)
                        {
                            denominator[t] = LogExtention.eLnSum(denominator[t], LogExtention.eLnProduct(parameters.Alpha[t][i],
                                                                                                         LogExtention.eLnProduct(LogExtention.eLn(parameters.Model.TransitionProbabilityMatrix[i][j]),
                                                                                                                                 LogExtention.eLnProduct(parameters.Beta[t + 1][j], LogExtention.eLn(o)))));
                        }
                        else
                        {
                            denominator[t] += parameters.Alpha[t][i] * parameters.Model.TransitionProbabilityMatrix[i][j] * parameters.Beta[t + 1][j] * o;
                        }
                    }
                }
            }

            _ksi = new double[parameters.Observations.Count][, ];
            for (var t = 0; t < parameters.Observations.Count - 1; t++)
            {
                _ksi[t] = new double[parameters.Model.N, parameters.Model.N];
                for (var i = 0; i < parameters.Model.N; i++)
                {
                    for (var j = 0; j < parameters.Model.N; j++)
                    {
                        var o = EstimatorUtilities.GetProbability(parameters.Model.Emission[j], parameters.Observations, t + 1);
                        if (parameters.Normalized)
                        {
                            var nominator = LogExtention.eLnProduct(parameters.Alpha[t][i],
                                                                    LogExtention.eLnProduct(LogExtention.eLn(parameters.Model.TransitionProbabilityMatrix[i][j]),
                                                                                            LogExtention.eLnProduct(parameters.Beta[t + 1][j], LogExtention.eLn(o))));
                            _ksi[t][i, j] = LogExtention.eLnProduct(nominator, -denominator[t]);
                        }
                        else
                        {
                            var nominator = parameters.Alpha[t][i] * parameters.Model.TransitionProbabilityMatrix[i][j] * parameters.Beta[t + 1][j] * o;
                            _ksi[t][i, j] = nominator / denominator[t];
                        }
                    }
                }
            }

            return(_ksi);
        }
Exemplo n.º 3
0
        public double[][] Estimate(BasicEstimationParameters <TDistribution> parameters)
        {
            if (_alpha != null)
            {
                return(_alpha);
            }

            _alpha    = new double[parameters.Observations.Count][];
            _alpha[0] = new double[parameters.Model.N];
            // Initialize
            for (var i = 0; i < parameters.Model.N; i++)
            {
                var o = EstimatorUtilities.GetProbability(parameters.Model.Emission[i], parameters.Observations, 0);
                _alpha[0][i] = (parameters.Normalized) ? LogExtention.eLnProduct(LogExtention.eLn(parameters.Model.Pi[i]), LogExtention.eLn(o)) : parameters.Model.Pi[i] * o;
            }
            // Induction
            for (var t = 1; t < parameters.Observations.Count; t++)
            {
                _alpha[t] = new double[parameters.Model.N];
                for (var j = 0; j < parameters.Model.N; j++)
                {
                    var sum = (parameters.Normalized) ? double.NaN : 0d;
                    for (var i = 0; i < parameters.Model.N; i++)
                    {
                        if (parameters.Normalized)
                        {
                            sum = LogExtention.eLnSum(sum, LogExtention.eLnProduct(_alpha[t - 1][i], LogExtention.eLn(parameters.Model.TransitionProbabilityMatrix[i][j])));
                        }
                        else
                        {
                            sum += _alpha[t - 1][i] * parameters.Model.TransitionProbabilityMatrix[i][j];
                        }
                    }
                    var o = EstimatorUtilities.GetProbability(parameters.Model.Emission[j], parameters.Observations, t);
                    if (parameters.Normalized)
                    {
                        _alpha[t][j] = LogExtention.eLnProduct(sum, LogExtention.eLn(o));
                    }
                    else
                    {
                        _alpha[t][j] = sum * o;
                    }
                }
            }

            return(_alpha);
        }