Пример #1
0
def compute_expectation_analytic(qgy):
    Tk = qgy.Tk
    ans = [1]
    for k in range(1, len(Tk)):
        EtT = qgy.E_tT_simple(0, k, qgy.phi_y(k), qgy.psi_y(k))
        XtT = 1
        ans.append(EtT * XtT)
    return ans
Пример #2
0
def test_mc_analy_comparison():
    qgy_md = qgy.QgyModel()
    num_sim = 10000
    n_per_year = 50
    EtXT_analy = compute_expectation_analytic(qgy_md)
    EtXT_mc = compute_expectation_mc(qgy_md, num_sim, n_per_year)
    plt.plot(qgy_md.Tk, EtXT_analy, 'r--')
    plt.plot(qgy_md.Tk, EtXT_mc, 'b-o')
    plt.show()
Пример #3
0
def test_convergence():
    qgy_md = qgy.QgyModel()
    num_sim = 100
    n_per_year = 100
    EtXT_analy = compute_expectation_analytic(qgy_md)
    for i in range(1, 10):
        current_num = num_sim * i * 2
        EtXT_mc = compute_expectation_mc(qgy_md, current_num, n_per_year)
        RMSE = np.sqrt(np.sum((EtXT_analy - EtXT_mc)**2))
        print("sim_num = ", current_num, "err = ", RMSE, "N * err = ",
              current_num * RMSE, "sqrt(N) * err = ",
              np.sqrt(current_num) * RMSE)
Пример #4
0
def compute_expectation_mc(qgy, num_sim, n_per_year):
    n = qgy.n
    sigma2 = []
    sigma2_prime = []
    dt = 1 / n_per_year
    for i in range(1, n):
        for j in range(n_per_year):
            t = qgy.Tk[i - 1] + dt * (j + 1)
            sigma2.append(qgy.inf_vol(t))
            sigma2_prime.append(qgy.inf_vol_prime(t))
    sigma2_prime = None
    sigma_n = np.repeat(1, n_per_year * (n - 1))
    sigma_n_prime = None  #np.repeat(0, n_per_year * (n - 1))

    phi_Tk = gen_phi_vec_list(qgy)
    psi_Tk = gen_psi_matx_list(qgy)

    #quasi monte carlo
    permut_matrix = qgy.generate_permutation_matrix(num_sim,
                                                    (n - 1) * n_per_year)
    sob_seq = qgy.generate_sobol_squence(num_sim, 3)
    ######################

    np.random.seed(seed=12345)
    ans = np.zeros(n)
    for i in range(num_sim):
        # use pesudo random number
        [x_n, x_y1] = qgy.generate_two_correlated_gauss(
            sigma_n, sigma2, qgy.rho_n_y1, (n - 1) * n_per_year,
            1 / n_per_year, sigma_n_prime, sigma2_prime)
        x_y2 = qgy.generate_one_gauss(sigma2, (n - 1) * n_per_year,
                                      1 / n_per_year, sigma2_prime)

        # use quasi random number
        #[x_n, x_y1] = qgy.generate_two_correlated_quasi_gauss(sigma_n, sigma2, qgy.rho_n_y1, dt, i, permut_matrix, sob_seq, [1, 2])
        #x_y2 = qgy.generate_one_quasi_gauss(sigma2, dt, i, permut_matrix, sob_seq, 0)

        x_Tk_y1 = np.concatenate([[0], x_y1[n_per_year - 1::n_per_year]])
        x_Tk_y2 = np.concatenate([[0], x_y2[n_per_year - 1::n_per_year]])
        x_n_Tk = np.concatenate([[0], x_n[n_per_year - 1::n_per_year]])

        one_path = np.zeros(n)
        for j in range(len(x_Tk_y1)):
            x_Tk = np.matrix([x_n_Tk[j], x_Tk_y1[j], x_Tk_y2[j]]).T
            X_Tk = qgy.Xt(x_Tk, phi_Tk[j], psi_Tk[j])
            one_path[j] = X_Tk
        #plt.plot(qgy.Tk, one_path, 'g.')
        ans += one_path
    ans /= num_sim
    ans[0] = 1
    return ans
Пример #5
0
from Model.QgyModel import *

qgy = QgyModel()
ytT = qgy.price_yoy_infln_fwd()
I0_Tk_corr = qgy.fit_yoy_convexity_correction(qgy.I0_Tk)

plt.subplot(1, 2, 1)
plt.plot(qgy.Tk[1:], -0.00012662 * np.log(qgy.Tk[1:]), '-')
plt.plot(qgy.Tk[1:], ytT[1:] - (qgy.I0_Tk[1:] / qgy.I0_Tk[:-1] - 1), 'or')

plt.subplot(1, 2, 2)
plt.plot(qgy.Tk[1:], ytT[1:] - (I0_Tk_corr[1:] / I0_Tk_corr[:-1] - 1), 'or')
print(qgy.I0_Tk)
print(I0_Tk_corr)
plt.show()
Пример #6
0
from Model.QgyModel import *

qgy = QgyModel()
x1 = [-272797080.58203894] * 30
x2 = [-104016145.57136315] * 30
y = qgy.generate_yoy_structure_from_drivers(x1, x2)
print("y = ", y)
I_Tk = 1.6605024038790817
I_Tk_1 = 1.6072677347677349
A = 0.018957673336304
x1 = -272797080.582039
x2 = -104016145.571363
phi = -5.05381517625226E-10
psi1 = 1.71200463931491E-17
psi12 = 1.08610097797903E-17
exponent = A - (phi + 0.5 * psi1 * x1 + psi12 * x2) * x1

