MAPLE in Finance
Igor Hlivka  MUFG Securities International
Introduction
Many aspects of Financial Economics are technical in nature and require mathematical formulation to solve them.Extensive use of mathematical methods in Finance has led to the development of Mathematical Finance  a distinct subgroup of mathematical and statistical rules and theorems that deal with particular problems arising in financial analysis.
In pure mathematical terms, problems of Financial Economics are typically reduced to (i) equations solving, (ii) optimization routines, (iii) simulations, (iv) parameters estimation or (v) process modeling which are all wellsuited for computer algebra packages using symbolic manipulation and highly efficient numerical solvers.
The purpose of this document is to demonstrate how Maple  with its powerful analytical engine and robust numerical routines  can be used to tackle various topics in Financial Economics / Mathematics, and lead to elegant solutions through its symbolic and numerical interfaces.
Interest base conversion, compounding and discounting
Rates compounding
There are many many different rates in the economy  they differ by:
 Maturity: shortterm vs. longterm rates
 Structure: zerocoupon (spot), par, forward rates etc.
 Compounding frequency: annual, semiannual, quarterly, daily, continuously compounded etc.
Here we examine how one can easily obtain conversion formulas for interest rates under various compounding regimes:
Assume we are given a rate with discrete compounding frequency . We may want to express this rate [now denoted ]with different discrete compounding frequency . Further assume that the rate has duration of years.
 We solve this problem using nonarbitrage concept: investing at rates and must yield the same result, otherwise any pricing anomaly will be arbitraged away
> 



(2.1.1) 
> 


(2.1.2) 
 Maple finds a general conversion formula that can be used to obtain an appropriate rate for given input:
Eg: T = 3 Years, r = 5% with annual frequency: =1. What is the equivalent rate with a quarterly frequency?
> 


(2.1.3) 
 The rate with quarterly compounding is lower [ 4.9088%]  as expected  due to reinvestment effect
 When rates are continuously compounded, the task is not very much different...
 Now assume that rate is continuously compounded. What is the equivalent rate with a discrete frequency compounding ?
> 


(2.1.4) 
 Expressing discrete rate in terms of continuously compounded rate and vice versa...
Continuously compounded rates will always be lower than rates with discrete compounding due to the compounding effect'
Discounting
Discounting future cash flows:
The present value of the a single CF received in the future:
A) Discrete case: =
B) Continuously compounded case:
However, what is the present value of a stream of CF for a given period of time (nyears?)  i.e. the PV of Annuity?
This quantity depends on the compounding frequency:
A): Discrete compounding: B): Continuous compounding:
> 


(2.2.1) 
> 


(2.2.2) 
PV of ?1 received over the period of 5Y semiannually:
> 


(2.2.3) 
PV of ?1 received over the period of 5Y continuously:
> 


(2.2.4) 
The annuity factor for continuously compounded case is higher, again due to higher compounding frequency
Fixed Income Mathematics
Bonds
Fixed Coupon
Let's define fixedcoupon bond as with coupon , yieldtomaturity , annual payment frequency and maturity years. The value of the bond can be expressed as a present value of future cash flows:
> 


(3.1.1.1) 
 What is the coupon of the bond that gives me the Par Value (100) ?
> 


(3.1.1.2) 
 How risky is the Bond  i.e. what is the Bond's sensitivity to the underlying yield?
The firstorder risk is called Duration, while the secondorder effect is known as Convexity:
> 

 Maple easily finds a symbolic form of both bond's derivatives and these can be evaluated with arbitrary set of given parameters:
Eg: 10Y bond with 5% coupon payable semiannually [m=2] and yield to maturity 5%:
> 



(3.1.1.4) 
 Duration of a bond is always negative, while Convexity will always be positive.
 Duration of a bond decreases as bond's yield increases⇒bond will "repay" quicker when yield is high.
> 

Floating rate note [FRN]
 FLOATING RATE NOTE  a bond with coupon that fluctuates with market movement for shortterm rates (typically 3M or 6M moneymarket rate such as Libor)

(3.1.2.1) 
 In the FRN formula, is the ith period shortterm interest rate (Libor), m represents a coupon margin and s is a credit spread => factor reflecting credit riskiness of an issuer.
 By how much the FRN price changes if the creditworthiness of the issuer deteriorates? We are interested in FRN's sensitivity with respect to the credit spread s
> 


(3.1.2.2) 
 Maple is capable of handling this complex dependency with ease...
