CoCalc Public FilesFinancialMath / time_series.sagews
Author: Carl Eberhart
Views : 118
Compute Environment: Ubuntu 18.04 (Deprecated)

### Black Scholes formulas for European Call and Put

$C(S,t)=S N(d_1)-E e^{-r(T-t)}N(d_2),\ \ \ P(S,t)=E e^{-r(T-t)}N(-d_2)-S N(-d_1)$

where $N(x)=\dfrac{1}{\sqrt(2\pi)}\int_{-\infty}^x e^{\frac{-y^2}{2}}dy = \dfrac{1}{2} \text{ erf}(x) +\dfrac{1}{2}$, $S$ is Asset price, $E$ is Expiry Price, $T$ is expiry time, $r$ is bank interest rate, $t$ is time, $d_1=\ln(\dfrac{S}{E})+(r+\dfrac{1}{2}\sigma^2)(T-t))\ \dfrac{1}{\sigma \sqrt{T-t}}$ and $d_2=\ln(\dfrac{S}{E})+(r-\dfrac{1}{2}\sigma^2)(T-t))\ \dfrac{1}{\sigma \sqrt{T-t}}$


︠1f46626b-510f-4a6c-b0bf-563bed296c78︠
S, t, T, E, r, sigma ,y, dt=var('S t T E r sigma y dt')

@interact(layout={'top': [['n', 'm','S0','t0','SM'],['r','sigma','E','T','TM']]})
def showit(n=20,m=20,S0=.8,t0=.5,r=.1,sigma=.2,E=1,T=1,SM=1.5,TM=1):
def N(x):
#return 1/sqrt(2*pi)*integral(exp(-y^2/2),(y,-infinity,x))
return erf(x)/2+1/2
r=r
sigma=sigma
E=E
T=T
def C3(S,t):
d1=(ln(S/E)+(r+1/2*sigma^2)*(T-t))/(sigma*sqrt(T-t))
d2=(ln(S/E)+(r-1/2*sigma^2)*(T-t))/(sigma*sqrt(T-t))
return S*N(d1)-E*exp(-r*(T-t))*N(d2)
Ttab=[(S0.n(digits=4),(TM*(1-i/n)).n(digits=10),C3(S0,TM*(1-i/n)).n(digits=10)) for i in range(n)]
Stab=[((SM*i/m).n(digits=10),t0.n(digits=4),C3(SM*i/m,t0).n(digits=10)) for i in range(m+1)]
G=Graphics()
G+=plot3d(C3,(S,0,SM),(t,0,TM),mesh=false,dots=true,opacity=.5,figsize=10,axes=false)
G+=line(Ttab,color='red',thickness=1)
G+=line(Stab,color='green',thickness=1)

G.show()
pretty_print(html( '<h3>&nbsp;&nbsp;&nbsp;Graph of the Black Scholes European Call:  x=S Asset, y=t time, z=C Call</h4>'))
print
print table([('','Red Line','')]+[('Asset Price','Time','Call Price')]+Ttab)
print
print table([('','Green Line','')]+[('Asset Price','Time','Call Price')]+Stab)

Interact: please open in CoCalc
var('S t T E r sigma y dt')
r=.1;sigma=.2;E=1;T=1

def N(x):
#return 1/sqrt(2*pi)*integral(exp(-y^2/2),(y,-infinity,x))
return erf(x)/2+1/2
def C(S):
d1=(ln(S/E)+(r+1/2*sigma^2)*(dt))/(sigma*sqrt(dt))
d2=(ln(S/E)+(r-1/2*sigma^2)*(dt))/(sigma*sqrt(dt))
return S*N(d1)-E*exp(-r*(dt))*N(d2)
def C3(S,t):
d1=(ln(S/E)+(r+1/2*sigma^2)*(T-t))/(sigma*sqrt(T-t))
d2=(ln(S/E)+(r-1/2*sigma^2)*(T-t))/(sigma*sqrt(T-t))
return S*N(d1)-E*exp(-r*(T-t))*N(d2)

(S, t, T, E, r, sigma, y, dt)
pretty_print(('This is $2+2$.'))

This is $2+2$.

help(pretty_print)

Help on function show in module smc_sagews.sage_salvus: show(*objs, **kwds) Show a 2d or 3d graphics object (or objects), animation, or matplotlib figure, or show an expression typeset nicely using LaTeX. - display: (default: True); if True, use display math for expression (big and centered). - svg: (default: True); if True, show 2d plots using svg (otherwise use png) - d3: (default: True); if True, show graphs (vertices and edges) using an interactive D3 viewer for the many options for this viewer, type 'import graphics; graphics.graph_to_d3_jsonable?' If false, graphs are converted to plots and displayed as usual. - renderer: (default: 'webgl'); for 3d graphics - 'webgl' (fastest) using hardware accelerated 3d; - 'canvas' (slower) using a 2d canvas, but may work better with transparency; - 'tachyon' -- a ray traced static image. - spin: (default: False); spins 3d plot, with number determining speed (requires mouse over plot) - events: if given, {'click':foo, 'mousemove':bar}; each time the user clicks, the function foo is called with a 2-tuple (x,y) where they clicked. Similarly for mousemove. This works for Sage 2d graphics and matplotlib figures. ANIMATIONS: - animations are by default encoded and displayed using an efficiently web-friendly format (currently webm, which is **not supported** by Safari or IE). - delay - integer (default: 20); delay in hundredths of a second between frames. - gif=False -- if you set gif=True, instead use an animated gif, which is much less efficient, but works on all browsers. You can also use options directly to the animate command, e.g., the figsize option below: a = animate([plot(sin(x + a), (x, 0, 2*pi)) for a in [0, pi/4, .., 2*pi]], figsize=6) show(a, delay=30) EXAMPLES: Some examples: show(2/3) show([1, 4/5, pi^2 + e], 1+pi) show(x^2, display=False) show(e, plot(sin)) Here's an example that illustrates creating a clickable image with events:: @interact def f0(fun=x*sin(x^2), mousemove='', click='(0,0)'): click = sage_eval(click) g = plot(fun, (x,0,5), zorder=0) + point(click, color='red', pointsize=100, zorder=10) ymax = g.ymax(); ymin = g.ymin() m = fun.derivative(x)(x=click[0]) b = fun(x=click[0]) - m*click[0] g += plot(m*x + b, (click[0]-1,click[0]+1), color='red', zorder=10) def h(p): f0.mousemove = p def c(p): f0(click=p) show(g, events={'click':c, 'mousemove':h}, svg=True, gridlines='major', ymin=ymin, ymax=ymax)

