Skip to content

Commit 2222865

Browse files
committed
feat: add some tests
1 parent c930e4a commit 2222865

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

src/stochastic/diffusion/fou.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct FOU<T: FloatExt> {
3131
impl<T: FloatExt> FOU<T> {
3232
#[must_use]
3333
pub fn new(hurst: T, theta: T, mu: T, sigma: T, n: usize, x0: Option<T>, t: Option<T>) -> Self {
34+
assert!(n >= 2, "n must be at least 2");
35+
3436
Self {
3537
hurst,
3638
theta,
@@ -97,6 +99,35 @@ mod tests {
9799
}
98100
}
99101

102+
#[test]
103+
fn fou_dt_alignment_holds_for_multiple_grid_sizes() {
104+
let theta = 0.9_f64;
105+
let mu = -0.1_f64;
106+
let x0 = 0.35_f64;
107+
let hs = [0.55_f64, 0.9_f64];
108+
let ns = [3_usize, 17, 129, 1000];
109+
let ts = [0.7_f64, 2.0_f64];
110+
111+
for &h in &hs {
112+
for &n in &ns {
113+
for &t in &ts {
114+
let p = FOU::<f64>::new(h, theta, mu, 0.0, n, Some(x0), Some(t));
115+
let x = p.sample();
116+
117+
let dt = t / (n as f64 - 1.0);
118+
let mut expected = x0;
119+
for i in 1..n {
120+
expected = expected + theta * (mu - expected) * dt;
121+
assert!(
122+
(x[i] - expected).abs() < 1e-12,
123+
"mismatch at i={i}, n={n}, t={t}, h={h}"
124+
);
125+
}
126+
}
127+
}
128+
}
129+
}
130+
100131
#[test]
101132
fn fou_sample_is_finite() {
102133
let p = FOU::<f64>::new(0.65, 1.0, 0.0, 0.5, 256, Some(0.1), Some(1.0));

src/stochastic/noise/fgn/core.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,23 @@ mod tests {
124124

125125
#[test]
126126
fn dt_and_scale_use_requested_length_not_fft_padding() {
127-
let n = 1000_usize;
128-
let h = 0.7_f64;
129-
let t = 2.0_f64;
130-
let fgn = FGN::<f64>::new(h, n, Some(t));
131-
132-
// Internal FFT length is padded, but time-step must follow requested n.
133-
assert_eq!(fgn.n, 1024);
134-
assert!((fgn.dt() - (t / n as f64)).abs() < 1e-15);
135-
136-
let expected_scale = (n as f64).powf(-h) * t.powf(h);
137-
assert!((fgn.scale - expected_scale).abs() < 1e-15);
127+
let hs = [0.2_f64, 0.7_f64];
128+
let ns = [3_usize, 17, 1000, 4095];
129+
let ts = [0.7_f64, 2.0_f64];
130+
131+
for &h in &hs {
132+
for &n in &ns {
133+
for &t in &ts {
134+
let fgn = FGN::<f64>::new(h, n, Some(t));
135+
136+
// Internal FFT length is padded, but dt/scale must follow requested n.
137+
assert!(fgn.n >= n && fgn.n.is_power_of_two());
138+
assert!((fgn.dt() - (t / n as f64)).abs() < 1e-15);
139+
140+
let expected_scale = (n as f64).powf(-h) * t.powf(h);
141+
assert!((fgn.scale - expected_scale).abs() < 1e-15);
142+
}
143+
}
144+
}
138145
}
139146
}

0 commit comments

Comments
 (0)