CMT
Constantmaturity Treasury is a bond with variable coupon where the coupon value is linked to a longerdated yield on other (typically) government securities.
The problem in evaluation of CMTs is reduced to the determination of future coupons: (i) forward par rates and (ii) convexity correction.
 Let's define forward par rate as and convexity term as
 Convexity is further defined as secondorder Taylor expansion w.r.t yield and is used to express a nonlinear relationship between bond's price and yield.
> 



(3.1.3.1) 
CMT floater is then defined as FRN with coupons being equal to
> 


(3.1.3.2) 
Maple's powerful symbolic engine returns the desired formula and is capable of finding any desired comparative static of this instrument. We can differentiate the CMT formula to obtain the first and second derivative of the CMT price w.r.t to yield:
> 

 Despite complexity, Maple finds the solution easily
Asset Swap Package on CMT
CMTs are frequently compared or swapped into a standard FRN. Investors may want to swap a longerdated CMT par yield for a shortterm moneymarket yield when they expect the shorter rates to outperform the longerdated yields.
In CMT Asset Swap package, one needs to determine the value of a spread over moneymarket rate (say: 3M Libor) that makes the both legs of transaction equal:
The asset swap on CMT is then expressed as as Asset Swap Spread (AS)
Floating leg definition: L[i] = ith Libor rate, s = spread, N = notional principal, DF[i] = ith Discount factor, δ = year fraction
> 


(3.1.4.1) 
CMT Leg definition: CMT[i] = ith CMT coupon, ξ = CMT spread, DP = "dirty price of a bond", Par = Par bond
> 


(3.1.4.2) 
> 



(3.1.4.3) 
Derivative Products
Standard Financial Options
 What is financial option?
 Contract that gives the right (but not obligation) to enter into a certain financial transaction: a purchase or sale of financial security  stock, bond etc.
 There are two basic types of options: CALL ⇒Right to buy PUT ⇒ Right to sell
 In mathematical notation an option is a discounted expectation of future payoff: ,0)]
Put = ,0)]
 So, to value an option we need to define the stochastic path for the underlying security: Following BlackScholesMerton continuoustime model we assume that the underlying security (say stock price) follows a geometric Brownian motion:
 here r is constant drift coefficient and σ is constant (positive) volatility coefficient
 Since the terminal value of this process is wellknown, using Ito formula we can derive the model's value explicitly:
> 


(4.1.1) 
 y is a standard normal variable:
Define the Standard Normal Density
> 


(4.1.2) 
Determine the critical value for which the stock's terminal value is equal to the strike price K: S[T] = K
> 


(4.1.3) 
Specify the option's payoff:
> 


(4.1.4) 
Price an option:
> 



(4.1.5) 
 This is the famous BlackScholes option formula for a stock price that pays no dividends in "nonabbreviated form" ⇒functional parameters are included in the pricing formula.
 We can substitute actual values to obtain numerical form of the expression and plot it against two fundamental variables: stock price and volatility σ:
 It is worth mentioning that the put value increases when the volatility gets higher even if the option's intrinsic value max(KS[T],0) is zero .
Option's sensitivities
 Valuing option is only one part of the story....
 We also need to find the ways how to hedge an option and understand option's sensitivities w.r.t functional parameters. This is the area where Maple comes particularly handy...
 We define four basic option sensitivities (using Call Option):
 a) Option Delta → first derivative of option price w.r.t stock price
b) Option Gamma → second derivative of option price w.r.t stock price
c) Option Vega → first derivative of option price w.r.t volatility
d) Option Theta → first derivative of option price w.r.t time (time decay)
> 

 We can visualize the sensitivities in any desired format....
> 

NonStandard Options
BlackScholes option pricing formula is a standard option with piecewise linear payoff.
However, we can use Maple's symbolic engine to derive various nonstandard option formulas where the terminal payoff is not any longer piecewise linear.
Power Option
The option payoff is now the square of the stock price:
> 


(4.3.1.1) 
Powered Option
The payoff is the difference between the stock price and the strike price squared:
> 


(4.3.2.1) 
Digital
Option will pay a predetermined amount PA if S[T] ≥ K
> 


(4.3.3.1) 
Range
Option that pays a fixed amount A if the stock price at option maturity is within the range of [LP,HP]:
Performance Option
 The option will pay amount A if the stock price at at time T exceeds its value at time U S[T] > S[U] with U< T


(4.3.5.1) 
 We need to determine the critical value for the option payoff
> 


