use ndarray::Array1;
use rand_distr::Distribution;
use stochastic_rs_core::simd_rng::Deterministic;
use stochastic_rs_core::simd_rng::SeedExt;
use stochastic_rs_core::simd_rng::Unseeded;
use crate::noise::fgn::Fgn;
use crate::process::cpoisson::CompoundPoisson;
use crate::traits::FloatExt;
use crate::traits::ProcessExt;
pub struct JumpFou<T, D, S: SeedExt = Unseeded>
where
T: FloatExt,
D: Distribution<T> + Send + Sync,
{
pub hurst: T,
pub theta: T,
pub mu: T,
pub sigma: T,
pub n: usize,
pub x0: Option<T>,
pub t: Option<T>,
pub cpoisson: CompoundPoisson<T, D>,
fgn: Fgn<T>,
pub seed: S,
}
impl<T, D> JumpFou<T, D>
where
T: FloatExt,
D: Distribution<T> + Send + Sync,
{
pub fn new(
hurst: T,
theta: T,
mu: T,
sigma: T,
n: usize,
x0: Option<T>,
t: Option<T>,
cpoisson: CompoundPoisson<T, D>,
) -> Self {
assert!(n >= 2, "n must be at least 2");
Self {
hurst,
theta,
mu,
sigma,
n,
x0,
t,
cpoisson,
fgn: Fgn::new(hurst, n - 1, t),
seed: Unseeded,
}
}
}
impl<T, D> JumpFou<T, D, Deterministic>
where
T: FloatExt,
D: Distribution<T> + Send + Sync,
{
pub fn seeded(
hurst: T,
theta: T,
mu: T,
sigma: T,
n: usize,
x0: Option<T>,
t: Option<T>,
cpoisson: CompoundPoisson<T, D>,
seed: u64,
) -> Self {
assert!(n >= 2, "n must be at least 2");
Self {
hurst,
theta,
mu,
sigma,
n,
x0,
t,
cpoisson,
fgn: Fgn::new(hurst, n - 1, t),
seed: Deterministic::new(seed),
}
}
}
impl<T, D, S: SeedExt> ProcessExt<T> for JumpFou<T, D, S>
where
T: FloatExt,
D: Distribution<T> + Send + Sync,
{
type Output = Array1<T>;
fn sample(&self) -> Self::Output {
let dt = self.fgn.dt();
let fgn = &self.fgn.sample();
let jump_increments = self.cpoisson.sample_grid_increments(self.n, dt);
let mut jump_fou = Array1::<T>::zeros(self.n);
jump_fou[0] = self.x0.unwrap_or(T::zero());
for i in 1..self.n {
jump_fou[i] = jump_fou[i - 1]
+ self.theta * (self.mu - jump_fou[i - 1]) * dt
+ self.sigma * fgn[i - 1]
+ jump_increments[i];
}
jump_fou
}
}
#[cfg(feature = "python")]
#[pyo3::prelude::pyclass]
pub struct PyJumpFou {
inner_f32: Option<JumpFou<f32, crate::traits::CallableDist<f32>>>,
inner_f64: Option<JumpFou<f64, crate::traits::CallableDist<f64>>>,
seeded_f32:
Option<JumpFou<f32, crate::traits::CallableDist<f32>, crate::simd_rng::Deterministic>>,
seeded_f64:
Option<JumpFou<f64, crate::traits::CallableDist<f64>, crate::simd_rng::Deterministic>>,
}
#[cfg(feature = "python")]
#[pyo3::prelude::pymethods]
impl PyJumpFou {
#[new]
#[pyo3(signature = (hurst, theta, mu, sigma, distribution, lambda_, n, x0=None, t=None, seed=None, dtype=None))]
fn new(
hurst: f64,
theta: f64,
mu: f64,
sigma: f64,
distribution: pyo3::Py<pyo3::PyAny>,
lambda_: f64,
n: usize,
x0: Option<f64>,
t: Option<f64>,
seed: Option<u64>,
dtype: Option<&str>,
) -> Self {
use crate::process::cpoisson::CompoundPoisson;
use crate::process::poisson::Poisson;
let mut s = Self {
inner_f32: None,
inner_f64: None,
seeded_f32: None,
seeded_f64: None,
};
match dtype.unwrap_or("f64") {
"f32" => {
let cpoisson = CompoundPoisson::new(
crate::traits::CallableDist::new(distribution),
Poisson::new(lambda_ as f32, Some(n), t.map(|v| v as f32)),
);
match seed {
Some(sd) => {
s.seeded_f32 = Some(JumpFou::seeded(
hurst as f32,
theta as f32,
mu as f32,
sigma as f32,
n,
x0.map(|v| v as f32),
t.map(|v| v as f32),
cpoisson,
sd,
));
}
None => {
s.inner_f32 = Some(JumpFou::new(
hurst as f32,
theta as f32,
mu as f32,
sigma as f32,
n,
x0.map(|v| v as f32),
t.map(|v| v as f32),
cpoisson,
));
}
}
}
_ => {
let cpoisson = CompoundPoisson::new(
crate::traits::CallableDist::new(distribution),
Poisson::new(lambda_, Some(n), t),
);
match seed {
Some(sd) => {
s.seeded_f64 = Some(JumpFou::seeded(
hurst, theta, mu, sigma, n, x0, t, cpoisson, sd,
));
}
None => {
s.inner_f64 = Some(JumpFou::new(hurst, theta, mu, sigma, n, x0, t, cpoisson));
}
}
}
}
s
}
fn sample<'py>(&self, py: pyo3::Python<'py>) -> pyo3::Py<pyo3::PyAny> {
use numpy::IntoPyArray;
use pyo3::IntoPyObjectExt;
use crate::traits::ProcessExt;
py_dispatch!(self, |inner| inner
.sample()
.into_pyarray(py)
.into_py_any(py)
.unwrap())
}
fn sample_par<'py>(&self, py: pyo3::Python<'py>, m: usize) -> pyo3::Py<pyo3::PyAny> {
use numpy::IntoPyArray;
use numpy::ndarray::Array2;
use pyo3::IntoPyObjectExt;
use crate::traits::ProcessExt;
py_dispatch!(self, |inner| {
let paths = inner.sample_par(m);
let n = paths[0].len();
let mut result = Array2::zeros((m, n));
for (i, path) in paths.iter().enumerate() {
result.row_mut(i).assign(path);
}
result.into_pyarray(py).into_py_any(py).unwrap()
})
}
}