Code covered by the BSD License  

Highlights from
3D Plot for Greeks

image thumbnail
from 3D Plot for Greeks by Rodolphe Sitter
Plot in 3 dimensions the Greeks under the Black-Scholes model for a European call

VisualizeGreeks(varargin)
function VisualizeGreeks(varargin)
% VisualizeGreeks Select a data set from the pop-up menu, then
% click one of the plot-type push buttons. Clicking the button
% plots the selected data in the axes.
 
   %  Create and then hide the GUI as it is being constructed.
   f = figure('Visible','on','Position',[660,800,850,585]);
 
   %  Construct the components.
   hsurf = uicontrol('Style','pushbutton','String','Surf',...
          'Position',[1135,250,90,35],...
          'Callback',{@surfbutton_Callback});
   hmesh = uicontrol('Style','pushbutton','String','Mesh',...
          'Position',[315,200,90,35],...
          'Callback',{@meshbutton_Callback});
   hTwoDImage = uicontrol('Style','pushbutton',...
          'String','2D Image',...
          'Position',[315,150,90,35],...
          'Callback',{@TwoDImagebutton_Callback}); 
   htext = uicontrol('Style','text','String','Select Greek',...
          'Position',[325,480,80,15],'FontSize',10);
   hpopup = uicontrol('Style','popupmenu',...
          'String',{'delta','gamma','vega','theta','rho'},...
          'Position',[300,450,100,25],...
          'Callback',{@popup_menu_Callback},...
      'FontSize',14);
   ha = axes('Units','Pixels','Position',[50,50,550,485]); 
   align([hsurf,hmesh,hTwoDImage,htext,hpopup],'Center','None');
   
   % Create the data to plot: the Black-Scholes Greeks
 
   S1=[0:3:100];
   T1=[0.04:0.1:5];
   S=repmat(S1,length(T1),1);
   T=repmat(T1',1,length(S1));

   delta_data = normcdf((log( S / 50 ) + .06 * T ) ./ (.2 * sqrt(T)));
   delta_title='$\Delta=\frac{\partial{C}}{\partial{S}}:~~~Delta~for~a~European~Call~~~~~~~~~Strike~K=50$';
   
   gamma_data = (1 / sqrt(2 * pi) * exp(-0.5 * ((log( S / 50 ) + (.04  + (0.5/2) ^ 2) .* T ) ./ (.2 * sqrt(T))).^ 2)) ./ (S .* (.2 * sqrt(T)));
    gamma_title='$\Gamma=\frac{\partial^2{C}}{\partial{S^2}}:~~~Gamma~for~a~European~Call~~~~~~~~~Strike~K=50$';
    
   vega_data =S.* sqrt(T).* (1./ sqrt(2 * pi).* exp(-0.5 * ((log( S./ 50 ) + .06.* T )./(.2.*sqrt(T))).^ 2));
    vega_title='$\nu=\frac{\partial{C}}{\partial{\sigma}}:~~~Vega~for~a~European~Call~~~~~~~~~Strike~K=50$';
    
   theta_data =-((S.* (normcdf(-((log( S / 50 ) + .06 * T )./ (.2 * sqrt(T)))))*.2)./ (2 * sqrt(T))) - (.04*50* exp(-.04 * T).* normcdf((log( S / 50 ) + .06 * T )./ (.2.* sqrt(T))-.2.*sqrt(T)));
    theta_title='$\Theta=\frac{\partial{C}}{\partial{t}}:~~~Theta~for~a~European~Call~~~~~~~~~Strike~K=50$';
    
    rho_data =50*T.*exp(-.04 * T).*normcdf((log( S / 50 ) - .06 * T ) ./ (.2 * sqrt(T)));
    rho_title='$\rho=\frac{\partial{C}}{\partial{r}}:~~~Rho~for~a~European~Call~~~~~~~~~Strike~K=50$';
    
   % Initialize the GUI.
   % Change units to normalized so components resize 
   % automatically.
   set([f,ha,hsurf,hmesh,hTwoDImage,htext,hpopup],...
   'Units','normalized');
   %Create a plot in the axes.
  
   current_data = delta_data;
  
   xlabel('Current Stock Price S_0');
      ylabel('Time to Maturity T (years)');
  current_title='Select~a~Black-Scloles~Greek~then~press~a~plot~button';
  title(current_title,'interpreter','latex','FontSize',16);
   % Assign the GUI a name to appear in the window title.
   set(f,'Name','Visualize Greeks')
   % Move the GUI to the center of the screen.
   movegui(f,'center')
   % Make the GUI visible.
   set(f,'Visible','on');
 
   %  Callbacks for VisualizeGreeks. These callbacks automatically
   %  have access to component handles and initialized data 
   %  because they are nested at a lower level.
 
   %  Pop-up menu callback. Read the pop-up menu Value property
   %  to determine which item is currently displayed and make it
   %  the current data.
      function popup_menu_Callback(source,eventdata) 
         % Determine the selected data set.
         str = get(source, 'String');
         val = get(source,'Value');
         % Set current data to the selected data set.
         switch str{val};
         case 'delta' % User selects delta.
            current_data = delta_data;
            current_title=delta_title;
         case 'gamma' % User selects gamma.
            current_data = gamma_data;
            current_title=gamma_title;
         case 'vega' % User selects vega.
            current_data = vega_data;
            current_title=vega_title;
         case 'theta' % User selects theta.
            current_data = theta_data;
            current_title=theta_title;
         case 'rho' % User selects rho.
            current_data = rho_data;
            current_title=rho_title;
         end
      end
  
   % Push button callbacks. Each callback plots current_data in
   % the specified plot type.
 
   function surfbutton_Callback(source,eventdata) 
   % Display surf plot of the currently selected data.                
   surf(S,T,current_data);
      xlabel('Current Stock Price S_0');
      ylabel('Time to Maturity T (years)');
      title(current_title,'interpreter','latex','FontSize',13);
       
   end
 
   function meshbutton_Callback(source,eventdata) 
   % Display mesh plot of the currently selected data.
    mesh(S,T,current_data);
       xlabel('Current Stock Price S_0');
       ylabel('Time to Maturity T (years)');
       title(current_title,'interpreter','latex','FontSize',13);
   end

   function TwoDImagebutton_Callback(source,eventdata) 
   % Display TwoDImage plot of the currently selected data.
   imagesc(S1,T1,current_data);
       xlabel('Current Stock Price S_0');
       ylabel('Time to Maturity T (years)');
       title(current_title,'interpreter','latex','FontSize',13);
   end 
 
end 

% by Rodolphe Sitter

Contact us at files@mathworks.com