print(I_Tk / I_Tk_1 * np.exp(exponent))
Пример #7
0
from Model.QgyModel import *

test = [10, 20, 40, 80, 160, 320]
num_sim = 10000

for num_per_year in test:
    dt = 1 / num_per_year
    qgy = QgyModel()
    qgy.n_per_year = num_per_year
    N = num_per_year * (qgy.n - 1)
    sigma = np.repeat(1, N)

    res = []
    for i in range(num_sim):
        one_path = qgy.generate_one_gauss(sigma, N, dt)
        res.append(one_path[-1])
        #plt.plot(one_path, 'g-')
    #plt.show()

    var = np.var(res)
    mean = np.mean(res)

    print("num_per_year = ", num_per_year, "mean = ", mean, ", var = ", var)
Пример #8
0
from Model.QgyModel import *

qgy = QgyModel()
qgy.n_per_year = 10
N = qgy.n_per_year * (qgy.n - 1)
sigma1 = np.repeat(1, N)
sigma2 = np.repeat(2, N)
rho = -0.1
dt = 1 / qgy.n_per_year

num_sim = 10000
res1 = []
res2 = []

for i in range(num_sim):
    [x_n, x_y1] = qgy.generate_two_correlated_gauss(sigma1, sigma2, rho, N, dt)
    res1.append(x_n[-1])
    res2.append(x_y1[-1])

corr = np.corrcoef(res1, res2)
print("corr = ", corr)
Пример #9
0
def gen_psi_matx_list(qgy):
    ans = []
    for i in range(qgy.n):
        ans.append(qgy.psi_y(i))
    return ans
Пример #10
0
def gen_phi_vec_list(qgy):
    ans = []
    for i in range(qgy.n):
        ans.append(qgy.phi_y(i))
    return ans
Пример #11
0
import numpy as np
from Model.QgyModel import *


qgy = QgyModel()
qgy.generate_terms_structure()
Tk = qgy.Tk
num_iters = 1000
strike = 0.05


avg = np.zeros(len(Tk))
for i in range(num_iters):
    qgy.generate_terms_structure()
    Y_Tk = qgy.Y_Tk
    D_Tk = qgy.D_t
    caplet = np.maximum(0.0, Y_Tk - (strike+1))* D_Tk/D_Tk[0]
    plt.plot(Tk, caplet, 'g')
    avg += caplet

avg /= num_iters
plt.plot(Tk, avg)
plt.show()
Пример #12
0
import Model.QgyModel as qgy
import matplotlib.pyplot as plt
import numpy as np


qgy = qgy.QgyModel()
qgy.n_per_year = 50
num_path = 10000
P_Tk = qgy.P_0T(qgy.Tk)

Sigma_Tk_y = 0.045
v_Tk_y = 0.8
rho_Tk_y = -0.5
rho_t_ny1 = -0.1
#R_Tk = np.array([0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]) * 0.01
R_Tk = np.array([0, 1, 2, 4, 8, 16, 32, 64, 128, 256]) * 0.01
count = 0
for R in R_Tk:
    mc_res = np.zeros(len(qgy.Tk))
    qgy.fill_spherical_parameters(Sigma_Tk_y, v_Tk_y, rho_Tk_y, rho_t_ny1, R)
    for k in range(num_path):
        qgy.generate_terms_structure()
        disc = qgy.D_t
        yoy = qgy.Y_Tk
        disc_price = yoy * disc
        mc_res += disc_price
        #plt.plot(qgy.Tk, disc_price/P_Tk - 1, 'g-')
    avg_price = mc_res/num_path
    count += 1
    plt.plot(qgy.Tk, avg_price/P_Tk - 1, color=[0, count/len(R_Tk), 0])
Пример #13
0
from Model.QgyModel import *
from scipy import stats
import seaborn as sns

qgy = QgyModel()
qgy.n_per_year = 500
N = qgy.n_per_year * (qgy.n - 1)
sigma2 = []
sigma2_prime = []
dt = 1 / qgy.n_per_year
for i in range(1, len(qgy.R_Tk_y)):
    for j in range(qgy.n_per_year):
        t = qgy.Tk[i - 1] + dt * (j + 1)
        sigma2.append(qgy.inf_vol(t))
        sigma2_prime.append(qgy.inf_vol_prime(t))

sigma_n = np.repeat(1, N)
sigma_n_prime = np.repeat(0, N)
t = np.linspace(1, qgy.Tk[-1], qgy.n_per_year * (qgy.n - 1))

dist = []
N = 100
for i in range(0, N):
    [x_n,
     x_y1] = qgy.generate_two_correlated_gauss(sigma_n, sigma2, qgy.rho_n_y1,
                                               (qgy.n - 1) * qgy.n_per_year,
                                               1 / qgy.n_per_year,
                                               sigma_n_prime, sigma2_prime)
    x_y2 = qgy.generate_one_gauss(sigma2, (qgy.n - 1) * qgy.n_per_year,
                                  1 / qgy.n_per_year, sigma2_prime)
    x_Tk_y1 = x_y1[::qgy.n_per_year]