Skip to content

Instantly share code, notes, and snippets.

@Daijunxu
Created October 29, 2011 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daijunxu/1325037 to your computer and use it in GitHub Desktop.
Save Daijunxu/1325037 to your computer and use it in GitHub Desktop.
Vectorizing
%% Import test data
CLC=xlsread('CL.xlsx','CallOptions');
%% plot and compare
figure % CL
% Call option of CL
% Bisection methods
[EstimatedPrice,Interations,CL_B_call_bid]=arrayfun(@bisection_iv,CLC(:,3),CLC(:,1),CLC(:,2),0.0012.*ones(length(CLC),1),0.25.*ones(length(CLC),1));
subplot(2,2,1);plot(CL_B_call_bid,CLC(:,2))
title('Besection methods implied volatility for call option in CL')
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ EstimatedPrice,Interations,ImpliedVolatility ] = bisection_iv(optpx,S,K,r,T)
% Using bisection methods to find the implied volatility
% Set the Black Scholers methods to calculate options price
flag=1; % Default option type is call
d1=@(S,K,r,vol,T)(log(S./K)+(r+vol.^2./2).*T)./(vol.*sqrt(T));
d2=@(S,K,r,vol,T)(log(S./K)+(r-vol.^2./2).*T)./(vol.*sqrt(T));
bscallpx=@(S,K,r,vol,T)S.*cdf('norm',d1(S,K,r,vol,T))-K.*exp(-r.*T).*cdf('norm',d2(S,K,r,vol,T));
bsputpx=@(S,K,r,vol,T)K.*exp(-r.*T).*cdf('norm',-d2(S,K,r,vol,T))-S.*cdf('norm',-d1(S,K,r,vol,T));
bspx=@(S,K,r,vol,T,flag)flag.*bscallpx(S,K,r,vol,T)+(1-flag).*bsputpx(S,K,r,vol,T);
%bsvega=@(S,K,r,vol,T)S.*pdf('norm',d1(S,K,r,vol,T)).*sqrt(T);
Threshold=0.000001;
vol_lb=0; % define a lowerbound to guess implied volatility
vol_ub=2; % define a upbound to guess implied volatility
vol=(vol_ub+vol_lb)./2;
i=1;
while(abs(optpx-bspx(S,K,r,vol,T,flag))/optpx>Threshold) % Set the accuracy
if optpx>bspx(S,K,r,vol,T,flag)
vol_lb=vol;
vol=(vol+vol_ub)/2;
else
vol_ub=vol;
vol=(vol+vol_lb)/2;
end
i=i+1;
end
EstimatedPrice=bspx(S,K,r,vol,T,flag);
Interations=i;
ImpliedVolatility=vol;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment