Skip to content

Commit

Permalink
Small edits
Browse files Browse the repository at this point in the history
  • Loading branch information
domokane committed Jul 31, 2024
1 parent 2291bd1 commit 61ea95e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
11 changes: 7 additions & 4 deletions financepy/models/black.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,21 @@ def delta(self,

if option_type in (OptionTypes.EUROPEAN_CALL,
OptionTypes.EUROPEAN_PUT):

if self.implementation_type == BlackTypes.ANALYTICAL:
return black_delta(f, t, k, r, v, option_type)
else:
raise FinError("Implementation not available for this product")

raise FinError("Implementation not available for this product")

elif option_type in (OptionTypes.AMERICAN_CALL,
OptionTypes.AMERICAN_PUT):
if self.implementation_type == BlackTypes.CRR_TREE:
results = crr_tree_val_avg(
f, 0.0, 0.0, v, self.num_steps, t, option_type.value, k)
return results['delta']
else:
raise FinError("Implementation not available for this product")

raise FinError("Implementation not available for this product")

else:
raise FinError(
"Option type must be a European/American Call or Put")
Expand Down
2 changes: 1 addition & 1 deletion financepy/models/equity_barrier_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def value_barrier(t, k, h, s, r, q, v, option_type, nobs):
df = np.exp(-r * t)
dq = np.exp(-q * t)

c = s * dq * N(d1) - k * df * N(d2)
c = s * dq * N(+d1) - k * df * N(+d2)
p = k * df * N(-d2) - s * dq * N(-d1)

if s >= h:
Expand Down
43 changes: 22 additions & 21 deletions financepy/models/lmm_mc.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ def lmm_swaption_vol_approx(a, b, fwd0, taus, zetas, rho):
term = wti * wtj * fi * fj * rho[i][j] * intsigmaij / (sab**2)
swaption_var += term

taua = 0.0
tau_a = 0.0
for i in range(0, a):
taua += taus[i]
tau_a += taus[i]

taub = 0.0
tau_b = 0.0
for i in range(0, b):
taub += taus[i]
tau_b += taus[i]

swaption_vol = np.sqrt(swaption_var/taua)
swaption_vol = np.sqrt(swaption_var/tau_a)
return swaption_vol


Expand Down Expand Up @@ -166,6 +166,7 @@ def lmm_sim_swaption_vol(a, b, fwd0, fwds, taus):
df = 1.0

for k in range(a, b):

f = fwds[i_path, a, k]
tau = taus[k]
df = df / (1.0 + tau * f)
Expand Down Expand Up @@ -241,14 +242,14 @@ def lmm_fwd_fwd_correlation(num_fwds, num_paths, i_time, fwds):

@njit(float64[:](float64[:], float64[:], int64, float64, float64[:]),
cache=True, fastmath=True)
def lmm_price_caps_black(fwd0, vol_caplet, p, K, taus):
def lmm_price_caps_black(fwd0, vol_caplet, p, k, taus):
""" Price a strip of capfloorlets using Black's model using the time grid
of the LMM model. The prices can be compared with the LMM model prices. """

caplet = np.zeros(p+1)
disc_fwd = np.zeros(p+1)

if K <= 0.0:
if k <= 0.0:
raise FinError("Negative strike not allowed.")

# Set up initial term structure
Expand All @@ -261,13 +262,13 @@ def lmm_price_caps_black(fwd0, vol_caplet, p, K, taus):

for i in range(1, p): # 1 to p-1

K = fwd0[i]
k = fwd0[i]
t_exp += taus[i]
vol = vol_caplet[i]
F = fwd0[i]
d1 = (np.log(F/K) + vol * vol * t_exp / 2.0) / vol / np.sqrt(t_exp)
f = fwd0[i]
d1 = (np.log(f/k) + vol * vol * t_exp / 2.0) / vol / np.sqrt(t_exp)
d2 = d1 - vol * np.sqrt(t_exp)
caplet[i] = (F * N(d1) - K * N(d2)) * taus[i] * disc_fwd[i]
caplet[i] = (f * N(d1) - k * N(d2)) * taus[i] * disc_fwd[i]

return caplet

Expand Down Expand Up @@ -318,7 +319,7 @@ def lmm_simulate_fwds_nf(num_fwds, num_paths, fwd0, zetas, correl, taus, seed):
half_num_paths = int(num_paths/2)

fwd = np.empty((num_paths, num_fwds, num_fwds))
fwdB = np.zeros(num_fwds)
fwd_b = np.zeros(num_fwds)

disc_fwd = np.zeros(num_fwds)

Expand Down Expand Up @@ -369,13 +370,13 @@ def lmm_simulate_fwds_nf(num_fwds, num_paths, fwd0, zetas, correl, taus, seed):

zi = zetas[i]

muA = 0.0
mu_a = 0.0
for k in range(j, i+1):
rho = corr[j][k-j, i-j]
fk = fwd[i_path, j-1, k]
zk = zetas[k]
tk = taus[k]
muA += zi * fk * tk * zk * rho / (1.0 + fk * tk)
mu_a += zi * fk * tk * zk * rho / (1.0 + fk * tk)

w = 0.0
for k in range(0, num_fwds-j):
Expand All @@ -385,19 +386,19 @@ def lmm_simulate_fwds_nf(num_fwds, num_paths, fwd0, zetas, correl, taus, seed):
avgg += w
stdg += w*w

fwdB[i] = fwd[i_path, j-1, i] \
* np.exp(muA * dt - 0.5 * (zi**2) * dt + zi * w * sqrt_dt)
fwd_b[i] = fwd[i_path, j-1, i] \
* np.exp(mu_a * dt - 0.5 * (zi**2) * dt + zi * w * sqrt_dt)

muB = 0.0
mu_b = 0.0
for k in range(j, i+1):
rho = corr[j][k-j, i-j]
fk = fwdB[k]
fk = fwd_b[k]
zk = zetas[k]
tk = taus[k]
muB += zi * fk * tk * zk * rho / (1.0 + fk * tk)
mu_b += zi * fk * tk * zk * rho / (1.0 + fk * tk)

muAvg = 0.5*(muA + muB)
x = np.exp(muAvg * dt - 0.5 * (zi**2) * dt + zi * w * sqrt_dt)
mu_avg = 0.5*(mu_a + mu_b)
x = np.exp(mu_avg * dt - 0.5 * (zi**2) * dt + zi * w * sqrt_dt)
fwd[i_path, j, i] = fwd[i_path, j-1, i] * x

return fwd
Expand Down
9 changes: 3 additions & 6 deletions financepy/models/volatility_fns.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
##############################################################################

from enum import Enum

import numpy as np
from numba import njit, float64

Expand All @@ -12,9 +14,6 @@
# Parametric functions for option volatility to use in a Black-Scholes model
###############################################################################

from enum import Enum


class VolFuncTypes(Enum):
CLARK = 0
SABR = 1
Expand Down Expand Up @@ -242,9 +241,7 @@ def vol_function_ssvi(params, f, k, t):
x = np.log(f/k)

vart = ssvi_local_varg(x, gamma, sigma, rho, t)

if vart < 0.0:
vart = 0.0
vart = max(vart, 0.0);

sigma = np.sqrt(vart)

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ numpy==1.26.4
scipy==1.13.1
llvmlite==0.43.0
ipython==8.25.0
matplotlib==3.8.4
pandas==2.1.2
prettytable==3.9.0

0 comments on commit 61ea95e

Please sign in to comment.