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
samplepaths/__init__.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Generate realizations of stochastic processes."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
from .continuous import (
|
|
6
|
+
BesselProcess,
|
|
7
|
+
BrownianBridge,
|
|
8
|
+
BrownianExcursion,
|
|
9
|
+
BrownianMeander,
|
|
10
|
+
BrownianMotion,
|
|
11
|
+
CauchyProcess,
|
|
12
|
+
FractionalBrownianMotion,
|
|
13
|
+
GammaProcess,
|
|
14
|
+
GeometricBrownianMotion,
|
|
15
|
+
InverseGaussianProcess,
|
|
16
|
+
MixedPoissonProcess,
|
|
17
|
+
MultifractionalBrownianMotion,
|
|
18
|
+
PoissonProcess,
|
|
19
|
+
SquaredBesselProcess,
|
|
20
|
+
VarianceGammaProcess,
|
|
21
|
+
WienerProcess,
|
|
22
|
+
)
|
|
23
|
+
from .diffusion import (
|
|
24
|
+
ConstantElasticityVarianceProcess,
|
|
25
|
+
CoxIngersollRossProcess,
|
|
26
|
+
DiffusionProcess,
|
|
27
|
+
ExtendedVasicekProcess,
|
|
28
|
+
OrnsteinUhlenbeckProcess,
|
|
29
|
+
VasicekProcess,
|
|
30
|
+
)
|
|
31
|
+
from .discrete import (
|
|
32
|
+
BernoulliProcess,
|
|
33
|
+
ChineseRestaurantProcess,
|
|
34
|
+
DirichletProcess,
|
|
35
|
+
MarkovChain,
|
|
36
|
+
MoranProcess,
|
|
37
|
+
RandomWalk,
|
|
38
|
+
)
|
|
39
|
+
from .noise import (
|
|
40
|
+
BlueNoise,
|
|
41
|
+
BrownianNoise,
|
|
42
|
+
ColoredNoise,
|
|
43
|
+
FractionalGaussianNoise,
|
|
44
|
+
GaussianNoise,
|
|
45
|
+
PinkNoise,
|
|
46
|
+
RedNoise,
|
|
47
|
+
VioletNoise,
|
|
48
|
+
WhiteNoise,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
__version__ = version("samplepaths")
|
|
52
|
+
|
|
53
|
+
__all__ = [
|
|
54
|
+
"__version__",
|
|
55
|
+
# continuous
|
|
56
|
+
"BesselProcess",
|
|
57
|
+
"BrownianBridge",
|
|
58
|
+
"BrownianExcursion",
|
|
59
|
+
"BrownianMeander",
|
|
60
|
+
"BrownianMotion",
|
|
61
|
+
"CauchyProcess",
|
|
62
|
+
"FractionalBrownianMotion",
|
|
63
|
+
"GammaProcess",
|
|
64
|
+
"GeometricBrownianMotion",
|
|
65
|
+
"InverseGaussianProcess",
|
|
66
|
+
"MixedPoissonProcess",
|
|
67
|
+
"MultifractionalBrownianMotion",
|
|
68
|
+
"PoissonProcess",
|
|
69
|
+
"SquaredBesselProcess",
|
|
70
|
+
"VarianceGammaProcess",
|
|
71
|
+
"WienerProcess",
|
|
72
|
+
# diffusion
|
|
73
|
+
"ConstantElasticityVarianceProcess",
|
|
74
|
+
"CoxIngersollRossProcess",
|
|
75
|
+
"DiffusionProcess",
|
|
76
|
+
"ExtendedVasicekProcess",
|
|
77
|
+
"OrnsteinUhlenbeckProcess",
|
|
78
|
+
"VasicekProcess",
|
|
79
|
+
# discrete
|
|
80
|
+
"BernoulliProcess",
|
|
81
|
+
"ChineseRestaurantProcess",
|
|
82
|
+
"DirichletProcess",
|
|
83
|
+
"MarkovChain",
|
|
84
|
+
"MoranProcess",
|
|
85
|
+
"RandomWalk",
|
|
86
|
+
# noise
|
|
87
|
+
"BlueNoise",
|
|
88
|
+
"BrownianNoise",
|
|
89
|
+
"ColoredNoise",
|
|
90
|
+
"FractionalGaussianNoise",
|
|
91
|
+
"GaussianNoise",
|
|
92
|
+
"PinkNoise",
|
|
93
|
+
"RedNoise",
|
|
94
|
+
"VioletNoise",
|
|
95
|
+
"WhiteNoise",
|
|
96
|
+
]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Base classes of the processes."""
|
|
2
|
+
|
|
3
|
+
from .base_process import BaseProcess
|
|
4
|
+
from .base_sequence_process import BaseSequenceProcess
|
|
5
|
+
from .base_time_process import BaseTimeProcess
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"BaseProcess",
|
|
9
|
+
"BaseSequenceProcess",
|
|
10
|
+
"BaseTimeProcess",
|
|
11
|
+
]
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""Base class of all processes."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import numpy.typing as npt
|
|
7
|
+
from numpy.random import Generator
|
|
8
|
+
|
|
9
|
+
from .. import random
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class BaseProcess(ABC):
|
|
13
|
+
"""Base class of all processes.
|
|
14
|
+
|
|
15
|
+
Handles the random number generator and defines the sampling interface.
|
|
16
|
+
|
|
17
|
+
:param rng: a custom random number generator
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
*,
|
|
23
|
+
rng: Generator | None = None,
|
|
24
|
+
) -> None:
|
|
25
|
+
self.rng = rng
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def rng(self) -> Generator:
|
|
29
|
+
"""Random number generator used by the process.
|
|
30
|
+
|
|
31
|
+
This is the generator passed at instantiation, or the package default
|
|
32
|
+
:py:data:`samplepaths.random.generator` when none was given.
|
|
33
|
+
"""
|
|
34
|
+
return self.__rng or random.generator
|
|
35
|
+
|
|
36
|
+
@rng.setter
|
|
37
|
+
def rng(self, value: Generator | None) -> None:
|
|
38
|
+
if value is None:
|
|
39
|
+
self.__rng = None
|
|
40
|
+
elif isinstance(value, Generator):
|
|
41
|
+
self.__rng = value
|
|
42
|
+
else:
|
|
43
|
+
raise TypeError("rng must be of type `numpy.random.Generator`")
|
|
44
|
+
|
|
45
|
+
@abstractmethod
|
|
46
|
+
def sample(self, n: int) -> npt.NDArray[np.float64] | npt.NDArray[np.int64]:
|
|
47
|
+
"""Generate a realization.
|
|
48
|
+
|
|
49
|
+
:param n: the number of increments to generate
|
|
50
|
+
:returns: the realization. Processes with integer values, such as
|
|
51
|
+
the state of a Markov chain, return an array of integers.
|
|
52
|
+
"""
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""Base class of the processes defined on a time interval."""
|
|
2
|
+
|
|
3
|
+
from abc import abstractmethod
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import numpy.typing as npt
|
|
7
|
+
from numpy.random import Generator
|
|
8
|
+
|
|
9
|
+
from ..utils import generate_times
|
|
10
|
+
from ..utils.validation import check_positive_integer, check_positive_number
|
|
11
|
+
from .base_process import BaseProcess
|
|
12
|
+
|
|
13
|
+
_NOT_SAMPLED = "No realization has been generated yet; call sample() first."
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class BaseTimeProcess(BaseProcess):
|
|
17
|
+
"""Base class of the processes defined on a time interval.
|
|
18
|
+
|
|
19
|
+
Handles the end time of the process and the time values of its
|
|
20
|
+
realizations.
|
|
21
|
+
|
|
22
|
+
:param t: the right hand endpoint of the time interval :math:`[0,t]`
|
|
23
|
+
for the process
|
|
24
|
+
:param rng: a custom random number generator
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
*,
|
|
30
|
+
t: float = 1.0,
|
|
31
|
+
rng: Generator | None = None,
|
|
32
|
+
) -> None:
|
|
33
|
+
super().__init__(rng=rng)
|
|
34
|
+
|
|
35
|
+
self.t = t
|
|
36
|
+
|
|
37
|
+
self.__n: int | None = None
|
|
38
|
+
self.__times: npt.NDArray[np.float64] | None = None
|
|
39
|
+
|
|
40
|
+
@abstractmethod
|
|
41
|
+
def sample(self, n: int) -> npt.NDArray[np.float64]:
|
|
42
|
+
"""Generate a realization.
|
|
43
|
+
|
|
44
|
+
:param n: the number of increments to generate
|
|
45
|
+
:returns: the realization
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def t(self) -> float:
|
|
50
|
+
"""End time of the process."""
|
|
51
|
+
return self._t
|
|
52
|
+
|
|
53
|
+
@t.setter
|
|
54
|
+
def t(self, value: float) -> None:
|
|
55
|
+
check_positive_number(value=value, name="Time end")
|
|
56
|
+
|
|
57
|
+
self._t = float(value)
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def n(self) -> int:
|
|
61
|
+
"""Number of increments of the most recent realization.
|
|
62
|
+
|
|
63
|
+
:raises RuntimeError: if no realization has been generated yet
|
|
64
|
+
"""
|
|
65
|
+
if self.__n is None:
|
|
66
|
+
raise RuntimeError(_NOT_SAMPLED)
|
|
67
|
+
return self.__n
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def times(self) -> npt.NDArray[np.float64]:
|
|
71
|
+
"""Time values of the most recent realization.
|
|
72
|
+
|
|
73
|
+
Holds ``n + 1`` equally spaced values on the interval :math:`[0,t]`.
|
|
74
|
+
|
|
75
|
+
:raises RuntimeError: if no realization has been generated yet
|
|
76
|
+
"""
|
|
77
|
+
if self.__times is None:
|
|
78
|
+
raise RuntimeError(_NOT_SAMPLED)
|
|
79
|
+
return self.__times
|
|
80
|
+
|
|
81
|
+
def set_times_with_t(self, end_t: float, n: int) -> bool:
|
|
82
|
+
"""Generate the time values of n increments on [0, end_t].
|
|
83
|
+
|
|
84
|
+
:param end_t: the right hand endpoint of the time interval
|
|
85
|
+
:param n: the number of increments
|
|
86
|
+
:returns: ``True`` if the time values changed, ``False`` if they were
|
|
87
|
+
already generated for the same arguments
|
|
88
|
+
"""
|
|
89
|
+
check_positive_integer(n=n, name="Number of increments")
|
|
90
|
+
# check_positive_number(end_t, "End time")
|
|
91
|
+
|
|
92
|
+
if self.__n == n and self.__times is not None and self.__times[-1] == end_t:
|
|
93
|
+
return False
|
|
94
|
+
|
|
95
|
+
self.__n = n
|
|
96
|
+
self.__times = generate_times(end=end_t, n=n)
|
|
97
|
+
|
|
98
|
+
return True
|
|
99
|
+
|
|
100
|
+
def set_times(self, n: int) -> bool:
|
|
101
|
+
"""Generate the time values of n increments on [0, t].
|
|
102
|
+
|
|
103
|
+
:param n: the number of increments
|
|
104
|
+
:returns: ``True`` if the time values changed, ``False`` if they were
|
|
105
|
+
already generated for the same arguments
|
|
106
|
+
"""
|
|
107
|
+
return self.set_times_with_t(self.t, n)
|
|
108
|
+
|
|
109
|
+
def sample_at(
|
|
110
|
+
self,
|
|
111
|
+
times: npt.NDArray[np.float64],
|
|
112
|
+
) -> npt.NDArray[np.float64]:
|
|
113
|
+
"""Generate a realization at specified times.
|
|
114
|
+
|
|
115
|
+
:param times: a vector of increasing time values at which to generate
|
|
116
|
+
the realization
|
|
117
|
+
:returns: the realization
|
|
118
|
+
:raises NotImplementedError: if the process does not support
|
|
119
|
+
sampling at specified times
|
|
120
|
+
"""
|
|
121
|
+
raise NotImplementedError(
|
|
122
|
+
f"{type(self).__name__} cannot be sampled at specified times. "
|
|
123
|
+
"Use the sample method with a number of increments."
|
|
124
|
+
)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Continuous-time processes."""
|
|
2
|
+
|
|
3
|
+
from .bessel_process import BesselProcess
|
|
4
|
+
from .brownian_bridge import BrownianBridge
|
|
5
|
+
from .brownian_excursion import BrownianExcursion
|
|
6
|
+
from .brownian_meander import BrownianMeander
|
|
7
|
+
from .brownian_motion import BrownianMotion
|
|
8
|
+
from .cauchy_process import CauchyProcess
|
|
9
|
+
from .fractional_brownian_motion import FractionalBrownianMotion
|
|
10
|
+
from .gamma_process import GammaProcess
|
|
11
|
+
from .geometric_brownian_motion import GeometricBrownianMotion
|
|
12
|
+
from .inverse_gaussian_process import InverseGaussianProcess
|
|
13
|
+
from .mixed_poisson_process import MixedPoissonProcess
|
|
14
|
+
from .multifractional_brownian_motion import MultifractionalBrownianMotion
|
|
15
|
+
from .poisson_process import PoissonProcess
|
|
16
|
+
from .squared_bessel_process import SquaredBesselProcess
|
|
17
|
+
from .variance_gamma_process import VarianceGammaProcess
|
|
18
|
+
from .wiener_process import WienerProcess
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"BesselProcess",
|
|
22
|
+
"BrownianBridge",
|
|
23
|
+
"BrownianExcursion",
|
|
24
|
+
"BrownianMeander",
|
|
25
|
+
"BrownianMotion",
|
|
26
|
+
"CauchyProcess",
|
|
27
|
+
"FractionalBrownianMotion",
|
|
28
|
+
"GammaProcess",
|
|
29
|
+
"GeometricBrownianMotion",
|
|
30
|
+
"InverseGaussianProcess",
|
|
31
|
+
"MixedPoissonProcess",
|
|
32
|
+
"MultifractionalBrownianMotion",
|
|
33
|
+
"PoissonProcess",
|
|
34
|
+
"SquaredBesselProcess",
|
|
35
|
+
"VarianceGammaProcess",
|
|
36
|
+
"WienerProcess",
|
|
37
|
+
]
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Bessel 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 (
|
|
10
|
+
check_positive_integer,
|
|
11
|
+
)
|
|
12
|
+
from .brownian_motion import BrownianMotion
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class BesselProcess(BrownianMotion):
|
|
16
|
+
r"""Bessel process.
|
|
17
|
+
|
|
18
|
+
.. image:: /_static/bessel_process.png
|
|
19
|
+
:scale: 50%
|
|
20
|
+
|
|
21
|
+
The Bessel process is the Euclidean norm of an :math:`n`-dimensional
|
|
22
|
+
Wiener process, e.g. :math:`\|\mathbf{W}_t\|`
|
|
23
|
+
|
|
24
|
+
Generate Bessel process realizations using :py:attr:`dim` independent
|
|
25
|
+
Brownian motion processes on the interval :math:`[0,t]`
|
|
26
|
+
|
|
27
|
+
:param dim: the number of underlying independent Brownian motions to
|
|
28
|
+
use
|
|
29
|
+
:param t: the right hand endpoint of the time interval :math:`[0,t]`
|
|
30
|
+
for the process
|
|
31
|
+
:param rng: a custom random number generator
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
*,
|
|
37
|
+
dim: int = 1,
|
|
38
|
+
t: float = 1.0,
|
|
39
|
+
rng: Generator | None = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
super().__init__(t=t, rng=rng)
|
|
42
|
+
|
|
43
|
+
self.dim = dim
|
|
44
|
+
|
|
45
|
+
def __str__(self) -> str:
|
|
46
|
+
return f"Bessel process of {self.dim} Wiener processes on [0, {self.t}]"
|
|
47
|
+
|
|
48
|
+
def __repr__(self) -> str:
|
|
49
|
+
return f"BesselProcess(dim={self.dim}, t={self.t})"
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def dim(self) -> int:
|
|
53
|
+
"""Dimensions, or independent Brownian motions."""
|
|
54
|
+
return self._dim
|
|
55
|
+
|
|
56
|
+
@dim.setter
|
|
57
|
+
def dim(self, value: int) -> None:
|
|
58
|
+
check_positive_integer(n=value, name="Dimension")
|
|
59
|
+
|
|
60
|
+
self._dim = value
|
|
61
|
+
|
|
62
|
+
def _sample_bessel_process(self, n: int) -> npt.NDArray[np.float64]:
|
|
63
|
+
"""Generate a realization of a Bessel process."""
|
|
64
|
+
check_positive_integer(n=n, name="Number of increments")
|
|
65
|
+
|
|
66
|
+
samples = [self._sample_brownian_motion(n) for _ in range(self.dim)]
|
|
67
|
+
|
|
68
|
+
return np.array([np.linalg.norm(coord) for coord in zip(*samples, strict=True)])
|
|
69
|
+
|
|
70
|
+
def _sample_bessel_process_at(
|
|
71
|
+
self,
|
|
72
|
+
times: npt.NDArray[np.float64],
|
|
73
|
+
) -> npt.NDArray[np.float64]:
|
|
74
|
+
"""Generate a realization of a Bessel process."""
|
|
75
|
+
samples = [self._sample_brownian_motion_at(times) for _ in range(self.dim)]
|
|
76
|
+
return np.array([np.linalg.norm(coord) for coord in zip(*samples, strict=True)])
|
|
77
|
+
|
|
78
|
+
@override
|
|
79
|
+
def sample(self, n: int) -> npt.NDArray[np.float64]:
|
|
80
|
+
return self._sample_bessel_process(n)
|
|
81
|
+
|
|
82
|
+
@override
|
|
83
|
+
def sample_at(
|
|
84
|
+
self,
|
|
85
|
+
times: npt.NDArray[np.float64],
|
|
86
|
+
) -> npt.NDArray[np.float64]:
|
|
87
|
+
return self._sample_bessel_process_at(times)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Brownian bridge."""
|
|
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_numeric
|
|
10
|
+
from .brownian_motion import BrownianMotion
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BrownianBridge(BrownianMotion):
|
|
14
|
+
"""Brownian bridge.
|
|
15
|
+
|
|
16
|
+
.. image:: /_static/brownian_bridge.png
|
|
17
|
+
:scale: 50%
|
|
18
|
+
|
|
19
|
+
A Brownian bridge is a Brownian motion with a conditional value on the
|
|
20
|
+
right endpoint of the process.
|
|
21
|
+
|
|
22
|
+
:param b: the right endpoint value of the Brownian bridge at time t
|
|
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
|
+
b: float = 0.0,
|
|
32
|
+
t: float = 1.0,
|
|
33
|
+
rng: Generator | None = None,
|
|
34
|
+
) -> None:
|
|
35
|
+
super().__init__(
|
|
36
|
+
drift=0.0,
|
|
37
|
+
scale=1.0,
|
|
38
|
+
t=t,
|
|
39
|
+
rng=rng,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
self.b = b
|
|
43
|
+
|
|
44
|
+
def __str__(self) -> str:
|
|
45
|
+
return f"Brownian bridge from 0 to {self.b} on [0, {self.t}]"
|
|
46
|
+
|
|
47
|
+
def __repr__(self) -> str:
|
|
48
|
+
return f"BrownianBridge(b={self.b}, t={self.t})"
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def b(self) -> float:
|
|
52
|
+
"""Right endpoint value."""
|
|
53
|
+
return self._b
|
|
54
|
+
|
|
55
|
+
@b.setter
|
|
56
|
+
def b(self, value: float) -> None:
|
|
57
|
+
check_numeric(value=value, name="Right endpoint value")
|
|
58
|
+
|
|
59
|
+
self._b = value
|
|
60
|
+
|
|
61
|
+
def _sample_brownian_bridge(self, n: int) -> npt.NDArray[np.float64]:
|
|
62
|
+
"""Generate a realization of a Brownian bridge."""
|
|
63
|
+
self.set_times(n)
|
|
64
|
+
bm = self._sample_brownian_motion(n)
|
|
65
|
+
|
|
66
|
+
ret = bm + self.times * (self.b - bm[-1]) / self.t
|
|
67
|
+
|
|
68
|
+
assert isinstance(ret, np.ndarray)
|
|
69
|
+
|
|
70
|
+
return ret
|
|
71
|
+
|
|
72
|
+
def _sample_brownian_bridge_at(
|
|
73
|
+
self,
|
|
74
|
+
times: npt.NDArray[np.float64],
|
|
75
|
+
) -> npt.NDArray[np.float64]:
|
|
76
|
+
"""Generate a realization of a Brownian bridge at times."""
|
|
77
|
+
bm = self._sample_brownian_motion_at(times)
|
|
78
|
+
ret = bm + np.array(times) * (self.b - bm[-1]) / times[-1]
|
|
79
|
+
|
|
80
|
+
assert isinstance(ret, np.ndarray)
|
|
81
|
+
|
|
82
|
+
return ret
|
|
83
|
+
|
|
84
|
+
@override
|
|
85
|
+
def sample(self, n: int) -> npt.NDArray[np.float64]:
|
|
86
|
+
return self._sample_brownian_bridge(n)
|
|
87
|
+
|
|
88
|
+
@override
|
|
89
|
+
def sample_at(
|
|
90
|
+
self,
|
|
91
|
+
times: npt.NDArray[np.float64],
|
|
92
|
+
) -> npt.NDArray[np.float64]:
|
|
93
|
+
return self._sample_brownian_bridge_at(times)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Brownian excursion."""
|
|
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 .brownian_bridge import BrownianBridge
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class BrownianExcursion(BrownianBridge):
|
|
13
|
+
"""Brownian excursion.
|
|
14
|
+
|
|
15
|
+
.. image:: /_static/brownian_excursion.png
|
|
16
|
+
:scale: 50%
|
|
17
|
+
|
|
18
|
+
A Brownian excursion is a Brownian bridge from (0, 0) to (t, 0) which is
|
|
19
|
+
conditioned to be nonnegative on the interval [0, t].
|
|
20
|
+
|
|
21
|
+
Generated using method by
|
|
22
|
+
|
|
23
|
+
* Biane, Philippe. "Relations entre pont et excursion du mouvement
|
|
24
|
+
Brownien reel." Ann. Inst. Henri Poincare 22, no. 1 (1986): 1-7.
|
|
25
|
+
|
|
26
|
+
* Vervaat, Wim. "A relation between Brownian bridge and Brownian
|
|
27
|
+
excursion." The Annals of Probability (1979): 143-149.
|
|
28
|
+
|
|
29
|
+
:param t: the right hand endpoint of the time interval :math:`[0,t]`
|
|
30
|
+
for the process
|
|
31
|
+
:param rng: a custom random number generator
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
*,
|
|
37
|
+
t: float = 1.0,
|
|
38
|
+
rng: Generator | None = None,
|
|
39
|
+
) -> None:
|
|
40
|
+
super().__init__(
|
|
41
|
+
b=0.0,
|
|
42
|
+
t=t,
|
|
43
|
+
rng=rng,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def __str__(self) -> str:
|
|
47
|
+
return f"Brownian excursion on [0, {self.t}]"
|
|
48
|
+
|
|
49
|
+
def __repr__(self) -> str:
|
|
50
|
+
return f"BrownianExcursion(t={self.t})"
|
|
51
|
+
|
|
52
|
+
def _sample_brownian_excursion(self, n: int) -> npt.NDArray[np.float64]:
|
|
53
|
+
"""Generate a Brownian excursion."""
|
|
54
|
+
brownian_bridge = self._sample_brownian_bridge(n)
|
|
55
|
+
idx_min = np.argmin(brownian_bridge)
|
|
56
|
+
s = np.array(
|
|
57
|
+
[
|
|
58
|
+
brownian_bridge[(idx_min + idx) % n] - brownian_bridge[idx_min]
|
|
59
|
+
for idx in range(n + 1)
|
|
60
|
+
]
|
|
61
|
+
)
|
|
62
|
+
return s
|
|
63
|
+
|
|
64
|
+
def _sample_brownian_excursion_at(
|
|
65
|
+
self,
|
|
66
|
+
times: npt.NDArray[np.float64],
|
|
67
|
+
) -> npt.NDArray[np.float64]:
|
|
68
|
+
"""Generate a Brownian excursion."""
|
|
69
|
+
if times[0] != 0:
|
|
70
|
+
zero = False
|
|
71
|
+
times = np.array([0] + list(times))
|
|
72
|
+
else:
|
|
73
|
+
zero = True
|
|
74
|
+
|
|
75
|
+
brownian_bridge = self._sample_brownian_bridge_at(times)
|
|
76
|
+
idx_min = np.argmin(brownian_bridge)
|
|
77
|
+
n = len(brownian_bridge)
|
|
78
|
+
s = np.array(
|
|
79
|
+
[
|
|
80
|
+
brownian_bridge[(idx_min + idx) % (n - 1)] - brownian_bridge[idx_min]
|
|
81
|
+
for idx in range(n)
|
|
82
|
+
]
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
if zero:
|
|
86
|
+
return s
|
|
87
|
+
|
|
88
|
+
return s[1:]
|
|
89
|
+
|
|
90
|
+
@override
|
|
91
|
+
def sample(self, n: int) -> npt.NDArray[np.float64]:
|
|
92
|
+
return self._sample_brownian_excursion(n)
|
|
93
|
+
|
|
94
|
+
@override
|
|
95
|
+
def sample_at(
|
|
96
|
+
self,
|
|
97
|
+
times: npt.NDArray[np.float64],
|
|
98
|
+
) -> npt.NDArray[np.float64]:
|
|
99
|
+
return self._sample_brownian_excursion_at(times)
|