(4.3.5.2) 
 We can now value the option:
> 

Alternative Process Dynamics
 BS process is a representative example of multiplicative geometric Brownian motion.
 We can define alternative process  the general stochastic process  using Bachelier normal dynamics  in our particular example as driftless:
> 


(4.4.1) 
 We need this process to model securities paths where value is allowed to become negative
 CMT coupons (mentioned earlier) are frequently issued with a ZeroFloors V = [Max(CMT,0)]
 We can use Bachelier process to value this embedded optionality directly:
> 



(4.4.2) 
 Obtaining the sensitivity is easy
> 



(4.4.3) 
> 

> 

PDEs in Finance
Finding probability density function [PDF] for B/S process
 SDE with the initial condition determines a diffusion process.
 In finance SDEs are used in two fundamental ways: (i) defining an expected payoff, and (ii) finding transitional probability at being in a certain state at a given time
 These views are different, but not unrelated ⇒BS PDE solves the backward Kolmogorov PDE, whereas probability density [PDF} solves the forward Kolmogorov equation  being itself an adjoint of the backward PDE
 SDE process for a financial product: where a = drift and b = diffusion coefficient
 The process can be expressed in terms of diffusion PDE
> 


(5.1.1) 
 To solve this PDE analytically, we will try to change it into constantcoefficient PDE by change of variables
> 


(5.1.2) 
 We want to solve this equation with initial condition
> 


(5.1.3) 
 Since the PDE is now in "desirable format", we apply Fourier transform to solve it:
> 



(5.1.4) 
 We have now removed a spatial variable from the equation ⇒ transformed PDE into ODE, which we can now solve with the given initial condition:
> 



(5.1.5) 
 Since Fourier transform in invertible, we retrieve back the desired probability density function:
> 


(5.1.6) 
 Finally, we want to check the correctness of our result:
> 


(5.1.7) 
PDF in higher dimension
 Using suitable change of variables, Maple can efficiently solve PDEs in higher dimensions using Fourier transform
PDF for JumpDiffusion
 Standard B/S process assumes constant volatility and normal distribution of logreturns
 In reality, the empirical distribution is not normal ⇒returns exhibit "fat tails": extreme values probabilities are higher than the one suggested by normal distribution
 Jumpdiffusion is one of the solutions that addresses the nonnormality of the underlying process
 How does this work? Standard diffusion is "enriched" with jumps
 Jump process:  Poisson process with intensity λ ⇒ jump is distributed with Poisson PMF, and over the given time period there are in total N jumps with magnitude J
 PDE for jumpdiffusion:

(5.3.1) 
 here is the PDF of the jump
 Again, specify initial condition:
> 


(5.3.2) 
 Apply Fourier transform to the PDE
> 


(5.3.3) 
 Maple knows how to apply Fourier transform to convolution, and converts spatial variable into parameter. We have obtained ODE that we will solve with the given initial condition:
> 

 We will try to invert it back to retrieve the original function
> 


(5.3.5) 
 Maple successfully finds a "closedform" solution, which can be further simplified by specification of the jump density
Numerical Computation
Monte Carlo method for standard option
Here we demonstrate how Maple's Statistics package can be efficiently used to value Europeantype financial options numerically
 We need to define stock price process and simulation parameters:
S[0] = 20; σ=25%; time = 1Y, time bucket = T/N = 1/100; number of stock price simulations = 1000


(6.1.1) 
Monte Carlo method with stochastic volatility
 We now allow the volatility of a financial product to follow its own stochastic process  correlated with the underlying security:
 We assume that the volatility of the stock σ follows driftless Brownian motion with constant volatility parameter η


(6.2.1) 
Normal option with stochastic volatility  numerical integration
Here we show how Maple's numerical integration can be efficiently applied to solve the option pricing when the volatility itself is stochastic
 We define the underlying price process as Bachelier normal process with drift μ and stochastic volatility σ[T]
 We also assume that the volatility follows a geometric Brownian motion with drift λ and constant diffusion parameter η
 Two Wiener process are correlated with coefficient ρ⇒ the underlying density is bivariate normal
 Determine the critical value for the underlying process:
> 



(6.3.2) 
 Write down the option formula  example using Put Option:
> 


(6.3.3) 
 Evaluate the Put option numerically, using Maple's numerical integration techniques:


(6.3.4) 
 Although we derived the option price numerically, we can still use Maple to calculate for us option sensitivities, for example Delta
> 


(6.3.5) 
> 


