Deep Hedging Demo - An Example of Using Machine Learning for Derivative Pricing.

Overview

Deep Hedging Demo

Pricing Derivatives using Machine Learning

Image of Demo

1) Jupyter version: Run ./colab/deep_hedging_colab.ipynb on Colab.

2) Gui version: Run python ./pyqt5/main.py Check ./requirements.txt for main dependencies.

The Black-Scholes (BS) model – developed in 1973 and based on Nobel Prize winning works – has been the de-facto standard for pricing options and other financial derivatives for nearly half a century. The model can be used, under the assumption of a perfect financial market, to calculate an options price and the associated risk sensitivities. These risk sensitivities can then be theoretically used by a trader to create a perfect hedging strategy that eliminates all risks in a portfolio of options. However, the necessary conditions for a perfect financial market, such as zero transaction cost and the possibility of continuous trading, are difficult to meet in the real world. Therefore, in practice, banks have to rely on their traders’ intuition and experience to augment the BS model hedges with manual adjustments to account for these market imperfections. The derivative desks of every bank all hedge their positions, and their PnL and risk exposure depend crucially on the quality of their hedges. If their hedges does not properly account for market imperfections, banks might underestimate the true risk exposure of their portfolios. On the other hand, if their hedges overestimate the cost of market imperfections, banks might overprice their positions (relative to their competitors) and hence risk losing trades and/or customers. Over the last few decades, the financial market has become increasingly sophisticated. Intuition and experience of traders might not be sufficiently fast and accurate to compute the impact of market imperfections on their portfolios and to come up with good manual adjustments to their BS model hedges.

These limitations of the BS model are well-known, but neither academics nor practitioners have managed to develop alternatives to properly and systematically account for market frictions – at least not successful enough to be widely adopted by banks. Could machine learning (ML) be the cure? Last year, the Risk magazine reported that JP Morgan has begun to use machine learning to hedge (a.k.a. Deep Hedging) a portion of its vanilla index options flow book and plan to roll out the similar technology for single stocks, baskets and light exotics. According to Risk.net (2019), the technology can create hedging strategies that “automatically factor in market fictions, such as transaction costs, liquidity constraints and risk limits”. More amazingly, the ML algorithm “far outperformed” hedging strategies derived from the BS model, and it could reduce the cost of hedging (in certain asset class) by “as much as 80%”. The technology has been heralded by some as “a breakthrough in quantitative finance, one that could mark the end of the Black-Scholes era.” Hence, it is not surprising that firms, such as Bank of America, Societe Generale and IBM, are reportedly developing their own ML-based system for derivative hedging.

Machine learning algorithms are often referred to as “black boxes” because of the inherent opaqueness and difficulties to inspect how an algorithm is able to accomplishing what is accomplishing. Buhler et al (2019) recently published a paper outlining the mechanism of this ground-breaking technology. We follow their outlined methodology to implement and replicate the “deep hedging” algorithm under different simulated market conditions. Given a distribution of the underlying assets and trader preference, the “deep hedging” algorithm attempts to identify the optimal hedge strategy (as a function of over 10k model parameters) that minimizes the residual risk of a hedged portfolio. We implement the “deep hedging” algorithm to demonstrate its potential benefit in a simplified yet sufficiently realistic setting. We first benchmark the deep hedging strategy against the classic Black-Scholes hedging strategy in a perfect world with no transaction cost, in which case the performance of both strategies should be similar. Then, we benchmark again in a world with market friction (i.e. non-zero transaction costs), in which case the deep hedging strategy should outperform the classic Black-Scholes hedging strategy.

References:

Risk.net, (2019). “Deep hedging and the end of the Black-Scholes era.”

Hans Buhler et al, (2019). “Deep Hedging.” Quantitative Finance, 19(8).

