声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3715|回复: 3

[编程技巧] 关于求解马尔科夫链的平稳分布,大牛进!!!

[复制链接]
发表于 2012-3-18 10:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
本帖最后由 yangfan715 于 2012-3-18 12:26 编辑

知道马尔科夫链的转移流量图,如何求解马尔科夫链的平稳分布?或者说,知道N*N矩阵中各元素之间的关系,如何求各元素的值?在线等。。。。。。
例如:状态(i,j),0<i,j<N,转移流程图如下:

如何求状态(i,j)的平稳分布? 绘图1.jpg
回复
分享到:

使用道具 举报

发表于 2012-11-15 17:29 | 显示全部楼层
发表于 2012-11-28 15:47 | 显示全部楼层
一个马尔科夫链的matlab程序

  1. function [chain,state]=markov(T,n,s0,V);
  2. %function [chain,state]=markov(T,n,s0,V);
  3. %  chain generates a simulation from a Markov chain of dimension
  4. %  the size of T
  5. %
  6. %  T is transition matrix
  7. %  n is number of periods to simulate
  8. %  s0 is initial state (initial probabilities)
  9. %  V is the quantity corresponding to each state
  10. %  state is a matrix recording the number of the realized state at time t
  11. %
  12. % Original author: Tom Sargent
  13. % Comments added by Qiang Chen


  14. [r c]=size(T);   % r is # of rows, c is # of columns of T
  15. if nargin == 1;  % "nargin" refers to "number of arguments in". So only T is provided in this case
  16.   V=[1:r];
  17.   s0=1;
  18.   n=100;
  19. end;
  20. if nargin == 2;  % both T and n are provided
  21.   V=[1:r];
  22.   s0=1;
  23. end;
  24. if nargin == 3;  % T, n and S0 are provided
  25.   V=[1:r];
  26. end;
  27. % check if the transition matrix T is square
  28. if r ~= c;  
  29.   disp('error using markov function');
  30.   disp('transition matrix must be square');
  31.   return;  % break the program and return
  32. end;
  33. % check if each row of T sums up to 1
  34. for k=1:r;
  35.   if sum(T(k,:)) ~= 1;
  36.    disp('error using markov function')
  37.     disp(['row ',num2str(k),' does not sum to one']);  % "num2str" converts numbers to a string.
  38.     disp(' it sums to :');
  39.     disp([ sum(T(k,:)) ]);
  40.     disp(['normalizing row ',num2str(k),'']);
  41.     T(k,:)=T(k,:)/sum(T(k,:));
  42.   end;
  43. end;
  44. [v1 v2]=size(V);
  45. if v1 ~= 1 | v2 ~=r   % "|" means "or"
  46.   disp('error using markov function');
  47.   disp(['state value vector V must be 1 x ',num2str(r),''])
  48.   if v2 == 1 &v2 == r;
  49.     disp('transposing state valuation vector');
  50.     V=V';  % change it to a column vector
  51.   else;
  52.     return;
  53.   end;  
  54. end
  55. if s0 < 1 |s0 > r;
  56.   disp(['initial state ',num2str(s0),' is out of range']);
  57.   disp(['initial state defaulting to 1']);
  58.   s0=1;
  59. end;

  60. % The simulation of Markov chain formally starts from here
  61. %T
  62. %rand('uniform');

  63. X=rand(n-1,1);  % generate (n-1) random numbers drawn from uniform distribution on [0,1], each number to be used in one simulation.

  64. s=zeros(r,1);   % initiate the state vector "s" to be a rx1 zero vector

  65. s(s0)=1;        % change the "s0"th element of "s" to 1

  66. cum=T*triu(ones(size(T)));
  67. % "triu(ones(size(T)))" generates an upper triangular matrix with all elements equal to 1
  68. % cum is a rxr matrix whose ith column is the cumulative sum from the 1st column to the ith column
  69. % the ith row of cum is the cumulative distribution for the next period given the current state.

  70. for k=1:length(X);  % "length(X)" returns the size of the longest dimension of X. "k" indicates the kth simulation.
  71.    
  72.   state(:,k)=s;     % state is a matrix recording the number of the realized state at time k
  73.   
  74.   ppi=[0 s'*cum];   % this is the conditional cumulative distribution for the next period given the current state s
  75.   
  76.   s=((X(k)<=ppi(2:r+1)).*(X(k)>ppi(1:r)))';   
  77.   % compares each element of ppi(2:r+1) or ppi(1:r) with a scalar X(k), and
  78.   % returns 1 if the inequality holds and 0 otherwise
  79.   % this formula assigns 1 when both inequalities hold, and 0 otherwise
  80.   
  81. end;
  82. chain=V*state;
复制代码


发表于 2013-11-29 16:30 | 显示全部楼层
我想问一下这个转移流量图是怎么建立的啊?
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-28 21:43 , Processed in 0.119125 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表