C#,数值计算——隐式马尔科夫模型(Hidden Markov Models)的计算方法与源程序

本文主要是介绍C#,数值计算——隐式马尔科夫模型(Hidden Markov Models)的计算方法与源程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// Hidden Markov Models
    /// </summary>
    public class HMM
    {
        private int fbdone { get; set; }
        private int mstat { get; set; }
        private int nobs { get; set; }
        private int ksym { get; set; }
        private int lrnrm { get; set; }
        private double BIG { get; set; }
        private double BIGI { get; set; }
        private double lhood { get; set; }
        private double[,] a { get; set; }
        private double[,] b { get; set; }
        private int[] obs { get; set; }
        private double[,] alpha { get; set; }
        private double[,] beta { get; set; }
        private double[,] pstate { get; set; }
        private int[] arnrm { get; set; }
        private int[] brnrm { get; set; }

        public HMM(double[,] aa, double[,] bb, int[] obss)
        {
            this.a = aa;
            this.b = bb;
            this.obs = obss;
            this.fbdone = 0;
            this.mstat = a.GetLength(0);
            this.nobs = obs.Length;
            this.ksym = b.GetLength(1);
            this.alpha = new double[nobs, mstat];
            this.beta = new double[nobs, mstat];
            this.pstate = new double[nobs, mstat];
            this.arnrm = new int[nobs];
            this.brnrm = new int[nobs];
            this.BIG = 1.0e20;
            this.BIGI = 1.0 / BIG;

            if (a.GetLength(1) != mstat)
            {
                throw new Exception("transition matrix not square");
            }
            if (b.GetLength(0) != mstat)
            {
                throw new Exception("symbol prob matrix wrong size");
            }
            for (int i = 0; i < nobs; i++)
            {
                if (obs[i] < 0 || obs[i] >= ksym)
                {
                    throw new Exception("bad data in obs");
                }
            }
            for (int i = 0; i < mstat; i++)
            {
                double sum = 0.0;
                for (int j = 0; j < mstat; j++)
                {
                    sum += a[i, j];
                }
                if (Math.Abs(sum - 1.0) > 0.01)
                {
                    throw new Exception("transition matrix not normalized");
                }
                for (int j = 0; j < mstat; j++)
                {
                    a[i, j] /= sum;
                }
            }
            for (int i = 0; i < mstat; i++)
            {
                double sum = 0.0;
                for (int k = 0; k < ksym; k++)
                {
                    sum += b[i, k];
                }
                if (Math.Abs(sum - 1.0) > 0.01)
                {
                    throw new Exception("symbol prob matrix not normalized");
                }
                for (int k = 0; k < ksym; k++)
                {
                    b[i, k] /= sum;
                }
            }
        }

        public double loglikelihood()
        {
            return Math.Log(lhood) + lrnrm * Math.Log(BIGI);
        }

        public void forwardbackward()
        {
            for (int i = 0; i < mstat; i++)
            {
                alpha[0, i] = b[i, obs[0]];
            }
            arnrm[0] = 0;
            for (int t = 1; t < nobs; t++)
            {
                double asum = 0;
                for (int j = 0; j < mstat; j++)
                {
                    double sum = 0.0;
                    for (int i = 0; i < mstat; i++)
                    {
                        sum += alpha[t - 1, i] * a[i, j] * b[j, obs[t]];
                    }
                    alpha[t, j] = sum;
                    asum += sum;
                }
                arnrm[t] = arnrm[t - 1];
                if (asum < BIGI)
                {
                    ++arnrm[t];
                    for (int j = 0; j < mstat; j++)
                    {
                        alpha[t, j] *= BIG;
                    }
                }
            }
            for (int i = 0; i < mstat; i++)
            {
                beta[nobs - 1, i] = 1.0;
            }
            brnrm[nobs - 1] = 0;
            for (int t = nobs - 2; t >= 0; t--)
            {
                double bsum = 0.0;
                for (int i = 0; i < mstat; i++)
                {
                    double sum = 0.0;
                    for (int j = 0; j < mstat; j++)
                    {
                        sum += a[i, j] * b[j, obs[t + 1]] * beta[t + 1, j];
                    }
                    beta[t, i] = sum;
                    bsum += sum;
                }
                brnrm[t] = brnrm[t + 1];
                if (bsum < BIGI)
                {
                    ++brnrm[t];
                    for (int j = 0; j < mstat; j++)
                    {
                        beta[t, j] *= BIG;
                    }
                }
            }
            lhood = 0.0;
            for (int i = 0; i < mstat; i++)
            {
                lhood += alpha[0, i] * beta[0, i];
            }
            lrnrm = arnrm[0] + brnrm[0];
            if (lhood != 0.0)
            {
                while (lhood < BIGI)
                {
                    lhood *= BIG;
                    lrnrm++;
                }
            }
            for (int t = 0; t < nobs; t++)
            {
                double sum = 0.0;
                for (int i = 0; i < mstat; i++)
                {
                    sum += (pstate[t, i] = alpha[t, i] * beta[t, i]);
                }
                // sum = lhood*pow(BIGI, lrnrm - arnrm[t] - brnrm[t]);
                for (int i = 0; i < mstat; i++)
                {
                    pstate[t, i] /= sum;
                }
            }
            fbdone = 1;
        }

        public void baumwelch()
        {
            double[,] bnew = new double[mstat, ksym];
            double[] powtab = new double[10];
            for (int i = 0; i < 10; i++)
            {
                powtab[i] = Math.Pow(BIGI, i - 6);
            }
            if (fbdone != 1)
            {
                throw new Exception("must do forwardbackward first");
            }
            for (int i = 0; i < mstat; i++)
            {
                double denom = 0.0;
                for (int k = 0; k < ksym; k++)
                {
                    bnew[i, k] = 0.0;
                }
                for (int t = 0; t < nobs - 1; t++)
                {
                    double term = (alpha[t, i] * beta[t, i] / lhood) * powtab[arnrm[t] + brnrm[t] - lrnrm + 6];
                    denom += term;
                    bnew[i, obs[t]] += term;
                }
                for (int j = 0; j < mstat; j++)
                {
                    double num = 0.0;
                    for (int t = 0; t < nobs - 1; t++)
                    {
                        num += alpha[t, i] * b[j, obs[t + 1]] * beta[t + 1, j] * powtab[arnrm[t] + brnrm[t + 1] - lrnrm + 6] / lhood;
                    }
                    a[i, j] *= (num / denom);
                }
                for (int k = 0; k < ksym; k++)
                {
                    bnew[i, k] /= denom;
                }
            }
            b = bnew;
            fbdone = 0;
        }

        /// <summary>
        /// Markov Models and Hidden Markov Modeling
        /// </summary>
        /// <param name="atrans"></param>
        /// <param name="xout"></param>
        /// <param name="istart"></param>
        /// <param name="seed"></param>
        /// <exception cref="Exception"></exception>
        public static void markovgen(double[,] atrans, int[] xout, int istart = 0, int seed = 1)
        {
            int m = atrans.GetLength(0);
            int n = xout.Length;
            //double[,] cum = new double[,](atrans);
            double[,] cum = Globals.CopyFrom(atrans);
            Ran ran = new Ran((ulong)seed);
            if (m != atrans.GetLength(1))
            {
                throw new Exception("transition matrix must be square");
            }
            for (int i = 0; i < m; i++)
            {
                for (int ja = 1; ja < m; ja++)
                {
                    cum[i, ja] += cum[i, ja - 1];
                }
                if (Math.Abs(cum[i, m - 1] - 1.0) > 0.01)
                {
                    throw new Exception("transition matrix rows must sum to 1");
                }
            }
            int j = istart;
            xout[0] = j;
            for (int ii = 1; ii < n; ii++)
            {
                double r = ran.doub() * cum[j, m - 1];
                int ilo = 0;
                int ihi = m;
                while (ihi - ilo > 1)
                {
                    int ia = (ihi + ilo) >> 1;
                    if (r > cum[j, ia - 1])
                    {
                        ilo = ia;
                    }
                    else
                    {
                        ihi = ia;
                    }
                }
                xout[ii] = j = ilo;
            }
        }
    }
}
 

