samplepaths 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- samplepaths/__init__.py +96 -0
- samplepaths/base/__init__.py +11 -0
- samplepaths/base/base_process.py +52 -0
- samplepaths/base/base_sequence_process.py +10 -0
- samplepaths/base/base_time_process.py +124 -0
- samplepaths/continuous/__init__.py +37 -0
- samplepaths/continuous/bessel_process.py +87 -0
- samplepaths/continuous/brownian_bridge.py +93 -0
- samplepaths/continuous/brownian_excursion.py +99 -0
- samplepaths/continuous/brownian_meander.py +148 -0
- samplepaths/continuous/brownian_motion.py +116 -0
- samplepaths/continuous/cauchy_process.py +106 -0
- samplepaths/continuous/fractional_brownian_motion.py +58 -0
- samplepaths/continuous/gamma_process.py +162 -0
- samplepaths/continuous/geometric_brownian_motion.py +166 -0
- samplepaths/continuous/inverse_gaussian_process.py +193 -0
- samplepaths/continuous/mixed_poisson_process.py +114 -0
- samplepaths/continuous/multifractional_brownian_motion.py +128 -0
- samplepaths/continuous/poisson_process.py +114 -0
- samplepaths/continuous/squared_bessel_process.py +61 -0
- samplepaths/continuous/variance_gamma_process.py +156 -0
- samplepaths/continuous/wiener_process.py +36 -0
- samplepaths/diffusion/__init__.py +17 -0
- samplepaths/diffusion/constant_elasticity_variance_process.py +81 -0
- samplepaths/diffusion/cox_ingersoll_ross_process.py +70 -0
- samplepaths/diffusion/diffusion_process.py +175 -0
- samplepaths/diffusion/extended_vasicek_process.py +72 -0
- samplepaths/diffusion/ornstein_uhlenbeck_process.py +61 -0
- samplepaths/diffusion/vasicek_process.py +66 -0
- samplepaths/discrete/__init__.py +17 -0
- samplepaths/discrete/bernoulli_process.py +78 -0
- samplepaths/discrete/chinese_restaurant_process.py +194 -0
- samplepaths/discrete/dirichlet_process.py +97 -0
- samplepaths/discrete/markov_chain.py +131 -0
- samplepaths/discrete/moran_process.py +122 -0
- samplepaths/discrete/random_walk.py +115 -0
- samplepaths/noise/__init__.py +25 -0
- samplepaths/noise/colored/__init__.py +19 -0
- samplepaths/noise/colored/blue_noise.py +32 -0
- samplepaths/noise/colored/brownian_noise.py +18 -0
- samplepaths/noise/colored/colored_noise.py +120 -0
- samplepaths/noise/colored/pink_noise.py +32 -0
- samplepaths/noise/colored/red_noise.py +32 -0
- samplepaths/noise/colored/violet_noise.py +32 -0
- samplepaths/noise/colored/white_noise.py +32 -0
- samplepaths/noise/fractional_gaussian_noise.py +219 -0
- samplepaths/noise/gaussian_noise.py +78 -0
- samplepaths/py.typed +0 -0
- samplepaths/random.py +50 -0
- samplepaths/utils/__init__.py +48 -0
- samplepaths/utils/validation.py +176 -0
- samplepaths-0.1.0.dist-info/METADATA +159 -0
- samplepaths-0.1.0.dist-info/RECORD +55 -0
- samplepaths-0.1.0.dist-info/WHEEL +4 -0
- samplepaths-0.1.0.dist-info/licenses/LICENSE +22 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""Brownian meander process."""
|
|
2
|
+
|
|
3
|
+
from typing import override
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import numpy.typing as npt
|
|
7
|
+
from numpy.random import Generator
|
|
8
|
+
|
|
9
|
+
from ..utils.validation import check_nonnegative_number
|
|
10
|
+
from .brownian_bridge import BrownianBridge
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BrownianMeander(BrownianBridge):
|
|
14
|
+
"""Brownian meander process.
|
|
15
|
+
|
|
16
|
+
.. image:: /_static/brownian_meander.png
|
|
17
|
+
:scale: 50%
|
|
18
|
+
|
|
19
|
+
A Brownian motion conditioned such that the process is nonnegative.
|
|
20
|
+
|
|
21
|
+
Generated using method by
|
|
22
|
+
|
|
23
|
+
* Williams, David. "Decomposing the Brownian path." Bulletin of the
|
|
24
|
+
American Mathematical Society 76, no. 4 (1970): 871-873.
|
|
25
|
+
|
|
26
|
+
* Imhof, J-P. "Density factorizations for Brownian motion, meander and the
|
|
27
|
+
three-dimensional Bessel process, and applications." Journal of Applied
|
|
28
|
+
Probability 21, no. 3 (1984): 500-510.
|
|
29
|
+
|
|
30
|
+
:param t: the right hand endpoint of the time interval :math:`[0,t]`
|
|
31
|
+
for the process
|
|
32
|
+
:param rng: a custom random number generator
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
*,
|
|
38
|
+
t: float = 1.0,
|
|
39
|
+
rng: Generator | None = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
super().__init__(
|
|
42
|
+
b=0.0,
|
|
43
|
+
t=t,
|
|
44
|
+
rng=rng,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
def __str__(self) -> str:
|
|
48
|
+
return f"Brownian meander on [0, {self.t}]"
|
|
49
|
+
|
|
50
|
+
def __repr__(self) -> str:
|
|
51
|
+
return f"BrownianMeander(t={self.t})"
|
|
52
|
+
|
|
53
|
+
def _sample_brownian_meander(
|
|
54
|
+
self,
|
|
55
|
+
n: int,
|
|
56
|
+
b: float | None = None,
|
|
57
|
+
) -> npt.NDArray[np.float64]:
|
|
58
|
+
r"""Generate a Brownian meander realization.
|
|
59
|
+
|
|
60
|
+
Williams, 1970, or Imhof, 1984.
|
|
61
|
+
|
|
62
|
+
:param n: the number of increments to generate
|
|
63
|
+
:param b: the nonnegative right hand endpoint of the meander. If
|
|
64
|
+
not provided, one is randomly selected from a :math:`\sqrt{2E}`
|
|
65
|
+
random variable where :math:`E` is exponential.
|
|
66
|
+
"""
|
|
67
|
+
if b is None:
|
|
68
|
+
b = np.sqrt(2 * self.t * self.rng.exponential())
|
|
69
|
+
else:
|
|
70
|
+
check_nonnegative_number(value=b, name="Right endpoint")
|
|
71
|
+
|
|
72
|
+
self.set_times(n)
|
|
73
|
+
|
|
74
|
+
bridge_1 = self._sample_brownian_bridge(n)
|
|
75
|
+
bridge_2 = self._sample_brownian_bridge(n)
|
|
76
|
+
bridge_3 = self._sample_brownian_bridge(n)
|
|
77
|
+
|
|
78
|
+
ret = np.sqrt((b * self.times / self.t + bridge_1) ** 2 + bridge_2**2 + bridge_3**2)
|
|
79
|
+
|
|
80
|
+
return ret
|
|
81
|
+
|
|
82
|
+
def _sample_brownian_meander_at(
|
|
83
|
+
self,
|
|
84
|
+
times: npt.NDArray[np.float64],
|
|
85
|
+
b: float | None = None,
|
|
86
|
+
) -> npt.NDArray[np.float64]:
|
|
87
|
+
r"""Generate a Brownian meander realization.
|
|
88
|
+
|
|
89
|
+
Williams, 1970, or Imhof, 1984.
|
|
90
|
+
:param times: a vector of increasing time values at which to generate
|
|
91
|
+
the realization
|
|
92
|
+
:param b: the right endpoint value for :py:attr:`times` [-1]. If
|
|
93
|
+
not provided, one is randomly selected from a :math:`\sqrt{2tE}`
|
|
94
|
+
random variable where :math:`E` is exponential and :math:`t` is
|
|
95
|
+
:py:attr:`times` [-1].
|
|
96
|
+
"""
|
|
97
|
+
if b is None:
|
|
98
|
+
b = np.sqrt(2 * times[-1] * self.rng.exponential())
|
|
99
|
+
else:
|
|
100
|
+
check_nonnegative_number(value=b, name="Right endpoint")
|
|
101
|
+
|
|
102
|
+
bridge_1 = self._sample_brownian_bridge_at(times)
|
|
103
|
+
bridge_2 = self._sample_brownian_bridge_at(times)
|
|
104
|
+
bridge_3 = self._sample_brownian_bridge_at(times)
|
|
105
|
+
|
|
106
|
+
ret = np.sqrt((b * times / times[-1] + bridge_1) ** 2 + bridge_2**2 + bridge_3**2)
|
|
107
|
+
|
|
108
|
+
assert isinstance(ret, np.ndarray)
|
|
109
|
+
|
|
110
|
+
return ret
|
|
111
|
+
|
|
112
|
+
@override
|
|
113
|
+
def sample(self, n: int) -> npt.NDArray[np.float64]:
|
|
114
|
+
return self._sample_brownian_meander(n)
|
|
115
|
+
|
|
116
|
+
@override
|
|
117
|
+
def sample_at(
|
|
118
|
+
self,
|
|
119
|
+
times: npt.NDArray[np.float64],
|
|
120
|
+
) -> npt.NDArray[np.float64]:
|
|
121
|
+
return self._sample_brownian_meander_at(times)
|
|
122
|
+
|
|
123
|
+
def sample_with_endpoint(
|
|
124
|
+
self,
|
|
125
|
+
n: int,
|
|
126
|
+
b: float,
|
|
127
|
+
) -> npt.NDArray[np.float64]:
|
|
128
|
+
"""Generate a realization with a specified endpoint.
|
|
129
|
+
|
|
130
|
+
:param n: the number of increments to generate
|
|
131
|
+
:param b: the nonnegative right hand endpoint of the meander
|
|
132
|
+
:returns: the realization
|
|
133
|
+
"""
|
|
134
|
+
return self._sample_brownian_meander(n, b)
|
|
135
|
+
|
|
136
|
+
def sample_at_with_endpoint(
|
|
137
|
+
self,
|
|
138
|
+
times: npt.NDArray[np.float64],
|
|
139
|
+
b: float,
|
|
140
|
+
) -> npt.NDArray[np.float64]:
|
|
141
|
+
"""Generate a realization at specified times with a specified endpoint.
|
|
142
|
+
|
|
143
|
+
:param times: a vector of increasing time values at which to generate
|
|
144
|
+
the realization
|
|
145
|
+
:param b: the nonnegative right hand endpoint of the meander
|
|
146
|
+
:returns: the realization
|
|
147
|
+
"""
|
|
148
|
+
return self._sample_brownian_meander_at(times, b)
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""Brownian motion."""
|
|
2
|
+
|
|
3
|
+
from typing import override
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import numpy.typing as npt
|
|
7
|
+
from numpy.random import Generator
|
|
8
|
+
|
|
9
|
+
from ..noise.gaussian_noise import GaussianNoise
|
|
10
|
+
from ..utils.validation import check_numeric, check_positive_number
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BrownianMotion(GaussianNoise):
|
|
14
|
+
"""Brownian motion.
|
|
15
|
+
|
|
16
|
+
.. image:: /_static/brownian_motion.png
|
|
17
|
+
:scale: 50%
|
|
18
|
+
|
|
19
|
+
A standard Brownian motion (discretely sampled) has independent and
|
|
20
|
+
identically distributed Gaussian increments with variance equal to
|
|
21
|
+
increment length. Non-standard Brownian motion includes a linear drift
|
|
22
|
+
parameter and scale factor.
|
|
23
|
+
|
|
24
|
+
:param drift: rate of change of the expected value
|
|
25
|
+
:param scale: scale factor of the Gaussian process
|
|
26
|
+
:param t: the right hand endpoint of the time interval :math:`[0,t]`
|
|
27
|
+
for the process
|
|
28
|
+
:param rng: a custom random number generator
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
*,
|
|
34
|
+
drift: float = 0.0,
|
|
35
|
+
scale: float = 1.0,
|
|
36
|
+
t: float = 1.0,
|
|
37
|
+
rng: Generator | None = None,
|
|
38
|
+
) -> None:
|
|
39
|
+
super().__init__(t=t, rng=rng)
|
|
40
|
+
|
|
41
|
+
self.drift = drift
|
|
42
|
+
self.scale = scale
|
|
43
|
+
|
|
44
|
+
def __str__(self) -> str:
|
|
45
|
+
if self.drift == 0 and self.scale == 1:
|
|
46
|
+
return f"Standard Brownian motion on interval [0, {self.t}]."
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
f"Brownian motion with drift {self.drift} and "
|
|
50
|
+
f"scale {self.scale} on interval [0, {self.t}]."
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
def __repr__(self) -> str:
|
|
54
|
+
return f"BrownianMotion(drift={self.drift}, scale={self.scale}, t={self.t})"
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def drift(self) -> float:
|
|
58
|
+
"""Drift parameter."""
|
|
59
|
+
return self._drift
|
|
60
|
+
|
|
61
|
+
@drift.setter
|
|
62
|
+
def drift(self, value: float) -> None:
|
|
63
|
+
check_numeric(value=value, name="Drift")
|
|
64
|
+
|
|
65
|
+
self._drift = value
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def scale(self) -> float:
|
|
69
|
+
"""Scale parameter."""
|
|
70
|
+
return self._scale
|
|
71
|
+
|
|
72
|
+
@scale.setter
|
|
73
|
+
def scale(self, value: float) -> None:
|
|
74
|
+
check_positive_number(value=value, name="Scale")
|
|
75
|
+
|
|
76
|
+
self._scale = value
|
|
77
|
+
|
|
78
|
+
def _sample_brownian_motion(self, n: int) -> npt.NDArray[np.float64]:
|
|
79
|
+
"""Generate a realization of Brownian Motion.
|
|
80
|
+
|
|
81
|
+
Generate a Brownian motion realization with n increments. If zero is
|
|
82
|
+
True then include W_0 = 0.
|
|
83
|
+
"""
|
|
84
|
+
bm = np.cumsum(self.scale * self._sample_gaussian_noise(n))
|
|
85
|
+
bm = np.insert(bm, [0], 0)
|
|
86
|
+
|
|
87
|
+
if self.drift != 0:
|
|
88
|
+
return self.drift * self.times + bm
|
|
89
|
+
|
|
90
|
+
return bm
|
|
91
|
+
|
|
92
|
+
@override
|
|
93
|
+
def sample(self, n: int) -> npt.NDArray[np.float64]:
|
|
94
|
+
return self._sample_brownian_motion(n)
|
|
95
|
+
|
|
96
|
+
def _sample_brownian_motion_at(
|
|
97
|
+
self,
|
|
98
|
+
times: npt.NDArray[np.float64],
|
|
99
|
+
) -> npt.NDArray[np.float64]:
|
|
100
|
+
"""Generate a Brownian motion at specified times."""
|
|
101
|
+
bm = np.cumsum(self.scale * self._sample_gaussian_noise_at(times))
|
|
102
|
+
|
|
103
|
+
if times[0] == 0:
|
|
104
|
+
bm = np.insert(bm, 0, [0])
|
|
105
|
+
|
|
106
|
+
if self.drift != 0:
|
|
107
|
+
bm += [self.drift * t for t in times]
|
|
108
|
+
|
|
109
|
+
return bm
|
|
110
|
+
|
|
111
|
+
@override
|
|
112
|
+
def sample_at(
|
|
113
|
+
self,
|
|
114
|
+
times: npt.NDArray[np.float64],
|
|
115
|
+
) -> npt.NDArray[np.float64]:
|
|
116
|
+
return self._sample_brownian_motion_at(times)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Symmetric Cauchy process."""
|
|
2
|
+
|
|
3
|
+
from typing import override
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import numpy.typing as npt
|
|
7
|
+
from numpy.random import Generator
|
|
8
|
+
from scipy.stats import levy
|
|
9
|
+
|
|
10
|
+
from ..utils.validation import check_positive_integer
|
|
11
|
+
from .brownian_motion import BrownianMotion
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CauchyProcess(BrownianMotion):
|
|
15
|
+
"""Symmetric Cauchy process.
|
|
16
|
+
|
|
17
|
+
.. image:: /_static/cauchy_process.png
|
|
18
|
+
:scale: 50%
|
|
19
|
+
|
|
20
|
+
The symmetric Cauchy process is a Brownian motion with a Levy subordinator
|
|
21
|
+
using location parameter 0 and scale parameter :math:`t^2/2`.
|
|
22
|
+
|
|
23
|
+
:param t: the right hand endpoint of the time interval :math:`[0,t]`
|
|
24
|
+
for the process
|
|
25
|
+
:param rng: a custom random number generator
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
*,
|
|
31
|
+
t: float = 1.0,
|
|
32
|
+
rng: Generator | None = None,
|
|
33
|
+
) -> None:
|
|
34
|
+
super().__init__(
|
|
35
|
+
drift=0.0,
|
|
36
|
+
scale=1.0,
|
|
37
|
+
t=t,
|
|
38
|
+
rng=rng,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
def __str__(self) -> str:
|
|
42
|
+
return f"Cauchy process on [0, {self.t}]"
|
|
43
|
+
|
|
44
|
+
def __repr__(self) -> str:
|
|
45
|
+
return f"CauchyProcess(t={self.t})"
|
|
46
|
+
|
|
47
|
+
def _sample_cauchy_process(self, n: int) -> npt.NDArray[np.float64]:
|
|
48
|
+
"""Generate a realization of a Cauchy process."""
|
|
49
|
+
check_positive_integer(n=n, name="Number of increments")
|
|
50
|
+
self.set_times(n)
|
|
51
|
+
|
|
52
|
+
delta_t = 1.0 * self.t / n
|
|
53
|
+
|
|
54
|
+
times = np.cumsum(
|
|
55
|
+
levy.rvs(
|
|
56
|
+
loc=0,
|
|
57
|
+
scale=delta_t**2 / 2,
|
|
58
|
+
size=n,
|
|
59
|
+
random_state=self.rng,
|
|
60
|
+
)
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
times = np.insert(times, 0, [0])
|
|
64
|
+
return self._sample_brownian_motion_at(times)
|
|
65
|
+
|
|
66
|
+
def _sample_cauchy_process_at(
|
|
67
|
+
self,
|
|
68
|
+
times: npt.NDArray[np.float64],
|
|
69
|
+
) -> npt.NDArray[np.float64]:
|
|
70
|
+
"""Generate a realization of a Cauchy process."""
|
|
71
|
+
if times[0] != 0:
|
|
72
|
+
zero = False
|
|
73
|
+
times = np.insert(times, 0, [0])
|
|
74
|
+
else:
|
|
75
|
+
zero = True
|
|
76
|
+
|
|
77
|
+
deltas = np.diff(times)
|
|
78
|
+
levys = [
|
|
79
|
+
levy.rvs(
|
|
80
|
+
loc=0,
|
|
81
|
+
scale=d**2 / 2,
|
|
82
|
+
size=1,
|
|
83
|
+
random_state=self.rng,
|
|
84
|
+
)
|
|
85
|
+
for d in deltas
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
ts: npt.NDArray[np.float64] = np.cumsum(
|
|
89
|
+
levys, # type: ignore[arg-type]
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
if zero:
|
|
93
|
+
ts = np.insert(ts, 0, [0])
|
|
94
|
+
|
|
95
|
+
return self._sample_brownian_motion_at(ts)
|
|
96
|
+
|
|
97
|
+
@override
|
|
98
|
+
def sample(self, n: int) -> npt.NDArray[np.float64]:
|
|
99
|
+
return self._sample_cauchy_process(n)
|
|
100
|
+
|
|
101
|
+
@override
|
|
102
|
+
def sample_at(
|
|
103
|
+
self,
|
|
104
|
+
times: npt.NDArray[np.float64],
|
|
105
|
+
) -> npt.NDArray[np.float64]:
|
|
106
|
+
return self._sample_cauchy_process_at(times)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Fractional Brownian motion process."""
|
|
2
|
+
|
|
3
|
+
from typing import override
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import numpy.typing as npt
|
|
7
|
+
|
|
8
|
+
from ..noise import FractionalGaussianNoise
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FractionalBrownianMotion(FractionalGaussianNoise):
|
|
12
|
+
"""Fractional Brownian motion process.
|
|
13
|
+
|
|
14
|
+
.. image:: /_static/fractional_brownian_motion.png
|
|
15
|
+
:scale: 50%
|
|
16
|
+
|
|
17
|
+
A fractional Brownian motion (discretely sampled) has correlated Gaussian
|
|
18
|
+
increments defined by Hurst parameter :math:`H`. When :math:`H = 1/2`,
|
|
19
|
+
the process is a standard Brownian motion. When :math:`H > 1/2`, the
|
|
20
|
+
increments are positively correlated. When :math:`H < 1/2`, the
|
|
21
|
+
increments are negatively correlated.
|
|
22
|
+
|
|
23
|
+
Hosking's method:
|
|
24
|
+
|
|
25
|
+
* Hosking, Jonathan RM. "Modeling persistence in hydrological time series
|
|
26
|
+
using fractional differencing." Water resources research 20, no. 12
|
|
27
|
+
(1984): 1898-1908.
|
|
28
|
+
|
|
29
|
+
Davies Harte method:
|
|
30
|
+
|
|
31
|
+
* Davies, Robert B., and D. S. Harte. "Tests for Hurst effect." Biometrika
|
|
32
|
+
74, no. 1 (1987): 95-101.
|
|
33
|
+
|
|
34
|
+
:param hurst: the Hurst parameter on the interval (0, 1)
|
|
35
|
+
:param t: the right hand endpoint of the time interval :math:`[0,t]`
|
|
36
|
+
for the process
|
|
37
|
+
:param rng: a custom random number generator
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __str__(self) -> str:
|
|
41
|
+
return f"Fractional Brownian motion with Hurst {self.hurst} on [0, {self.t}]."
|
|
42
|
+
|
|
43
|
+
def __repr__(self) -> str:
|
|
44
|
+
return f"FractionalBrownianMotion(hurst={self.hurst}, t={self.t})"
|
|
45
|
+
|
|
46
|
+
def _sample_fractional_brownian_motion(
|
|
47
|
+
self,
|
|
48
|
+
n: int,
|
|
49
|
+
) -> npt.NDArray[np.float64]:
|
|
50
|
+
"""Generate a realization of fractional Brownian motion."""
|
|
51
|
+
fgn = self.sample_davies_harte(n)
|
|
52
|
+
fbm = fgn.cumsum()
|
|
53
|
+
fbm = np.insert(fbm, [0], 0)
|
|
54
|
+
return fbm
|
|
55
|
+
|
|
56
|
+
@override
|
|
57
|
+
def sample(self, n: int) -> npt.NDArray[np.float64]:
|
|
58
|
+
return self._sample_fractional_brownian_motion(n)
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""Gamma process."""
|
|
2
|
+
|
|
3
|
+
from typing import override
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import numpy.typing as npt
|
|
7
|
+
from numpy.random import Generator
|
|
8
|
+
|
|
9
|
+
from samplepaths.utils.validation import (
|
|
10
|
+
check_positive_integer,
|
|
11
|
+
check_positive_number,
|
|
12
|
+
times_to_increments,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from ..base import BaseTimeProcess
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class GammaProcess(BaseTimeProcess):
|
|
19
|
+
"""Gamma process.
|
|
20
|
+
|
|
21
|
+
.. image:: /_static/gamma_process.png
|
|
22
|
+
:scale: 50%
|
|
23
|
+
|
|
24
|
+
A Gamma process (discretely sampled) is the summation of stationary
|
|
25
|
+
independent increments which are distributed as gamma random variables.
|
|
26
|
+
This class supports instantiation using the mean/variance parametrization
|
|
27
|
+
or the rate/scale parametrization.
|
|
28
|
+
|
|
29
|
+
:param mean: mean increase per unit time; supply with
|
|
30
|
+
:py:attr:`variance`
|
|
31
|
+
:param variance: variance of increase per unit time; supply with
|
|
32
|
+
:py:attr:`mean`
|
|
33
|
+
:param rate: the rate of jump arrivals; supply with :py:attr:`scale`
|
|
34
|
+
:param scale: the size of the jumps; supply with :py:attr:`rate`
|
|
35
|
+
:param t: the right hand endpoint of the time interval :math:`[0,t]`
|
|
36
|
+
for the process
|
|
37
|
+
:param rng: a custom random number generator
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
*,
|
|
43
|
+
mean: float | None = None,
|
|
44
|
+
variance: float | None = None,
|
|
45
|
+
rate: float | None = None,
|
|
46
|
+
scale: float | None = None,
|
|
47
|
+
t: float = 1.0,
|
|
48
|
+
rng: Generator | None = None,
|
|
49
|
+
) -> None:
|
|
50
|
+
super().__init__(t=t, rng=rng)
|
|
51
|
+
if rate is None and scale is None:
|
|
52
|
+
if mean is None or variance is None:
|
|
53
|
+
raise ValueError(
|
|
54
|
+
"Invalid parametrization. Must provide either mean and "
|
|
55
|
+
"variance or rate and scale."
|
|
56
|
+
)
|
|
57
|
+
self.mean = mean
|
|
58
|
+
self.variance = variance
|
|
59
|
+
self.rate = mean**2.0 / variance
|
|
60
|
+
self.scale = 1.0 * mean / variance
|
|
61
|
+
elif mean is None and variance is None:
|
|
62
|
+
if rate is None or scale is None:
|
|
63
|
+
raise ValueError(
|
|
64
|
+
"Invalid parametrization. Must provide either mean and "
|
|
65
|
+
"variance or rate and scale."
|
|
66
|
+
)
|
|
67
|
+
self.rate = rate
|
|
68
|
+
self.scale = scale
|
|
69
|
+
self.mean = 1.0 * self.rate / self.scale
|
|
70
|
+
self.variance = 1.0 * self.mean / self.scale
|
|
71
|
+
else:
|
|
72
|
+
raise ValueError(
|
|
73
|
+
"Invalid parametrization. Must provide either mean and variance or rate and scale."
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
def __str__(self) -> str:
|
|
77
|
+
return f"Gamma process with rate = {self.rate} and scale = {self.scale} on [0, {self.t}]"
|
|
78
|
+
|
|
79
|
+
def __repr__(self) -> str:
|
|
80
|
+
return f"GammaProcess(rate={self.rate}, scale={self.scale}, t={self.t})"
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def mean(self) -> float:
|
|
84
|
+
"""Mean increase per unit time."""
|
|
85
|
+
return self.__mean
|
|
86
|
+
|
|
87
|
+
@mean.setter
|
|
88
|
+
def mean(self, value: float) -> None:
|
|
89
|
+
check_positive_number(value=value, name="Mean parameter")
|
|
90
|
+
|
|
91
|
+
self.__mean = float(value)
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def rate(self) -> float:
|
|
95
|
+
"""Rate of jump arrivals."""
|
|
96
|
+
return self.__rate
|
|
97
|
+
|
|
98
|
+
@rate.setter
|
|
99
|
+
def rate(self, value: float) -> None:
|
|
100
|
+
check_positive_number(value=value, name="Rate parameter")
|
|
101
|
+
|
|
102
|
+
self.__rate = value
|
|
103
|
+
|
|
104
|
+
@property
|
|
105
|
+
def scale(self) -> float:
|
|
106
|
+
"""Scale parameter for jump sizes."""
|
|
107
|
+
return self.__scale
|
|
108
|
+
|
|
109
|
+
@scale.setter
|
|
110
|
+
def scale(self, value: float) -> None:
|
|
111
|
+
check_positive_number(value=value, name="Scale parameter")
|
|
112
|
+
|
|
113
|
+
self.__scale = value
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def variance(self) -> float:
|
|
117
|
+
"""Variance of increase per unit time."""
|
|
118
|
+
return self.__variance
|
|
119
|
+
|
|
120
|
+
@variance.setter
|
|
121
|
+
def variance(self, value: float) -> None:
|
|
122
|
+
check_positive_number(value=value, name="Variance parameter")
|
|
123
|
+
|
|
124
|
+
self.__variance = value
|
|
125
|
+
|
|
126
|
+
def _sample_gamma_process(self, n: int) -> npt.NDArray[np.float64]:
|
|
127
|
+
"""Sample a Gamma process."""
|
|
128
|
+
check_positive_integer(n=n, name="Number of increments")
|
|
129
|
+
self.set_times(n)
|
|
130
|
+
|
|
131
|
+
delta_t = 1.0 * self.t / n
|
|
132
|
+
|
|
133
|
+
shape = 1.0 * self.mean**2 * delta_t / self.variance
|
|
134
|
+
scale = 1.0 * self.variance / self.mean
|
|
135
|
+
|
|
136
|
+
samples = np.cumsum(self.rng.gamma(shape=shape, scale=scale, size=n))
|
|
137
|
+
return np.concatenate(([0], samples))
|
|
138
|
+
|
|
139
|
+
def _sample_gamma_process_at(self, times: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
|
|
140
|
+
"""Sample a Gamma process at specific times."""
|
|
141
|
+
s = []
|
|
142
|
+
if times[0] != 0:
|
|
143
|
+
times = np.insert(times, 0, [0])
|
|
144
|
+
else:
|
|
145
|
+
s.append(0)
|
|
146
|
+
increments = times_to_increments(times=times)
|
|
147
|
+
|
|
148
|
+
scale = self.variance / self.mean
|
|
149
|
+
shape_coef = self.mean**2 / self.variance
|
|
150
|
+
|
|
151
|
+
for inc in increments:
|
|
152
|
+
s.append(self.rng.gamma(shape=shape_coef * inc, scale=scale))
|
|
153
|
+
|
|
154
|
+
return np.cumsum(s)
|
|
155
|
+
|
|
156
|
+
@override
|
|
157
|
+
def sample(self, n: int) -> npt.NDArray[np.float64]:
|
|
158
|
+
return self._sample_gamma_process(n)
|
|
159
|
+
|
|
160
|
+
@override
|
|
161
|
+
def sample_at(self, times: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
|
|
162
|
+
return self._sample_gamma_process_at(times)
|