- Start in a state (eg. state 1 for Bull).
- Wait for an exponentially distributed time with rate = '−Q(i,i)'.
- Jump to the other state.
- Repeat until you reach your total simulation time ('T_max')
Simulation of Markov Chain with Generator
4 次查看(过去 30 天)
显示 更早的评论
How to generate sample paths of a markov chain with given generator Q where
and also given a step size
on the interval
. I believe I need graphs of the sample paths and we've discussed "two-state" Markov chains where {1,2} are the states for bull and bear markets. I'm unsure if I need to have 1 and 2 on the y axis.



0 个评论
回答(1 个)
Shantanu Dixit
2025-5-30
Hi Richard,
If I understood the query correctly, you want to simulate sample paths of a two-state continuous-time Markov chain with generator matrix:
% From state 1 (Bull) to state 2 (Bear): rate = 6
% From state 2 (Bear) to state 1 (Bull): rate = 10
Q = [-6 6; 10 -10];
To simulate this in MATLAB you can:
You can refer to the below example that simulates and plots a sample path using 'stairs': https://www.mathworks.com/help/matlab/ref/stairs.html
Q = [-6 6; 10 -10];
T_max = 10; t = 0; state = 1;
times = 0; states = state;
while t < T_max
lambda = -Q(state,state);
delta_t = exprnd(1/lambda);
t = t + delta_t;
if t > T_max, break; end
state = 3 - state; % toggle between 1 and 2
times(end+1) = t;
states(end+1) = state;
end
times(end+1) = T_max; states(end+1) = states(end);
% Resample with fixed step size (e.g., 0.1)
stepSize = 0.1;
t_fixed = 0:stepSize:T_max;
states_fixed = interp1(times, states, t_fixed, 'previous');
stairs(t_fixed, states_fixed, 'LineWidth', 2)
ylim([0.5 2.5]); yticks([1 2]); yticklabels({'Bull','Bear'})
xlabel('Time'); ylabel('Market State')
title('Markov Chain Sample Path (Fixed Step Size)'); grid on
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Markov Chain Models 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!