Skip to content

Commit

Permalink
Merge pull request #8 from ll01/main
Browse files Browse the repository at this point in the history
Exposing all test models
  • Loading branch information
ssoudan authored Oct 11, 2023
2 parents e0e8ef0 + 05e0dc1 commit 43ac82d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 50 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions benches/adf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
});
}
}
Expand All @@ -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))
});
}
}
Expand All @@ -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))
});
}
}
Expand All @@ -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))
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions benches/dickeyfuller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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))
});
}
}
Expand All @@ -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))
});
}
}
Expand Down
13 changes: 6 additions & 7 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,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
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
15 changes: 10 additions & 5 deletions src/tools/adf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: RealField + Scalar + Float>(
pub fn adf_test<F: RealField + Scalar + Float>(
y: &DVector<F>,
lag: usize,
regression: Regression,
) -> Result<Report<F>, 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)?;

Expand All @@ -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() {
Expand All @@ -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,
Expand All @@ -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);
Expand Down
36 changes: 18 additions & 18 deletions src/tools/dickeyfuller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,24 +59,25 @@ 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>(
pub fn dickeyfuller_test<F: Float + Scalar + RealField>(
series: &DVector<F>,
regression: Regression,
) -> Result<Report<F>, 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)?;

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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());
}

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

0 comments on commit 43ac82d

Please sign in to comment.