<jupyter_start><jupyter_text>Heston Nandi Options<jupyter_code>%load_ext autoreload
%autoreload 2
import finoptions as fo
import numpy as np
import pandas as pd
lamb=-0.5; omega=2.3e-6; alpha=2.9e-6; beta=0.85; gamma=184.25; S=100; K=100; t=252; r=0.05/252
opt = fo.heston_nandi_options.HestonNandiOption(S=S, K=K, t=t, r=r, lamb=lamb,omega=omega,alpha=alpha,beta=beta,gamma=gamma)
opt.call()
opt.put()

opt.delta()
opt.gamma()
const = 1
phi = 20
for k in range(0,500):
    # Model Parameters:
    gamma = gamma + lamb + 1 / 2
    lamb = -1 / 2
    sigma2 = (omega + alpha) / (1 - beta - alpha * gamma ** 2)
    # Function to be integrated:
    cphi0 = phi * np.array([1j])
    cphi = cphi0 + const
    a = cphi * r
    b = lamb * cphi + cphi * cphi / 2
    # fmt: off
    for i in range(1, t):
        a = a + cphi*r + b*omega - np.log(1-2*alpha*b)/2
        b = cphi*(lamb+gamma) - gamma**2/2 + beta*b + 0.5*(cphi-gamma)**2/(1-2*alpha*b)
anp = np.array([0j]*t)
anp[0] = a[0]
bnp = np.array([0j]*t)
bnp[0] = b[0]<jupyter_output><empty_output><jupyter_text>Garch Functions and Sims<jupyter_code>%load_ext autoreload
%autoreload 2
import finoptions as fo
import numpy as np
import pandas as pd
from scipy.optimize import minimize as _minimize
ts = pd.read_csv("../pytest/garch_ts.csv").x
ts = np.array(ts)

lamb = 4
omega = 8e-5
alpha = 6e-5
beta = 0.7
gamma = 0
rf = 0

rfr = rf
symmetric=True
trace=False
x=ts
rfr = rf
gam = gamma

# Continue:
params = dict(lamb = lamb, omega = omega, alpha = alpha,
    beta = beta, gamma = gam, rf = rfr)

# Transform Parameters and Calculate Start Parameters:
par_omega = -np.log((1-omega)/omega)  # for 2
par_alpha = -np.log((1-alpha)/alpha)  # for 3
par_beta = -np.log((1-beta)/beta)     # for 4
par_start = [lamb, par_omega, par_alpha, par_beta]
if ~symmetric:
    par_start.append(gam)

# Initial Log Likelihood:
opt = dict()
opt['value'] = fo.heston_nandi_options._llhHNGarch(x0 = par_start, trace = trace, symmetric = symmetric, rfr = rfr, x = x)
opt['estimate'] = par_start

if trace:
    print(lamb, omega, alpha, beta, gam)
    print(opt['value'])

# Estimate Parameters:
opt = _minimize(fo.heston_nandi_options._llhHNGarch, par_start, args=(trace, symmetric, rfr, x), method='L-BFGS-B')
opt
fo.heston_nandi_options.hngarch_fit(ts, lamb=lamb, omega=omega, alpha=alpha, beta=beta, trace=False, symmetric=True)['model']<jupyter_output><empty_output><jupyter_text>HN Garch Sim<jupyter_code>x = fo.heston_nandi_options.hngarch_sim(lamb = 4, omega = 8e-5, alpha = 6e-5, beta = 0.7, gamma = 0, rf = 0)
import matplotlib.pyplot as plt
plt.plot(x)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1529051987767584, 'fraction_numerical': 0.03555045871559633, 'mean_word_length': 4.111328125, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '306894', 'license': 'MIT', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1077, 'n_words': 259, 'path': 'notebooks/5_Heston.ipynb', 'repo_name': 'bbcho/finoptions-dev', 'size': '2616'}