(6.3.6) 
Numerical PDE method
Standard BlackScholes approach
Define the drift and diffusion parameters of BS PDE:
> 


(6.4.1.1) 
Specify the numerical values and Initial / Boundary conditions
> 


(6.4.1.2) 
Solve the PDE numerically
> 


(6.4.1.3) 
Visualize the result:
> 



(6.4.1.4) 
Alternative PDE process
 Let's examine the squareroot process for security S:
> 


(6.4.2.1) 
 Define parameters values and IBCs and solve PDE
> 


(6.4.2.2) 
> 


(6.4.2.3) 


(6.4.2.4) 
 Note the difference in option profile at option maturity [time value = 0], and on inception τ = 0:: → full benefits of option time value.
Data Analysis
 Maple has a broad range of data fitting routines to analyze time series
 This particular example focuses on relationship between real and nominal rates in a hypothetical economy
> 



(7.1) 
We use Maple's Statistics package to test various regression models on the given set of data
 Model 1: Logarithmic regression:
 Model 2: Userdefined regression:
 Model 3: Polynomial model of 4th order:
Selection of regression models is arbitrary and many other model combinations are feasible.
 Maple finds leastsquare parameters of given models easily...
 Maple can calculates the required "goodnessoffit" statistics for us....
> 


(7.3) 
> 

 Now, we want to backtest our regression models and see how each model fits the actual market data...
Risk Application
In this section we examine the calculation of ValueAtRisk statistics for a small portfolio of financial instruments.
ValueatRisk [Value@Risk] determines how much an institution may loose over a given time (typically number of days), at predetermined confidence level.
Practical Approach
 Assume a portfolio of 5 financial instruments with volatility vector U, weight vector W and correlation matrix CM
 Determine the size of the portfolio and compute VarianceCovariance Matrix:
> 


(8.1.2) 
 Apply weights to obtain portfolio volatility: W' *VarCov * W
> 


(8.1.3) 
 This is the portfolio's volatility given weights and correlations shown above
 We want to test the Value@Risk in nonstandard fashion  by applying "fattail" distribution to the portfolio risk.
 To estimate this impact, we will compare a tail behavior of Normal and ExtremeValue [Gumbel] distributions
> 


(8.1.4) 
> 

 As shown on the chart above, the Gumbel density exhibits "longer" and "fatter" tail in the extreme region. This may be particularly attractive if we want to obtain Value@Risk under alternative distributional measure in nonNormal environment.
 To obtain the Value@Risk measure, we will need a quantile value of a particular distribution to feed into the Value@Risk formula:
Value@Risk = Market Exposure*Volatility*Quantile*
> 



(8.1.5) 
We see that the quantile of Gumbel density at 99% confidence level is almost double of that of standard normal density:
Example: Market exposure = 100 million, t= 10 Days
The difference in the risk measure is significant:
 Under extremevalue assumption, the VaR figure is higher by 727,000 than the measure suggested by the standard market approach
Theoretical Approach
 How can VaR theory enhance our Value@Risk calculation?
 Apart from standard VaR measure, we may be interested in "Incremental" VaR  i.e. changes in VaR measure due to variability of input
 It is quite legitimate to ask a question: "By how much my VaR is going to change if a security's volatility, weight or correlation changes?"
 Define 3security portfolio with weight vector and covariance matrix
> 

> 


(8.2.2) 
Having determined the theoretical portfolio's volatility, we can obtain comparative static for the VaR behavior:
 By how much will VaR change if volatility of the Asset[1] changes?
 By how much will VaR change if weight of Asset[1] changes?
 How VaR will be affected if correlation between Asset[1] and Asset[2] changes?
> 

Conclusion
 The purpose of this document is to show that Maple is a versatile tool that meets various needs of finance professionals in arbitrary level of complexity. The selection of problems demonstrates just small universe of potential applications of the product in the reallife environment, however actual usage in the finance industry is virtually unlimited. Maple's symbolic manipulation and powerful numerical computation combined with excellent visualization tools make the product an efficient and robust tool for model design, test and validation, however other areas can be accommodated within this framework in equally effective manner.
Legal Notice: The copyright for this application is owned by the author. Neither Maplesoft nor the author are responsible for any errors contained within and are not liable for any damages resulting from the use of this material. This application is intended to demonstrate the use of Maple to solve particular set of problems.It has been made available for product evaluation purposes only and may not be used in any other context without the express permission of Maplesoft and/or author.