%martingale representation of max
dt=0.0001;
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 1]);
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 0.5]);
plot(t(1:n-1), inand, 'Color',[1 1 0.5]);
plot(t(1:n-1), sqrt(2/pi)+It, 'Color',[1 0 0]);
legend('Bm', 'run max', 'integrand', 'integral',-1);
hold on;

title('Martingale representation of B.m. max on [0,1]');