%
%  math495/stat490 -- 24 nov 03 -- djm
%
%  w13bridge.m:  

clear;

%  walk parameters
T = 1.0;  WT = 1.0;  sig2 = 1.0;
Nst = 300;  dt = T/Nst;  dx = sqrt(sig2*dt);

%  initialize random number generator
phone = 2914814;
rand('state',phone);  %  initialize with my office phone number

%  plot 10 sample random walks
figure(1);  clf;  hold on
time = [0;dt*(1:Nst)'];  

Nw1 = 10;
walks = [zeros(1,Nw1);cumsum(2*(rand(Nst,Nw1)>0.5)-1)*dx];
bridge = walks + time*(WT-walks(end,:)/T);    
plot(time,bridge)

%  gather statistics for 5000 walks
Nw1 = 5000;
walks = [zeros(1,Nw1);cumsum(2*(rand(Nst,Nw1)>0.5)-1)*dx];
bridge = walks + time*(WT-walks(end,:)/T);    
plot(time,mean(bridge'),'k')
plot(time,mean(bridge')+sqrt(var(bridge')),'k--')
plot(time,mean(bridge')-sqrt(var(bridge')),'k--')

title('\bf 10 sample random bridges with computed mean & \pm \surdvar (5000 walks)')
ylabel('\bf W(T)-axis')
xlabel('\bf T-axis')

