# TI-Basic Z80 Programming/Mathematical Finance Programming

TI-BASIC is a simple programming language used on Texas Instruments graphing calculators. This module shows you how to program some standard financial calculations:

## TI-BASIC programsEdit

### Ito's lemmaEdit

Let's begin defining a stochastic process through its Ito's definition:

:defsto(t,x)
:Func
:{t,x}
:EndFunc


So for our TI-calculator, a diffusion process formally defined by:

${\displaystyle dS=f(S,t)dt+g(S,t)dW}$

is defined by a set of two terms:

{ f(S,t), g(S,t) }


For an exponential brownian motion, we define:

defsto(m*s, sigma*s) → ds(s)


Now we want to use Ito's lemma on functions of ${\displaystyle S}$  and ${\displaystyle t}$ :

:dsto(f,x,t,ds)
:Func
:{d(f,t)+ds[1]*d(f,x)+ds[2]^2*d(d(f,x),x)/2 , ds[2]*d(f,x)}
:EndFunc


This can now be used to apply Ito's lemma to ${\displaystyle \ln(S)}$ :

dsto(ln(S),S,t,ds(S))
>> { m - sigma^2/2 , sigma }


this tell us that:

${\displaystyle d\ln(S)=\left(m-{1 \over 2}\sigma ^{2}\right)dt+\sigma dW,\;{\rm {when}}\;dS=mSdt+\sigma SdW}$

### Black-Scholes EquationEdit

Now we can try to prove the Black-Scholes equation.

Define a portfolio with an option and ${\displaystyle \Delta }$  shares of ${\displaystyle S}$ :

V(S,t) - Delta * S → Pi


and apply Ito's lemma to obtain ${\displaystyle d\Pi }$ :

dsto(Pi, S, t, ds(S)) → dPi


we now want to nullify the stochastic part of ${\displaystyle d\Pi }$  by chosing an appropriate value for ${\displaystyle \Delta }$ :

solve( dPi[2]=0, Delta)
>> Delta = d(V(S,t), S) or sigma S = 0


we now know that the correct value for ${\displaystyle \Delta }$  is:

${\displaystyle \Delta ={\partial V(S,t) \over \partial S}}$

On another side, we have:

${\displaystyle d\Pi =r\Pi dt=r(V(S,t)-\Delta \cdot S)dt}$

which leads us to the equation:

${\displaystyle (\partial _{t}V+{1 \over 2}\sigma ^{2}S^{2}\partial _{S}^{2}V)dt=r(V(S,t)-\Delta S)dt}$

At first we need to replace ${\displaystyle \Delta }$  by its value into ${\displaystyle d\Pi }$ , and then equalize with

solve( dPi[2]=0, Delta) | sigma > 0 and S > 9 → sol
>> Delta = d(V(S,t), S)
dPi | sol → dPi
>> {sigma^2 d^2(V(S,t), S^2) S^2 /2 + d(V(S,t), t) , 0 }
dPi = defsto( r(V(S,t) - Delta S) ) | sol → BS
>> { BS_equation , true }


and now we've got the Black-Schole Differential Equation into the variable BS_equation[1]!