Exemplo n.º 1
0
        public void ValueFromDistributionTest()
        {
            string  filename = string.Empty;          // TODO: инициализация подходящего значения
            HMM_QPN target   = new HMM_QPN(filename); // TODO: инициализация подходящего значения

            DistributionPare[] DP       = null;       // TODO: инициализация подходящего значения
            double[]           expected = null;       // TODO: инициализация подходящего значения
            double[]           actual;
            actual = target.ValueFromDistribution(DP);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Проверьте правильность этого метода теста.");
        }
Exemplo n.º 2
0
        private static int Max_Period(HMM_QPN Model)
        {
            int max_period = 0;//максимальная длительность по всем состояниям (у Рабинера D)
            int i          = 0;

            for (i = 0; i <= Model.A.GetLength(0) - 1; i++)//определяем max_period
            {
                double[] f_values     = Model.ValueFromDistribution(Model.F[i]);
                int      max_in_state = (int)HMM_QPN.MaxElementOfArray(f_values);
                if (max_in_state > max_period)
                {
                    max_period = max_in_state;
                }
            }
            return(max_period);
        }
Exemplo n.º 3
0
        public static double Likelihood(HMM_QPN Model, int[] res, out double[,] norm_alpha_par, out double[,] alpha_par, out double[] norm_coeff, out double prob)// считает вероятность того, что последовательность наблюдений res получена с помощью модели Model
        //res numeruetsya ot 1
        {
            #region Описание переменных
            int n = Model.A.GetLength(0);                     //число состояний
            int T = res.Length - 1;                           //длина последовательности наблюдений
            double[,] alpha           = new double[n, T + 1]; //прямая переменная
            double[,] auxiliary_alpha = new double[n, T + 1];
            double[,] norm_alpha      = new double[n, T + 1]; //нормированная прямая переменная
            double[] c          = new double[T + 1];          //коэффициенты нормировки
            int      max_period = 0;                          //максимальная длительность по всем состояниям (у Рабинера D)
            #endregion
            #region Определение максимальной длительности по всем состояниям
            int i = 0;
            for (i = 0; i < n; i++)//определяем max_period
            {
                double[] f_values     = Model.ValueFromDistribution(Model.F[i]);
                int      max_in_state = (int)HMM_QPN.MaxElementOfArray(f_values);
                if (max_in_state > max_period)
                {
                    max_period = max_in_state;
                }
            }
            #endregion
            //преобразуем p_i(d) к виду ряда распределения где d принадлежит от 1 до D(то есть) max_period,а вероятность =0 если такой длины нет
            #region Матрица плотностей длительностей состояний
            float[,] length_density = new float[n, max_period + 1];//матрица плотностей длительности состояний.
            //Второй индекс соответствует длине, значение - вероятности
            for (i = 0; i < n; i++)
            {
                for (int d = 1; d <= max_period; d++)
                {
                    length_density[i, d] = 0;//занулили все элементы матрицы
                }
                for (int j = 0; j < Model.F[i].Length; j++)
                {
                    length_density[i, Model.F[i][j].value] = Model.F[i][j].probability;//изменили те элементы, для которых вероятность нулю не равна
                }
            }
            #endregion
            #region Старый вариант формул
            //#region Для t=1
            ////для t=1
            //c[1] = 0;
            //for (i=0; i<n; i++)
            //{
            //    if (length_density[i, 1] > 0)
            //    {
            //        float[] fi = new float[1];
            //        fi = HMM_QPN.integral(Model.Ro[i], Model.Ro[i].Length, 1);
            //        alpha[i, 1] = SymbolFinalProbability(Model, fi[0], 1, i, res[1]) * Model.Pi[i] * length_density[i, 1];
            //        auxiliary_alpha[i, 1] = SymbolFinalProbability(Model, fi[0], 1, i, res[1]) * Model.Pi[i] * length_density[i, 1];
            //    }
            //    else
            //    {
            //        alpha[i, 1] = 0;
            //        auxiliary_alpha[i, 1] =0;
            //    }
            //    c[1] += auxiliary_alpha[i, 1];
            //}
            //c[1] =(c[1]!=0)?1 / c[1]:0;//вычислили нормировочный коэффициент
            //for (i = 0; i < n; i++)
            //{
            //    norm_alpha[i, 1] = auxiliary_alpha[i, 1] * c[1];
            //}
            //#endregion
            //#region Для 1<t<=D
            ////вторая строчка формулы
            //int t=2;
            //for (t = 2; (t <= max_period)&&(t<=T); t++)
            //{
            //    c[t] = 0;
            //    for (i = 0; i < n; i++)
            //    {
            //        //первое слагаемое формулы 2
            //        float term1 = 0;
            //        if (length_density[i, t] > 0)
            //        {
            //            float[] fi = new float[t];
            //            fi = HMM_QPN.integral(Model.Ro[i], Model.Ro[i].Length, t);
            //            float prod_tilda_b1 = 1;//переменная для произведения b c волной
            //            for (int s = 1; s <= t; s++)
            //            {
            //                prod_tilda_b1 *= SymbolFinalProbability(Model, fi[s - 1], t, i, res[s]);
            //            }
            //            term1 = Model.Pi[i] * length_density[i, t] * prod_tilda_b1;
            //        }
            //        float term2 = 0;
            //        float norm_term2 = 0;
            //        for (int d = 1; d <= t - 1; d++)
            //            for (int j = 0; j < n; j++)
            //            {
            //                float prod_tilda_b2 = 1;
            //                if (length_density[i, d] > 0)
            //                {
            //                    float[] fi2 = new float[d];
            //                    fi2 = HMM_QPN.integral(Model.Ro[i], Model.Ro[i].Length, d);

            //                    for (int s = t - d + 1; s <= t; s++)
            //                    {
            //                        prod_tilda_b2 *= SymbolFinalProbability(Model, fi2[s - t + d - 1],d, i, res[s]);
            //                    }
            //                    term2 += alpha[j, t - d] * Model.A[j, i] * length_density[i, d] * prod_tilda_b2;
            //                    norm_term2 += norm_alpha[j, t - d] * Model.A[j, i] * length_density[i, d] * prod_tilda_b2;
            //                }
            //            }
            //        alpha[i, t] = term1 + term2;
            //        auxiliary_alpha[i, t] = term1 + norm_term2;
            //        c[t] += auxiliary_alpha[i, t];
            //    }
            //    c[t] = (c[t] != 0) ? 1 / c[t] : 0;
            //    for (i = 0; i < n; i++)
            //    {
            //        norm_alpha[i, t] = auxiliary_alpha[i, t] * c[t];
            //    }
            //}
            //#endregion
            //#region Для t>D
            ////третья строчка формулы
            //for (t = max_period + 1; t <= T; t++)
            //{
            //    c[t] = 0;
            //    for (i = 0; i < n; i++)
            //    {
            //        float term = 0;
            //        float norm_term = 0;
            //        for (int d = 1; d <= max_period; d++)
            //            for (int j = 0; j < n; j++)
            //            {
            //                if (length_density[i, d] > 0)
            //                {
            //                    float prod_tilda_b = 1;
            //                    float[] fi = new float[d];
            //                    fi = HMM_QPN.integral(Model.Ro[i], Model.Ro[i].Length, d);

            //                    for (int s = t - d + 1; s <= t; s++)
            //                    {
            //                        prod_tilda_b *= SymbolFinalProbability(Model, fi[s - t + d - 1], d, i, res[s]);
            //                    }
            //                    term += alpha[j, t - d] * Model.A[j, i] * length_density[i, d] * prod_tilda_b;
            //                    norm_term += norm_alpha[j, t - d] * Model.A[j, i] * length_density[i, d] * prod_tilda_b;
            //                }
            //            }
            //        alpha[i, t] = term;
            //        auxiliary_alpha[i, t] = norm_term;
            //        c[t] += auxiliary_alpha[i, t];
            //    }
            //    c[t] = (c[t] != 0) ? 1 / c[t] : 0;
            //    for (i = 0; i < n; i++)
            //    {
            //        norm_alpha[i, t] = auxiliary_alpha[i, t] * c[t];
            //    }
            //}
            //  #endregion
            #endregion Старый вариант формул
            #region Для t=1
            //для t=1
            c[1] = 0;
            for (i = 0; i < n; i++)
            {
                if (length_density[i, 1] > 0)
                {
                    double[] fi = new double[1];
                    fi                    = HMM_QPN.integral(Model.Ro[i], Model.Ro[i].Length, 1);
                    alpha[i, 1]           = SymbolFinalProbability(Model, fi[0], 1, i, res[1]) * Model.Pi[i] * length_density[i, 1];
                    auxiliary_alpha[i, 1] = SymbolFinalProbability(Model, fi[0], 1, i, res[1]) * Model.Pi[i] * length_density[i, 1];
                }
                else
                {
                    alpha[i, 1]           = 0;
                    auxiliary_alpha[i, 1] = 0;
                }
                c[1] += auxiliary_alpha[i, 1];
            }
            c[1] = (c[1] != 0) ? 1 / c[1] : 0;//вычислили нормировочный коэффициент
            for (i = 0; i < n; i++)
            {
                norm_alpha[i, 1] = auxiliary_alpha[i, 1] * c[1];
            }
            #endregion

            #region Для t<T
            int t = 2;

            for (t = 2; t <= T; t++)
            {
                c[t] = 0;
                for (i = 0; i < n; i++)
                {
                    double term      = 0;
                    double norm_term = 0;
                    int    D         = max_period >= t ? t : max_period;
                    for (int d = 1; d <= D; d++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            if (length_density[i, d] > 0)
                            {
                                double   prod_tilda_b = 1;
                                double[] fi           = new double[d];
                                fi = HMM_QPN.integral(Model.Ro[i], Model.Ro[i].Length, d);

                                for (int s = t - d + 1; s <= t; s++)
                                {
                                    prod_tilda_b *= SymbolFinalProbability(Model, fi[s - t + d - 1], d, i, res[s]);
                                }
                                term      += alpha[j, t - d] * Model.A[j, i] * length_density[i, d] * prod_tilda_b;
                                norm_term += norm_alpha[j, t - d] * Model.A[j, i] * length_density[i, d] * prod_tilda_b;
                            }
                        }
                    }
                    alpha[i, t]           = term;
                    auxiliary_alpha[i, t] = norm_term;
                    c[t] += auxiliary_alpha[i, t];
                }
                c[t] = (c[t] != 0) ? 1 / c[t] : 0;
                for (i = 0; i < n; i++)
                {
                    norm_alpha[i, t] = auxiliary_alpha[i, t] * c[t];
                }
            }
            #endregion Для t<T
            double probability = 0;
            prob = 0;
            double coeffsum = 0;
            for (i = 0; i < n; i++)
            {
                //          probability+=norm_alpha[i,T];
                prob += alpha[i, T];
            }
            for (t = 1; t <= T; t++)
            {
                coeffsum += c[t];
            }
            probability    = 1 / coeffsum;
            alpha_par      = alpha;
            norm_alpha_par = norm_alpha;
            norm_coeff     = c;
            return(probability);
        }