收藏 分享(赏)

马尔可夫编程matlab.doc

上传人:HR专家 文档编号:5981658 上传时间:2019-03-22 格式:DOC 页数:3 大小:30KB
下载 相关 举报
马尔可夫编程matlab.doc_第1页
第1页 / 共3页
马尔可夫编程matlab.doc_第2页
第2页 / 共3页
马尔可夫编程matlab.doc_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

1、%function chain,state=markov(T,n,s0,V);%function chain,state=markov(T,n,s0,V);% chain generates a simulation from a Markov chain of dimension% the size of T% T is transition matrix% n is number of periods to simulate% s0 is initial state (initial probabilities)% V is the quantity corresponding to ea

2、ch state% state is a matrix recording the number of the realized state at time t% Original author: Tom Sargent% Comments added by Qiang Chenr c=size(T); % r is # of rows, c is # of columns of Tif nargin = 1; % “nargin“ refers to “number of arguments in“. So only T is provided in this caseV=1:r;s0=1;

3、n=100;end;if nargin = 2; % both T and n are provided V=1:r;s0=1;end;if nargin = 3; % T, n and S0 are providedV=1:r;end;% check if the transition matrix T is squareif r = c; disp(error using markov function);disp(transition matrix must be square);return; % break the program and returnend;% check if e

4、ach row of T sums up to 1for k=1:r;if sum(T(k,:) = 1;disp(error using markov function)disp(row ,num2str(k), does not sum to one); % “num2str“ converts numbers to a string. disp( it sums to :); disp( sum(T(k,:) ); disp(normalizing row ,num2str(k),);T(k,:)=T(k,:)/sum(T(k,:);end;end;v1 v2=size(V);if v1

5、 = 1 | v2 =r % “|“ means “or“disp(error using markov function); disp(state value vector V must be 1 x ,num2str(r),)if v2 = 1 disp(transposing state valuation vector);V=V; % change it to a column vectorelse;return;end; endif s0 r;disp(initial state ,num2str(s0), is out of range);disp(initial state de

6、faulting to 1);s0=1;end;% The simulation of Markov chain formally starts from here%T%rand(uniform);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.s=zeros(r,1); % initiate the state vector “s“ to be a rx1 zero vectors(s0

7、)=1; % change the “s0“th element of “s“ to 1cum=T*triu(ones(size(T); % “triu(ones(size(T)“ generates an upper triangular matrix with all elements equal to 1% cum is a rxr matrix whose ith column is the cumulative sum from the 1st column to the ith column % the ith row of cum is the cumulative distri

8、bution for the next period given the current state. for k=1:length(X); % “length(X)“ returns the size of the longest dimension of X. “k“ indicates the kth simulation. state(:,k)=s; % state is a matrix recording the number of the realized state at time k ppi=0 s*cum; % this is the conditional cumulative distribution for the next period given the current state ss=(X(k)ppi(1:r); % compares each element of ppi(2:r+1) or ppi(1:r) with a scalar X(k), and% returns 1 if the inequality holds and 0 otherwise% this formula assigns 1 when both inequalities hold, and 0 otherwiseend;chain=V*state;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 经营企划

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报