From 7262ccab24ff2a8ebaa2591665ef21638971583f Mon Sep 17 00:00:00 2001 From: tzar-beep Date: Tue, 24 Nov 2026 05:56:53 +0000 Subject: [PATCH 1/2] Test the Black-Scholes finite difference pricer with negative rates The new tests fail on master, where black_scholes_fd rejects a negative risk-free rate or dividend yield. --- tests/unit/test_model_finite_difference.py | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/unit/test_model_finite_difference.py b/tests/unit/test_model_finite_difference.py index 3d07c878..dad07243 100644 --- a/tests/unit/test_model_finite_difference.py +++ b/tests/unit/test_model_finite_difference.py @@ -1,4 +1,5 @@ import numpy as np +import pytest from pytest import approx from financepy.utils.global_types import OptionTypes @@ -8,6 +9,8 @@ from financepy.utils.date import Date from financepy.utils.global_vars import G_DAYS_IN_YEAR from financepy.models.equity_crr_tree import crr_tree_val_avg +from financepy.models.bs_american_analytic import BlackScholesAlgorithmTypes +from financepy.models.bs_american_analytic import american_value from financepy.models.finite_difference import black_scholes_fd, fn_dx, fn_dxx from financepy.models.finite_difference import solve_tridiagonal_matrix @@ -550,6 +553,50 @@ def test_put_option(): ######################################################################################## +@pytest.mark.parametrize("r, q", [(-0.01, 0.02), (0.03, -0.02), (-0.02, -0.01)]) +@pytest.mark.parametrize("opt_type", [OptionTypes.EUROPEAN_CALL, OptionTypes.EUROPEAN_PUT]) +def test_european_fd_negative_rate_or_dividend(r, q, opt_type): + """Negative rates and dividend yields are valid inputs (e.g. EUR, CHF, JPY).""" + s, t, k, vol = 100.0, 1.5, 95.0, 0.25 + + v = black_scholes_fd(s, vol, t, k, r, q, opt_type.value) + + assert v == approx(american_value(s, t, k, r, q, vol, opt_type), abs=1e-3) + + +######################################################################################## + + +def test_american_put_fd_negative_rate_no_early_exercise(): + """With r <= 0 and q >= 0 early exercise of a put is never optimal, so it is worth + the European put.""" + s, t, k, r, q, vol = 80.0, 2.0, 100.0, -0.01, 0.02, 0.3 + + v = black_scholes_fd(s, vol, t, k, r, q, OptionTypes.AMERICAN_PUT.value) + + assert v == approx(american_value(s, t, k, r, q, vol, OptionTypes.EUROPEAN_PUT), abs=1e-3) + + +######################################################################################## + + +def test_american_call_fd_negative_rate(): + """With r < 0 < q the call can have two exercise boundaries, which the analytic + approximations do not handle. Compare with a fine Leisen-Reimer tree.""" + s, t, k, r, q, vol = 130.0, 3.0, 100.0, -0.03, 0.03, 0.4 + + v = black_scholes_fd(s, vol, t, k, r, q, OptionTypes.AMERICAN_CALL.value) + v_lr = american_value( + s, t, k, r, q, vol, OptionTypes.AMERICAN_CALL, BlackScholesAlgorithmTypes.LEISEN_REIMER, num_steps=2001 + ) + + assert v == approx(v_lr, abs=2e-2) + assert v >= american_value(s, t, k, r, q, vol, OptionTypes.EUROPEAN_CALL) + + +######################################################################################## + + # def test_dx(): # np.testing.assert_array_equal( From aadc18cee7e6e0a272c767c2c042ae76b5247dd8 Mon Sep 17 00:00:00 2001 From: tzar-beep Date: Tue, 24 Nov 2026 05:56:53 +0000 Subject: [PATCH 2/2] Allow negative rates and dividend yields in the finite difference pricer black_scholes_fd raised ValueError for a negative risk-free rate or dividend yield, so BlackScholesAlgorithmTypes.FINITE_DIFFERENCE could not price options in negative-rate markets. Nothing in the theta scheme depends on their sign: the PDE discounts at r with drift (r - q) S, and American options are floored at the exercise value after each step, which also covers the two exercise boundaries that negative rates can create. Remove the two checks. With the checks removed, European prices match the analytic formula to 1.3e-4, and American prices match a 2001-step Leisen-Reimer tree to within 0.012 (0.03%) over 162 cases with r and q in [-0.03, 0.03]. --- financepy/models/finite_difference.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/financepy/models/finite_difference.py b/financepy/models/finite_difference.py index eb59beec..db192cc8 100644 --- a/financepy/models/finite_difference.py +++ b/financepy/models/finite_difference.py @@ -31,11 +31,9 @@ def validate_black_scholes_fd_inputs( if strike_price <= 0.0: raise ValueError("strike_price must be positive") - if risk_free_rate < 0.0: - raise ValueError("risk_free_rate must be positive") - - if dividend_yield < 0.0: - raise ValueError("dividend_yield must be positive") + # Negative risk_free_rate and dividend_yield are allowed: the theta scheme does + # not depend on their sign, and the early-exercise check handles the + # double-boundary American cases that negative rates can create. if volatility < 0.0: raise ValueError("volatility must be non-negative")