Putting Mathematica to Work:
A Simple Example of Delta Hedging

Creating Mathematica Functions: The Black-Scholes Model

To demonstrate Mathematica's practical application in financial modeling, we turn to Benninga and Wiener's exploration of a simple delta hedging problem. As a preliminary step, the well-known Black-Scholes theoretical price function for a vanilla European call is defined. The code for this formula, and many others, is available in MathSource or for purchase in application packages such as Derivatives Expert.

normalcdf[x_] := (1/2) Erf[(x/(Sqrt[2]))] + (1/2);

d1[s_, x_, σ_, T_, 
          r_] := (((((σ^2)/2) + r) T + Log[(s/x)])/(σ (Sqrt[T])));
d2[s_, x_, σ_, T_, r_] := 
        d1[s, x, σ, T, r] - σ (Sqrt[T]);

bsCall[s_, x_, σ_, T_, r_] := 
        s normalcdf[
              d1[s, x, σ, T, 
                r]] - ((x*normalcdf[d2[s, x, σ, T, r]])/(E^(r T)));

Now, to take an example from Hull (2000), we can find the price for writing a European call option on 100,000 shares, given the following parameters:

		Current stock price=s = 49
		Strike price=x = $50
		Stock volatility=σ = 20  %
		Option time to maturity=T = 20 weeks
		Market rate of interest=r = 5  %

bsCall[49, 50, 0.2, 20/52, 0.05]*100000

Symbolic Calculus with Mathematica: Deriving the Delta Function

To create a delta hedge, δ(C/(δs )) shares of the underlying stock are purchased, where C is the call price. As a first pass, a static, one-time hedge is considered. Here, Mathematica's symbolic calculus capabilities are used to derive the complicated definition of the delta function; then numeric parameters are fed into this new equation to find the specific hedging ratio.

δ[s_, x_, σ_, T_, r_] := 
      D[bsCall[s1, x, σ, T, r], s1] /. s1 -> s // 
        FullSimplify

δ[s, x, σ, T, r]

δ[49, 50, 0.2, 20/52, 0.05]*100000

52160*49.  // AccountingForm

So, to properly hedge this call, 52,160 shares of the underlying stock are bought at a cost of $49 each, for a total of $2,555,840. To finance this purchase, the capital is borrowed at the market rate of interest (which has been defined as 5%).

Mathematica as a Calculator: Analyzing the Results with Numbers

Now, suppose, after one week the underlying stock price rises to $49.50. The value of the option position has grown from $240,053 to $258,422. If the writer of this call had not hedged the position with a purchase of the underlying stock, the small price movement would have created a loss of over $18,000.

bsCall[49.5, 50, 0.2, 19/52, 0.05]*100000.

240053 - 258422

However, with the hedge, the net outcome for the writer of the call is the following:

   net gain = Original price received for the call - price of the call one week later +
		price received for the underying stock one week later - repayment of principal and interest used to buy the underlying stock
		
In this case, it turns out to be a profit of $5,229.95.

bsCall[49, 50, 0.2, 20/52, 0.05]*100000.  - 
bsCall[
          49.5, 50, 0.2, 19/52, 
          0.05]*100000.  + 
52160*49.5 - 
2555863. *(1 + 0.05/52)

Mathematica as a Visualization Tool: Analyzing Relationships with Graphics

A unique feature of Mathematica as a programming language is its integrated graphics capabilities. Viewing these relationships graphically, we can definitevely see how marginal changes in the underlying stock price have a mitigated effect on returns, when the position is hedged.

<< Graphics`FilledPlot`
plot1 = 
      Plot[bsCall[49, 50, 0.2, 20/52, 
              0.05]*100000.  - 
bsCall[(49 + change), 
              50, 0.2, 19/52, 
              0.05]*100000.  + 
52160*(49 + 
                change) - 
2555863. *(1 + 
                0.05/52), {change, -2, 
          2},  
PlotLabel -> "Returns Created by \
Marginal Changes in Stock Price", 
        AxesLabel ->  {"Δs ($)", ""}, 
        PlotRange -> {-20000, 
            20000}, 
DefaultFont -> {"Arial", 
            14}]

To further this point, we can compare this return (in red) to writing an unhedged, or "naked," call (in black). From this graph, it is clear to see that the hedged call dramatically alters the risk of this individual's position in the market.

plot = 
        FilledPlot[{bsCall[49, 50, 0.2, 20/52, 
                  0.05]*100000.  - 
bsCall[(49 + 
                      change), 50, 0.2, 19/52, 
                  0.05]*100000.  + 
52160*(49 + 
                    change) - 
2555863. *(1 + 
                    0.05/52), 
bsCall[49, 50, 0.2, 20/52, 
                  0.05]*100000.  - 
bsCall[(49 + 
                      change), 50, 0.2, 19/52, 
                  0.05]*100000. }, 
{change, -2, 2},  
          AxesLabel ->  {"Δs ($)", "Return ($)"}, 
          PlotRange -> {-20000, 20000}, 
          DefaultFont -> {"Arial", 
              14}, 
PlotStyle -> {{RGBColor[0.8,  
                  0.1,  0.1]}, {RGBColor[0, 0, 0]}}, 
 
          Fills -> GrayLevel[ .7]];
label1 = 
        Graphics[Text["foregone profit", {-1.25, 12500}], 
          RGBColor[0.8,  0.1,  0.1]];
label2 = 
        Graphics[Text["losses avoided", {1.05, -10000}], 
          DefaultFont -> {"Arial", 14}, 
          RGBColor[0.8,  0.1,  0.1]];
Show[plot, label1,  label2]

From this illustration, it is clear that Mathematica is a useful modeling tool because, in one document, analysts can quickly and easily alternate between symbolic, numeric, and graphic analysis of the same problem. This flexibility can provide a deeper understanding of financial models than any other single system can.

The Next Step: Mathematica as an extensible, programmable application

The purpose of this exercise has been to walk through a simple modeling exercise to better demonstrate the basic design of Mathematica and the "look and feel" of the notebook interface. To extend this discussion, the next step is to look at changing the delta position each week, allowing dynamic management of the risk exposure over the 20-week life span of the call.  This is covered in the full text of Benninga and Wiener's article.

Note: All of the code used in this example was modified from the article "Dynamic Hedging Strategies" by Simon Benninga and Zvi Wiener. The article is a more thorough introduction to this subject and may be downloaded in PDF file format from http://finance.wharton.upenn.edu/~benninga/wiener.html.