Question: Your task is to extend this function to also price European options. Ensure that you do not alter the structure of the input arguments to

Your task is to extend this function to also price European options. Ensure that you do not alter the structure of the input arguments to maintain backwards compatibility with the original version.
function price = binomial_pricer(S0, K, r, sigma, T, type, NumSteps, varargin)
% Values American Options, both calls and puts, accommodating different
% types of dividends
%INPUTS
% S0 : Initial Stock Price
% K : Strike Price
% r : Risk-free Rate
% sigma : Stock volatility
% T : Term to Maturity
% type : Option type = Call 'C' or Put 'P'
% NumSteps : Number of steps in binomial tree
% varargin : variable number of other input arguments.
% No dividend: enter no extra input argument.
% Continuous dividend yield
% y : -1 extra input which is the yield.
% Discrete dividends
% D : the dividend vector
% xdate : time to ex-dividend date vector (in years)
%OUTPUT
% price : option value
%% compute tree parameters
dt = T/NumSteps; %length of each time step, in years
u = exp(sigma*sqrt(dt));
d =1/u;
q =(exp(r *dt)- d)/(u - d); %risk neutral prob of up state
df = exp(-r*dt); %discount factor
%% Forward propagation to construct the underlying price tree
% First, preallocation
St = zeros(NumSteps +1, NumSteps +1); % matrix to hold underlying prices
ft = zeros(NumSteps +1, NumSteps +1); % matrix to hold option prices
% Construct price tree
% Go through different scenarios of dividend based on the number of
% variable input arguments
n = length(varargin);
switch n
case 0% No dividend
% Populate the tree with prices
St(1,1)= S0;
for j =2:NumSteps +1
for i =1:j-1
St(i,j)= St(i, j-1)*u;
end
St(i+1,j)= St(i, j-1)*d;
end
case 1% dividend yield
y = varargin{1}; %
% Recalculate q
q =(exp((r - y)*dt)- d)/(u - d);
% Populate the tree with prices
St(1,1)= S0;
for j =2:NumSteps +1
for i =1:j-1
St(i,j)= St(i, j-1)*u;
end
St(i+1,j)= St(i, j-1)*d;
end
case 2% Scenario 3: discrete dividend
% load input variables for this scenario
D = varargin{1}; %dividend amount
xdate = varargin{2}; %dividend timing
% Remove NPV of dividends from initial price
pv_D = npv(D, xdate, r);
St(1,1)= S0-pv_D;
% Construct price tree that excludes the dividend
for j =2:NumSteps +1
for i =1:j-1
St(i,j)= St(i, j-1)*u;
end
St(i+1,j)= St(i, j-1)*d;
end
% Then add back dividend for nodes that occur before ex div date
tau=0; %Track time through tree steps
for j =1:NumSteps
% check if current time step is before the dividend
time_to_xdate = xdate - tau;
idx = find(time_to_xdate>0);
if ~isempty(idx)
time_to_xdate = time_to_xdate(idx);
div = D(idx);
div_to_addback = npv(div, time_to_xdate, r); % pv of relevant future dividends
St(:,j)=St(:,j)+div_to_addback;
end
taudt; %Increase by 1 timestep
end
end
%% Compute option prices at terminal nodes and perform backward induction to complete the option price tree
if type =='C'
ft(:, end)= max(St(:, end)- K,0);
% Backward induction
for j = NumSteps :-1:1
for i =1:j
CV = df *(q*ft(i, j+1)+(1-q)*ft(i+1, j+1));
IV = St(i,j)- K;
ft(i,j)= max(CV, IV);
end
end
elseif type =='P'
ft(:, end)= max( K - St(:, end),0);
% Backward induction
for j = NumSteps :-1:1
for i =1:j
CV = df *(q*ft(i, j+1)+(1-q)*ft(i+1, j+1));
IV = K - St(i,j);
ft(i,j)= max(CV, IV);
end
end
else
error('binomial_pricer - Option type must be C or P');
end
price = ft(1,1);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!