Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Fix default params in cir and Heston stochastic process #401

Merged
3 commits merged into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pfhedge/stochastic/cir.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def generate_cir(
n_paths: int,
n_steps: int,
init_state: Tuple[TensorOrScalar, ...] = (0.04,),
init_state: Optional[Tuple[TensorOrScalar, ...]] = None,
kappa: TensorOrScalar = 1.0,
theta: TensorOrScalar = 0.04,
sigma: TensorOrScalar = 0.2,
Expand All @@ -37,10 +37,11 @@ def generate_cir(
Args:
n_paths (int): The number of simulated paths.
n_steps (int): The number of time steps.
init_state (tuple[torch.Tensor | float], default=(0.04,)): The initial state of
init_state (tuple[torch.Tensor | float], optional): The initial state of
the time series.
This is specified by a tuple :math:`(X(0),)`.
It also accepts a :class:`torch.Tensor` or a :class:`float`.
If ``None`` (default), it uses :math:`(\theta, )`.
kappa (torch.Tensor or float, default=1.0): The parameter :math:`\kappa`.
theta (torch.Tensor or float, default=0.04): The parameter :math:`\theta`.
sigma (torch.Tensor or float, default=2.0): The parameter :math:`\sigma`.
Expand Down Expand Up @@ -71,6 +72,9 @@ def generate_cir(
tensor([[0.0400, 0.0408, 0.0411, 0.0417, 0.0422],
[0.0400, 0.0395, 0.0452, 0.0434, 0.0446]])
"""
if init_state is None:
init_state = (theta,)

# Accept Union[float, Tensor] as well because making a tuple with a single element
# is troublesome
if isinstance(init_state, (float, Tensor)):
Expand Down
7 changes: 5 additions & 2 deletions pfhedge/stochastic/heston.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def volatility(self) -> Tensor:
def generate_heston(
n_paths: int,
n_steps: int,
init_state: Tuple[TensorOrScalar, ...] = (1.0, 0.04),
init_state: Optional[Tuple[TensorOrScalar, ...]] = None,
kappa: float = 1.0,
theta: float = 0.04,
sigma: float = 0.2,
Expand Down Expand Up @@ -62,9 +62,10 @@ def generate_heston(
Args:
n_paths (int): The number of simulated paths.
n_steps (int): The number of time steps.
init_state (tuple[torch.Tensor | float], default=(1.0,)): The initial state of
init_state (tuple[torch.Tensor | float], optional): The initial state of
the time series.
This is specified by a tuple :math:`(S(0), V(0))`.
If ``None`` (default), it uses :math:`(1.0, \\theta)`.
kappa (float, default=1.0): The parameter :math:`\\kappa`.
theta (float, default=0.04): The parameter :math:`\\theta`.
sigma (float, default=2.0): The parameter :math:`\\sigma`.
Expand Down Expand Up @@ -101,6 +102,8 @@ def generate_heston(
tensor([[0.0400, 0.0408, 0.0411, 0.0417, 0.0422],
[0.0400, 0.0395, 0.0452, 0.0434, 0.0446]])
"""
if init_state is None:
init_state = (1.0, theta)
# Cast to init_state: Tuple[Tensor, ...] with desired dtype and device
init_state = cast(Tuple[Tensor, ...], tuple(map(torch.as_tensor, init_state)))
init_state = tuple(map(lambda t: t.to(dtype=dtype, device=device), init_state))
Expand Down