This project explores the capability of neural networks to learn and approximate the Black-Scholes option pricing model. The goal is to investigate whether artificial intelligence can effectively learn complex financial pricing models and potentially capture additional market dynamics that the traditional Black-Scholes model might miss.
The Black-Scholes model is a fundamental mathematical model used for pricing European-style options. While it provides an elegant closed-form solution, it relies on several assumptions that may not hold in real markets (constant volatility, continuous trading, no transaction costs, etc.).
This project aims to:
- Implement the traditional Black-Scholes model
- Generate synthetic option pricing data
- Train a neural network to learn the Black-Scholes pricing function
- Compare the neural network's predictions with theoretical prices
- Expose the trained model via REST API
- Python: Primary programming language
- NumPy: For numerical computations and array operations
- SciPy: For statistical functions and cumulative normal distribution
- PyTorch: For neural network implementation
- Redis: For efficient caching of API responses
The Black-Scholes formula for European call options is:
- Sâ‚€: Initial stock price
- K: Strike price
- T: Time to maturity
- r: Risk-free rate
- σ: Volatility
- N(·): Cumulative normal distribution function
Assumptions:
- The asset follows a geometric Brownian motion with constant volatility.
- The risk-free rate r, volatility \sigma, and dividend yield q are constant.
- The option is European-style (exercisable only at expiration).
- The model does not use implied volatility; it assumes volatility is known and constant.
- Run Redis:
docker run -d --name redis-server -p 6379:6379 redis- Build the Docker image:
docker build -t black-scholes-nn .- Run the API:
docker run -p 8000:8000 --env REDIS_HOST=host.docker.internal black-scholes-nnThis will:
- Generate the training data
- Train the neural network model
- Start the FastAPI server on port 8000
- Create a virtual environment:
python -m venv venv
source venv/bin/activate - Install dependencies:
pip install -r requirements.txt- Run the main script to generate dataset, train model, and evaluate:
python main.py- Start the API server:
uvicorn main:app --host 0.0.0.0 --port 8000The project follows this workflow:
-
Data Generation (
src/data_generation.py):- Generates synthetic option pricing data
- Creates training and validation datasets
- Saves data to CSV files in the
data/directory
-
Model Training (
src/train.py):- Loads the training data
- Trains the neural network to approximate Black-Scholes
- Saves the trained model to
models/model.pth
-
Model Evaluation (
src/evaluate.py):- Evaluates model performance on test data
- Generates performance metrics and comparisons
-
API Service:
- Provides REST endpoint for option price predictions
Once the server is running, you can make predictions using the following curl command:
curl -X POST "http://127.0.0.1:8000/predict" \
-H "Content-Type: application/json" \
-d '{"S": 100, "K": 120, "q": 0.01, "r": 0.05, "sigma": 0.3, "t": 0.5}'Parameters:
S: Current stock priceK: Strike priceq: Dividend yieldr: Risk-free ratesigma: Volatilityt: Time to maturity (in years)
After training the neural network, the model achieved the following metrics:
| Metric | Value |
|---|---|
| Mean Absolute Error (MAE) | 0.1952 |
| Root Mean Squared Error (RMSE) | 0.3047 |
The plot below compares the predicted call option prices from the neural network (ANN) with the theoretical Black-Scholes prices:
The neural network closely tracks the Black-Scholes formula across a range of asset prices, demonstrating that a well-trained ANN can effectively learn the Black-Scholes mapping. However, some deviations are visible, especially for higher asset prices, which may be due to the network's capacity, data distribution, or the inherent limitations of the Black-Scholes model itself.
Conclusion:
While the Black-Scholes model provides a solid theoretical foundation for option pricing, it relies on simplifying assumptions such as constant volatility, log-normal price dynamics, and frictionless markets. These assumptions often do not hold in real financial markets, leading to pricing errors. Neural networks, on the other hand, offer a flexible, data-driven approach that can potentially capture more complex, nonlinear relationships present in real-world data. This project demonstrates that neural networks can approximate the Black-Scholes formula with high accuracy, and opens the door to further research where ANNs are trained on real market data to capture features and risks that traditional models may overlook.



