-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ENH: Add `pfhedge.__version__` support (#514) * ENH: Add Black-Scholes formulas as functional (#489) (#506) * ENH: Add `end_index` to forward start payoff functional (#518) * ENH: Add `clauses`, `named_clauses` to derivative (#520) * ENH: implicit args for Black-Scholes modules (#516) * ENH: Add bilinear interpolation function `bilerp` (close #523) (#527) * ENH: Add `.float16()`, `.float32(), `.float64()` to Instrument (#524) * BUG: Stop assigning arbitrary strike to autogreek.delta (#517) * DOC: Update functional documentations (#508) * DOC: Add note on discrete/continuous monitoring (#513) * DOC: Add note on adding clause (#515) * DOC: Elaborate documentation on payoff (#519) * MAINT: Refactor BlackScholes module using factory (close #509) (#510) * MAINT: Miscellaneous refactoring (#507) (#521) (#525) * CHORE: Run Publish action on release (#504) * Bumping version from 0.17.0 to 0.18.0 (#532) Co-authored-by: Masanori HIRANO <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5c71d6f
commit 0460355
Showing
43 changed files
with
3,481 additions
and
465 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
name: Publish | ||
|
||
on: | ||
push: | ||
branches: main | ||
release: | ||
types: [published] | ||
workflow_dispatch: | ||
|
||
jobs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
Adding clause to derivative | ||
=========================== | ||
|
||
One can customize a derivative by registering additional clauses; | ||
see :func:`pfhedge.instruments.BaseDerivative.add_clause`. | ||
|
||
Let us see how to add clauses to derivatives by taking European capped call option as an example. | ||
This option is a variant of a European option, which is given by | ||
|
||
.. code:: python | ||
>>> from pfhedge.instruments import BrownianStock | ||
>>> from pfhedge.instruments import EuropeanOption | ||
... | ||
>>> strike = 1.0 | ||
>>> maturity = 1.0 | ||
>>> stock = BrownianStock() | ||
>>> european = EuropeanOption(stock, strike=strike, maturity=maturity) | ||
The capped call associates ''a barrier clause''; | ||
if the underlier's spot reaches the barrier price :math:`B`, | ||
being greater than the strike :math:`K` and the spot at inception, | ||
the option immediately expires and pays off its intrinsic value at that moment :math:`B - K`. | ||
|
||
This clause can be registered to a derivative as follows: | ||
|
||
.. code:: python | ||
>>> def cap_clause(derivative, payoff): | ||
... barrier = 1.4 | ||
... max_spot = derivative.ul().spot.max(-1).values | ||
... capped_payoff = torch.full_like(payoff, barrier - strike) | ||
... return torch.where(max_spot < barrier, payoff, capped_payoff) | ||
... | ||
>>> capped_european = EuropeanOption(stock, strike=strike, maturity=maturity) | ||
>>> capped_european.add_clause("cap_clause", cap_clause) | ||
The method ``add_clause`` adds the clause and its name to the derivative. | ||
Here the function ``cap_caluse`` represents the clause to modify the payoff depending on the state of the derivative. | ||
The clause function should have the signature ``clause(derivative, payoff) -> modified payoff``. | ||
|
||
The payoff would be capped as intended: | ||
|
||
.. code:: python | ||
>>> n_paths = 100000 | ||
>>> capped_european.simulate(n_paths=n_paths) | ||
>>> european.payoff().max() | ||
>>> # 1.2... | ||
>>> capped_european.payoff().max() | ||
>>> # 0.4 | ||
The price of the capped European call option can be evaluated by using a European option as a control variates. | ||
|
||
.. code:: python | ||
>>> from math import sqrt | ||
... | ||
>>> payoff_european = european.payoff() | ||
>>> payoff_capped_european = capped_european.payoff() | ||
>>> bs_price = BlackScholes(european).price(0.0, european.maturity, stock.sigma).item() | ||
>>> price = bs_price + (payoff_capped_european - payoff_european).mean().item() | ||
>>> error = (payoff_capped_european - payoff_european).std().item() / sqrt(n_paths) | ||
>>> bs_price | ||
>>> # 0.07967... | ||
>>> price | ||
>>> # 0.07903... | ||
>>> error | ||
>>> # 0.00012... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import sys | ||
|
||
sys.path.append("..") | ||
|
||
from math import sqrt | ||
|
||
import torch | ||
|
||
from pfhedge.instruments import BrownianStock | ||
from pfhedge.instruments import EuropeanOption | ||
from pfhedge.nn import BlackScholes | ||
|
||
|
||
def main(): | ||
torch.manual_seed(42) | ||
|
||
strike = 1.0 | ||
maturity = 1.0 | ||
stock = BrownianStock() | ||
european = EuropeanOption(stock, strike=strike, maturity=maturity) | ||
|
||
def cap_clause(derivative, payoff): | ||
barrier = 1.4 | ||
max_spot = derivative.ul().spot.max(-1).values | ||
capped_payoff = torch.full_like(payoff, barrier - strike) | ||
return torch.where(max_spot < barrier, payoff, capped_payoff) | ||
|
||
capped_european = EuropeanOption(stock, strike=strike, maturity=maturity) | ||
capped_european.add_clause("cap_clause", cap_clause) | ||
|
||
n_paths = 100000 | ||
capped_european.simulate(n_paths=n_paths) | ||
|
||
payoff_european = european.payoff() | ||
payoff_capped_european = capped_european.payoff() | ||
|
||
max_spot = payoff_european.max().item() | ||
capped_max_spot = payoff_capped_european.max().item() | ||
print("Max payoff of vanilla European:", max_spot) | ||
print("Max payoff of capped European:", capped_max_spot) | ||
|
||
# Price using control variates | ||
bs_price = BlackScholes(european).price(0.0, european.maturity, stock.sigma).item() | ||
value0 = payoff_capped_european.mean().item() | ||
value1 = bs_price + (payoff_capped_european - payoff_european).mean().item() | ||
error0 = payoff_capped_european.std().item() / sqrt(n_paths) | ||
error1 = (payoff_capped_european - payoff_european).std().item() / sqrt(n_paths) | ||
|
||
print("BS price of vanilla European:", bs_price) | ||
print("Price of capped European without control variates:", value0) | ||
print("Price of capped European with control variates:", value1) | ||
print("Error of capped European without control variates:", error0) | ||
print("Error of capped European with control variates:", error1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Users can import Hedger either as `from pfhedge import Hedger` or | ||
# `from pfhedge.nn import Hedger` | ||
from pfhedge.nn import Hedger | ||
|
||
from .version import __version__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.