Stochastic Processes

1
2
StochasticProcessesExamples.m
9th June 2007
rand(’state’, 0);
%% Continuous-time Random Process
%%----------------------------------------------------------------
%% (a) Sinusoid with random amplitude
zeta = rand(3,1);
%% create 3 samples of zeta~U(0,1);
t = 0:.01:3;
%%time index
% create 3 relization
X1 = zeta(1) * cos(2 * pi * t);
X2 = zeta(2) * cos(2 * pi * t);
X3 = zeta(3) * cos(2 * pi * t);
figure(1), clf;
subplot(221), plot(t, X1, ’blue’);
axis([0, 3, -1.2, 1.2]);
grid;
hold on;
h = legend([’\zeta=’, num2str(zeta(1))], ’interpreter’,’latex’);
xlabel(’$t$’, ’interpreter’,’latex’);
ylabel(’$X t(\zeta)$’, ’interpreter’,’latex’);
title(’(a) Sinusoid with random amplitude: $X t(\zeta)=\zeta \cos(2 \pi t)$’, ...
’FontSize’, 13, ’interpreter’,’latex’);
% xlabel(’$t$’,’FontSize’,14, ’interpreter’,’latex’);
drawnow;
pause;
subplot(221), plot(t, X2, ’red’);
h=legend([’\zeta=’, num2str(zeta(1))],[’\zeta 2=’, num2str(zeta(2))], ...
’interpreter’,’latex’);
drawnow;
pause;
subplot(221), plot(t, X3, ’green’);
h=legend([’\zeta=’, num2str(zeta(1))],[’\zeta=’, num2str(zeta(2))], ...
[’\zeta=’, num2str(zeta(3))], ’interpreter’,’latex’);
hold off;
pause;
%% (b) Sinusoid with random phase
zeta = 2 * pi * rand(3,1);
%% create 3 samples of zeta~U(0,2 pi);
t = 0:.01:3;
%%time index
% create 3 relization
Y1 = cos(2 * pi * t + zeta(1));
1
3
Y2 = cos(2 * pi * t + zeta(2));
Y3 = cos(2 * pi * t + zeta(3));
figure(1),
subplot(222), plot(t, Y1, ’blue’);
axis([0, 3, -1.2, 1.2]);
grid;
hold on;
h = legend([’\zeta=’, num2str(zeta(1)/pi), ’\pi’], ’interpreter’,’latex’);
xlabel(’$t$’, ’interpreter’,’latex’);
ylabel(’$Y t(\zeta)$’, ’interpreter’,’latex’);
title(’(a) Sinusoid with random phase: $Y t(\zeta)= \cos(2 \pi t + \zeta)$’, ...
’FontSize’, 13, ’interpreter’,’latex’);
% xlabel(’$t$’,’FontSize’,14, ’interpreter’,’latex’);
drawnow;
pause;
subplot(222), plot(t, Y2, ’red’);
h=legend([’\zeta=’, num2str(zeta(1)/pi), ’\pi’],[’\zeta=’, num2str(zeta(2)/pi), ’\pi’], ...
’interpreter’,’latex’);
drawnow;
pause;
subplot(222), plot(t, Y3, ’green’);
h=legend([’\zeta=’, num2str(zeta(1)/pi), ’\pi’],[’\zeta=’, num2str(zeta(2)/pi), ’\pi’], ...
[’\zeta=’, num2str(zeta(3)/pi), ’\pi’], ’interpreter’,’latex’);
hold off;
pause;
%% Discrete-time Random Process
%%---------------------------------------------------------------rand(’state’, 0);
%% (c) Sinusoid with random amplitude
zeta = rand(3,1);
%% create 3 samples of zeta~U(0,1);
n = 0:30;
%%discrete-time index
% create 3 relization
X1 = zeta(1) * cos(2 * pi * n ./ 10);
X2 = zeta(2) * cos(2 * pi * n ./ 10);
X3 = zeta(3) * cos(2 * pi * n ./ 10);
figure(1);
subplot(223), h=stem(n, X1);
set(h,’MarkerFaceColor’,’blue’)
axis([0, 30, -1.2, 1.2]);
grid;
hold on;
h = legend([’\zeta=’, num2str(zeta(1))], ’interpreter’,’latex’);
xlabel(’$t$’, ’interpreter’,’latex’);
ylabel(’$X n(\zeta)$’, ’interpreter’,’latex’);
title(’(c) Sinusoid with random amplitude: $X n(\zeta)=\zeta \cos(2 \pi n)$’, ...
’FontSize’, 13, ’interpreter’,’latex’);
% xlabel(’$t$’,’FontSize’,14, ’interpreter’,’latex’);
drawnow;
pause;
2
4
subplot(223), h=stem(n, X2);
set(h,’MarkerFaceColor’,’red’)
h=legend([’\zeta=’, num2str(zeta(1))],[’\zeta=’, num2str(zeta(2))], ...
’interpreter’,’latex’);
drawnow;
pause;
subplot(223), h=stem(n, X3);
set(h,’MarkerFaceColor’,’green’)
h=legend([’\zeta=’, num2str(zeta(1))],[’\zeta=’, num2str(zeta(2))], ...
[’\zeta=’, num2str(zeta(3))], ’interpreter’,’latex’);
hold off;
pause;
%% (b) Sinusoid with random phase
zeta = rand(3,1);
%% create 3 samples of zeta~U(0,1);
% create 3 relization
zeta = 2 * pi * rand(3,1);
%% create 3 samples of zeta~U(0,2 pi);
n = 0:30;
%%discrete-time index
% create 3
Y1 = cos(2
Y2 = cos(2
Y3 = cos(2
relization
* pi * n / 10 + zeta(1));
* pi * n / 10 + zeta(2));
* pi * n / 10 + zeta(3));
figure(1);
subplot(224), h=stem(n, Y1);
set(h,’MarkerFaceColor’,’blue’)
axis([0, 30, -1.2, 1.2]);
grid;
hold on;
h = legend([’\zeta=’, num2str(zeta(1)/pi), ’\pi’], ’FontSize’, 13, ...
’interpreter’,’latex’);
xlabel(’$t$’, ’interpreter’,’latex’);
ylabel(’$Y n(\zeta)$’, ’interpreter’,’latex’);
title(’(c) Sinusoid with random amplitude: $X n(\zeta)= \cos(2 \pi n + \zeta)$’, ...
’interpreter’,’latex’);
% xlabel(’$t$’,’FontSize’,14, ’interpreter’,’latex’);
drawnow;
pause;
subplot(224), h=stem(n, Y2);
set(h,’MarkerFaceColor’,’red’)
h=legend([’\zeta=’, num2str(zeta(1)/pi), ’\pi’],[’\zeta=’, num2str(zeta(2)/pi), ’\pi’], ...
’interpreter’,’latex’);
drawnow;
pause;
subplot(224), h=stem(n, Y3);
set(h,’MarkerFaceColor’,’green’)
h=legend([’\zeta=’, num2str(zeta(1)/pi), ’\pi’],[’\zeta=’, num2str(zeta(2)/pi), ’\pi’], ...
[’\zeta=’, num2str(zeta(3)/pi), ’\pi’], ’interpreter’,’latex’);
hold off;
3
5
Figure 1: Simulation of stochastic processes.
4
6
7
8
1
StochasticProcessesExamples.m
function X = generateSamples(a, b, n)
% generateSamples
generates samples from the discrete probability vectors
%
P(X=0) = 1 - (a + b), P(X=1) = a, P(X = 2)=b
%
% Description
%
[1-(a+b), a, b] -- probability vector
%
n
-- number of samples, default is 1
%
%
% Returns
%
- vector of generated samples
%
% [X] = generateSamples(a, b, n)
%
%
% Author: Alon Tairy
% e-mail: [email protected]
% date: May 07
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (nargin == 2)
n = 1
end
Pac = [1 - (a + b), 1 - b, 1];
r = rand(n, 1);
i0 = find(r < 1 - (a + b));
i1 = find(r >= 1 - (a + b) & r < 1 - b);
i2 = find(r >= 1 - (b));
X =zeros(n, 1);
X(i1) = 1.0;
X(i2) = 2.0;
2
example1RandomWalk.m
%% Example 1 -- Random Walk as Markov Chain
rand(’state’, 0);
% probabilities
a = .45;
b = .3;
N = 100;
Z = generateSamples(a, b, N);
Z(find(Z==2)) = -1;
X = double(zeros(N + 1, 1));
%generate 50 samples
1
9
figure(1), clf;
Xn = 0;
for n = 1: N
Xn = Xn + Z(n);
X(n + 1) = Xn;
subplot(221), stem([0:n], X(1: n+1));
axis([0, N, -10, 30]);
xlabel(’$n$’, ’interpreter’,’latex’);
ylabel(’$X n$’, ’interpreter’,’latex’);
title(’(a) Random walk: $X {n+1} = X n + Z {n+1} $’, ’FontSize’, 13, ’interpreter’,’latex’);
grid;
subplot(222), plot(X(1: n+1), zeros(n+1, 1));
axis([-10, 30, -5, 5]);
grid
xlabel(’$X n$’, ’interpreter’,’latex’);
title([’(b) Random walk: path and current position for n =’, num2str(n) ], ...
’FontSize’, 13, ’interpreter’,’latex’);
hold on;
plot(X(n+1), 0, ’r*’);
hold off;
drawnow;
drawnow;
end
Figure 1: Simulation of random walk.
2
10
11
12
13
14
15
16
17
18
19