python/rwsims/distributions.py

65 lines
1.4 KiB
Python
Raw Normal View History

2024-06-19 17:10:49 +00:00
from __future__ import annotations
2024-06-20 17:19:55 +00:00
from abc import ABC, abstractmethod
import numpy as np
2024-08-01 16:46:28 +00:00
# from numpy.typing import ArrayLike
2024-06-19 17:10:49 +00:00
from numpy.random import Generator
2024-06-20 17:19:55 +00:00
class BaseDistribution(ABC):
2024-06-19 17:10:49 +00:00
def __init__(self, tau: float, rng: Generator | None = None):
self._tau = tau
self._rng = rng
2024-06-30 10:06:44 +00:00
self.tau_jump = tau
@property
def name(self) -> str:
return self.__class__.__name__
2024-06-20 17:19:55 +00:00
@abstractmethod
2024-06-19 17:10:49 +00:00
def __repr__(self):
2024-06-20 17:19:55 +00:00
pass
@abstractmethod
def start(self):
pass
@property
@abstractmethod
def mean_tau(self):
pass
2024-06-19 17:10:49 +00:00
def wait(self, size: int = 1) -> ArrayLike:
2024-06-30 10:06:44 +00:00
return self._rng.exponential(self.tau_jump, size=size)
2024-06-20 17:19:55 +00:00
class DeltaDistribution(BaseDistribution):
def __repr__(self):
2024-06-30 10:06:44 +00:00
return f'Delta Distribution (tau={self._tau})'
2024-06-20 17:19:55 +00:00
def start(self):
2024-06-30 10:06:44 +00:00
self.tau_jump = self._tau
2024-06-20 17:19:55 +00:00
@property
def mean_tau(self):
return self._tau
class LogGaussianDistribution(BaseDistribution):
def __init__(self, tau: float, sigma: float, rng: Generator):
super().__init__(tau=tau, rng=rng)
self._sigma = sigma
def __repr__(self):
return f'Log-Gaussian(tau={self._tau}, sigma={self._sigma})'
def start(self):
2024-06-30 10:06:44 +00:00
self.tau_jump = self._rng.lognormal(np.log(self._tau), self._sigma)
2024-06-20 17:19:55 +00:00
@property
def mean_tau(self):
return self._tau * np.exp(self._sigma**2 / 2)