%martingale representation of max
dt=0.00001;
t=[0:dt:1];
n=size(t);
n=n(2);

clf reset;
 
Bmt = cumsum(sqrt(dt)*randn(1,n));
plot(t,Bmt(1:n),'Color',[0 0 0]);
hold on;
   
Bmtr=Bmt(1:n-1); 
inand=zeros(1,n-1);
rmax=zeros(1,n-1);
rmax(1)=max(0,Bmt(1));
for i=2:n-1
    rmax(i)=max(Bmt(i),rmax(i-1)); %running max
end
inand=2-2*normcdf((rmax-Bmtr)./sqrt(1-t(1:n-1))); %integrand
intor=Bmt(2:n)-Bmt(1:n-1); %integrator
pr=inand.*intor;
It=cumsum(pr); %integral

plot(t(1:n-1), rmax, 'Color',[0 0 1]);
plot(t(1:n-1), inand, 'Color',[0 1 0]);
plot(t(1:n-1), sqrt(2/pi)+It, 'Color',[1 0 0]);
legend('B_t', 'running max. of B_t', 'integrand', 'integral','Location', 'northeastoutside');
hold on;
   
title('Martingale representation of max. of B.m. on [0,1]');

  