name=input( 'Please input your name ','s')
% problem with inputdlg--don't know how to convert from cell to string
%name  = inputdlg('Please input your name ','Name',1,{'M de la probabilite'}); 
disp(['Welcome, ', name])

ns=1;  S0=60; nd=52; nsim=10;% n=number of stocks, S0=initial stock price,nd=number of days
bar=63;  % this is the critical barrier
disp(['you are granted ',  num2str(nsim),  ' lives in this company'])

op=S0+(1:10);  sig=sqrt((1+1/nd)*(2*nd+1)/6);
%price=optionprice(op,S0,sig)
[price,avpr]=binoptionprice(S0,nd,op);
gl='<>';
for i=1:nsim
 R=binornd(1,.5,nd,1);
 S=[S0;S0+cumsum(2*R-1)];
 av=mean(S(1:nd));  %av is approx N(S0, (1/nd+1)*(2*nd+1)/6)
 subplot(3,1,1)
 plot((0:nd),S)
 hold on
 
 plot((0:nd),bar*ones(1,nd+1),'r')
 text(1,bar,['barrier at ' num2str(bar)])
 xlabel('Week Number')
 ylabel('Cost per barrel')
 h=(av>bar);
 D=['congratulations, you are promoted!';'sorry you are fired!              '];  %width=34
 disp([name ', ' D(h+1,:), ' Average price of fuel=',  num2str(av),gl(h+1),num2str(bar)])
end
pause(3)
subplot(3,1,2)
hist(avpr,45:75)
title('average fuel costs over 50,000 lifetimes')
xlabel('average cost')
ylabel('frequency/50,000')
pause(3)
disp('Touch any key to continue')
pause
disp(['If you wish, I  will guarantee your oil prices per barrel for a premium'])
pause(1)
cost=0;
disp(['    price    premium'])
     disp([op' price'])
     pr=100; % default value of pr
      button = questdlg('Do you wish to buy a Price Guarantee?','Price Guarantee','No')
     if(button(1)=='Y')
         pr=input( 'Enter price that you wish to guarantee: ')
         cost=binoptionprice(S0,nd,pr);
         disp(['cost per barrel for price guarantee ', num2str(cost)])
         pause(2)
end
subplot(3,1,3)
hold on
for i=1:nsim 
    disp( ['Life Number ' num2str(i)])
    pause(1)
 R=binornd(1,.5,nd,ns);
 S=[S0;S0+cumsum(2*R-1)];
 av=mean(S(1:nd));  %av is approx N(S0, (1/nd+1)*(2*nd+1)/6)
 plot((0:nd),S)
 plot((0:nd),(bar-cost)*ones(1,nd+1),'r')
 text(1,(bar-cost),[num2str(bar-cost),'=',num2str(bar), ' minus guarantee cost per barrel'])
 xlabel('Week Number')
 ylabel('Cost per barrel')
 h=((cost+min(av,pr))>bar);
 D=['congratulations, you are promoted!!!';'sorry, you are fired!               '];  %width=34
 disp([name ', ' D(h+1,:), ' Hedge cost+average price of fuel=',  num2str(cost),'+',num2str(min(av,pr)),'=',num2str(rnd(cost+min(av,pr),2)),gl(h+1),num2str(bar)])
end
hold off
bet=[6 7 7 8 8 9 9 10 10 11 11 1 1];