︠21c52425-8e79-4c8f-a2ca-758b5e52d37c︠

︠054ec4d6-a146-4893-bd1d-9be99c53b071︠

︠b9ec44b9-fbf4-4504-9019-b820e3f51919︠

︠88050a6c-860d-48e7-8d4e-73c10c4f634ai︠
S, t, T, E, r, sigma ,y, t=var('S t T E r sigma y t')
#r=.1;sigma=.2;E=1;T=1

#def C(S):
#    d1=(ln(S/E)+(r+1/2*sigma^2)*(dt))/(sigma*sqrt(dt))
#    d2=(ln(S/E)+(r-1/2*sigma^2)*(dt))/(sigma*sqrt(dt))
#    return S*N(d1)-E*exp(-r*(dt))*N(d2)

@interact(layout={'top': [['n', 'm','SM','TM'],['r','sigma','E','T'],['S0','S1','t0','t1']]})
def showit(n=20,m=20,t0=.5,S0=.8,r=.1,sigma=.2,E=1,T=1,SM=1.5,TM=1,S1=0,t1=0):
def N(x):
#return 1/sqrt(2*pi)*integral(exp(-y^2/2),(y,-infinity,x))
return erf(x)/2+1/2
r=r
sigma=sigma
E=E
T=T
def C3(S,t):
d1=(ln(S/E)+(r+1/2*sigma^2)*(T-t))/(sigma*sqrt(T-t))
d2=(ln(S/E)+(r-1/2*sigma^2)*(T-t))/(sigma*sqrt(T-t))
return S*N(d1)-E*exp(-r*(T-t))*N(d2)
Ttab=[(S0.n(digits=4),(TM*(1-i/n)).n(digits=10),C3(S0,TM*(1-i/n)).n(digits=10)) for i in range(n)]
if S1>0:
Ttab1=[(S1.n(digits=4),(TM*(1-i/n)).n(digits=10),C3(S1,TM*(1-i/n)).n(digits=10)) for i in range(n)]
Stab=[((SM*i/m).n(digits=10),t0.n(digits=4),C3(SM*i/m,t0).n(digits=10)) for i in range(m+1)]
if t1>0:
Stab1=[((SM*i/m).n(digits=10),t1.n(digits=4),C3(SM*i/m,t1).n(digits=10)) for i in range(m+1)]
G=Graphics()
G+=plot3d(C3,(S,0,SM),(t,0,TM),mesh=false,dots=true,opacity=.5,figsize=10,axes=false)
G+=line(Ttab,color='red',thickness=1)
if S1>0:
G+=line(Ttab1,color='red',thickness=1)
G+=line(Stab,color='green',thickness=1)
if t1>0:
G+=line(Stab1,color='green',thickness=1)

G.show()
pretty_print(html( '<h3>&nbsp;&nbsp;&nbsp;Graph of the Black Scholes European Call:  x=S Asset, y=t time, z=C Call</h4>'))
print
print table([('','Red Line','')]+[('Asset Price','Time','Call Price')]+Ttab)
print
print table([('','Green Line','')]+[('Asset Price','Time','Call Price')]+Stab)
if S1>0:
print
print table([('','Red Line1','')]+[('Asset Price','Time','Call Price')]+Ttab1)
if t1>0:
print
print table([('','Green Line1','')]+[('Asset Price','Time','Call Price')]+Stab1)


Interact: please open in CoCalc


@interact
def f0(fun=x*sin(x^2), mousemove='', click='(0,0)'):
click = sage_eval(click)
g = plot(fun, (x,0,5), zorder=0) + point(click, color='red', pointsize=100, zorder=10)
ymax = g.ymax(); ymin = g.ymin()
m = fun.derivative(x)(x=click[0])
b =  fun(x=click[0]) - m*click[0]
g += plot(m*x + b, (click[0]-1,click[0]+1), color='red', zorder=10)
def h(p):
f0.mousemove = p
def c(p):
f0(click=p)
show(g, events={'click':c, 'mousemove':h}, svg=True, gridlines='major', ymin=ymin, ymax=ymax)


Interact: please open in CoCalc
︠19096894-e97b-47ea-945f-c9db45734a87︠