Skip to content

Commit

Permalink
Release/0.7.5 (#187)
Browse files Browse the repository at this point in the history
* BIG: Fix invalid time series of CIR process (close #182) (#186)

* DOC: Add missing dt to generate_cir (#186)

* DOC: Update an example in README.md (#181)

Co-authored-by: GitHub Actions <action@github.com>
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 Aug 13, 2021
1 parent 9434de4 commit 743196e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ The `hedger` is also a [`Module`](https://pytorch.org/docs/stable/generated/torc
```py
hedger
# Hedger(
# inputs=['log_moneyness', 'expiry_time', 'volatility', 'prev_hedge'],
# inputs=['log_moneyness', 'expiry_time', 'volatility', 'prev_hedge']
# (model): MultiLayerPerceptron(
# (0): LazyLinear(in_features=None, out_features=32, bias=True)
# (0): LazyLinear(in_features=0, out_features=32, bias=True)
# (1): ReLU()
# (2): LazyLinear(in_features=None, out_features=32, bias=True)
# (2): Linear(in_features=32, out_features=32, bias=True)
# (3): ReLU()
# (4): LazyLinear(in_features=None, out_features=32, bias=True)
# (4): Linear(in_features=32, out_features=32, bias=True)
# (5): ReLU()
# (6): LazyLinear(in_features=None, out_features=32, bias=True)
# (6): Linear(in_features=32, out_features=32, bias=True)
# (7): ReLU()
# (8): LazyLinear(in_features=None, out_features=1, bias=True)
# (8): Linear(in_features=32, out_features=1, bias=True)
# (9): Identity()
# )
# (criterion): EntropicRiskMeasure()
Expand Down
8 changes: 4 additions & 4 deletions pfhedge/instruments/primary/heston.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class HestonStock(Primary):
>>> stock = HestonStock()
>>> stock.simulate(n_paths=2, time_horizon=5/250)
>>> stock.spot
tensor([[1.0000, 0.9958, 0.9940, 0.9895, 0.9765],
[1.0000, 1.0064, 1.0117, 1.0116, 1.0117]])
tensor([[1.0000, 0.9953, 0.9929, 0.9880, 0.9744],
[1.0000, 1.0043, 0.9779, 0.9770, 0.9717]])
>>> stock.variance
tensor([[0.0400, 0.0433, 0.0406, 0.0423, 0.0441],
[0.0400, 0.0251, 0.0047, 0.0000, 0.0000]])
tensor([[0.0400, 0.0445, 0.0437, 0.0458, 0.0479],
[0.0400, 0.0314, 0.0955, 0.0683, 0.0799]])
"""

def __init__(
Expand Down
13 changes: 6 additions & 7 deletions pfhedge/stochastic/cir.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def generate_cir(
.. math ::
dX(t) = \\kappa (\\theta - X(t)) + \\sigma \\sqrt{X(t)} dW(t) \\,.
dX(t) = \\kappa (\\theta - X(t)) dt + \\sigma \\sqrt{X(t)} dW(t) \\,.
Time-series is generated by Andersen's QE-M method (See Reference for details).
Expand Down Expand Up @@ -53,8 +53,8 @@ def generate_cir(
>>>
>>> _ = torch.manual_seed(42)
>>> generate_cir(2, 5)
tensor([[0.0400, 0.0433, 0.0406, 0.0423, 0.0441],
[0.0400, 0.0251, 0.0047, 0.0000, 0.0000]])
tensor([[0.0400, 0.0445, 0.0437, 0.0458, 0.0479],
[0.0400, 0.0314, 0.0955, 0.0683, 0.0799]])
References:
- Andersen, Leif B.G., Efficient Simulation of the Heston Stochastic
Expand Down Expand Up @@ -83,10 +83,9 @@ def generate_cir(
# Compute m, s, psi: Eq(17,18)
exp = (-tensor_kappa * tensor_dt).exp()
m = tensor_theta + (v - tensor_theta) * exp
s2 = sum(
v * (tensor_sigma ** 2) * exp * (1 - exp) / tensor_kappa,
tensor_theta * (tensor_sigma ** 2) * ((1 - exp) ** 2) / (2 * tensor_kappa),
)
s2 = v * (tensor_sigma ** 2) * exp * (1 - exp) / tensor_kappa + tensor_theta * (
tensor_sigma ** 2
) * ((1 - exp) ** 2) / (2 * tensor_kappa)
psi = s2 / (m ** 2 + EPSILON)

# Compute V(t + dt) where psi <= PSI_CRIT: Eq(23, 27, 28)
Expand Down
20 changes: 10 additions & 10 deletions pfhedge/stochastic/heston.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ def generate_heston(
>>> _ = torch.manual_seed(42)
>>> spot, variance = generate_heston(2, 5)
>>> spot
tensor([[1.0000, 0.9958, 0.9940, 0.9895, 0.9765],
[1.0000, 1.0064, 1.0117, 1.0116, 1.0117]])
tensor([[1.0000, 0.9953, 0.9929, 0.9880, 0.9744],
[1.0000, 1.0043, 0.9779, 0.9770, 0.9717]])
>>> variance
tensor([[0.0400, 0.0433, 0.0406, 0.0423, 0.0441],
[0.0400, 0.0251, 0.0047, 0.0000, 0.0000]])
tensor([[0.0400, 0.0445, 0.0437, 0.0458, 0.0479],
[0.0400, 0.0314, 0.0955, 0.0683, 0.0799]])
References:
- Andersen, Leif B.G., Efficient Simulation of the Heston Stochastic
Expand Down Expand Up @@ -107,12 +107,12 @@ def generate_heston(
k4 = GAMMA2 * dt * (1 - rho ** 2)
v0 = variance[:, i_step]
v1 = variance[:, i_step + 1]
log_spot[:, i_step + 1] = sum(
(
log_spot[:, i_step],
k0 + k1 * v0 + k2 * v1,
(k3 * v0 + k4 * v1).sqrt() * randn[:, i_step],
)
log_spot[:, i_step + 1] = (
log_spot[:, i_step]
+ k0
+ k1 * v0
+ k2 * v1
+ (k3 * v0 + k4 * v1).sqrt() * randn[:, i_step]
)

return (log_spot.exp(), variance)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pfhedge"
version = "0.7.4"
version = "0.7.5"
description = "Deep Hedging in PyTorch"
authors = ["Shota Imaki <shota.imaki.0801@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit 743196e

Please sign in to comment.