首页 > 代码库 > C#实现马尔科夫模型例子

C#实现马尔科夫模型例子

已知条件:三个缸N状态,每个缸中不同颜色球的个数M状态值,时间轴T,观察值序列O

参数:状态值序列,转移概率序列

求:概率

技术分享

后台代码如下

  1         const int N = 3, M = 4;//N状态,M状态值 (0橙色,1绿色,2蓝色,3黄色)  2         public int[] O = { 1, 1, 0, 2, 1, 3, 2, 0 };//观察值序列  3         public double[,] A = new double[N, N];//初始一个三行三列的二维数组(状态转移概率)  4         public double[,] B = new double[N, M];//初始一个三行四列的二维数组(观察值的概率矩阵)  5         public double[] PI = { 1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0 };//初始化概率 10  11         #region 获取观察值概率 12         /// <summary> 13         /// 获取观察值概率 14         /// </summary> 15         public void GetB() 16         { 17             //第一缸球的颜色 18             double[] one = { 4, 3, 1, 0 }; 19             //第二缸球的颜色 20             double[] two = { 2, 4, 2, 1 }; 21             //第一缸球的颜色 22             double[] three = { 2, 1, 4, 2 }; 23             //每个缸中球的总数量 24             int[] count = { 8, 9, 9 }; 25             for (int i = 0; i < 4; i++) 26             { 27                 B[0, i] = one[i] / count[0]; 28                 B[1, i] = two[i] / count[1]; 29                 B[2, i] = three[i] / count[2]; 30             } 31         } 32         #endregion 33  34         #region 获取概率P值 zhy 35         /// <summary> 36         /// 获取概率P值 37         /// </summary> 38         /// <param name="Q">状态值序列(0第一个缸,1第二个缸,2第三个缸)</param> 39         /// <param name="A">转移概率</param> 40         /// <returns></returns> 41         public double GetP(string Q, string A) 42         { 43             GetA(A); 44             GetB(); 45             int[] q = GetQ(Q); 46             //时间轴 47             int T = 8; 48             //初始概率Q[0]:第一缸, O[0]:第一个球 49             double p = PI[q[0]] * B[q[0], O[0]]; 50             for (int i = 1; i < T; i++) 51             { 52                 //this.A:q[i-1]取上一个刚,q[i]取当前缸,然后获得转移概率 53                 //B:q[i]取当前缸,O[i]取缸中哪个颜色的球 54                 p *= this.A[q[i - 1], q[i]] * B[q[i], O[i]]; 55             } 56             return p; 57         } 58         #endregion 59  60         #region 获取状态值序列 zhy 61         /// <summary> 62         /// 获取状态值序列 63         /// </summary> 64         /// <param name="Q">状态值字符串</param> 65         /// <returns>状态值数组</returns> 66         public int[] GetQ(string Q) 67         { 68             int[] q = null; 69             if (Q.TrimEnd(,).IndexOf(,) > 0) 70             { 71                 //获得状态序列 72                 string[] zhuangtai = Q.TrimEnd(,).Split(,); 73                 q = new int[zhuangtai.Length]; 74                 for (int i = 0; i < zhuangtai.Length; i++) 75                 { 76                     q[i] = Convert.ToInt32(zhuangtai[i]); 77                 } 78             } 79             else 80             { 81                 q = new int[1]; 82                 q[0] = Convert.ToInt32(Q); 83             } 84             return q; 85         } 86         #endregion 87  88         #region 获取转移概率序列 zhy 89         /// <summary> 90         /// 获取转移概率序列 91         /// </summary> 92         /// <param name="A">转移概率字符串</param> 93         public void GetA(string A) 94         { 95             if (A.TrimEnd(,).IndexOf(,) > 0) 96             { 97                 string[] gailv = A.TrimEnd(,).Split(,); 98                 //获取A的转移概率的二维数组 99                 for (int i = 0; i < 3; i++)100                 {101                     for (int j = 0; j < 3; j++)102                     {103                         this.A[i, j] = Convert.ToDouble(gailv[3 * i + j]);104                     }105                 }106             }107         }108         #endregion

html如下:

 1         <span>请输入状态值序列&nbsp;&nbsp;: </span><input type="text" id="zhuangtaizhi" value=http://www.mamicode.com/"0,2,1,1,2,0,0,1" /><span>&nbsp;&nbsp; 多个值请使用","隔开(0第一缸,1第二缸,2第三缸)</span><br /><br /> 2         <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a11-a13  :</span> 3         <input type="text" id="a11_a13" value=http://www.mamicode.com/"0.25,0.45,0.2" /><span>&nbsp;&nbsp; 请输入第一缸的转移概率以","隔开,三值相加等于1</span><br /><br /> 4         <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a21-a23  :</span> 5         <input type="text" id="a21_a23" value=http://www.mamicode.com/"0.1,0.85,0.15" /><span>&nbsp;&nbsp; 请输入第二缸的转移概率以","隔开,三值相加等于1</span><br /><br /> 6         <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a31-a33  :</span> 7         <input type="text" id="a31_a33" value=http://www.mamicode.com/"0.14,0.55,0.31" /><span>&nbsp;&nbsp; 请输入第三缸的转移概率以","隔开,三值相加等于1</span><br /><br /> 8         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9         <input type="button" value=http://www.mamicode.com/"计算" onclick="count()" />10         <div style="display:none;" id="jieguo">11             <span>概率为:</span><span id="gailv"></span>12         </div>

 

js如下

 1 function count() { 2         var zhuangtaizhi = $.trim($("#zhuangtaizhi").val()); 3         var a11_a13 = $.trim($("#a11_a13").val()); 4         var a21_a23 = $.trim($("#a21_a23").val()); 5         var a31_a33 = $.trim($("#a31_a33").val()); 6         if (zhuangtaizhi == "") { 7             alert("请输入状态值序列"); 8             return; 9         }10         if (a11_a13 == "") {11             alert("请输入a11-a13的转移概率");12             return;13         }14         if (a21_a23 == "") {15             alert("请输入a21-a23的转移概率");16             return;17         }18         if (a21_a23 == "") {19             alert("请输入a21-a23的转移概率");20             return;21         }22         var zhuanyigailv = a11_a13 + "," + a21_a23 + "," + a31_a33;23         $.post("/YinMa/GetP", { Q: zhuangtaizhi, A: zhuanyigailv }, function (msg) {24             $("#gailv").html(msg);25             $("#jieguo").show();26         });27     }

纯本人手写,转载请注明出处

C#实现马尔科夫模型例子