Problem Description
Consider a system which is described at any time as being in one of a set of N distinct states, 1,2,3,...,N. We denote the time instants associated with state changes as t = 1,2,..., and the actual state at time t as
. For the special case of a discrete, first order, Markovchain, the probabilistic description for the current state (at time tt) and the predecessor state is st. Furthermore we only consider those processes being independent of time, thereby leading to the set of state transition probability aijof the form: with the properties
and
. The stochastic process can be called an observable Markovmodel. Now, let us consider the problem of a simple 4-state Markov model of weather. We assume that once a day (e.g., at noon), the weather is observed as being one of the following:
State 1: snow
State 2: rain
State 3: cloudy
State 4: sunny
The matrix A of state transition probabilities is:
te transition probabilities is:
Given the model, several interesting questions about weather patterns over time can be asked (and answered). We canask the question: what is the probability (according to the given model) thatthe weather for the next k_ days willbe? Another interesting question we can ask: given that the model is in a knownstate, what is the expected number of consecutive days to stay in that state?Let us define the observation sequence O as
, and the probability of the observation sequence O given the model is defined as p(O|model). Also, let the expected number of consecutive days to stayin state i be Ei. Assume that the initial state probabilities
. Both p(O∣model) and Ei are real numbers.
Input
Line 1~4 for the state transition probabilities. Line 5 for the observation sequence O1, and line 6 for the observation sequence O2. Line 7 and line 8 for the states of interest to find the expected number of consecutive days to stay in these states.
Line 1:
Line 2:
Line 3:
Line 4:
Line 5:
Line 6:
Line 7: i
Line 8: j
Output
Line 1 and line 2 are used to show the probabilities of the observation sequences O1 and O2 respectively. Line 3 and line 4 are for the expected number of consecutive days to stay in states ii and jj respectively.
Line 1: p[O1∣model]
Line 2: p[O2∣model]
Line 3: Ei
Line 4: Ej
Please be reminded that the floating number should accurate to 10−8.
Sample Input
0.4 0.3 0.2 0.1
0.3 0.3 0.3 0.1
0.1 0.1 0.6 0.2
0.1 0.2 0.2 0.5
4 4 3 2 2 1 1 3 3
2 1 1 1 3 3 4
3
4Sample Output
0.00004320
0.00115200
2.50000000
2.00000000
题意:已知 1、2、3、4 分别表示一种天气状况,再给出一个 4*4 的一阶马尔科夫状态转移概率矩阵 G 用于表示天气之间转移的概率,现给出两个数串 a、b,问从数串的第一个数依次变换到最后一个数的概率是多少,最后再给出两个整数,分别代表两个天气状况 E1、E2,问该天气趋于平稳状态下的数学期望
思路:
一个阅读理解题,哪怕不了解马尔科夫链,读懂题后也能做
根据马尔科夫状态转移概率矩阵的性质,第 i 行表示当前观测的值,第 j 行表示现在观测的值,由于马尔科夫链转移过程中后一状态仅受前一状态的影响,因此对于第一问分别求两个数串转移概率,只要不断的记录 G[a[i]][a[i+1]] 的值然后相乘即可
当马尔科夫状态转移概率矩阵经过一系列的转移后,最后会趋于一个平稳状态,转移的数学期望也就变成一个定值,因此只要不断的令 G[E1][E1] 与自己相乘,再将相乘后的结果相加即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 100+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
double G[N][N];
int a[N],cntA;
int b[N],cntB;
int main(){
char ch;
int E1,E2;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
scanf("%lf",&G[i][j]);
getchar();
while((ch=getchar())!='\n'){
if(ch!=' ')
a[++cntA]=ch-'0';
}
while((ch=getchar())!='\n'){
if(ch!=' ')
b[++cntB]=ch-'0';
}
scanf("%d%d",&E1,&E2);
double res1=1,res2=1;
for(int i=1;i<=cntA-1;i++)
res1=res1*G[a[i]][a[i+1]];
for(int i=1;i<=cntB-1;i++)
res2=res2*G[b[i]][b[i+1]];
double res3=0,res4=0;
double temp=1;
for(int i=1;i<=10000;i++){
res3+=temp;
temp=temp*G[E1][E1];
}
temp=1;
for(int i=1;i<=10000;i++){
res4+=temp;
temp=temp*G[E2][E2];
}
printf("%.8lf\n",res1);
printf("%.8lf\n",res2);
printf("%.8lf\n",res3);
printf("%.8lf\n",res4);
return 0;
}