unit_root/lib.rs
1// Copyright (c) 2022. Sebastien Soudan
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http:www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Basic Unit root tests for time series data.
16//!
17//! # Examples
18//!
19//! ```rust
20//! use unit_root::prelude::distrib::dickeyfuller::get_critical_value;
21//! use unit_root::prelude::distrib::{AlphaLevel, Regression};
22//! use unit_root::prelude::nalgebra::DVector;
23//! use unit_root::prelude::tools::adf_test;
24//! use unit_root::prelude::*;
25//!
26//! let y = DVector::from_row_slice(&[
27//! -0.89642362f64,
28//! 0.3222552,
29//! -1.96581989,
30//! -1.10012936,
31//! -1.3682928,
32//! 1.17239875,
33//! 2.19561259,
34//! 2.54295031,
35//! 2.05530587,
36//! 1.13212955,
37//! -0.42968979,
38//! ]);
39//!
40//! let lag = 2;
41//! let regression = Regression::Constant;
42//! let report = adf_test(&y, lag, regression).unwrap();
43//!
44//! let critical_value: f64 =
45//! get_critical_value(regression, report.size, AlphaLevel::OnePercent).unwrap();
46//! assert_eq!(report.size, 8);
47//!
48//! let t_stat = report.test_statistic;
49//! println!("t-statistic: {}", t_stat);
50//! println!("critical_value: {}", critical_value);
51//! ```
52//!
53//! # References
54//! - [Augmented Dickey-Fuller test](https://en.wikipedia.org/wiki/Augmented_Dickey–Fuller_test)
55//! - [Dickey-Fuller test](https://en.wikipedia.org/wiki/Dickey%E2%80%93Fuller_test)
56//! - [Statsmodels](https://github.com/statsmodels/statsmodels/blob/main/statsmodels/tsa/stattools.py)
57//! - [Dickey-Fuller Test](https://www.real-statistics.com/time-series-analysis/stochastic-processes/dickey-fuller-test/)
58//! - [Augmented Dickey-Fuller Test](https://www.real-statistics.com/time-series-analysis/stochastic-processes/augmented-dickey-fuller-test/)
59//! - [Augmented Dickey-Fuller Table](https://www.real-statistics.com/statistics-tables/augmented-dickey-fuller-table/)
60//! - [Standard errors in OLS](https://lukesonnet.com/teaching/inference/200d_standard_errors.pdf)
61use thiserror::Error;
62
63pub(crate) mod distrib;
64pub(crate) mod tools;
65
66/// The public API.
67pub mod prelude;
68
69#[cfg(any(feature = "unstable", test))]
70/// unstable utils API
71pub mod utils;
72
73#[cfg(any(feature = "unstable", test))]
74/// unstable regression API
75pub mod regression;
76
77#[cfg(not(any(feature = "unstable", test)))]
78pub(crate) mod regression;
79
80/// The error type for this crate.
81#[derive(Debug, Clone, Error)]
82pub enum Error {
83 /// Failed to invert matrix.
84 #[error("Failed to invert matrix: {0}")]
85 FailedToInvertMatrix(String),
86 /// NotEnoughSamples
87 #[error("Not enough samples")]
88 NotEnoughSamples,
89 /// Failed to convert float.
90 #[error("Failed to convert float")]
91 ConversionFailed,
92}