Refactor historic_vol.rs to use dateutils for date retrieval functions

This commit is contained in:
Palash Tyagi 2025-04-10 20:51:36 +01:00
parent cf5177d36a
commit 5ae4d30560

View File

@ -1,4 +1,4 @@
use crate::utils::misc::*; use crate::utils::dateutils::{get_bdates_from_col, get_min_max_real_dates};
use crate::utils::qdf::pivots::*; use crate::utils::qdf::pivots::*;
use crate::utils::qdf::reduce_df::*; use crate::utils::qdf::reduce_df::*;
use chrono::NaiveDate; use chrono::NaiveDate;
@ -10,12 +10,10 @@ use polars::series::Series; // Series struct
/// Returns the annualization factor for 252 trading days. /// Returns the annualization factor for 252 trading days.
/// (SQRT(252)) /// (SQRT(252))
#[allow(dead_code)]
fn annualization_factor() -> f64 { fn annualization_factor() -> f64 {
252f64.sqrt() 252f64.sqrt()
} }
#[allow(dead_code)]
fn expo_weights(lback_periods: usize, half_life: f64) -> Array1<f64> { fn expo_weights(lback_periods: usize, half_life: f64) -> Array1<f64> {
// Calculates exponential series weights for finite horizon, normalized to 1. // Calculates exponential series weights for finite horizon, normalized to 1.
let decf = 2f64.powf(-1.0 / half_life); let decf = 2f64.powf(-1.0 / half_life);
@ -27,7 +25,6 @@ fn expo_weights(lback_periods: usize, half_life: f64) -> Array1<f64> {
weights /= weights.sum(); weights /= weights.sum();
weights weights
} }
#[allow(dead_code)]
fn expo_std(x: &Array1<f64>, w: &Array1<f64>, remove_zeros: bool) -> f64 { fn expo_std(x: &Array1<f64>, w: &Array1<f64>, remove_zeros: bool) -> f64 {
assert_eq!(x.len(), w.len(), "weights and window must have same length"); assert_eq!(x.len(), w.len(), "weights and window must have same length");
let (filtered_x, filtered_w) = if remove_zeros { let (filtered_x, filtered_w) = if remove_zeros {
@ -49,7 +46,6 @@ fn expo_std(x: &Array1<f64>, w: &Array1<f64>, remove_zeros: bool) -> f64 {
.fold(0.0, |acc, &x_val, &w_val| acc + w_val * x_val.abs()) .fold(0.0, |acc, &x_val, &w_val| acc + w_val * x_val.abs())
} }
#[allow(dead_code)]
fn flat_std(x: &Array1<f64>, remove_zeros: bool) -> f64 { fn flat_std(x: &Array1<f64>, remove_zeros: bool) -> f64 {
let filtered_x = if remove_zeros { let filtered_x = if remove_zeros {
x.iter() x.iter()
@ -62,7 +58,6 @@ fn flat_std(x: &Array1<f64>, remove_zeros: bool) -> f64 {
filtered_x.mapv(f64::abs).mean().unwrap_or(0.0) filtered_x.mapv(f64::abs).mean().unwrap_or(0.0)
} }
#[allow(unused_variables)]
fn freq_daily_calc( fn freq_daily_calc(
dfw: &DataFrame, dfw: &DataFrame,
lback_periods: usize, lback_periods: usize,
@ -128,7 +123,6 @@ fn freq_daily_calc(
Ok(new_df) Ok(new_df)
} }
#[allow(unused_variables)]
fn freq_period_calc( fn freq_period_calc(
dfw: &DataFrame, dfw: &DataFrame,
@ -265,7 +259,6 @@ fn get_period_indices(
/// - `postfix`: A string to append to XCAT of the result series. /// - `postfix`: A string to append to XCAT of the result series.
/// - `nan_tolerance`: The maximum proportion of NaN values allowed in the calculation. /// - `nan_tolerance`: The maximum proportion of NaN values allowed in the calculation.
/// ///
#[allow(unused_variables)]
pub fn historic_vol( pub fn historic_vol(
df: polars::prelude::DataFrame, df: polars::prelude::DataFrame,
xcat: String, xcat: String,
@ -324,8 +317,7 @@ pub fn historic_vol(
println!("Successfully got nan_tolerance."); println!("Successfully got nan_tolerance.");
let (dfw_start_date, dfw_end_date) = let (dfw_start_date, dfw_end_date) = get_min_max_real_dates(&dfw, "real_date")?;
crate::utils::misc::get_min_max_real_dates(&dfw, "real_date")?;
println!("Successfully got min and max real dates."); println!("Successfully got min and max real dates.");
let (start_date, end_date) = ( let (start_date, end_date) = (