diff --git a/README.md b/README.md index 096cd1a..d1e6f90 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ This project is licensed under the terms of the Apache License 2.0. Augmented Dickey-Fuller test: ```rust -use unit_root::prelude::distrib::AlphaLevel; +use unit_root::prelude::distrib::{AlphaLevel,Regression}; use unit_root::prelude::nalgebra::DVector; use unit_root::prelude::*; @@ -42,13 +42,16 @@ fn main() { let lag = 2; // compute the test statistic - let report = tools::adf::constant_no_trend_test(&y, lag).unwrap(); - + let regression = Regression::Constant; + let report = tools::adf::adf_test(&y, lag, regression).unwrap(); + // critical values for the model with a constant but no trend: - let critical_value = distrib::dickeyfuller::constant_no_trend_critical_value( + let critical_value = distrib::dickeyfuller::get_critical_value( + regression, report.size, AlphaLevel::OnePercent, - ); + ) + .unwrap(); assert_eq!(report.size, 10); // comparison diff --git a/benches/adf.rs b/benches/adf.rs index 03e236d..a209ec5 100644 --- a/benches/adf.rs +++ b/benches/adf.rs @@ -16,7 +16,8 @@ #![allow(missing_docs)] use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use rand::SeedableRng; -use unit_root::prelude::tools::adf; +use unit_root::prelude::distrib::Regression; +use unit_root::prelude::tools::adf::adf_test; use unit_root::utils::gen_ar_1; fn adf_benchmark_f32_2(c: &mut Criterion) { @@ -30,7 +31,7 @@ fn adf_benchmark_f32_2(c: &mut Criterion) { let y = gen_ar_1(&mut rng, *size, mu, delta, sigma); c.bench_with_input(BenchmarkId::new("adf_f32_2", size), &y, |b, y| { - b.iter(|| adf::constant_no_trend_test(y, lag)) + b.iter(|| adf_test(y, lag, Regression::Constant)) }); } } @@ -46,7 +47,7 @@ fn adf_benchmark_f64_2(c: &mut Criterion) { let y = gen_ar_1(&mut rng, *size, mu, delta, sigma); c.bench_with_input(BenchmarkId::new("adf_f64_2", size), &y, |b, y| { - b.iter(|| adf::constant_no_trend_test(y, lag)) + b.iter(|| adf_test(y, lag, Regression::Constant)) }); } } @@ -62,7 +63,7 @@ fn adf_benchmark_f32_10(c: &mut Criterion) { let y = gen_ar_1(&mut rng, *size, mu, delta, sigma); c.bench_with_input(BenchmarkId::new("adf_f32_10", size), &y, |b, y| { - b.iter(|| adf::constant_no_trend_test(y, lag)) + b.iter(|| adf_test(y, lag, Regression::Constant)) }); } } @@ -78,7 +79,7 @@ fn adf_benchmark_f64_10(c: &mut Criterion) { let y = gen_ar_1(&mut rng, *size, mu, delta, sigma); c.bench_with_input(BenchmarkId::new("adf_f64_10", size), &y, |b, y| { - b.iter(|| adf::constant_no_trend_test(y, lag)) + b.iter(|| adf_test(y, lag, Regression::Constant)) }); } } diff --git a/benches/dickeyfuller.rs b/benches/dickeyfuller.rs index 7217368..9ea3345 100644 --- a/benches/dickeyfuller.rs +++ b/benches/dickeyfuller.rs @@ -16,6 +16,7 @@ #![allow(missing_docs)] use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use rand::SeedableRng; +use unit_root::prelude::distrib::Regression; use unit_root::prelude::tools::dickeyfuller; use unit_root::utils::gen_ar_1; @@ -29,7 +30,7 @@ fn df_benchmark_f32(c: &mut Criterion) { let y = gen_ar_1(&mut rng, *size, mu, delta, sigma); c.bench_with_input(BenchmarkId::new("df_f32", size), &y, |b, y| { - b.iter(|| dickeyfuller::constant_no_trend_test(y)) + b.iter(|| dickeyfuller::dickeyfuller_test(y, Regression::Constant)) }); } } @@ -44,7 +45,7 @@ fn df_benchmark_f64(c: &mut Criterion) { let y = gen_ar_1(&mut rng, *size, mu, delta, sigma); c.bench_with_input(BenchmarkId::new("df_f64", size), &y, |b, y| { - b.iter(|| dickeyfuller::constant_no_trend_test(y)) + b.iter(|| dickeyfuller::dickeyfuller_test(y, Regression::Constant)) }); } } diff --git a/examples/adf.rs b/examples/adf.rs index 57e6b64..9b1dd2f 100644 --- a/examples/adf.rs +++ b/examples/adf.rs @@ -13,7 +13,7 @@ // limitations under the License. //! Example of the Augmented Dickey-Fuller test -use unit_root::prelude::distrib::AlphaLevel; +use unit_root::prelude::distrib::{AlphaLevel, Regression}; use unit_root::prelude::nalgebra::DVector; use unit_root::prelude::*; @@ -34,14 +34,13 @@ fn main() { // compute the test statistic let lag = 1; - let report = tools::adf::constant_no_trend_test(&y, lag).unwrap(); + let regression = Regression::Constant; + let report = tools::adf::adf_test(&y, lag, regression).unwrap(); // critical values for the model with a constant but no trend: - let critical_value = distrib::dickeyfuller::constant_no_trend_critical_value( - report.size, - AlphaLevel::OnePercent, - ) - .unwrap(); + let critical_value = + distrib::dickeyfuller::get_critical_value(regression, report.size, AlphaLevel::OnePercent) + .unwrap(); assert_eq!(report.size, 9); // comparison diff --git a/examples/dickeyfuller.rs b/examples/dickeyfuller.rs index 1c9181a..51ac134 100644 --- a/examples/dickeyfuller.rs +++ b/examples/dickeyfuller.rs @@ -14,7 +14,7 @@ //! A simple example of how to use the library use unit_root::prelude::distrib::dickeyfuller::constant_no_trend_critical_value; -use unit_root::prelude::distrib::AlphaLevel; +use unit_root::prelude::distrib::{AlphaLevel, Regression}; use unit_root::prelude::nalgebra::DVector; use unit_root::prelude::*; @@ -33,7 +33,7 @@ fn main() { -0.42968979, ]); - let report = tools::dickeyfuller::constant_no_trend_test(&y).unwrap(); + let report = tools::dickeyfuller::dickeyfuller_test(&y, Regression::Constant).unwrap(); let critical_value = constant_no_trend_critical_value(report.size, AlphaLevel::OnePercent).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 9458cbf..7f51910 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,9 +17,10 @@ //! # Examples //! //! ```rust -//! use unit_root::prelude::distrib::dickeyfuller::constant_no_trend_critical_value; -//! use unit_root::prelude::distrib::AlphaLevel; +//! use unit_root::prelude::distrib::dickeyfuller::get_critical_value; +//! use unit_root::prelude::distrib::{AlphaLevel, Regression}; //! use unit_root::prelude::nalgebra::DVector; +//! use unit_root::prelude::tools::adf::adf_test; //! use unit_root::prelude::*; //! //! let y = DVector::from_row_slice(&[ @@ -37,10 +38,11 @@ //! ]); //! //! let lag = 2; -//! let report = tools::adf::constant_no_trend_test(&y, lag).unwrap(); +//! let regression = Regression::Constant; +//! let report = adf_test(&y, lag, regression).unwrap(); //! //! let critical_value: f64 = -//! constant_no_trend_critical_value(report.size, AlphaLevel::OnePercent).unwrap(); +//! get_critical_value(regression, report.size, AlphaLevel::OnePercent).unwrap(); //! assert_eq!(report.size, 8); //! //! let t_stat = report.test_statistic; diff --git a/src/prelude.rs b/src/prelude.rs index aabce2c..5691969 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -29,12 +29,12 @@ pub mod tools { /// Dickey-Fuller test pub mod dickeyfuller { - pub use crate::tools::dickeyfuller::constant_no_trend_test; + pub use crate::tools::dickeyfuller::dickeyfuller_test; } /// Augmented Dickey-Fuller test pub mod adf { - pub use crate::tools::adf::constant_no_trend_test; + pub use crate::tools::adf::adf_test; } } diff --git a/src/tools/adf.rs b/src/tools/adf.rs index 36e7694..c32cd7c 100644 --- a/src/tools/adf.rs +++ b/src/tools/adf.rs @@ -25,11 +25,12 @@ use crate::{tools, Error}; /// - Constant and no trend model /// - Fixed lag /// - y must have strictly more than n + 1 elements. -pub fn constant_no_trend_test( +pub fn adf_test( y: &DVector, lag: usize, + regression: Regression, ) -> Result, Error> { - let (delta_y, x, size) = tools::prepare(y, lag, Regression::Constant)?; + let (delta_y, x, size) = tools::prepare(y, lag, regression)?; let (_betas, t_stats) = ols(&delta_y, &x)?; @@ -44,7 +45,9 @@ mod tests { use approx::assert_relative_eq; use nalgebra::DVector; + use crate::distrib::Regression; use crate::prelude::tools::dickeyfuller; + use crate::tools::adf::adf_test; #[test] fn test_t_statistics() { @@ -63,7 +66,7 @@ mod tests { 2.56363688, ]); - let report = super::constant_no_trend_test(&y, lag).unwrap(); + let report = adf_test(&y, lag, Regression::Constant).unwrap(); assert_eq!(report.size, 8); assert_relative_eq!( report.test_statistic, @@ -88,8 +91,10 @@ mod tests { let y = DVector::from(y); - let report = super::constant_no_trend_test(&y, lag).unwrap(); - let df_report = dickeyfuller::constant_no_trend_test(&y).unwrap(); + let regression = Regression::Constant; + + let report = adf_test(&y, lag, regression).unwrap(); + let df_report = dickeyfuller::dickeyfuller_test(&y, regression).unwrap(); assert_eq!(report.test_statistic, df_report.test_statistic); assert_eq!(report.size, df_report.size); diff --git a/src/tools/dickeyfuller.rs b/src/tools/dickeyfuller.rs index 83c4071..ce0afd0 100644 --- a/src/tools/dickeyfuller.rs +++ b/src/tools/dickeyfuller.rs @@ -29,9 +29,8 @@ use crate::Error; /// /// # Details /// -/// Critical values for **model 1** (constant, no trend): $\Delta y_t = \mu + -/// \delta*y_{t-1} + \epsilon_i $ can obtained from -/// `unit_root::prelude::distrib::dickeyfuller::model_1_approx_critical_value`. +/// Critical values for can obtained from +/// `unit_root::prelude::distrib::dickeyfuller::get_critical_value`. /// /// - If $t_{stat} < \mathrm{t_{\mathrm{crit}}(\alpha)}$ then reject $H_0$ at /// $alpha$ significance level - and thus conclude that the series is stationary. @@ -42,7 +41,7 @@ use crate::Error; /// # Examples: /// /// ```rust -/// use unit_root::prelude::distrib::AlphaLevel; +/// use unit_root::prelude::distrib::{AlphaLevel, Regression}; /// use unit_root::prelude::nalgebra::DVector; /// use unit_root::prelude::*; /// @@ -60,13 +59,13 @@ use crate::Error; /// -0.42968979, /// ]); /// -/// let report = tools::dickeyfuller::constant_no_trend_test(&y).unwrap(); +/// let regression = Regression::Constant; /// -/// let critical_value = distrib::dickeyfuller::constant_no_trend_critical_value( -/// report.size, -/// AlphaLevel::OnePercent, -/// ) -/// .unwrap(); +/// let report = tools::dickeyfuller::dickeyfuller_test(&y, regression).unwrap(); +/// +/// let critical_value = +/// distrib::dickeyfuller::get_critical_value(regression, report.size, AlphaLevel::OnePercent) +/// .unwrap(); /// assert_eq!(report.size, 10); /// /// let t_stat = report.test_statistic; @@ -74,10 +73,11 @@ use crate::Error; /// assert!((t_stat - -1.472691f64).abs() < 1e-6); /// assert!(t_stat > critical_value); /// ``` -pub fn constant_no_trend_test( +pub fn dickeyfuller_test( series: &DVector, + regression: Regression, ) -> Result, Error> { - let (delta_y, y_t_1, size) = prepare(series, 0, Regression::Constant)?; + let (delta_y, y_t_1, size) = prepare(series, 0, regression)?; let (_betas, t_stats) = ols(&delta_y, &y_t_1)?; @@ -106,7 +106,7 @@ mod tests { let delta: f32 = 0.5; let y = gen_ar_1(&mut rng, n, 0.0, delta, 1.0); - let report = constant_no_trend_test(&y).unwrap(); + let report = dickeyfuller_test(&y, Regression::Constant).unwrap(); let critical_value = match constant_no_trend_critical_value(report.size, AlphaLevel::OnePercent) { @@ -127,7 +127,7 @@ mod tests { let delta: f32 = 1.0; let y = gen_ar_1(&mut rng, n, 0.0, delta, 1.0); - let report = constant_no_trend_test(&y).unwrap(); + let report = dickeyfuller_test(&y, Regression::Constant).unwrap(); let critical_value = match constant_no_trend_critical_value(report.size, AlphaLevel::OnePercent) { @@ -148,7 +148,7 @@ mod tests { let delta: f64 = 0.5; let y = gen_ar_1(&mut rng, n, 0.0, delta, 1.0); - let report = constant_no_trend_test(&y).unwrap(); + let report = dickeyfuller_test(&y, Regression::Constant).unwrap(); let critical_value = match constant_no_trend_critical_value(report.size, AlphaLevel::OnePercent) { @@ -169,7 +169,7 @@ mod tests { let delta: f64 = 1.0; let y = gen_ar_1(&mut rng, n, 0.0, delta, 1.0); - let report = constant_no_trend_test(&y).unwrap(); + let report = dickeyfuller_test(&y, Regression::Constant).unwrap(); let critical_value = match constant_no_trend_critical_value(report.size, AlphaLevel::OnePercent) { @@ -184,7 +184,7 @@ mod tests { #[test] fn no_enough_data() { let y = DVector::from_row_slice(&[1.0]); - let report = constant_no_trend_test(&y); + let report = dickeyfuller_test(&y, Regression::Constant); assert!(report.is_err()); } @@ -194,7 +194,7 @@ mod tests { let y = DVector::from(y); - let report = super::constant_no_trend_test(&y).unwrap(); + let report = dickeyfuller_test(&y, Regression::Constant).unwrap(); assert_eq!(report.size, 9); }