Skip to content

Commit

Permalink
fix: optional delta for fou estimator
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Jan 4, 2025
1 parent 05c91ea commit 8fbedef
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ndrustfft = "0.5.0"
num-complex = { version = "0.4.6", features = ["rand"] }
plotly = { version = "0.10.0", features = ["plotly_ndarray"] }
polars = { version = "0.43.1", features = ["lazy"] }
prettytable-rs = "0.10.0"
# pyo3 = { version = "0.22.3", features = ["extension-module", "abi3-py38"] }
quadrature = "0.1.2"
rand = "0.8.5"
Expand Down
63 changes: 43 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use prettytable::{format, row, Table};
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader};

use stochastic_rs::stats::fd::FractalDim;
use stochastic_rs::stats::fou_estimator::{
FOUParameterEstimationV1, FOUParameterEstimationV2, FilterType,
};
use stochastic_rs::stats::fd::FractalDim;

// Import your estimator modules and types here
// use your_crate::{FOUParameterEstimationV1, FOUParameterEstimationV2, FilterType};

fn main() -> Result<(), Box<dyn Error>> {
// File paths
let paths = vec![
"./test/kecskekut_original.txt",
"./test/kecskekut_sim.txt",
"./test/kecskekut_without_jumps.txt",
// "./test/kecskekut_original.txt",
// "./test/kecskekut_sim.txt",
// "./test/kecskekut_without_jumps.txt",
"./test/komlos_original.txt",
"./test/komlos_sim.txt",
"./test/komlos_without_jumps.txt",
Expand All @@ -27,29 +28,51 @@ fn main() -> Result<(), Box<dyn Error>> {
let data = read_vector_from_file(path)?;

// V1 Estimation
let delta = 1.0; // Adjust as necessary
let mut estimator_v1 =
FOUParameterEstimationV1::new(data.clone().into(), FilterType::Daubechies);
FOUParameterEstimationV1::new(data.clone().into(), FilterType::Daubechies, Some(delta));
let hurst = FractalDim::new(data.clone().into());
let hurst = hurst.higuchi_fd(10);
estimator_v1.set_hurst(2. - hurst);
let hurst = hurst.higuchi_fd(12);
// estimator_v1.set_hurst(2. - hurst);
let (hurst_v1, sigma_v1, mu_v1, theta_v1) = estimator_v1.estimate_parameters();
println!("V1 - Estimated Parameters for {}:", path);
println!(" Hurst exponent: {}", hurst_v1);
println!(" Sigma: {}", sigma_v1);
println!(" Mu: {}", mu_v1);
println!(" Theta: {}", theta_v1);

// V2 Estimation
let delta = 1.0 / 256.0; // Adjust as necessary
let delta = 1.0; // Adjust as necessary
let n = data.len();
let mut estimator_v2 = FOUParameterEstimationV2::new(data.clone().into(), delta, n);
estimator_v2.set_hurst(2. - hurst);
let mut estimator_v2 = FOUParameterEstimationV2::new(data.clone().into(), Some(delta), n);
// estimator_v2.set_hurst(2. - hurst);
let (hurst_v2, sigma_v2, mu_v2, theta_v2) = estimator_v2.estimate_parameters();
println!("V1 - Estimated Parameters for {}:", path);
println!(" Hurst exponent: {}", hurst_v2);
println!(" Sigma: {}", sigma_v2);
println!(" Mu: {}", mu_v2);
println!(" Theta: {}", theta_v2);

let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
table.add_row(row![
"Version",
"Hurst",
"Sigma",
"Mu",
"Theta",
"exp(Theta)"
]);
table.add_row(row![
"V1",
format!("{:.4}", hurst_v1),
format!("{:.4}", sigma_v1),
format!("{:.4}", mu_v1),
format!("{:.4}", theta_v1),
format!("{:.4}", (-theta_v1).exp())
]);
table.add_row(row![
"V2",
format!("{:.4}", hurst_v2),
format!("{:.4}", sigma_v2),
format!("{:.4}", mu_v2),
format!("{:.4}", theta_v2),
format!("{:.4}", (-theta_v2).exp())
]);

// Táblázat kiíratása
println!("\nEstimated Parameters for {}:\n", path);
table.printstd();
}

Ok(())
Expand Down
11 changes: 6 additions & 5 deletions src/stats/fou_estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::stochastic::{noise::fgn::FGN, Sampling};
pub struct FOUParameterEstimationV1 {
pub path: Array1<f64>,
pub filter_type: FilterType,
pub delta: Option<f64>,
// Estimated parameters
hurst: Option<f64>,
sigma: Option<f64>,
Expand Down Expand Up @@ -64,7 +65,7 @@ impl FOUParameterEstimationV1 {
let L = self.L;

let series_length = self.path.len();
let delta = 1.0 / series_length as f64;
let delta: f64 = self.delta.unwrap_or(1.0 / series_length as f64);

let mut const_filter = 0.0;

Expand Down Expand Up @@ -174,7 +175,7 @@ impl FOUParameterEstimationV1 {
#[derive(ImplNew)]
pub struct FOUParameterEstimationV2 {
pub path: Array1<f64>,
pub delta: f64,
pub delta: Option<f64>,
pub series_length: usize,
// Estimated parameters
hurst: Option<f64>,
Expand Down Expand Up @@ -230,7 +231,7 @@ impl FOUParameterEstimationV2 {
let H = self.hurst.unwrap();
let X = &self.path;
let N = self.series_length as f64;
let delta = self.delta;
let delta = self.delta.unwrap_or(1.0 / N);

let numerator: f64 = (0..(self.series_length - 2))
.map(|i| {
Expand Down Expand Up @@ -437,7 +438,7 @@ mod tests {
let fgn = FGN::new(0.70, 4095, Some(1.0), None);
let fou = FOU::new(5.0, 2.8, 1.0, 4096, Some(X0), Some(16.0), None, fgn);
let path = fou.sample();
let mut estimator = FOUParameterEstimationV1::new(path, FilterType::Daubechies);
let mut estimator = FOUParameterEstimationV1::new(path, FilterType::Daubechies, None);

// Estimate the parameters
let (estimated_hurst, estimated_sigma, estimated_mu, estimated_theta) =
Expand All @@ -459,7 +460,7 @@ mod tests {
let fgn = FGN::new(0.70, N - 1, Some(1.0), None);
let fou = FOU::new(5.0, 2.8, 2.0, N, Some(X0), Some(16.0), None, fgn);
let path = fou.sample();
let mut estimator = FOUParameterEstimationV2::new(path, delta, N);
let mut estimator = FOUParameterEstimationV2::new(path, Some(delta), N);

// Estimate the parameters
let (estimated_hurst, estimated_sigma, estimated_mu, estimated_theta) =
Expand Down

0 comments on commit 8fbedef

Please sign in to comment.