python/src/rwsims/distributions.py

61 lines
1.3 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-06-19 17:10:49 +00:00
from numpy.typing import ArrayLike
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-20 17:19:55 +00:00
self._tau_jump = tau
@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-20 17:19:55 +00:00
return self._rng.exponential(self._tau_jump, size=size)
class DeltaDistribution(BaseDistribution):
def __repr__(self):
return f'No distribution(tau={self._tau})'
def start(self):
self._tau_jump = self._tau
@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):
self._tau_jump = self._rng.lognormal(np.log(self._tau), self._sigma)
@property
def mean_tau(self):
return self._tau * np.exp(self._sigma**2 / 2)