Skip to content

A package to learn optimal hedges by a deep feed forward neural network, to minimise the terminal error

License

Notifications You must be signed in to change notification settings

hb84ffm/deep-hedging

Repository files navigation

deep_call_hedger

A Python package which implements the deep hedging algorithm. Target is learning optimal hedges through a deep feed forward neural network (DFNN), in order to minimize terminal loss when hedging a written European call option.

FEATURES

  • Simulates stock prices following a geometric Brownian motion
  • Calculates call prices under the Black Scholes model
  • Trains a DFNN at each timestep on stocks, calls & strike to minimize terminal error
  • Runs predictions using the trained model
  • Benchmarks model performance against Black Scholes visible in charts, KPIs & KRIs

REQUIREMENTS

  • Python 3.11+ (tested with 3.11.9)
  • Required dependencies (see requirements.txt for details):
    numpy==2.1.3
    tensorflow==2.19.0
    scipy==1.16.0
    matplotlib==3.10.5
    seaborn==0.13.2

INSTALLATION

  1. Clone the repository:
    git clone https://github.com/hb84ffm/deep-hedging.git
    cd deep_call_hedger

  2. Create & activate your virtual environment:
    python3 -m venv venv
    source venv/bin/activate # On Mac/Linux
    venv\Scripts\activate # On Windows

  3. Install dependencies:
    pip install -r requirements.txt

USAGE

  1. Train the deep hedging model (or use the pretrained model deep_hedging_64.keras):
    python main_training.py
    -> Saves a trained ".keras" model to the models/ folder.

  2. Run prediction & analysis:
    python main_prediction.py
    -> Uses the trained model to run simulations & generate plots.

PACKAGE STRUCTURE

deep_call_hedger/
├─── __init__.py
├─── main_prediction.py             # Orchestrates the prediction
├─── main_training.py               # Orchestrates the training
├─── dh_model/
     ├─── __init__.py
     ├─── dh_model.py               # Designs the model (computational graph) by Keras functional API
├─── models/                        
     ├─── deep_hedging_64.keras     # Pretrained deep_hedging_64.keras model trained across 64 timesteps
├── options/
     ├─── __init__.py
     ├─── bs.py                     # Black Scholes calculator for European calls
├── prediction/
     ├─── analysis.py               # Derives analytics (charts, KPIs & KRIs) on predicted data
     ├─── prediction.py             # Generates prediction data & runs prediction using the trained model 
├── stocks/                  
     ├── __init__.py
     ├─── stocks.py                 # Stock simulation
├── training/                   
     ├─── __init__.py
     ├─── training.py               # Compiles the model & starts the training

EXAMPLE WORKFLOW

See provided Jupyter notebook example for explanation.

CREDITS

Josef Teichmann's implementation

AUTHOR

For questions or feedback reach out to me via: GitHub.