Comments
  • delta calculation reports Error

    delta calculation reports Error

    Dear YuMan, Thanks a lot for your help,and I have another issue in coding error, When I ran this line: nn_delta = submodel(I_range) I got InvalidArgumentError: Matrix size-incompatible: In[0]: [1,101], In[1]: [1,15] [Op:MatMul]

    image

    It seems that the the defined Delta_submodel have matrix size-incompatible problems. But the Delta_submodel doesn't seem to specify the dimension of the input. Could you seek out where the bug is? Thanks a lot for your help! I will appreciate it!

    opened by qiquanbuaa 2
  • Run the Deep Hedging Algorithm (Simple Network) model.fit无label

    Run the Deep Hedging Algorithm (Simple Network) model.fit无label

    同学您好,十分感谢您follow 的Git项目,对我帮助很大。我在执行您的Git项目时,有一处问题不太理解,请您耐心指教。 执行Run the Deep Hedging Algorithm (Simple Network)时, model_simple.fit(x=xtrain, batch_size=batch_size, epochs=epochs, validation_data=xtest, verbose=1) 输入变量为什么只有x,而没有label。 当我将其更改(xtrain, S_train, ..., xtest, S_test)时, 报错如下: ValueError: ('Error when checking model target: expected no data, but got:', 如果您有时间的话,请您指导。 有可能的话,也可以邮件联系,[email protected] 感谢您的帮助!

    opened by qiquanbuaa 2
  • delta calculation error in recurrent network

    delta calculation error in recurrent network

    Dear YuMan, There is an error in the delta calculation for the recurrent network. It works fine for the simple network. When I ran this line: if share_stretegy_across_time is False: submodel = Delta_SubModel(model = model_recurrent,
    days_from_today = days_from_today) else: submodel = Delta_SubModel(model = model_recurrent,
    days_from_today = 0)

    nn_delta = submodel(I_range)

    I got: Layer model_29 expects 2 input(s), but it received 1 input tensors. Can you please seek out the problem? I tried changing the function Delta_SubModel but the problem still remains. Thanks a lot for your help! I will really appreciate it!

    opened by SaraIqbal 1
  • minor Documentation issue

    minor Documentation issue

    Hey, in the 'README.md' file you have written:

    Run python ./pyqt/main.py. Check ./requirements.txt for main dependencies.

    Nevertheless there is a '5' currently missing and a "." should be removed, the correct line being:

    Run python **./pyqt5/main.py** Check ./requirements.txt for main dependencies.

    Thanks

    opened by GastonMazzei 1
  • "w" in def CVaR()

    What is the input "w" in the CVaR loss function.

    def CVaR(wealth = None, w = None, loss_param = None):
        alpha = loss_param
        # Expected shortfall risk measure
        return K.mean(w + (K.maximum(-wealth-w,0)/(1.0-alpha)))
    

    How do I have to initialize it in order to avoid the following error:

    optimizer = Adam(learning_rate=lr)
    
    # Setup and compile the model
    model_simple = Deep_Hedging_Model(N=N, d=d+2, m=m, risk_free=risk_free, \
              dt = dt, strategy_type="simple", epsilon = epsilon, \
              use_batch_norm = use_batch_norm, kernel_initializer = kernel_initializer, \
              activation_dense = activation_dense, activation_output = activation_output, \
              final_period_cost = final_period_cost, delta_constraint = delta_constraint, \
              share_stretegy_across_time = share_stretegy_across_time, \
              cost_structure = cost_structure)
    loss = CVaR(model_simple.output,None,loss_param)
    model_simple.add_loss(loss)
    
    model_simple.compile(optimizer=optimizer)
    
    early_stopping = EarlyStopping(monitor="loss", \
              patience=10, min_delta=1e-4, restore_best_weights=True)
    reduce_lr = ReduceLROnPlateau(monitor="loss", \
              factor=0.5, patience=2, min_delta=1e-3, verbose=0)
    
    callbacks = [early_stopping, reduce_lr]
    
    # Fit the model.
    model_simple.fit(x=xtrain, batch_size=batch_size, epochs=epochs, \
              validation_split=0.1, verbose=1)
    
    #clear_output()
    
    print("Finished running deep hedging algorithm! (Simple Network)")
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    [<ipython-input-68-f4e901423d20>](https://localhost:8080/#) in <module>()
          3 # Setup and compile the model
          4 model_simple = Deep_Hedging_Model(N=N, d=d+2, m=m, risk_free=risk_free,           dt = dt, strategy_type="simple", epsilon = epsilon,           use_batch_norm = use_batch_norm, kernel_initializer = kernel_initializer,           activation_dense = activation_dense, activation_output = activation_output,           final_period_cost = final_period_cost, delta_constraint = delta_constraint,           share_stretegy_across_time = share_stretegy_across_time,           cost_structure = cost_structure)
    ----> 5 loss = CVaR(model_simple.output,None,loss_param)
          6 model_simple.add_loss(loss)
          7 
    
    3 frames
    [/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py](https://localhost:8080/#) in error_handler(*args, **kwargs)
         65     except Exception as e:  # pylint: disable=broad-except
         66       filtered_tb = _process_traceback_frames(e.__traceback__)
    ---> 67       raise e.with_traceback(filtered_tb) from None
         68     finally:
         69       del filtered_tb
    
    ValueError: Exception encountered when calling layer "tf.math.subtract_2" (type TFOpLambda).
    
    Tried to convert 'y' to a tensor and failed. Error: None values not supported.
    
    Call arguments received:
      • x=tf.Tensor(shape=(None, 1), dtype=float32)
      • y=None
      • nam
    3 frames
    [/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py](https://localhost:8080/#) in error_handler(*args, **kwargs)
         65     except Exception as e:  # pylint: disable=broad-except
         66       filtered_tb = _process_traceback_frames(e.__traceback__)
    ---> 67       raise e.with_traceback(filtered_tb) from None
         68     finally:
         69       del filtered_tb
    
    ValueError: Exception encountered when calling layer "tf.math.subtract_2" (type TFOpLambda).
    
    Tried to convert 'y' to a tensor and failed. Error: None values not supported.
    
    Call arguments received:
      • x=tf.Tensor(shape=(None, 1), dtype=float32)
      • y=None
      • name=None
    ```e=None
    
    opened by NikolausKCL 0
  • Error in Google Colab Notebook

    Error in Google Colab Notebook

    Hello

    I am encountering the following error when attempting to run the notebook in Google Colab

    
    
    #@title <font color='Blue'>**Run the Deep Hedging Algorithm (Simple Network)!**</font>
    %autoreload 2
    
    optimizer = Adam(learning_rate=lr)
    
    # Setup and compile the model
    model_simple = Deep_Hedging_Model(N=N, d=d+2, m=m, risk_free=risk_free, \
              dt = dt, strategy_type="simple", epsilon = epsilon, \
              use_batch_norm = use_batch_norm, kernel_initializer = kernel_initializer, \
              activation_dense = activation_dense, activation_output = activation_output, \
              final_period_cost = final_period_cost, delta_constraint = delta_constraint, \
              share_stretegy_across_time = share_stretegy_across_time, \
              cost_structure = cost_structure)
    loss = Entropy(model_simple.output,None,loss_param)
    model_simple.add_loss(loss)
    
    model_simple.compile(optimizer=optimizer)
    
    early_stopping = EarlyStopping(monitor="loss", \
              patience=10, min_delta=1e-4, restore_best_weights=True)
    reduce_lr = ReduceLROnPlateau(monitor="loss", \
              factor=0.5, patience=2, min_delta=1e-3, verbose=0)
    
    callbacks = [early_stopping, reduce_lr]
    
    # Fit the model.
    model_simple.fit(x=xtrain, batch_size=batch_size, epochs=epochs, \
              validation_data=xtest, verbose=1)
    
    clear_output()
    
    print("Finished running deep hedging algorithm! (Simple Network)")
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-93bd618dd0f0> in <module>()
         17 
         18 # Fit the model.
    ---> 19 model_simple.fit(x=xtrain, batch_size=batch_size, epochs=epochs,           validation_data=xtest, verbose=1)
         20 
         21 clear_output()
    
    1 frames
    /usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
         65     except Exception as e:  # pylint: disable=broad-except
         66       filtered_tb = _process_traceback_frames(e.__traceback__)
    ---> 67       raise e.with_traceback(filtered_tb) from None
         68     finally:
         69       del filtered_tb
    
    /usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py in unpack_x_y_sample_weight(data)
       1579     error_msg = ("Data is expected to be in format `x`, `(x,)`, `(x, y)`, "
       1580                  "or `(x, y, sample_weight)`, found: {}").format(data)
    -> 1581     raise ValueError(error_msg)
       1582 
       1583 
    
    ValueError: Data is expected to be in format `x`, `(x,)`, `(x, y)`, or `(x, y, sample_weight)`, found: (array([[100.
    
    opened by armarion 3
Owner
Yu Man Tam
Yu Man Tam
A tool to analyze leveraged liquidity mining and find optimal option combination for hedging.

LP-Option-Hedging Description A Python program to analyze leveraged liquidity farming/mining and find the optimal option combination for hedging imper

Aureliano 18 Dec 19, 2022
ONNX Runtime Web demo is an interactive demo portal showing real use cases running ONNX Runtime Web in VueJS.

ONNX Runtime Web demo is an interactive demo portal showing real use cases running ONNX Runtime Web in VueJS. It currently supports four examples for you to quickly experience the power of ONNX Runtime Web.

Microsoft 58 Dec 18, 2022
Neon-erc20-example - Example of creating SPL token and wrapping it with ERC20 interface in Neon EVM

Example of wrapping SPL token by ERC2-20 interface in Neon Requirements Install

null 7 Mar 28, 2022
Example-custom-ml-block-keras - Custom Keras ML block example for Edge Impulse

Custom Keras ML block example for Edge Impulse This repository is an example on

Edge Impulse 8 Nov 2, 2022
Python-kafka-reset-consumergroup-offset-example - Python Kafka reset consumergroup offset example

Python Kafka reset consumergroup offset example This is a simple example of how

Willi Carlsen 1 Feb 16, 2022
Machine Learning From Scratch. Bare bones NumPy implementations of machine learning models and algorithms with a focus on accessibility. Aims to cover everything from linear regression to deep learning.

Machine Learning From Scratch About Python implementations of some of the fundamental Machine Learning models and algorithms from scratch. The purpose

Erik Linder-Norén 21.8k Jan 9, 2023
RealTime Emotion Recognizer for Machine Learning Study Jam's demo

Emotion recognizer Table of contents Clone project Dataset Install dependencies Main program Demo 1. Clone project git clone https://github.com/GDSC20

Google Developer Student Club - UIT 1 Oct 5, 2021
Example how to deploy deep learning model with aiohttp.

aiohttp-demos Demos for aiohttp project. Contents Imagetagger Deep Learning Image Classifier URL shortener Toxic Comments Classifier Moderator Slack B

aio-libs 661 Jan 4, 2023
A Python Automated Machine Learning tool that optimizes machine learning pipelines using genetic programming.

Master status: Development status: Package information: TPOT stands for Tree-based Pipeline Optimization Tool. Consider TPOT your Data Science Assista

Epistasis Lab at UPenn 8.9k Dec 30, 2022
Scripts of Machine Learning Algorithms from Scratch. Implementations of machine learning models and algorithms using nothing but NumPy with a focus on accessibility. Aims to cover everything from basic to advance.

Algo-ScriptML Python implementations of some of the fundamental Machine Learning models and algorithms from scratch. The goal of this project is not t

Algo Phantoms 81 Nov 26, 2022
An implementation demo of the ICLR 2021 paper Neural Attention Distillation: Erasing Backdoor Triggers from Deep Neural Networks in PyTorch.

Neural Attention Distillation This is an implementation demo of the ICLR 2021 paper Neural Attention Distillation: Erasing Backdoor Triggers from Deep

Yige-Li 84 Jan 4, 2023
Vowpal Wabbit is a machine learning system which pushes the frontier of machine learning with techniques such as online, hashing, allreduce, reductions, learning2search, active, and interactive learning.

This is the Vowpal Wabbit fast online learning code. Why Vowpal Wabbit? Vowpal Wabbit is a machine learning system which pushes the frontier of machin

Vowpal Wabbit 8.1k Jan 6, 2023
Codes for realizing theories learned from Data Mining, Machine Learning, Deep Learning without using the present Python packages.

Codes-for-Algorithms Codes for realizing theories learned from Data Mining, Machine Learning, Deep Learning without using the present Python packages.

Tracy (Shengmin) Tao 1 Apr 12, 2022
PyTorch implementation of the Deep SLDA method from our CVPRW-2020 paper "Lifelong Machine Learning with Deep Streaming Linear Discriminant Analysis"

Lifelong Machine Learning with Deep Streaming Linear Discriminant Analysis This is a PyTorch implementation of the Deep Streaming Linear Discriminant

Tyler Hayes 41 Dec 25, 2022
Deep learning (neural network) based remote photoplethysmography: how to extract pulse signal from video using deep learning tools

Deep-rPPG: Camera-based pulse estimation using deep learning tools Deep learning (neural network) based remote photoplethysmography: how to extract pu

Terbe Dániel 138 Dec 17, 2022
deep-table implements various state-of-the-art deep learning and self-supervised learning algorithms for tabular data using PyTorch.

deep-table implements various state-of-the-art deep learning and self-supervised learning algorithms for tabular data using PyTorch.

null 63 Oct 17, 2022
Demo project for real time anomaly detection using kafka and python

kafkaml-anomaly-detection Project for real time anomaly detection using kafka and python It's assumed that zookeeper and kafka are running in the loca

Rodrigo Arenas 36 Dec 12, 2022
Working demo of the Multi-class and Anomaly classification model using the CLIP feature space

??️ Hindsight AI: Crime Classification With Clip About For Educational Purposes Only This is a recursive neural net trained to classify specific crime

Miles Tweed 2 Jun 5, 2022