% Monte Carlo simulation to price exotics
clear
M = input('enter the number of paths: ');
N = input('enter the number of steps: ');
exotics_type = input('enter the type of exotics: ','s');

strike = 100;

S0 = input('enter the beginning price of S: ');
sigma = input('enter sigma: ');

r = 0.01;
mu = r - 0.5*sigma^2;
barrier = S0 + 20;
T = 0.5;
dt = T/N;

x = zeros(M,N+1);
w = randn(M,N);
payoff = zeros(M,1);

t = 0:dt:T;

% generate brownian paths:
for k=1:N
    x(:,k+1) = x(:,k) + mu*dt + sigma*sqrt(dt)*w(:,k);
end

% generate stock prices along the path:
Sprice = S0*exp(x);

for j=1:M
    switch exotics_type
        case 'up_and_out'
            if max(Sprice(j,:)) > barrier
                payoff(j) = 0;
            else
                payoff(j) = 0.5*(Sprice(j,N+1)-strike + abs(Sprice(j,N+1)-strike));
            end
        case 'asian'
            payoff(j) = max(mean(Sprice(j,:))-strike,0);
        case 'floating_call'
            payoff(j) = Sprice(j,N+1) - min(Sprice(j,:));
        otherwise
            warning('type not recognized');
    end
            
end

V0 = exp(-r*T)*mean(payoff);
Evar = var(payoff);

fprintf('MC price for the exotics is %10.6f, std is %10.6f\n',V0, sqrt(Evar));
