%% Call Option Sensitivity Measures (portfolio of options)
% This example plots gamma as a function of price and time for a portfolio of 10 Black-Scholes options. The plot shows a three-dimensional surface. For each point on the surface, the height (z-value) represents the sum of the gammas for each option in the portfolio weighted by the amount of each option. The x-axis represents changing price, and the y-axis represents time. The plot adds a fourth dimension by showing delta as surface color. This has applications in hedging.
%% Set Up

if ~exist('normpdf')
  msgbox('The Statistics and Machine Learning Toolbox is required to run this example.', ...
	  'Product dependency')
  return
end

%
%% Set Up a Portfolio of Options on a Single Stock

% Range of stock prices for sensitivity analysis
range = 20:90;
plen = length(range);

% Basic information for each option
exprice = [75 70 50 55 75 50 40 75 60 35];
rate = 0.1*ones(10,1);
time = [36  36  36  27  18  18  18  9  9  9];
sigma = 0.35*ones(10,1);

% Portfolio weights
numopt = 1000*[4  8  3  5  5.5  2  4.8  3  4.8  2.5];

zval = zeros(36, plen);
color = zeros(36, plen);

%
%% Loop Over Each Option in the Portfolio
% Calculate "Greeks" gamma and delta

for i = 1:10
       pad = ones(time(i),plen);
       newr = range(ones(time(i),1),:);

       t = (1:time(i))';
       newt = t(:,ones(plen,1));

       % Calculate gammas
       zval(36-time(i)+1:36,:) = zval(36-time(i)+1:36,:) ...
              + numopt(i) * blsgamma(newr, exprice(i)*pad, ...
              rate(i)*pad, newt/36, sigma(i)*pad);

       % Calculate deltas   
       color(36-time(i)+1:36,:) = color(36-time(i)+1:36,:) ...
              + numopt(i) * blsdelta(newr, exprice(i)*pad, ...
              rate(i)*pad, newt/36, sigma(i)*pad);
end

%
%% Plot Sensitivities of a Portfolio of Options
% Height is gamma (second derivative of option price with respect to stock price).
% Color is delta (first derivative of option price with respect to stock price).

figure('NumberTitle', 'off', ...
       'Name', 'Call Option Portfolio Sensitivity');

mesh(range, 1:36, zval, color);
view(60,60);
ax = gca;
ax.XDir = 'reverse';
ax.Tag = 'mesh_axes_3';
axis([20 90  0 36  -inf inf]);

title('Call Option Portfolio Sensitivity');
xlabel('Stock Price ($)');
ylabel('Time (months)');
zlabel('Gamma');
ax.Box = 'on';
cbx = colorbar('horiz');
apos = ax.Position;
cpos = cbx.Position;
cbx.Position = [cpos(1) .075 cpos(3) cpos(4)];
ax.Position = [apos(1) .25 apos(3) .68];

% Copyright 1995-2014 The MathWorks, Inc.