2 代码格式

using System;namespace Legalsoft.Truffer
{/// <summary>/// Hidden Markov Models/// </summary>public class HMM{private int fbdone { get; set; }private int mstat { get; set; }private int nobs { get; set; }private int ksym { get; set; }private int lrnrm { get; set; }private double BIG { get; set; }private double BIGI { get; set; }private double lhood { get; set; }private double[,] a { get; set; }private double[,] b { get; set; }private int[] obs { get; set; }private double[,] alpha { get; set; }private double[,] beta { get; set; }private double[,] pstate { get; set; }private int[] arnrm { get; set; }private int[] brnrm { get; set; }public HMM(double[,] aa, double[,] bb, int[] obss){this.a = aa;this.b = bb;this.obs = obss;this.fbdone = 0;this.mstat = a.GetLength(0);this.nobs = obs.Length;this.ksym = b.GetLength(1);this.alpha = new double[nobs, mstat];this.beta = new double[nobs, mstat];this.pstate = new double[nobs, mstat];this.arnrm = new int[nobs];this.brnrm = new int[nobs];this.BIG = 1.0e20;this.BIGI = 1.0 / BIG;if (a.GetLength(1) != mstat){throw new Exception("transition matrix not square");}if (b.GetLength(0) != mstat){throw new Exception("symbol prob matrix wrong size");}for (int i = 0; i < nobs; i++){if (obs[i] < 0 || obs[i] >= ksym){throw new Exception("bad data in obs");}}for (int i = 0; i < mstat; i++){double sum = 0.0;for (int j = 0; j < mstat; j++){sum += a[i, j];}if (Math.Abs(sum - 1.0) > 0.01){throw new Exception("transition matrix not normalized");}for (int j = 0; j < mstat; j++){a[i, j] /= sum;}}for (int i = 0; i < mstat; i++){double sum = 0.0;for (int k = 0; k < ksym; k++){sum += b[i, k];}if (Math.Abs(sum - 1.0) > 0.01){throw new Exception("symbol prob matrix not normalized");}for (int k = 0; k < ksym; k++){b[i, k] /= sum;}}}public double loglikelihood(){return Math.Log(lhood) + lrnrm * Math.Log(BIGI);}public void forwardbackward(){for (int i = 0; i < mstat; i++){alpha[0, i] = b[i, obs[0]];}arnrm[0] = 0;for (int t = 1; t < nobs; t++){double asum = 0;for (int j = 0; j < mstat; j++){double sum = 0.0;for (int i = 0; i < mstat; i++){sum += alpha[t - 1, i] * a[i, j] * b[j, obs[t]];}alpha[t, j] = sum;asum += sum;}arnrm[t] = arnrm[t - 1];if (asum < BIGI){++arnrm[t];for (int j = 0; j < mstat; j++){alpha[t, j] *= BIG;}}}for (int i = 0; i < mstat; i++){beta[nobs - 1, i] = 1.0;}brnrm[nobs - 1] = 0;for (int t = nobs - 2; t >= 0; t--){double bsum = 0.0;for (int i = 0; i < mstat; i++){double sum = 0.0;for (int j = 0; j < mstat; j++){sum += a[i, j] * b[j, obs[t + 1]] * beta[t + 1, j];}beta[t, i] = sum;bsum += sum;}brnrm[t] = brnrm[t + 1];if (bsum < BIGI){++brnrm[t];for (int j = 0; j < mstat; j++){beta[t, j] *= BIG;}}}lhood = 0.0;for (int i = 0; i < mstat; i++){lhood += alpha[0, i] * beta[0, i];}lrnrm = arnrm[0] + brnrm[0];if (lhood != 0.0){while (lhood < BIGI){lhood *= BIG;lrnrm++;}}for (int t = 0; t < nobs; t++){double sum = 0.0;for (int i = 0; i < mstat; i++){sum += (pstate[t, i] = alpha[t, i] * beta[t, i]);}// sum = lhood*pow(BIGI, lrnrm - arnrm[t] - brnrm[t]);for (int i = 0; i < mstat; i++){pstate[t, i] /= sum;}}fbdone = 1;}public void baumwelch(){double[,] bnew = new double[mstat, ksym];double[] powtab = new double[10];for (int i = 0; i < 10; i++){powtab[i] = Math.Pow(BIGI, i - 6);}if (fbdone != 1){throw new Exception("must do forwardbackward first");}for (int i = 0; i < mstat; i++){double denom = 0.0;for (int k = 0; k < ksym; k++){bnew[i, k] = 0.0;}for (int t = 0; t < nobs - 1; t++){double term = (alpha[t, i] * beta[t, i] / lhood) * powtab[arnrm[t] + brnrm[t] - lrnrm + 6];denom += term;bnew[i, obs[t]] += term;}for (int j = 0; j < mstat; j++){double num = 0.0;for (int t = 0; t < nobs - 1; t++){num += alpha[t, i] * b[j, obs[t + 1]] * beta[t + 1, j] * powtab[arnrm[t] + brnrm[t + 1] - lrnrm + 6] / lhood;}a[i, j] *= (num / denom);}for (int k = 0; k < ksym; k++){bnew[i, k] /= denom;}}b = bnew;fbdone = 0;}/// <summary>/// Markov Models and Hidden Markov Modeling/// </summary>/// <param name="atrans"></param>/// <param name="xout"></param>/// <param name="istart"></param>/// <param name="seed"></param>/// <exception cref="Exception"></exception>public static void markovgen(double[,] atrans, int[] xout, int istart = 0, int seed = 1){int m = atrans.GetLength(0);int n = xout.Length;//double[,] cum = new double[,](atrans);double[,] cum = Globals.CopyFrom(atrans);Ran ran = new Ran((ulong)seed);if (m != atrans.GetLength(1)){throw new Exception("transition matrix must be square");}for (int i = 0; i < m; i++){for (int ja = 1; ja < m; ja++){cum[i, ja] += cum[i, ja - 1];}if (Math.Abs(cum[i, m - 1] - 1.0) > 0.01){throw new Exception("transition matrix rows must sum to 1");}}int j = istart;xout[0] = j;for (int ii = 1; ii < n; ii++){double r = ran.doub() * cum[j, m - 1];int ilo = 0;int ihi = m;while (ihi - ilo > 1){int ia = (ihi + ilo) >> 1;if (r > cum[j, ia - 1]){ilo = ia;}else{ihi = ia;}}xout[ii] = j = ilo;}}}
}

这篇关于C#,数值计算——隐式马尔科夫模型(Hidden Markov Models)的计算方法与源程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/231534

相关文章

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

C#基础之委托详解(Delegate)

《C#基础之委托详解(Delegate)》:本文主要介绍C#基础之委托(Delegate),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 委托定义2. 委托实例化3. 多播委托(Multicast Delegates)4. 委托的用途事件处理回调函数LINQ

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应