Skip to content

Commit 55d6b2d

Browse files
committed
make sample abstract
1 parent ceb81d4 commit 55d6b2d

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

quantflow/sp/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class StochasticProcess(BaseModel, ABC):
1919
Base class for stochastic processes in continuous time
2020
"""
2121

22+
@abstractmethod
2223
def sample(self, n: int, t: float = 1, steps: int = 0) -> np.ndarray:
2324
"""Generate random paths from the process
2425
2526
:param n: Number of paths
2627
:param t: time horizon
2728
:param steps: number of time steps to arrive at horizon
2829
"""
29-
raise NotImplementedError
3030

3131
def sample_dt(self, t: float, steps: int = 0) -> Tuple[int, float]:
3232
"""Time delta for sampling paths

quantflow/sp/heston.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ def characteristic(self, t: float, u: Vector) -> Vector:
4848
a = theta_kappa * (2 * np.log(c) + (gamma - kappa) * t) / eta2
4949
return np.exp(-a - b * self.variance_process.rate)
5050

51-
def pdf(self, t: float, n: Vector) -> Vector:
52-
raise NotImplementedError
53-
54-
def cdf(self, t: float, n: Vector) -> Vector:
51+
def sample(self, n: int, t: float = 1, steps: int = 0) -> np.ndarray:
52+
# TODO: implement
5553
raise NotImplementedError

quantflow/sp/weiner.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from functools import lru_cache
44

55
import numpy as np
6+
from numpy.random import normal
67
from pydantic import Field
78
from scipy.stats import norm
89

@@ -32,6 +33,14 @@ def characteristic(self, t: float, u: Vector) -> Vector:
3233
su = self.sigma * u
3334
return np.exp(-0.5 * su * su * t)
3435

36+
def sample(self, n: int, t: float = 1, steps: int = 0) -> np.ndarray:
37+
time_steps, dt = self.sample_dt(t, steps)
38+
sdt = self.sigma * np.sqrt(dt)
39+
paths = np.zeros((time_steps + 1, n))
40+
for t in range(time_steps):
41+
paths[t + 1, :] = paths[t, :] + normal(scale=sdt, size=n)
42+
return paths
43+
3544

3645
class WeinerMarginal(StochasticProcess1DMarginal[WeinerProcess]):
3746
def mean(self) -> float:

tests/test_weiner.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ def test_characteristic(weiner: WeinerProcess) -> None:
1515
assert marginal.mean_from_characteristic() == 0
1616
assert marginal.std() == 0.5
1717
assert marginal.variance_from_characteristic() == pytest.approx(0.25)
18+
19+
20+
def test_sampling(weiner: WeinerProcess) -> None:
21+
paths = weiner.paths(1000, t=1, steps=1000)
22+
mean = paths.mean()
23+
assert mean[0] == 0
24+
std = paths.std()
25+
assert std[0] == 0

0 commit comments

Comments
 (0)