-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* REF: Refactor features (#111) (#113) * DOC: Add examples in docstrings (#115) * ENH: Run a placeholder forward only when a hedger is lazy (#116)
- Loading branch information
simaki
committed
Jun 19, 2021
1 parent
11c86c2
commit 0518c9a
Showing
28 changed files
with
263 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from torch.nn import Module | ||
from torch.nn.parameter import is_lazy | ||
|
||
|
||
def has_lazy(module: Module) -> bool: | ||
"""Returns `True` if a module has any `UninitializedParameter`. | ||
Args: | ||
module (torch.nn.Module): | ||
Returns: | ||
bool | ||
""" | ||
for t in module.parameters(): | ||
if is_lazy(t): | ||
return True | ||
return False |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import torch | ||
from torch import Tensor | ||
from torch.nn import Module | ||
|
||
|
||
def moneyness(i: int, derivative: "Derivative", hedger: Module = None) -> Tensor: | ||
"""Returns moneyness. | ||
Args: | ||
i (int): The index of time step. | ||
derivative (pfhedge.instruments.Derivative): | ||
hedger (pfhedge.nn.Hedger): | ||
Returns: | ||
torch.Tensor | ||
""" | ||
s = derivative.underlier.spot[..., [i]] | ||
k = derivative.strike | ||
return s / k | ||
|
||
|
||
def log_moneyness(i, derivative, hedger=None) -> Tensor: | ||
return moneyness(i, derivative=derivative).log() | ||
|
||
|
||
def expiry_time(i, derivative, hedger=None) -> Tensor: | ||
value = derivative.maturity - i * derivative.underlier.dt | ||
return torch.full_like(derivative.underlier.spot[:, :1], value) | ||
|
||
|
||
def volatility(i, derivative, hedger=None) -> Tensor: | ||
value = derivative.underlier.volatility | ||
return torch.full_like(derivative.underlier.spot[:, :1], value) | ||
|
||
|
||
def prev_hedge(i, derivative, hedger) -> Tensor: | ||
if hasattr(hedger, "prev_output"): | ||
return hedger.prev_output | ||
else: | ||
# spot: shape (N, T) | ||
return torch.zeros_like(derivative.underlier.spot[:, :1]) | ||
|
||
|
||
def barrier( | ||
i, derivative, hedger=None, threshold: float = 1.0, up: bool = True | ||
) -> Tensor: | ||
if up: | ||
# shape: (N, i) | ||
touch_threshold = derivative.underlier.spot[..., : i + 1] >= threshold | ||
else: | ||
touch_threshold = derivative.underlier.spot[..., : i + 1] <= threshold | ||
return touch_threshold.any(dim=-1, keepdim=True).to(derivative.underlier.spot) | ||
|
||
|
||
def zeros(i, derivative, hedger=None) -> Tensor: | ||
return torch.zeros_like(derivative.underlier.spot[:, :1]) | ||
|
||
|
||
def empty(i, derivative, hedger=None) -> Tensor: | ||
return torch.empty_like(derivative.underlier.spot[:, :1]) | ||
|
||
|
||
def max_moneyness(i, derivative, hedger=None) -> Tensor: | ||
m = derivative.underlier.spot[..., : i + 1].max(dim=1, keepdim=True).values | ||
k = derivative.strike | ||
return m / k | ||
|
||
|
||
def max_log_moneyness(i, derivative, hedger=None) -> Tensor: | ||
return max_moneyness(i, derivative=derivative).log() |
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
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.