Neural Network Option Pricing¶
Background¶
neural_network_option_pricing.py
This module implements Neural Network Option Pricing.
Author: Financial Math Library
Code¶
```python
-- coding: utf-8 --¶
""" neural_network_option_pricing.py
This module implements Neural Network Option Pricing.
Author: Financial Math Library """
import numpy as np import matplotlib.pyplot as plt
======================================================================¶
def neural_network_option_pricing(): """ Neural Network Option Pricing.
This function demonstrates the key concepts and computational techniques
for neural network option pricing.
Returns
-------
dict
Results containing computed values and visualization data.
"""
# Implementation of Neural Network Option Pricing
print(f"Computing Neural Network Option Pricing...")
# Create sample data/parameters
n_simulations = 1000
time_points = np.linspace(0, 1, 100)
# Core computation logic
results = {
"time_points": time_points,
"description": "Neural Network Option Pricing"
}
return results
def main(): """Main execution function.""" results = neural_network_option_pricing()
# Create visualization
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(results["time_points"], "b-", linewidth=2)
ax.set_xlabel("Time")
ax.set_ylabel("Value")
ax.set_title("Neural Network Option Pricing")
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig("/tmp/neural_network_option_pricing.png", dpi=150)
print(f"Figure saved to /tmp/neural_network_option_pricing.png")
plt.close()
return results
if name == "main": main() ```
Exercises¶
Exercise 1. A neural network for option pricing takes inputs \((S, K, T, r, \sigma)\) and outputs the option price \(C\). Describe the architecture and explain why universal approximation makes this approach viable.
Solution to Exercise 1
A typical architecture is a feedforward network with 2-4 hidden layers, each containing 50-200 neurons with ReLU or tanh activation, and a single output neuron (with softplus or ReLU activation to ensure \(C \geq 0\)). The universal approximation theorem guarantees that a sufficiently wide single-layer network can approximate any continuous function to arbitrary accuracy, including the Black-Scholes formula \(C(S, K, T, r, \sigma)\). In practice, deeper networks with fewer neurons per layer are preferred for efficiency, as they can capture the hierarchical structure of the pricing function.
Exercise 2. Training data for the neural network is generated by computing Black-Scholes prices for many parameter combinations. Describe the training data generation process and potential pitfalls.
Solution to Exercise 2
- Sample parameters: \(S \sim U[50, 150]\), \(K \sim U[50, 150]\), \(T \sim U[0.01, 2]\), \(r \sim U[0, 0.1]\), \(\sigma \sim U[0.05, 0.5]\).
- Compute \(C_{\text{BS}}(S, K, T, r, \sigma)\) for each sample.
- Normalize inputs and outputs (e.g., use \(\ln(S/K)\) instead of \(S, K\) separately).
Pitfalls: (1) deep out-of-the-money options have prices near zero, which are difficult to learn (use relative error loss); (2) the function has rapid variation near expiry (\(T \to 0\)) and at the money (\(S \approx K\)); (3) extrapolation beyond the training range is unreliable.
Exercise 3. Compare the speed of a trained neural network versus the analytical Black-Scholes formula for pricing \(10^6\) options. When would you prefer the neural network?
Solution to Exercise 3
- Black-Scholes: Requires computing \(\mathcal{N}(d_1)\), \(\mathcal{N}(d_2)\), logarithms, and exponentials for each option. Vectorized NumPy: \(\sim 50\) ms for \(10^6\) options.
- Neural network: A forward pass through 3 layers with 100 neurons each. With GPU acceleration: \(\sim 5\) ms for \(10^6\) options (10x faster).
The neural network is preferred when: (1) the pricing model is expensive (e.g., Heston model requiring numerical integration or PDE solving); (2) real-time pricing of many options is needed (market-making); (3) the network replaces a calibration + pricing pipeline (e.g., directly mapping market data to prices). For simple models like Black-Scholes, the analytical formula is preferred for its exactness.
Exercise 4. How would you extend the neural network approach to price options under stochastic volatility (Heston model)?
Solution to Exercise 4
- Inputs: Add Heston parameters \((\kappa, \bar{v}, \sigma_v, \rho, v_0)\) to the input vector, giving 10 inputs total.
- Training data: Generate prices using the Heston COS method or characteristic function integration for many parameter combinations. This is expensive but done offline.
- Architecture: Use a deeper network (4-6 layers) to capture the richer pricing function.
- Output: The option price, or alternatively, the implied volatility (which has smoother behavior).
- Application: Once trained, the network provides instant Heston prices, enabling real-time calibration (fitting \(\kappa, \bar{v}, \sigma_v, \rho, v_0\) to market option prices by calling the network in the optimization loop instead of the slow COS method).