Financial calculus in Mathematica

Copyright 2005
Stefan Dirnstorfer, all rights reserved

LICENSE:
This document may be used free of charge under the terms of the GNU PUBLIC LICENSE, Version 2
See  http://www.gnu.org/copyleft/gpl.html

Operators

Da[V_] := 
      V /. {c -> 0,  r -> 0.05, S -> 100,  
          Ind -> Ind0,  Event -> Event0}

T[index_, power_, V_] := 
      V  /.  {index -> index + power}

T[x, a[y], f[x, y]]

Ind0[indicator_] := If[indicator, 1, 0]

Ind[1 > 5]
Da[%]

Event0[choice_, V1_, V2_] := 
      Expand[choice*V1 + (1 - choice)*V2]

Financial Products and Instruments

Bond deal

bond[V_] := T[c, -90, Theta[T[c, 100, V]]]

Coupon bond

cbond[V_] := 
Nest[Function[w, Theta[T[c, 11, w]]], 
        V, 5]

European Option

europt[V_] := 
Nest[
        Function[w, Theta[w]], 
Event[Ind[A[V] > B[V]], 
          A[V], B[V]], 5]

A[V_] := V
B[V_] := T[c, S - K, V]
K := 130

American Option

amopt[V_] := 
Nest[
        Function[w,  Theta[Event[Ind[w > X[V]], w, X[V]]]], V, 5]

X[V_] := T[c, S - K, V]

Binomial tree model

Process operator

Theta1[V_] := 
T[c, 
        r c, 
Event[p, T[S, S*u - S, V], 
          T[S, S/u - S, V]]]

Risk neutral probability

Da[Theta1[S]]

Solve[
{Da[
            Theta1[S]/(1 + r) == S], 
Da[
            Theta1[S^2] - Theta1[S]^2 == (40. )^2]}, {p, 
          u}];
p2 := Evaluate[%[[1]]]
p2

Theta2[V_] := Theta1[V] /. p2

Da[Theta2[S]]
Da[T[c, 1, Theta2[c]]]

Da[Nest[Theta2,  S,  5]]

Pricing of Stock Options

Theta[V_] := Theta2[V]/(1 + r)

price := europt[c];
Da[price]

pl := Evaluate[Da[T[S, dS, price]]]

Plot[{pl},  {dS, -100, 200},  PlotRange -> All]

price := amopt[c];
Da[price]

Fixed income

Prices

Da[bond[c]]

Da[cbond[c]]

Sensitivities

Da[D[bond[c], r]]

Da[D[cbond[c], r]]