Git Product home page Git Product logo

Comments (3)

cyrilchim avatar cyrilchim commented on May 6, 2024

Hi Shota,

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

from tf-quant-finance.

cyrilchim avatar cyrilchim commented on May 6, 2024

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)

from tf-quant-finance.

 avatar commented on May 6, 2024

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!

from tf-quant-finance.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.