Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HestonModel.sample_paths returns variance converging to zero #42

Closed
ghost opened this issue Jan 25, 2021 · 3 comments
Closed

HestonModel.sample_paths returns variance converging to zero #42

ghost opened this issue Jan 25, 2021 · 3 comments

Comments

@ghost
Copy link

ghost commented Jan 25, 2021

I generated sample paths of HestonModel with parameters shown in the documentation.

However, variance eventually converges to zero and accordingly spot prices asymptotic to constant.
I attach the plot and a snippet to reproduce it.

Is this an expected behaviour?
I appreciate it if you could take a look at it.

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tf_quant_finance as tff
from tf_quant_finance.math import random
from tf_quant_finance.math.piecewise import PiecewiseConstantFunc

tf.random.set_seed(42)

epsilon = PiecewiseConstantFunc(jump_locations=[0.5], values=[1, 1.1], dtype=np.float64)
process = tff.models.HestonModel(
    kappa=0.5, theta=0.04, epsilon=epsilon, rho=0.1, dtype=np.float64
)
times = np.linspace(0.0, 1.0, 1000)
num_samples = 10  # number of trajectories
sample_paths = process.sample_paths(
    times,
    time_step=0.01,
    num_samples=num_samples,
    initial_state=np.array([1.0, 0.04]),
    random_type=random.RandomType.SOBOL,
)

s = np.array(sample_paths[..., 0]).T
v = np.array(sample_paths[..., 1]).T

plt.figure(figsize=(24, 6))
plt.plot(s)
plt.title("spot")
plt.show()

plt.figure(figsize=(24, 6))
plt.plot(v)
plt.title("variance")
plt.show()
$ pip show tf-quant-finance
Name: tf-quant-finance
Version: 0.0.1.dev24
Summary: High-performance TensorFlow library for quantitative finance.
Home-page: https://github.com/google/tf-quant-finance
Author: Google Inc.
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python3.8/site-packages
Requires: numpy, protobuf, tensorflow-probability, attrs
Required-by: 

s
v

@cyrilchim
Copy link
Contributor

Hi Shota,

Thank you for reporting the issue. Let me take a look and come back to you.

@cyrilchim
Copy link
Contributor

Thank you again for reaching out.

I think this behavior is expected.

If you run trajectories for longer they will "unstuck". In this case Feller condition is violated (epsilon**2 > 2 * kappa * theta) so the process hits zero eventually, but will also "unstuck" if you run it for longer.

You will get a less dramatic picture if you use RandomType.PSEUDO instead (since Sobol numbers are not meant to be random at all).

You can test sample paths quality by comparing Monte Carlo estimate for European option prices with tff.models.heston.approximations.european_option_price

Here is a code snippet:

tf.random.set_seed(42)
dtype = tf.float64
kappa = 0.5
theta = 0.04
epsilon = 1.1  # make it a constant so that Atari approximation can be used
rho = 0.1

initial_log_spot = tf.constant(1.0, dtype=dtype)
initial_vol = tf.constant(0.04, dtype=dtype)
initial_state = np.array([initial_log_spot, initial_vol])

# Set up Heston model
heston = tff.models.HestonModel(
    kappa=kappa, theta=theta, epsilon=epsilon, rho=rho, dtype=np.float64
)

# ATM European call option price seems to be valued correctly
maturity_time = 1.0
strike = tf.math.exp(initial_log_spot)

# Using Atari approximation
approximate_price = tff.models.heston.approximations.european_option_price(
      variances=initial_vol,
      strikes=strike,
      expiries=maturity_time,
      forwards=strike,
      is_call_options=True,
      kappas=kappa,
      thetas=theta,
      sigmas=epsilon,
      rhos=rho,
      dtype=tf.float64)
print("Atari approximation: ", approximate_price.numpy())
# Expected 0.1445071937036353 

# Using Monte Carlo sampling
samples = heston.sample_paths(
    times=[maturity_time / 2, maturity_time],
    initial_state=initial_state,
    time_step=0.01,
    num_samples=1000000,
    random_type=tff.math.random.RandomType.SOBOL,
    seed=42)
log_spots = samples[:, -1, 0]
monte_carlo_price = (
        tf.math.reduce_mean(tf.nn.relu(tf.math.exp(log_spots) - strike)))
print("Monte Carlo price (Andersen method): ", monte_carlo_price.numpy())
# Expected 0.1449164116664759

Please let me know if this all makes sense.

Please feel free to add additional tests for correctness (e.g., check that the expected boundary hitting times are correct)

@ghost
Copy link
Author

ghost commented Jan 26, 2021

Yes, that makes great sense.
Feller condition is indeed violated and thus variance will eventually be zero. Interesting.

It really helps.
Thank you so much for your time and kindness!

@ghost ghost closed this as completed Jan 26, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant