Skip to content

Commit

Permalink
updated unit tests and removed naming convention adf and dickyfuller
Browse files Browse the repository at this point in the history
  • Loading branch information
ll01 committed Oct 11, 2023
1 parent fa61e66 commit 066103a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 57 deletions.
4 changes: 2 additions & 2 deletions examples/adf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand All @@ -34,7 +34,7 @@ fn main() {

// compute the test statistic
let lag = 1;
let report = tools::adf::constant_no_trend_test(&y, lag).unwrap();
let report = tools::adf::adf_test(&y, lag, Regression::Constant).unwrap();

// critical values for the model with a constant but no trend:
let critical_value = distrib::dickeyfuller::constant_no_trend_critical_value(
Expand Down
4 changes: 2 additions & 2 deletions examples/dickeyfuller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand All @@ -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();
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&[
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
24 changes: 7 additions & 17 deletions src/tools/adf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ 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<F: RealField + Scalar + Float>(
y: &DVector<F>,
lag: usize,
) -> Result<Report<F>, Error> {
let (delta_y, x, size) = tools::prepare(y, lag, Regression::Constant)?;

let (_betas, t_stats) = ols(&delta_y, &x)?;

Ok(Report {
test_statistic: t_stats[0],
size,
})
}

pub fn adf_test<F: RealField + Scalar + Float>(
y: &DVector<F>,
lag: usize,
Expand All @@ -59,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() {
Expand All @@ -78,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,
Expand All @@ -103,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);
Expand Down
46 changes: 16 additions & 30 deletions src/tools/dickeyfuller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use num_traits::Float;
use crate::distrib::Regression;
use crate::prelude::nalgebra::DVector;
use crate::prelude::tools::Report;
use crate::regression::{self, ols};
use crate::regression::ols;
use crate::tools::prepare;
use crate::Error;

Expand All @@ -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.
Expand All @@ -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::*;
///
Expand All @@ -60,33 +59,20 @@ 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;
/// println!("t-statistic: {}", t_stat);
/// assert!((t_stat - -1.472691f64).abs() < 1e-6);
/// assert!(t_stat > critical_value);
/// ```
pub fn constant_no_trend_test<F: Float + Scalar + RealField>(
series: &DVector<F>,
) -> Result<Report<F>, Error> {
let (delta_y, y_t_1, size) = prepare(series, 0, Regression::Constant)?;

let (_betas, t_stats) = ols(&delta_y, &y_t_1)?;

Ok(Report {
test_statistic: t_stats[0],
size,
})
}

pub fn dickeyfuller_test<F: Float + Scalar + RealField>(
series: &DVector<F>,
regression: Regression,
Expand Down Expand Up @@ -120,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) {
Expand All @@ -141,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) {
Expand All @@ -162,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) {
Expand All @@ -183,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) {
Expand All @@ -198,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());
}

Expand All @@ -208,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);
}
Expand Down

0 comments on commit 066103a

Please sign in to comment.