Skip to content

Add Black-Scholes example with documentation#2056

Open
litchki wants to merge 9 commits intolululxvi:masterfrom
litchki:master
Open

Add Black-Scholes example with documentation#2056
litchki wants to merge 9 commits intolululxvi:masterfrom
litchki:master

Conversation

@litchki
Copy link
Copy Markdown

@litchki litchki commented Feb 23, 2026

Description

This PR adds a new example demonstrating Physics-Informed Neural Networks (PINNs) for pricing European call options under the Black-Scholes model. This complements the existing physics/engineering examples with a fundamental application in computational finance.

Problem Overview

The example solves the Black-Scholes PDE in time-to-maturity form ($\tau = T - t$):

$$ \frac{\partial V}{\partial \tau} = \frac{1}{2}\sigma^2 S^2 \frac{\partial^2 V}{\partial S^2} + r S \frac{\partial V}{\partial S} - r V $$

with appropriate boundary and initial conditions for a European call option.

Changes

  1. Source Code (examples/pinn_forward/black_scholes_call.py):

    • Implements the Black-Scholes PDE.
    • Uses Sobol sequence sampling with powers of 2 ($N=2048$) to improve domain coverage and avoid sampling warnings.
    • Implements a two-stage training strategy (Adam for 20k iters + L-BFGS).
    • Includes validation against the analytical solution.
  2. Documentation (docs/demos/pinn_forward/black_scholes.rst):

    • Added a comprehensive documentation page with mathematical formulation.
    • Explains implementation details (Geometry, Network Architecture, Hyperparameters).
  3. Index Update (docs/demos/pinn_forward.rst):

    • Updated the table of contents to include the new example.
    • Removed the old raw link to keep the index clean.

Performance & Validation

  • L2 relative error: ~8.5e-4 (0.00085)
  • Mean PDE residual: ~0.069
  • Training time: ~7 minutes (GPU) / ~15 minutes (CPU)

Testing Checklist

  • Code runs without errors on PyTorch/TensorFlow backends.
  • Output format matches existing examples (e.g., heat.py).
  • Validated against analytical solution (scipy.stats.norm).
  • Docstrings and comments added (PEP 8 style).
  • No Sobol warnings (uses powers of 2: 2048, 64, 128).
  • Documentation builds correctly.

Reference

Tanios, R. (2021). "Physics Informed Neural Networks in Computational Finance: High Dimensional Forward & Inverse Option Pricing". ETH Zurich Master's Thesis. https://doi.org/10.3929/ethz-b-000491555

Added black_scholes_call.py to Time-dependent PDEs section.
Updated the Black-Scholes equation explanation and implementation details, including problem setup, boundary conditions, and training strategy.

.. code-block:: python

def pde(x, y):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this area, it is probably helpful to denote that x[:, 0] is stock prices, x[:, 1] are taus, y is stock prices.

Furthermore, it is likely best to write dV instead of dy.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion! I have updated the notation for clarity and consistency.

To clarify: in this example, y represents the option price V(S, tau),
not the stock price — I believe that was a small typo in the review comment.

The variables are now annotated as follows:

  • x[:, 0]: asset price S
  • x[:, 1]: time to maturity tau
  • y: option price V(S, tau) (network output)

I also renamed dy_* to dV_* in both the Python example and the
corresponding .rst documentation.

S_max = 150.0 # Maximum stock price


def pde(x, y):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are on board with the comment on R67 (same place in docs), change for consistency

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, updated consistently with the changes in the .rst documentation.

@echen5503
Copy link
Copy Markdown
Contributor

@litchki still working on this?

litchki added 3 commits April 6, 2026 11:32
Updated the PDE residual explanation and variable descriptions in the black_scholes.rst file.
Removed extra newline before boundary conditions section.
@litchki
Copy link
Copy Markdown
Author

litchki commented Apr 6, 2026

@litchki still working on this?
Yes! Sorry for the delay — I've just pushed the updates addressing your earlier review comments. Thank you for the follow-up!

)

**Sampling Strategy**:
To improve domain coverage and training stability, we use the **Sobol sequence** for sampling points. We select sample sizes as powers of 2 to avoid Sobol sequence warnings (2048 domain points, 64 boundary points, 128 initial points):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user might not know what sobol sequence is and hence have no idea what you're talking about when you say powers of two for sobol sequence warnings. Linking to a source or directly explaining would help.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion — I've updated the Sampling Strategy section to briefly
explain what Sobol sequences are (low-discrepancy quasi-random sequences providing
more uniform domain coverage than pseudorandom sampling), and added references to
both the original Sobol paper and the SciPy documentation for the power-of-two
requirement.

Please let me know if you'd prefer a more explicit explanation for first-time readers.

@litchki litchki requested a review from echen5503 April 6, 2026 04:24
Expanded explanation of Sobol sequences and their properties. Clarified validation results and hyperparameter tuning.
@echen5503
Copy link
Copy Markdown
Contributor

Checked documentation on docs build, ran code. All good.

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

Successfully merging this pull request may close these issues.

2 participants