Skip to content

Commit

Permalink
Release/0.14.2 (#404)
Browse files Browse the repository at this point in the history
* MAINT: Fix default params in CIR and Heston stochastic process (#401) (Thank you, @masanorihirano !)

* TEST: Add and refactor tests (#400) (#394)

* DOC: Clean documentation (#396)

* DOC: Add `example_heston_iv.py` (#403)

Co-authored-by: Masanori HIRANO <masanorihirano@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: GitHub Actions <action@github.com>
  • Loading branch information
4 people committed Nov 23, 2021
1 parent a14df77 commit 4d7ff17
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 127 deletions.
50 changes: 31 additions & 19 deletions docs/source/nn.functional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,42 @@ pfhedge.nn.functional
Payoff Functions
----------------

.. autofunction:: european_payoff
.. autofunction:: lookback_payoff
.. autofunction:: american_binary_payoff
.. autofunction:: european_binary_payoff
.. autosummary::
:nosignatures:
:toctree: generated

european_payoff
lookback_payoff
american_binary_payoff
european_binary_payoff

Criterion Functions
-------------------

.. autofunction:: exp_utility
.. autofunction:: isoelastic_utility
.. autofunction:: entropic_risk_measure
.. autofunction:: expected_shortfall
.. autofunction:: value_at_risk
.. autosummary::
:nosignatures:
:toctree: generated

exp_utility
isoelastic_utility
entropic_risk_measure
expected_shortfall
value_at_risk

Other Functions
-----------------

.. autofunction:: leaky_clamp
.. autofunction:: clamp
.. autofunction:: topp
.. autofunction:: realized_variance
.. autofunction:: realized_volatility
.. autofunction:: terminal_value
.. autofunction:: ncdf
.. autofunction:: npdf
.. autofunction:: d1
.. autofunction:: d2
.. autosummary::
:nosignatures:
:toctree: generated

leaky_clamp
clamp
topp
realized_variance
realized_volatility
terminal_value
ncdf
npdf
d1
d2
4 changes: 2 additions & 2 deletions docs/source/nn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
pfhedge.nn
==========

`pfhedge.nn` provides `torch.nn.Module` that are useful for Deep Hedging.
`pfhedge.nn` provides :class:`torch.nn.Module` that are useful for Deep Hedging.

See `PyTorch Documentation <https://pytorch.org/docs/stable/generated/torch.nn.Module.html>`_
for general usage of `torch.nn.Module`.
for general usage of :class:`torch.nn.Module`.

.. currentmodule:: pfhedge

Expand Down
20 changes: 16 additions & 4 deletions docs/source/stochastic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@ pfhedge.stochastic
Brownian Motion
---------------

.. autofunction:: generate_brownian
.. autofunction:: generate_geometric_brownian
.. autosummary::
:nosignatures:
:toctree: generated

generate_brownian
generate_geometric_brownian

Cox-Ingersoll-Ross Process
--------------------------

.. autofunction:: generate_cir
.. autosummary::
:nosignatures:
:toctree: generated

generate_cir

Heston Process
--------------

.. autofunction:: generate_heston
.. autosummary::
:nosignatures:
:toctree: generated

generate_heston
43 changes: 43 additions & 0 deletions examples/example_heston_iv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sys
from math import exp

import matplotlib.pyplot as plt
import torch

sys.path.append("..")
from pfhedge.instruments import EuropeanOption
from pfhedge.instruments import HestonStock
from pfhedge.nn import BlackScholes

LOG_MONEYNESS_MIN = -0.1
LOG_MONEYNESS_MAX = 0.1
LOG_MONEYNESS_STEPS = 20
LOG_MONEYNESS_RANGE = torch.linspace(
LOG_MONEYNESS_MIN, LOG_MONEYNESS_MAX, LOG_MONEYNESS_STEPS
)


def compute_iv(log_moneyness: float, rho: float) -> float:
torch.manual_seed(42)

d = EuropeanOption(HestonStock(rho=rho))
spot = exp(log_moneyness) * d.strike
d.simulate(n_paths=int(1e5), init_state=(spot, d.ul().theta))
p = d.payoff().mean(0)

return BlackScholes(d).implied_volatility(log_moneyness, d.maturity, p).item()


def main():
plt.figure()
for rho in [-0.7, 0.0, 0.7]:
y = [compute_iv(s, rho) for s in LOG_MONEYNESS_RANGE]
plt.plot(LOG_MONEYNESS_RANGE, y, label=f"rho={rho}")
plt.xlabel("Log moneyness")
plt.ylabel("Implied volatility")
plt.legend()
plt.savefig("output/heston-iv.png")


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
matplotlib
tqdm
33 changes: 15 additions & 18 deletions pfhedge/instruments/derivative/american_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class AmericanBinaryOption(BaseOption):
"""An American binary Option.
r"""American binary Option.
An American binary call option pays an unit amount of cash if and only if
the maximum of the underlying asset's price until maturity is equal or greater
Expand All @@ -27,27 +27,25 @@ class AmericanBinaryOption(BaseOption):
The payoff of an American binary call option is given by:
.. math::
\mathrm{payoff} =
\begin{cases}
1 & (\mathrm{Max} \geq K) \\
0 & (\text{otherwise})
\end{cases}
\\mathrm{payoff} =
\\begin{cases}
1 & (\\mathrm{Max} \\geq K) \\\\
0 & (\\text{otherwise})
\\end{cases}
Here, :math:`\\mathrm{Max}` is the maximum of the underlying asset's price
Here, :math:`\mathrm{Max}` is the maximum of the underlying asset's price
until maturity and :math:`K` is the strike price (`strike`) of the option.
The payoff of an American binary put option is given by:
.. math::
\mathrm{payoff} =
\begin{cases}
1 & (\mathrm{Min} \leq K) \\
0 & (\text{otherwise})
\end{cases}
\\mathrm{payoff} =
\\begin{cases}
1 & (\\mathrm{Min} \\leq K) \\\\
0 & (\\text{otherwise})
\\end{cases}
Here, :math:`\\mathrm{Min}` is the minimum of the underlying asset's price.
Here, :math:`\mathrm{Min}` is the minimum of the underlying asset's price.
.. seealso::
:func:`pfhedge.nn.functional.american_binary_payoff`: Payoff function.
Expand All @@ -64,14 +62,13 @@ class AmericanBinaryOption(BaseOption):
device (torch.device): The device where the simulated time-series are.
Examples:
>>> import torch
>>> from pfhedge.instruments import BrownianStock
>>> from pfhedge.instruments import AmericanBinaryOption
>>>
>>> _ = torch.manual_seed(42)
>>> derivative = AmericanBinaryOption(BrownianStock(), \
maturity=5/250, strike=1.01)
>>> derivative = AmericanBinaryOption(
... BrownianStock(), maturity=5/250, strike=1.01)
>>> derivative.simulate(n_paths=2)
>>> derivative.underlier.spot
tensor([[1.0000, 1.0016, 1.0044, 1.0073, 0.9930, 0.9906],
Expand Down
5 changes: 1 addition & 4 deletions pfhedge/instruments/derivative/european.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class EuropeanOption(BaseOption):
r"""A European option.
r"""European option.
A European option provides its holder the right to buy (for call option)
or sell (for put option) an underlying asset with the strike price
Expand All @@ -23,7 +23,6 @@ class EuropeanOption(BaseOption):
The payoff of a European call option is given by:
.. math::
\mathrm{payoff} = \max(S - K, 0)
Here, :math:`S` is the underlying asset's price at maturity and
Expand All @@ -32,7 +31,6 @@ class EuropeanOption(BaseOption):
The payoff of a European put option is given by:
.. math::
\mathrm{payoff} = \max(K - S, 0)
.. seealso::
Expand All @@ -50,7 +48,6 @@ class EuropeanOption(BaseOption):
device (torch.device): The device where the simulated time-series are.
Examples:
>>> import torch
>>> from pfhedge.instruments import BrownianStock
>>> from pfhedge.instruments import EuropeanOption
Expand Down
25 changes: 11 additions & 14 deletions pfhedge/instruments/derivative/european_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class EuropeanBinaryOption(BaseOption):
"""A European binary option.
r"""European binary option.
An American binary call option pays an unit amount of cash if and only if
the underlying asset's price at maturity is equal or greater than the strike price.
Expand All @@ -25,25 +25,23 @@ class EuropeanBinaryOption(BaseOption):
The payoff of an American binary call option is given by:
.. math::
\\mathrm{payoff} =
\\begin{cases}
1 & (S \\geq K) \\\\
0 & (\\text{otherwise})
\\end{cases}
\mathrm{payoff} =
\begin{cases}
1 & (S \geq K) \\
0 & (\text{otherwise})
\end{cases}
with :math:`S` being the underlying asset's price at maturity and
:math:`K` being the strike price (`strike`) of the option
The payoff of an American binary put option is given by:
.. math::
\\mathrm{payoff} =
\\begin{cases}
1 & (S \\leq K) \\\\
0 & (\\text{otherwise})
\\end{cases}
\mathrm{payoff} =
\begin{cases}
1 & (S \leq K) \\
0 & (\text{otherwise})
\end{cases}
.. seealso::
:func:`pfhedge.nn.functional.european_binary_payoff`: Payoff function.
Expand All @@ -60,7 +58,6 @@ class EuropeanBinaryOption(BaseOption):
device (torch.device): The device where the simulated time-series are.
Examples:
>>> import torch
>>> from pfhedge.instruments import BrownianStock
>>> from pfhedge.instruments import EuropeanBinaryOption
Expand Down
13 changes: 5 additions & 8 deletions pfhedge/instruments/derivative/lookback.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class LookbackOption(BaseOption):
"""A lookback option with fixed strike.
r"""Lookback option with fixed strike.
A lookback call option provides its holder the right to buy an underlying with
the strike price and to sell with the highest price until the date of maturity.
Expand All @@ -25,19 +25,17 @@ class LookbackOption(BaseOption):
The payoff of a lookback call option is given by:
.. math::
\mathrm{payoff} = \max(\mathrm{Max} - K, 0)
\\mathrm{payoff} = \\max(\\mathrm{Max} - K, 0)
Here, :math:`\\mathrm{Max}` is the maximum of the underlying asset's price
Here, :math:`\mathrm{Max}` is the maximum of the underlying asset's price
until maturity and :math:`K` is the strike price (`strike`) of the option.
The payoff of a lookback put option is given by:
.. math::
\mathrm{payoff} = \max(K - \mathrm{Min}, 0)
\\mathrm{payoff} = \\max(K - \\mathrm{Min}, 0)
Here, :math:`\\mathrm{Min}` is the minimum of the underlying asset's price.
Here, :math:`\mathrm{Min}` is the minimum of the underlying asset's price.
.. seealso::
:func:`pfhedge.nn.functional.lookback_payoff`: Payoff function.
Expand All @@ -54,7 +52,6 @@ class LookbackOption(BaseOption):
device (torch.device): The device where the simulated time-series are.
Examples:
>>> import torch
>>> from pfhedge.instruments import BrownianStock
>>> from pfhedge.instruments import LookbackOption
Expand Down
7 changes: 3 additions & 4 deletions pfhedge/instruments/derivative/variance_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class VarianceSwap(BaseDerivative):
"""A variance swap.
r"""Variance swap.
A variance swap pays cash in the amount of the realized variance
until the maturity and levies the cash of the strike variance.
Expand All @@ -22,9 +22,9 @@ class VarianceSwap(BaseDerivative):
.. math::
\\mathrm{payoff} = \\sigma^2 - K
\mathrm{payoff} = \sigma^2 - K
where :math:`\\sigma^2` is the realized variance of the underlying asset
where :math:`\sigma^2` is the realized variance of the underlying asset
until maturity and :math:`K` is the strike variance (``strike``).
See :func:`pfhedge.nn.functional.realized_variance` for the definition of
the realized variance.
Expand All @@ -40,7 +40,6 @@ class VarianceSwap(BaseDerivative):
device (torch.device): The device where the simulated time-series are.
Examples:
>>> import torch
>>> from pfhedge.nn.functional import realized_variance
>>> from pfhedge.instruments import BrownianStock
Expand Down
3 changes: 1 addition & 2 deletions pfhedge/nn/modules/bs/black_scholes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class BlackScholes(Module):
"""Initialize Black-Scholes formula module from a derivative.
"""Creates Black-Scholes formula module from a derivative.
The ``forward`` method returns the Black-Scholes delta.
Expand All @@ -28,7 +28,6 @@ class BlackScholes(Module):
all but the last dimension are the same shape as the input.
Examples:
One can instantiate Black-Scholes module by using a derivative.
For example, one can instantiate :class:`BSEuropeanOption` using
a :class:`pfhedge.instruments.EuropeanOption`.
Expand Down
Loading

0 comments on commit 4d7ff17

Please sign in to comment.