markov.m
来自「Neural Network in Finance (神经网络在金融界:赢得预言」· M 代码 · 共 25 行
M
25 行
function S = markov(P,T,S0);
%MARKOV generates a realization of a Markov chain with transition matrix P.
% S = markov(P,T) returns a T x 1 vector S, which is a realization
% of a stationary Markov chain that has transition probabilities
% in P. The elements of S are integers from 1 to n where n is
% the dimension of P.
% S = markov(P,T,S0) uses the initial state S0 for S(1).
% Ellen McGrattan, 9-7-92
% Revised, ERM, 5-23-94
n = length(P);
if nargin<3;
S(1) = ceil(rand*n);
else
S(1) = S0;
end;
Q = cumsum(P');
for t = 2:T;
x = find(rand < Q(:,S(t-1)));
S(t) = x(1);
end;
S = S(:);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?