Skip to content

Commit

Permalink
Testing improved
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehyukchoi committed Oct 26, 2022
1 parent 5959117 commit 7f0895b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
22 changes: 22 additions & 0 deletions tests/test_heston.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ def test_price_mc(self):
np.testing.assert_allclose(vol0, vol1, atol=5e-3)
np.testing.assert_allclose(m.result['spot error'], 0, atol=2e-3)

def test_avgvar_mv(self):
"""
mean and variance of var_t and average variance
"""
#sigma, vov, mr, rho, texp, spot = 0.04, 1, 0.5, -0.9, 10, 100
m, *_ = pf.HestonMcChoiKwok2023PoisGe.init_benchmark(1)
m.set_num_params(n_path=32e4, rn_seed=123456, kk=8)

for texp in (1.0, 3.0, 5.0):
# analytic mean and variance
var_m, var_v = m.var_mv(texp)
# analytic mean and average variance
avgvar_m, avgvar_v = m.avgvar_mv(texp)
# MC samples of variance and avgvar
var_t, avgvar, *_ = m.cond_states_step(texp, m.sigma)

np.testing.assert_allclose(np.mean(var_t), var_m, rtol=0.02)
np.testing.assert_allclose(np.var(var_t), var_v, rtol=0.02)

np.testing.assert_allclose(np.mean(avgvar), avgvar_m, rtol=0.02)
np.testing.assert_allclose(np.var(avgvar), avgvar_v, rtol=0.02)


if __name__ == "__main__":
print(f"Pyfeng loaded from {pf.__path__}")
Expand Down
8 changes: 4 additions & 4 deletions tests/test_ousv.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ def test_MomentsIntVariance(self):
sig_t = m.vol_step(texp, sigma0, zz)
cvol, cvar = m.cond_avgvolvar_m(texp, sigma0, sig_t)

assert np.isclose(mvol, np.sum(cvol * ww))
assert np.isclose(mvar, np.sum(cvar * ww))
np.testing.assert_allclose(mvol, np.sum(cvol * ww))
np.testing.assert_allclose(mvar, np.sum(cvar * ww))

mvol, _ = m.avgvol_mv(texp, sigma0 - m.theta, nz_theta=True)
mvar, _ = m.avgvar_mv(texp, sigma0 - m.theta, nz_theta=True)

sig_t = m.vol_step(texp, sigma0 - m.theta, zz, nz_theta=True)
cvol, cvar = m.cond_avgvolvar_m(texp, sigma0 - m.theta, sig_t, nz_theta=True)

assert np.isclose(mvol, np.sum(cvol * ww))
assert np.isclose(mvar, np.sum(cvar * ww))
np.testing.assert_allclose(mvol, np.sum(cvol * ww))
np.testing.assert_allclose(mvar, np.sum(cvar * ww))


if __name__ == "__main__":
Expand Down
24 changes: 12 additions & 12 deletions tests/test_sabr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_Hagan2002(self):
# print(f'Sheet {k:02d}: {ref}')
v1 = np.round(m.vol_for_price(**rv["args_pricing"]), 4)
v2 = df["IV Hagan"].values
np.testing.assert_almost_equal(v1, v2)
np.testing.assert_allclose(v1, v2)

def test_SabrNorm(self):
"""
Expand All @@ -31,7 +31,7 @@ def test_SabrNorm(self):
v1 = m.price(**rv["args_pricing"])
m, df, rv = pf.SabrChoiWu2021H.init_benchmark(k)
v2 = m.price(**rv["args_pricing"])
np.testing.assert_almost_equal(v1, v2)
np.testing.assert_allclose(v1, v2)

def test_SabrNormATM(self):
"""
Expand All @@ -40,13 +40,13 @@ def test_SabrNormATM(self):
for k in [22, 23]:
m, df, rv = pf.SabrNormVolApprox.init_benchmark(k)
m.is_atmvol = True
np.testing.assert_almost_equal(m.vol_smile(0, 0, texp=0.1), m.sigma)
np.testing.assert_almost_equal(m.vol_smile(0, 0, texp=10), m.sigma)
np.testing.assert_allclose(m.vol_smile(0, 0, texp=0.1), m.sigma)
np.testing.assert_allclose(m.vol_smile(0, 0, texp=10), m.sigma)

m, df, rv = pf.Nsvh1.init_benchmark(k)
m.is_atmvol = True
np.testing.assert_almost_equal(m.vol_smile(0, 0, texp=0.1), m.sigma)
np.testing.assert_almost_equal(m.vol_smile(0, 0, texp=10), m.sigma)
np.testing.assert_allclose(m.vol_smile(0, 0, texp=0.1), m.sigma)
np.testing.assert_allclose(m.vol_smile(0, 0, texp=10), m.sigma)

def test_PaulotBsm(self):
"""
Expand All @@ -58,7 +58,7 @@ def test_PaulotBsm(self):
# print(f'Sheet {k:02d}: {ref}')
v1 = np.round(m.vol_for_price(**rv["args_pricing"]), 4)
v2 = df["IV HL-P"].values
np.testing.assert_almost_equal(v1, v2)
np.testing.assert_allclose(v1, v2)

def test_UnCorrChoiWu2021(self):
"""
Expand All @@ -72,8 +72,8 @@ def test_UnCorrChoiWu2021(self):
mass2 = 0.7623543217183134
p2 = np.array([0.04533777, 0.04095806, 0.03889591, 0.03692339, 0.03324944, 0.02992918])

np.testing.assert_almost_equal(mass, mass2)
np.testing.assert_almost_equal(p, p2)
np.testing.assert_allclose(mass, mass2, atol=1e-8)
np.testing.assert_allclose(p, p2, atol=1e-8)

def test_McTimeDisc(self):
"""
Expand All @@ -83,7 +83,7 @@ def test_McTimeDisc(self):
m, df, rv = pf.SabrMcTimeDisc.init_benchmark(k)
m.set_num_params(n_path=5e4, dt=0.05, rn_seed=1234)
p = m.price(**rv["args_pricing"])
np.testing.assert_almost_equal(p, rv["val"], decimal=4)
np.testing.assert_allclose(p, rv["val"], rtol=5e-4)

def test_MomentsIntVariance(self):
"""
Expand All @@ -98,8 +98,8 @@ def test_MomentsIntVariance(self):
m1, v = m.avgvar_mv(vovn)
cond_m1, cond_m2 = m.cond_avgvar_mv(vovn, zhat, False)

np.testing.assert_almost_equal(np.sum(cond_m1 * ww)/m1, 1.0)
np.testing.assert_almost_equal(np.sum(cond_m2 * ww)/(m1**2 + v), 1.0)
np.testing.assert_allclose(np.sum(cond_m1 * ww), m1)
np.testing.assert_allclose(np.sum(cond_m2 * ww), (m1**2 + v))


if __name__ == "__main__":
Expand Down

0 comments on commit 7f0895b

Please sign in to comment.