Correlated geometric Brownian motions
Two assets whose prices follow geometric Brownian motions.
- Class:
CorrelatedGeometricBrownianMotionSystem - Import:
from skcomplex.systems.economics.finance import CorrelatedGeometricBrownianMotionSystem - Field:
economics/finance - Topics:
asset,sde,correlated noise - References:
- Black and Scholes 1973
- Merton 1973
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.economics.finance import CorrelatedGeometricBrownianMotionSystem
trajectory = CorrelatedGeometricBrownianMotionSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
prices |
(100.0, 100.0) |
mu |
(0.05, 0.08) |
sigma |
(0.2, 0.3) |
rho |
0.7 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | continuous |
| Space | none |
| Laws | noise, rate |
| Randomness | stochastic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class CorrelatedGeometricBrownianMotionSystem(BaseSystem):
"""Two assets whose prices follow geometric Brownian motions.
Correlated Wiener processes drive the motions. A `WienerProcessSpec` of dimension 2 with the covariance
`[[1, rho], [rho, 1]]` drives the two prices, so their log returns over a short time have the correlation `rho`. The
system has the assets `first` and `second`, whose motions share the Wiener processes `market`.
Args:
prices:
The initial prices of the two assets.
mu:
The drifts of the two assets.
sigma:
The volatilities of the two assets.
rho:
The correlation of the two Wiener processes, from -1 to 1.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.economics.finance import CorrelatedGeometricBrownianMotionSystem
>>> CorrelatedGeometricBrownianMotionSystem(rho=0.5).prepare().system_.interactions[1].process.covariance
[[1.0, 0.5], [0.5, 1.0]]
"""
field = 'economics/finance'
topics = ('asset', 'sde', 'correlated noise')
references = ('Black and Scholes 1973', 'Merton 1973')
until = 1.0
dt = 0.01
def __init__(
self,
prices: tuple[float, float] = (100.0, 100.0),
*,
mu: tuple[float, float] = (0.05, 0.08),
sigma: tuple[float, float] = (0.2, 0.3),
rho: float = 0.7,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.prices = prices
self.mu = mu
self.sigma = sigma
self.rho = rho
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of the components built from the model parameters."""
super()._prepare(context)
labels = ('first', 'second')
market = WienerProcessSpec('market', dimension=2, covariance=[[1.0, self.rho], [self.rho, 1.0]])
self.system_ = System(
self.label,
elements=[AssetElement(label, price=price) for label, price in zip(labels, self.prices, strict=True)],
interactions=[
GeometricBrownianMotionInteraction(
f'{label}_motion',
target=label,
process=market,
index=index,
mu=drift,
sigma=volatility,
)
for index, (label, drift, volatility) in enumerate(zip(labels, self.mu, self.sigma, strict=True))
],
solver=EulerMaruyamaSolver(dt=1e-3),
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
AssetElement, fromElement
Interactions:
GeometricBrownianMotionInteraction, fromInteraction
class AssetElement(Element):
"""Traded asset described by its price.
Args:
label:
The label of the asset.
price:
The initial price.
Examples:
>>> from skcomplex.systems.economics.finance import AssetElement
>>> AssetElement('stock', price=100.0).price
100.0
"""
def __init__(
self,
label: str,
*,
price: Annotated[float, RealSet.open(0, oo)] = 1.0,
) -> None:
super().__init__(label)
self.price = price
class GeometricBrownianMotionInteraction(Interaction):
"""Geometric Brownian motion of the price of an asset.
It follows `dS = mu * S * dt + sigma * S * dW`. The Wiener process `dW` is independent by default, or process
`index` of a `WienerProcessSpec` that correlates it with the motions of other assets.
Args:
label:
The label of the interaction.
target:
The path of the asset.
mu:
The drift.
sigma:
The volatility.
process:
The Wiener processes, one of which drives the price, or `None` for an independent process.
index:
The process of `process` that drives the price, or `None` without processes.
Examples:
>>> from skcomplex.systems.economics.finance import GeometricBrownianMotionInteraction
>>> from skcomplex.core.components.interactions import WienerProcessSpec
>>> GeometricBrownianMotionInteraction('gbm', target='stock', sigma=0.3).sigma
0.3
>>> market = WienerProcessSpec('market', dimension=2)
>>> GeometricBrownianMotionInteraction('gbm', target='stock', process=market, index=1).index
1
"""
law_classes = (RateLaw, NoiseLaw)
def __init__(
self,
label: str,
*,
target: Role,
mu: float = 0.05,
sigma: Annotated[float, RealSet.closed_open(0, oo)] = 0.2,
process: WienerProcessSpec | None = None,
index: int | None = None,
) -> None:
super().__init__(label)
self.target = target
self.mu = mu
self.sigma = sigma
self.process = process
self.index = index
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the drift `mu * S` and the noise `sigma * S dW` of the price as the laws.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
price = context.target.price
process = None
if self.process is not None:
process = context.process.dW if self.index is None else context.process.dW[self.index]
self.laws_: list[BaseLaw] = [
RateLaw(price, context.mu * price),
NoiseLaw(price, context.sigma * price, process=process),
]
Equations
The equations are those that symbolic returns for the system with its default parameters.
first.price: diff(price_first(t), t) == mu_first_motion*price_first(t)
second.price: diff(price_second(t), t) == mu_second_motion*price_second(t)
