add annualization factor and implement frequency calculation for historic volatility

This commit is contained in:
Palash Tyagi 2024-11-26 00:31:59 +00:00
parent 26a220cc4c
commit d692476d05

View File

@ -1,6 +1,14 @@
use chrono::NaiveDate;
use ndarray::{Array, Array1, Zip};
use polars::prelude::*;
/// Returns the annualization factor for 252 trading days.
/// (SQRT(252))
#[allow(dead_code)]
fn annualization_factor() -> f64 {
252f64.sqrt()
}
#[allow(dead_code)]
fn expo_weights(lback_periods: usize, half_life: f64) -> Array1<f64> {
// Calculates exponential series weights for finite horizon, normalized to 1.
@ -48,25 +56,37 @@ fn flat_std(x: &Array1<f64>, remove_zeros: bool) -> f64 {
filtered_x.mapv(f64::abs).mean().unwrap_or(0.0)
}
fn freq_daily_calc(
dfw: &DataFrame,
lback_periods: usize,
lback_method: &str,
half_life: Option<f64>,
remove_zeros: bool,
nan_tolerance: f64,
) -> Result<DataFrame, Box<dyn std::error::Error>> {
if lback_method == "xma" {
assert!(
half_life.is_some(),
"If lback_method is 'xma', half_life must be provided."
);
}
let mut new_df = DataFrame::new(vec![])?;
match lback_method {
"ma" => Ok(new_df),
"xma" => Ok(new_df),
_ => Err("Invalid lookback method.".into()),
}
}
// #[allow(dead_code)]
// fn single
// fn single_calc(
// // end_date: -- naive datetime
// end_date: NaiveDate,
// wide_df: &DataFrame,
// lback_periods: usize,
// lback_method: &str,
// nan_tolerance: f64,
/// Calculate historic volatility.
/// Arguments:
@ -101,46 +121,6 @@ pub fn historic_vol(
) -> Result<DataFrame, Box<dyn std::error::Error>> {
println!("Calculating historic volatility with the following parameters:");
println!("xcat: {:?},\ncids: {:?},\nlback_periods: {:?},lback_method: {:?},\nhalf_life: {:?},\nstart: {:?},\nend: {:?},\nest_freq: {:?},\nremove_zeros: {:?},\npostfix: {:?},\nnan_tolerance: {:?}", xcat, cids, lback_periods,lback_method, half_life, start, end, est_freq, remove_zeros, postfix, nan_tolerance);
Ok(df.to_owned())
}