├── R ├── RcppURT │ ├── src │ │ ├── Makevars.win │ │ ├── Makevars │ │ ├── ADF.cpp │ │ ├── DFGLS.cpp │ │ ├── RcppOLSreg.cpp │ │ ├── KPSS.cpp │ │ ├── PP.cpp │ │ └── RcppOLS.cpp │ ├── NAMESPACE │ ├── DESCRIPTION │ ├── man │ │ └── URT-package.Rd │ ├── R │ │ ├── OLSreg.R │ │ ├── OLSlist.R │ │ ├── OLS.R │ │ ├── KPSS.R │ │ ├── UnitRootTests.R │ │ ├── PP.R │ │ ├── ADF.R │ │ └── DFGLS.R │ └── include │ │ ├── ADF.hpp │ │ ├── DFGLS.hpp │ │ ├── KPSS.hpp │ │ ├── PP.hpp │ │ ├── OLS.hpp │ │ ├── URT.hpp │ │ ├── CsvManager.hpp │ │ ├── Coeff_kpss.hpp │ │ ├── Tools.hpp │ │ ├── Coeff_dfgls.hpp │ │ ├── UnitRoot.hpp │ │ └── Coeff_adf.hpp ├── example2.R ├── example1.R └── benchmark.R ├── lib └── README.md ├── data └── README.md ├── examples ├── example4.cpp ├── example5.cpp ├── example3.cpp ├── example2.cpp └── example1.cpp ├── LICENSE.txt ├── Python ├── benchmark.py ├── example.py ├── setup.py ├── CyURT.pxd └── CyBlaze.pxd ├── benchmark └── benchmark.cpp ├── include ├── ADF.hpp ├── DFGLS.hpp ├── KPSS.hpp ├── PP.hpp ├── OLS.hpp ├── URT.hpp ├── CsvManager.hpp ├── Coeff_kpss.hpp ├── Tools.hpp ├── Coeff_dfgls.hpp ├── UnitRoot.hpp └── Coeff_adf.hpp └── src ├── ADF.cpp ├── DFGLS.cpp ├── KPSS.cpp └── PP.cpp /R/RcppURT/src/Makevars.win: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /R/RcppURT/NAMESPACE: -------------------------------------------------------------------------------- 1 | useDynLib(RcppURT) 2 | importFrom(Rcpp, evalCpp) 3 | exportPattern("^[[:alpha:]]+") 4 | -------------------------------------------------------------------------------- /lib/README.md: -------------------------------------------------------------------------------- 1 | This folder will contain the shared libraries obtained from compiling the C++ source code by running ./URT/build/makefile. 2 | -------------------------------------------------------------------------------- /data/README.md: -------------------------------------------------------------------------------- 1 | This folder will contain 2 csv files when running ./URT/examples/example1.cpp. 2 | 3 | These 2 csv files will be used when testing R and Python wrappers. 4 | -------------------------------------------------------------------------------- /R/RcppURT/DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: RcppURT 2 | Type: Package 3 | Title: Fast Unit Root Tests 4 | Version: 1.0 5 | Date: 2016-11-16 6 | Author: Olivier Mallet 7 | Maintainer: Who to complain to 8 | Description: Fast Unit Root Tests, OLS regression, Lag Dependent and P-values by Bootstrap 9 | License: No license 10 | Depends: Rcpp (>= 0.12.6) 11 | LinkingTo: Rcpp, RcppArmadillo 12 | -------------------------------------------------------------------------------- /R/RcppURT/src/Makevars: -------------------------------------------------------------------------------- 1 | # build with R CMD build RcppURT 2 | 3 | # add -fopenmp if parallelism required 4 | PKG_CXXFLAGS = -std=c++11 -Wall -O3 -march=native -DUSE_ARMA 5 | 6 | # external libraries path (adjust these paths to your owns) 7 | MKLPATH = /opt/intel/mkl/lib/intel64/ 8 | BLASPATH = /usr/local/lib/ 9 | 10 | # with dynamic link to Intel MKL 11 | PKG_LIBS = -L${MKLPATH} -Wl,--no-as-needed -lmkl_gf_lp64 -lmkl_sequential -lmkl_core -ldl 12 | 13 | # or with static link to Intel MKL 14 | #PKG_LIBS = -Wl,--start-group ${MKLPATH}libmkl_gf_lp64.a ${MKLPATH}libmkl_core.a ${MKLPATH}libmkl_sequential.a -Wl,--end-group -ldl 15 | 16 | # or with dynamic link to OpenBLAS 17 | #PKG_LIBS = -L${BLASPATH} -lopenblas 18 | 19 | # or with static link to OpenBLAS 20 | #PKG_LIBS = $(BLASPATH)libopenblas.a 21 | 22 | -------------------------------------------------------------------------------- /examples/example4.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | int main() 8 | { 9 | // generating non-stationary random data 10 | urt::Vector data = urt::wiener_process(1000); 11 | 12 | // initializing Phillips-Perron normalized test with lags of type long and constant term 13 | urt::PP test(data, "long", "c", "rho"); 14 | 15 | // outputting test results 16 | test.show(); 17 | 18 | // switching to t-statistic test 19 | test.test_type = "tau"; 20 | 21 | // outputting test results 22 | test.show(); 23 | } 24 | -------------------------------------------------------------------------------- /examples/example5.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | int main() 8 | { 9 | int nobs = 1000; 10 | 11 | // generating stationary random data 12 | urt::Vector data = urt::gaussian_noise(nobs); 13 | 14 | // initializing KPSS test with lags of type short and constant trend 15 | urt::KPSS test(data, "short", "ct"); 16 | 17 | // outputting test results 18 | test.show(); 19 | 20 | // switching to test with 5 lags and constant term 21 | test.lags = 5; 22 | test.trend = "c"; 23 | 24 | // outputting test results 25 | test.show(); 26 | } 27 | -------------------------------------------------------------------------------- /examples/example3.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | int main() 8 | { 9 | int nobs = 1000; 10 | 11 | // generating non-stationary random data 12 | urt::Vector data = urt::wiener_process(nobs); 13 | 14 | // initializing DFGLS test with lag length optimization using BIC and constant term 15 | urt::DFGLS test(data, "BIC"); 16 | 17 | // outputting test results 18 | test.show(); 19 | 20 | // switching to test with 10 lags and constant trend 21 | test.trend = "ct"; 22 | test.lags = 10; 23 | 24 | // outputting test results 25 | test.show(); 26 | } 27 | -------------------------------------------------------------------------------- /R/RcppURT/man/URT-package.Rd: -------------------------------------------------------------------------------- 1 | \name{RcppURT-package} 2 | \alias{RcppURT-package} 3 | \alias{RcppURT} 4 | \docType{package} 5 | \title{ 6 | \packageTitle{RcppURT} 7 | } 8 | \description{ 9 | \packageDescription{RcppURT} 10 | } 11 | \details{ 12 | 13 | The DESCRIPTION file: 14 | \packageDESCRIPTION{RcppURT} 15 | \packageIndices{RcppURT} 16 | ~~ An overview of how to use the package, including the most important ~~ 17 | ~~ functions ~~ 18 | } 19 | \author{ 20 | \packageAuthor{RcppURT} 21 | 22 | Maintainer: \packageMaintainer{RcppURT} 23 | } 24 | \references{ 25 | ~~ Literature or other references for background information ~~ 26 | } 27 | ~~ Optionally other standard keywords, one per line, from file KEYWORDS in ~~ 28 | ~~ the R documentation directory ~~ 29 | \keyword{ package } 30 | \seealso{ 31 | ~~ Optional links to other man pages, e.g. ~~ 32 | ~~ \code{\link[:-package]{}} ~~ 33 | } 34 | \examples{ 35 | ~~ simple examples of the most important functions ~~ 36 | } 37 | -------------------------------------------------------------------------------- /examples/example2.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | int main() 8 | { 9 | int nobs = 1000; 10 | 11 | // generating non-stationary random data 12 | urt::Vector data = urt::wiener_process(nobs); 13 | 14 | // initializing ADF test with 10 lags and constant trend 15 | urt::ADF test(data, 10, "ct"); 16 | 17 | // outputting test results 18 | test.show(); 19 | 20 | // switching to test with lag length optimization and p-value computation by bootstrap with 10000 iterations 21 | test.method = "AIC"; 22 | test.bootstrap = true; 23 | test.niter = 10000; 24 | 25 | // outputting test results 26 | test.show(); 27 | } 28 | -------------------------------------------------------------------------------- /examples/example1.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | int main() 8 | { 9 | int nrows = 1000; 10 | int ncols = 10; 11 | 12 | // generating random arrays 13 | urt::Vector y = urt::wiener_process(nrows); 14 | urt::Matrix x = urt::wiener_process(nrows, ncols); 15 | 16 | // adding intercept to matrix of independent variables 17 | urt::add_intercept(x); 18 | 19 | // writting data to CSV files 20 | urt::WriteToCSV("./URT/data/y.csv", y); 21 | urt::WriteToCSV("./URT/data/x.csv", x); 22 | 23 | // running OLS regression 24 | urt::OLS fit(y, x, true); 25 | 26 | // outputting regression results 27 | fit.show(); 28 | } 29 | -------------------------------------------------------------------------------- /R/example2.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | suppressMessages(library(RcppURT)) 6 | 7 | run <- function() 8 | { 9 | x = as.matrix(read.table("../data/x.csv", sep=",")) 10 | y = as.matrix(read.table("../data/y.csv", sep=",")) 11 | 12 | # running OLS regression as in ./examples/example1.cpp using double precision type 13 | fit = OLSreg_d(y, x, stats=TRUE, output=TRUE) 14 | 15 | # running OLS regression as in ./examples/example1.cpp using single precision type 16 | fit = OLSreg_f(y, x, stats=TRUE, output=TRUE) 17 | 18 | # running first ADF test as in ./examples/example2.cpp using double precision type 19 | test = ADFtest_d(y, lags=10, trend='ct', output=TRUE) 20 | 21 | # running second ADF test as in ./examples/example2.cpp using double precision type 22 | test = ADFtest_d(y, method='AIC', trend='ct', output=TRUE, bootstrap=TRUE, niter=10000) 23 | } 24 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Olivier Mallet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /R/example1.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | suppressMessages(library(RcppURT)) 6 | 7 | run <- function() 8 | { 9 | x = as.matrix(read.table("../data/x.csv", sep=",")) 10 | y = as.matrix(read.table("../data/y.csv", sep=",")) 11 | 12 | # running OLS regression as in ./examples/example1.cpp using double precision type 13 | fit = OLS_d$new(y, x, stats=TRUE) 14 | fit$show() 15 | 16 | # running OLS regression as in ./examples/example1.cpp using single precision type 17 | fit = OLS_f$new(y, x, stats=TRUE) 18 | fit$show() 19 | 20 | # running first ADF test as in ./examples/example2.cpp using double precision type 21 | test = ADF_d$new(y, lags=10, trend='ct') 22 | test$show() 23 | 24 | # running second ADF test as in ./examples/example2.cpp using double precision type 25 | test$method = 'AIC' 26 | test$bootstrap = TRUE 27 | test$niter = 10000 28 | test$show() 29 | } 30 | -------------------------------------------------------------------------------- /R/RcppURT/R/OLSreg.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | #-------------------------------------------------------------------------------------------------- 6 | 7 | # C++ class OLS as a function 8 | OLSreg_d <- function(y, x, stats = FALSE, output = FALSE) { 9 | .Call('OLSreg_d', PACKAGE = 'RcppURT', y_ = y, x_ = x, stats_ = stats, output_ = output) 10 | } 11 | 12 | #-------------------------------------------------------------------------------------------------- 13 | ################################################################################################### 14 | #-------------------------------------------------------------------------------------------------- 15 | 16 | # C++ class OLS as a function 17 | OLSreg_f <- function(y, x, stats = FALSE, output = FALSE) { 18 | .Call('OLSreg_f', PACKAGE = 'RcppURT', y_ = y, x_ = x, stats_ = stats, output_ = output) 19 | } 20 | 21 | #-------------------------------------------------------------------------------------------------- 22 | -------------------------------------------------------------------------------- /Python/benchmark.py: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | import numpy as np 6 | import CyURT as urt 7 | from timeit import default_timer as timer 8 | 9 | if __name__ == "__main__": 10 | 11 | sizes = [100,150,200,250,300,350,400,450,500,1000,1500,2000,2500,3000,3500,4000,4500,5000] 12 | 13 | for i in range(len(sizes)): 14 | 15 | # generating Wiener process 16 | data = np.cumsum(np.random.normal(size=sizes[i])) 17 | # uncomment this line and comment the one above to switch to single precision 18 | #data = np.cumsum(np.random.normal(size=sizes[i])).astype(np.float32) 19 | 20 | if sizes[i] < 1000: niter = 10000 21 | else: niter = 1000 22 | 23 | start = timer() 24 | for k in range(niter): 25 | test = urt.ADF_d(data, method='AIC') 26 | # uncomment this line and comment the one above to switch to single precision 27 | #test = urt.ADF_f(data, method='AIC') 28 | test.statistic() 29 | end = timer() 30 | 31 | print '{:8d}'.format(sizes[i]), '{:8.1f}'.format(end - start) 32 | 33 | -------------------------------------------------------------------------------- /benchmark/benchmark.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | #ifndef USE_ARMA 8 | #include 9 | #endif 10 | 11 | // define USE_FLOAT when compiling to switch to single precision 12 | #ifdef USE_FLOAT 13 | using T = float; 14 | #else 15 | using T = double; 16 | #endif 17 | 18 | int main() 19 | { 20 | int niter = 0; 21 | 22 | arma::wall_clock timer; 23 | 24 | std::vector sizes = {100,150,200,250,300,350,400,450,500,1000,1500,2000,2500,3000,3500,4000,4500,5000}; 25 | 26 | std::cout << std::fixed << std::setprecision(1); 27 | 28 | for (int i = 0; i < sizes.size(); ++i) { 29 | 30 | urt::Vector data = urt::wiener_process(sizes[i]); 31 | 32 | (sizes[i] < 1000) ? niter = 10000 : niter = 1000; 33 | 34 | timer.tic(); 35 | for (int k = 0; k < niter; ++k) { 36 | urt::ADF test(data, "AIC"); 37 | test.statistic(); 38 | } 39 | 40 | auto duration = timer.toc(); 41 | 42 | std::cout << std::setw(8) << sizes[i]; 43 | std::cout << std::setw(8) << duration << "\n"; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Python/example.py: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | import numpy as np 6 | import pandas as pd 7 | 8 | import CyURT as urt 9 | 10 | if __name__ == "__main__": 11 | 12 | y = pd.read_csv('../data/y.csv', sep=',', header=None) 13 | x = pd.read_csv('../data/x.csv', sep=',', header=None) 14 | 15 | yd = np.asarray(y).reshape(y.size) 16 | yf = yd.astype(np.float32) 17 | xd = np.asarray(x, order='F') 18 | xf = xd.astype(np.float32) 19 | 20 | # running OLS regression as in ./examples/example1.cpp using double precision type 21 | fit = urt.OLS_d(yd, xd, True) 22 | fit.show() 23 | 24 | # running OLS regression as in ./examples/example1.cpp using single precision type 25 | fit = urt.OLS_f(yf, xf, True) 26 | fit.show() 27 | 28 | # running first ADF test as in ./examples/example2.cpp using double precision type 29 | test = urt.ADF_d(yd, lags=10, trend='ct') 30 | test.show() 31 | 32 | # running second ADF test as in ./examples/example2.cpp using double precision type 33 | test.method = 'AIC' 34 | test.bootstrap = True 35 | test.niter = 10000 36 | test.show() 37 | -------------------------------------------------------------------------------- /R/benchmark.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | suppressMessages(library(RcppURT)) 6 | 7 | run <- function() 8 | { 9 | sizes = c(100,150,200,250,300,350,400,450,500,1000,1500,2000,2500,3000,3500,4000,4500,5000) 10 | 11 | for (i in 1:length(sizes)) { 12 | 13 | # generating Wiener process 14 | data = cumsum(rnorm(n=sizes[i])) 15 | 16 | if (sizes[i] < 1000) niter = 10000 17 | else niter = 1000 18 | 19 | # with R6 classes 20 | start1 = Sys.time() 21 | for (k in 1:niter) { 22 | test = ADF_d$new(data, method='AIC') 23 | # uncomment this line and comment the one above to switch to single precision 24 | #test = ADF_f$new(data, method='AIC') 25 | test$statistic() 26 | } 27 | end1 = Sys.time() 28 | 29 | # with Rcpp functions 30 | start2 = Sys.time() 31 | for (k in 1:niter) { 32 | test = ADFtest_d(data, method='AIC') 33 | # uncomment this line and comment the one above to switch to single precision 34 | #test = ADFtest_f(data, method='AIC') 35 | } 36 | end2 = Sys.time() 37 | 38 | cat(sprintf("%8d", sizes[i])) 39 | cat(sprintf("%8.1f", end1 - start1)) 40 | cat(sprintf("%8.1f\n", end2 - start2)) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /include/ADF.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef ADF_HPP 6 | #define ADF_HPP 7 | 8 | #include "Coeff_adf.hpp" 9 | 10 | namespace urt { 11 | 12 | //================================================================================================= 13 | 14 | // Augmented Dickey-Fuller test 15 | template 16 | class ADF : public UnitRoot 17 | { 18 | public: 19 | // parameter constructor for computing ADF test for a given number of lags 20 | ADF(const Vector& data, int lags, const std::string& trend = "c", bool regression = false); 21 | // parameter constructor for computing ADF test with lag length optimization 22 | ADF(const Vector& data, const std::string& method, const std::string& trend = "c", bool regression = false); 23 | // compute test statistic 24 | const T& statistic() override; 25 | // compute test p-value 26 | const T& pvalue() override; 27 | // output test results 28 | void show() override; 29 | 30 | private: 31 | using ur = UnitRoot; 32 | const char* _test_name = "Augmented Dickey-Fuller"; 33 | const std::vector _valid_trends{"nc","c","ct","ctt"}; 34 | }; 35 | 36 | //================================================================================================= 37 | 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /R/RcppURT/include/ADF.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef ADF_HPP 6 | #define ADF_HPP 7 | 8 | #include "Coeff_adf.hpp" 9 | 10 | namespace urt { 11 | 12 | //================================================================================================= 13 | 14 | // Augmented Dickey-Fuller test 15 | template 16 | class ADF : public UnitRoot 17 | { 18 | public: 19 | // parameter constructor for computing ADF test for a given number of lags 20 | ADF(const Vector& data, int lags, const std::string& trend = "c", bool regression = false); 21 | // parameter constructor for computing ADF test with lag length optimization 22 | ADF(const Vector& data, const std::string& method, const std::string& trend = "c", bool regression = false); 23 | // compute test statistic 24 | const T& statistic() override; 25 | // compute test p-value 26 | const T& pvalue() override; 27 | // output test results 28 | void show() override; 29 | 30 | private: 31 | using ur = UnitRoot; 32 | const char* _test_name = "Augmented Dickey-Fuller"; 33 | const std::vector _valid_trends{"nc","c","ct","ctt"}; 34 | }; 35 | 36 | //================================================================================================= 37 | 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /Python/setup.py: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | # run with: 6 | # python setup.py build_ext --inplace 7 | 8 | # before running Python program you will need to export the C++ shared library path: 9 | # export LD_LIBRARY_PATH=/path/to/URT/lib:$LD_LIBRARY_PATH 10 | 11 | from distutils.core import setup 12 | from distutils.extension import Extension 13 | from Cython.Distutils import build_ext 14 | from Cython.Build import cythonize 15 | from numpy import get_include 16 | 17 | # linking to C++ libURT.so library 18 | ext = Extension('CyURT', 19 | sources = ['CyURT.pyx'], 20 | include_dirs = [get_include()], 21 | libraries = ['URT'], 22 | extra_compile_args = ['-std=c++11','-Wall','-march=native','-DUSE_BLAZE','-DBLAZE_BLAS_INCLUDE_FILE '], 23 | extra_link_args = ['-L../lib'], 24 | language='c++') 25 | 26 | # NB: in extra_compile_args replace the header by the one associated with the BLAS/LAPACK replacement library, for example OpenBLAS will use the header 27 | 28 | ext.cython_directives = {'boundscheck': False,'wraparound': False} 29 | # turn off bounds-checking for entire function 30 | # turn off negative index wrapping for entire function 31 | 32 | setup(cmdclass = {'build_ext' : build_ext}, ext_modules = [ext]) 33 | -------------------------------------------------------------------------------- /include/DFGLS.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef DFGLS_HPP 6 | #define DFGLS_HPP 7 | 8 | #include "Coeff_dfgls.hpp" 9 | 10 | namespace urt { 11 | 12 | //================================================================================================= 13 | 14 | // Dickey-Fuller Generalized Least-Squares test 15 | template 16 | class DFGLS : public UnitRoot 17 | { 18 | public: 19 | // parameter constructor for computing DF-GLS test for a given number of lags 20 | DFGLS(const Vector& data, int lags, const std::string& trend = "c", bool regression = false); 21 | // parameter constructor for computing ADF test with lag length optimization 22 | DFGLS(const Vector& data, const std::string& method, const std::string& trend = "c", bool regression = false); 23 | // compute test statistic 24 | const T& statistic() override; 25 | // compute test p-value 26 | const T& pvalue() override; 27 | // output test results 28 | void show() override; 29 | 30 | private: 31 | using ur = UnitRoot; 32 | const char* _test_name = "Dickey-Fuller GLS"; 33 | const std::vector _valid_trends{"c","ct"}; 34 | }; 35 | 36 | //================================================================================================= 37 | 38 | } 39 | 40 | #endif 41 | 42 | 43 | -------------------------------------------------------------------------------- /R/RcppURT/include/DFGLS.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef DFGLS_HPP 6 | #define DFGLS_HPP 7 | 8 | #include "Coeff_dfgls.hpp" 9 | 10 | namespace urt { 11 | 12 | //================================================================================================= 13 | 14 | // Dickey-Fuller Generalized Least-Squares test 15 | template 16 | class DFGLS : public UnitRoot 17 | { 18 | public: 19 | // parameter constructor for computing DF-GLS test for a given number of lags 20 | DFGLS(const Vector& data, int lags, const std::string& trend = "c", bool regression = false); 21 | // parameter constructor for computing ADF test with lag length optimization 22 | DFGLS(const Vector& data, const std::string& method, const std::string& trend = "c", bool regression = false); 23 | // compute test statistic 24 | const T& statistic() override; 25 | // compute test p-value 26 | const T& pvalue() override; 27 | // output test results 28 | void show() override; 29 | 30 | private: 31 | using ur = UnitRoot; 32 | const char* _test_name = "Dickey-Fuller GLS"; 33 | const std::vector _valid_trends{"c","ct"}; 34 | }; 35 | 36 | //================================================================================================= 37 | 38 | } 39 | 40 | #endif 41 | 42 | 43 | -------------------------------------------------------------------------------- /include/KPSS.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef KPSS_HPP 6 | #define KPSS_HPP 7 | 8 | #include "Coeff_kpss.hpp" 9 | 10 | namespace urt { 11 | 12 | //================================================================================================= 13 | 14 | // Kwiatkowski–Phillips–Schmidt–Shin test 15 | template 16 | class KPSS : public UnitRoot 17 | { 18 | public: 19 | // parameter constructor for computing KPSS test for a given number of lags 20 | KPSS(const Vector& data, int lags, const std::string& trend = "c"); 21 | // parameter constructor for computing PP test for a default number of lags (long or short) 22 | KPSS(const Vector& data, const std::string lags_type, const std::string& trend = "c"); 23 | // compute KPSS test statistic 24 | const T& statistic() override; 25 | // compute KPSS test p-value 26 | const T& pvalue() override; 27 | // output KPSS test results 28 | void show() override; 29 | 30 | private: 31 | using ur = UnitRoot; 32 | const char* _test_name = "KPSS"; 33 | const std::vector _valid_trends{"c","ct"}; 34 | // compute test statistic 35 | void compute_stat(); 36 | }; 37 | 38 | //================================================================================================= 39 | 40 | } 41 | 42 | #endif 43 | 44 | 45 | -------------------------------------------------------------------------------- /R/RcppURT/include/KPSS.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef KPSS_HPP 6 | #define KPSS_HPP 7 | 8 | #include "Coeff_kpss.hpp" 9 | 10 | namespace urt { 11 | 12 | //================================================================================================= 13 | 14 | // Kwiatkowski–Phillips–Schmidt–Shin test 15 | template 16 | class KPSS : public UnitRoot 17 | { 18 | public: 19 | // parameter constructor for computing KPSS test for a given number of lags 20 | KPSS(const Vector& data, int lags, const std::string& trend = "c"); 21 | // parameter constructor for computing PP test for a default number of lags (long or short) 22 | KPSS(const Vector& data, const std::string lags_type, const std::string& trend = "c"); 23 | // compute KPSS test statistic 24 | const T& statistic() override; 25 | // compute KPSS test p-value 26 | const T& pvalue() override; 27 | // output KPSS test results 28 | void show() override; 29 | 30 | private: 31 | using ur = UnitRoot; 32 | const char* _test_name = "KPSS"; 33 | const std::vector _valid_trends{"c","ct"}; 34 | // compute test statistic 35 | void compute_stat(); 36 | }; 37 | 38 | //================================================================================================= 39 | 40 | } 41 | 42 | #endif 43 | 44 | 45 | -------------------------------------------------------------------------------- /include/PP.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef PP_HPP 6 | #define PP_HPP 7 | 8 | #include "Coeff_pp.hpp" 9 | 10 | namespace urt { 11 | 12 | //================================================================================================= 13 | 14 | // Phillips-Perron test 15 | template 16 | class PP : public UnitRoot 17 | { 18 | public: 19 | // PP has its own lags variable 20 | int lags = 0; 21 | // parameter constructor for computing PP test for a given number of lags 22 | PP(const Vector& data, int lags, const std::string& trend = "c", const std::string& test_type = "tau", bool regression = false); 23 | // parameter constructor for computing PP test for a default number of lags (long or short) 24 | PP(const Vector& data, const std::string& lags_type, const std::string& trend = "c", const std::string& test_type = "tau", bool regression = false); 25 | // compute PP test statistic 26 | const T& statistic() override; 27 | // compute PP test p-value 28 | const T& pvalue() override; 29 | // output PP test results 30 | void show() override; 31 | 32 | private: 33 | using ur = UnitRoot; 34 | const char* _test_name = "Phillips-Perron"; 35 | const std::vector _valid_trends{"nc","c","ct"}; 36 | // compute test statistic 37 | void compute_stat(); 38 | }; 39 | 40 | //================================================================================================= 41 | 42 | } 43 | 44 | #endif 45 | 46 | 47 | -------------------------------------------------------------------------------- /R/RcppURT/include/PP.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef PP_HPP 6 | #define PP_HPP 7 | 8 | #include "Coeff_pp.hpp" 9 | 10 | namespace urt { 11 | 12 | //================================================================================================= 13 | 14 | // Phillips-Perron test 15 | template 16 | class PP : public UnitRoot 17 | { 18 | public: 19 | // PP has its own lags variable 20 | int lags = 0; 21 | // parameter constructor for computing PP test for a given number of lags 22 | PP(const Vector& data, int lags, const std::string& trend = "c", const std::string& test_type = "tau", bool regression = false); 23 | // parameter constructor for computing PP test for a default number of lags (long or short) 24 | PP(const Vector& data, const std::string& lags_type, const std::string& trend = "c", const std::string& test_type = "tau", bool regression = false); 25 | // compute PP test statistic 26 | const T& statistic() override; 27 | // compute PP test p-value 28 | const T& pvalue() override; 29 | // output PP test results 30 | void show() override; 31 | 32 | private: 33 | using ur = UnitRoot; 34 | const char* _test_name = "Phillips-Perron"; 35 | const std::vector _valid_trends{"nc","c","ct"}; 36 | // compute test statistic 37 | void compute_stat(); 38 | }; 39 | 40 | //================================================================================================= 41 | 42 | } 43 | 44 | #endif 45 | 46 | 47 | -------------------------------------------------------------------------------- /include/OLS.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef OLS_HPP 6 | #define OLS_HPP 7 | 8 | namespace urt { 9 | 10 | //================================================================================================= 11 | 12 | // Multi-Linear Regression by Ordinary Least Squares 13 | // NB: we chose never to make hard copies of x and y for performance reason, indeed in ADF test with lag length optimization we can quickly get a very large x matrix 14 | template 15 | class OLS 16 | { 17 | public: 18 | Vector param; // regressors parameters 19 | Vector t_stat; // regressors statistics 20 | Vector resid; // residuals 21 | Vector var; // regressors variances 22 | 23 | T MSE; // mean of squares for error 24 | T SSR; // sum of squares residuals 25 | T R2 = 0; // R-squared 26 | T adj_R2 = 0; // adjusted R-squared 27 | T F_stat = 0; // F statistic 28 | T DW_stat = 0; // Durbin-Watson statistic 29 | T IC = 0; // model information criterion (for Unit-Root tests) 30 | 31 | int nobs; // number of observations 32 | int nreg; // number of regressors 33 | int ndf; // number of degrees of freedom 34 | int lags = 0; // number of lags (for Unit-Root tests) 35 | 36 | // default (nullary) constructor 37 | OLS() {} 38 | // parameter constructor 39 | OLS(const Vector& y, const Matrix& x, bool stats = false); 40 | // compute OLS statistics (R2, adj_R2, F and DW stats) 41 | void get_stats(const Vector& y, const Matrix& x); 42 | // print results 43 | void show(); 44 | 45 | private: 46 | int j0; // intercept index in matrix of independent variables 47 | int nvar = 0; // number of independent variables (regressors excluding intercept) 48 | bool intercept = false; // control if OLS is with intercept or without 49 | bool stats = false; // control if OLS statistics need to be computed 50 | }; 51 | 52 | //================================================================================================= 53 | 54 | } 55 | 56 | #endif 57 | 58 | 59 | -------------------------------------------------------------------------------- /R/RcppURT/include/OLS.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef OLS_HPP 6 | #define OLS_HPP 7 | 8 | namespace urt { 9 | 10 | //================================================================================================= 11 | 12 | // Multi-Linear Regression by Ordinary Least Squares 13 | // NB: we chose never to make hard copies of x and y for performance reason, indeed in ADF test with lag length optimization we can quickly get a very large x matrix 14 | template 15 | class OLS 16 | { 17 | public: 18 | Vector param; // regressors parameters 19 | Vector t_stat; // regressors statistics 20 | Vector resid; // residuals 21 | Vector var; // regressors variances 22 | 23 | T MSE; // mean of squares for error 24 | T SSR; // sum of squares residuals 25 | T R2 = 0; // R-squared 26 | T adj_R2 = 0; // adjusted R-squared 27 | T F_stat = 0; // F statistic 28 | T DW_stat = 0; // Durbin-Watson statistic 29 | T IC = 0; // model information criterion (for Unit-Root tests) 30 | 31 | int nobs; // number of observations 32 | int nreg; // number of regressors 33 | int ndf; // number of degrees of freedom 34 | int lags = 0; // number of lags (for Unit-Root tests) 35 | 36 | // default (nullary) constructor 37 | OLS() {} 38 | // parameter constructor 39 | OLS(const Vector& y, const Matrix& x, bool stats = false); 40 | // compute OLS statistics (R2, adj_R2, F and DW stats) 41 | void get_stats(const Vector& y, const Matrix& x); 42 | // print results 43 | void show(); 44 | 45 | private: 46 | int j0; // intercept index in matrix of independent variables 47 | int nvar = 0; // number of independent variables (regressors excluding intercept) 48 | bool intercept = false; // control if OLS is with intercept or without 49 | bool stats = false; // control if OLS statistics need to be computed 50 | }; 51 | 52 | //================================================================================================= 53 | 54 | } 55 | 56 | #endif 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/ADF.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | namespace urt { 8 | 9 | //================================================================================================= 10 | 11 | // parameter constructor for computing ADF test for a given number of lags 12 | template 13 | ADF::ADF(const Vector& data, int lags, const std::string& trend, bool regression) : ur(data, lags, trend, regression) 14 | { 15 | ur::test_name = _test_name; 16 | ur::valid_trends = _valid_trends; 17 | } 18 | 19 | //************************************************************************************************* 20 | 21 | // parameter constructor for computing ADF test with lag length optimization 22 | template 23 | ADF::ADF(const Vector& data, const std::string& method, const std::string& trend, bool regression) : ur(data, method, trend, regression) 24 | { 25 | ur::test_name = _test_name; 26 | ur::valid_trends = _valid_trends; 27 | } 28 | 29 | //************************************************************************************************* 30 | 31 | // compute test statistic 32 | template 33 | const T& ADF::statistic() 34 | { 35 | // setting type of lags (if a default type of lags value has been chosen) 36 | ur::set_lags_type(); 37 | // setting optimization method 38 | ur::set_method(); 39 | // setting number of lags 40 | ur::set_lags(); 41 | // setting regression trend 42 | ur::set_trend(); 43 | // computing ADF test 44 | ur::compute_adf(); 45 | 46 | return ur::stat; 47 | } 48 | 49 | //************************************************************************************************* 50 | 51 | // compute test p-value 52 | template 53 | const T& ADF::pvalue() 54 | { 55 | // computing test statistic 56 | this->statistic(); 57 | // setting critical values coefficients pointer 58 | ur::coeff_ptr = &coeff_adf.at(ur::trend); 59 | // computing p-value 60 | ur::pvalue(); 61 | 62 | return ur::pval; 63 | } 64 | 65 | //************************************************************************************************* 66 | 67 | // output test results 68 | template 69 | void ADF::show() 70 | { 71 | // in case user modified test type, for ADF it should always be empty 72 | ur::test_type = std::string(); 73 | // computing p-value 74 | this->pvalue(); 75 | // outputting results 76 | ur::show(); 77 | } 78 | 79 | //================================================================================================= 80 | 81 | } 82 | 83 | -------------------------------------------------------------------------------- /R/RcppURT/src/ADF.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | namespace urt { 8 | 9 | //================================================================================================= 10 | 11 | // parameter constructor for computing ADF test for a given number of lags 12 | template 13 | ADF::ADF(const Vector& data, int lags, const std::string& trend, bool regression) : ur(data, lags, trend, regression) 14 | { 15 | ur::test_name = _test_name; 16 | ur::valid_trends = _valid_trends; 17 | } 18 | 19 | //************************************************************************************************* 20 | 21 | // parameter constructor for computing ADF test with lag length optimization 22 | template 23 | ADF::ADF(const Vector& data, const std::string& method, const std::string& trend, bool regression) : ur(data, method, trend, regression) 24 | { 25 | ur::test_name = _test_name; 26 | ur::valid_trends = _valid_trends; 27 | } 28 | 29 | //************************************************************************************************* 30 | 31 | // compute test statistic 32 | template 33 | const T& ADF::statistic() 34 | { 35 | // setting type of lags (if a default type of lags value has been chosen) 36 | ur::set_lags_type(); 37 | // setting optimization method 38 | ur::set_method(); 39 | // setting number of lags 40 | ur::set_lags(); 41 | // setting regression trend 42 | ur::set_trend(); 43 | // computing ADF test 44 | ur::compute_adf(); 45 | 46 | return ur::stat; 47 | } 48 | 49 | //************************************************************************************************* 50 | 51 | // compute test p-value 52 | template 53 | const T& ADF::pvalue() 54 | { 55 | // computing test statistic 56 | this->statistic(); 57 | // setting critical values coefficients pointer 58 | ur::coeff_ptr = &coeff_adf.at(ur::trend); 59 | // computing p-value 60 | ur::pvalue(); 61 | 62 | return ur::pval; 63 | } 64 | 65 | //************************************************************************************************* 66 | 67 | // output test results 68 | template 69 | void ADF::show() 70 | { 71 | // in case user modified test type, for ADF it should always be empty 72 | ur::test_type = std::string(); 73 | // computing p-value 74 | this->pvalue(); 75 | // outputting results 76 | ur::show(); 77 | } 78 | 79 | //================================================================================================= 80 | 81 | } 82 | 83 | -------------------------------------------------------------------------------- /Python/CyURT.pxd: -------------------------------------------------------------------------------- 1 | cimport cython 2 | 3 | from libcpp cimport bool 4 | from libcpp.string cimport string 5 | from libcpp.vector cimport vector 6 | 7 | from CyBlaze cimport _unaligned 8 | from CyBlaze cimport _unpadded 9 | from CyBlaze cimport _columnVector 10 | from CyBlaze cimport _columnMajor 11 | 12 | from CyBlaze cimport CustomVector 13 | from CyBlaze cimport CustomMatrix 14 | from CyBlaze cimport DynamicVector 15 | 16 | cdef extern from "../include/URT.hpp" namespace "urt": 17 | 18 | cdef cppclass OLS[T]: 19 | # members 20 | DynamicVector[T] param 21 | DynamicVector[T] t_stat 22 | DynamicVector[T] resid 23 | DynamicVector[T] var 24 | T MSE 25 | T R2 26 | T adj_R2 27 | T F_stat 28 | T DW_stat 29 | T IC 30 | # constructors 31 | OLS() 32 | OLS(const CustomVector[T,_unaligned,_unpadded,_columnVector]&, const CustomMatrix[T,_unaligned,_unpadded,_columnMajor]&, bool) except + 33 | # method 34 | void show() except + 35 | 36 | cdef cppclass UnitRoot[T]: 37 | # members 38 | string lags_type 39 | string method 40 | string test_type 41 | string trend 42 | T level 43 | int lags 44 | int max_lags 45 | int niter 46 | bool bootstrap 47 | bool regression 48 | # method 49 | const T& get_stat() const 50 | const T& get_pval() const 51 | const OLS[T]& get_ols() const 52 | const vector[string]& get_trends() const 53 | const T& statistic() except + 54 | const T& pvalue() except + 55 | void show() except + 56 | 57 | cdef cppclass ADF[T](UnitRoot[T]): 58 | # constructors 59 | ADF(const CustomVector[T,_unaligned,_unpadded,_columnVector]&, int, const char*, bool) except + 60 | ADF(const CustomVector[T,_unaligned,_unpadded,_columnVector]&, const char*, const char*, bool) except + 61 | 62 | cdef cppclass DFGLS[T](UnitRoot[T]): 63 | # constructors 64 | DFGLS(const CustomVector[T,_unaligned,_unpadded,_columnVector]&, int, const char*, bool) except + 65 | DFGLS(const CustomVector[T,_unaligned,_unpadded,_columnVector]&, const char*, const char*, bool) except + 66 | 67 | cdef cppclass PP[T](UnitRoot[T]): 68 | # constructorss 69 | PP(const CustomVector[T,_unaligned,_unpadded,_columnVector]&, int, const char*, const char*, bool) except + 70 | PP(const CustomVector[T,_unaligned,_unpadded,_columnVector]&, const char*, const char*, const char*, bool) except + 71 | 72 | cdef cppclass KPSS[T](UnitRoot[T]): 73 | # constructors 74 | KPSS(const CustomVector[T,_unaligned,_unpadded,_columnVector]&, int, const char*) except + 75 | KPSS(const CustomVector[T,_unaligned,_unpadded,_columnVector]&, const char*, const char*) except + 76 | -------------------------------------------------------------------------------- /R/RcppURT/R/OLSlist.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | #-------------------------------------------------------------------------------------------------- 6 | 7 | # OLS as a list for unit-root tests 8 | OLSlist_d <- function(ptr) { 9 | 10 | ols_ptr <- .Call('UnitRoot_d_get_ols', PACKAGE = 'RcppURT', ptr_ = ptr) 11 | 12 | return(list("param" = .Call('OLS_d_get_param', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 13 | "resid" = .Call('OLS_d_get_resid', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 14 | "t_stat" = .Call('OLS_d_get_t_stat', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 15 | "var" = .Call('OLS_d_get_var', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 16 | "MSE" = .Call('OLS_d_get_MSE', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 17 | "ndf" = .Call('OLS_d_get_ndf', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 18 | "R2" = .Call('OLS_d_get_R2', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 19 | "adj_R2" = .Call('OLS_d_get_adj_R2', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 20 | "F_stat" = .Call('OLS_d_get_F_stat', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 21 | "DW_stat" = .Call('OLS_d_get_DW_stat', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 22 | "IC" = .Call('OLS_d_get_IC', PACKAGE = 'RcppURT', ptr_ = ols_ptr))) 23 | } 24 | 25 | #-------------------------------------------------------------------------------------------------- 26 | ################################################################################################### 27 | #-------------------------------------------------------------------------------------------------- 28 | 29 | # OLS as a list for unit-root tests 30 | OLSlist_f <- function(ptr) { 31 | 32 | ols_ptr <- .Call('UnitRoot_f_get_ols', PACKAGE = 'RcppURT', ptr_ = ptr) 33 | 34 | return(list("param" = .Call('OLS_f_get_param', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 35 | "resid" = .Call('OLS_f_get_resid', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 36 | "t_stat" = .Call('OLS_f_get_t_stat', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 37 | "var" = .Call('OLS_f_get_var', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 38 | "MSE" = .Call('OLS_f_get_MSE', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 39 | "ndf" = .Call('OLS_f_get_ndf', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 40 | "R2" = .Call('OLS_f_get_R2', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 41 | "adj_R2" = .Call('OLS_f_get_adj_R2', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 42 | "F_stat" = .Call('OLS_f_get_F_stat', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 43 | "DW_stat" = .Call('OLS_f_get_DW_stat', PACKAGE = 'RcppURT', ptr_ = ols_ptr), 44 | "IC" = .Call('OLS_f_get_IC', PACKAGE = 'RcppURT', ptr_ = ols_ptr))) 45 | } 46 | 47 | 48 | #-------------------------------------------------------------------------------------------------- 49 | -------------------------------------------------------------------------------- /src/DFGLS.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | namespace urt { 8 | 9 | //================================================================================================= 10 | 11 | // parameter constructor for computing DF-GLS test for a given number of lags 12 | template 13 | DFGLS::DFGLS(const Vector& data, int lags, const std::string& trend, bool regression) : ur(data, lags, trend, regression) 14 | { 15 | ur::test_name = _test_name; 16 | ur::valid_trends = _valid_trends; 17 | } 18 | 19 | //************************************************************************************************* 20 | 21 | // parameter constructor for computing ADF test with lag length optimization 22 | template 23 | DFGLS::DFGLS(const Vector& data, const std::string& method, const std::string& trend, bool regression) : ur(data, method, trend, regression) 24 | { 25 | ur::test_name = _test_name; 26 | ur::valid_trends = _valid_trends; 27 | } 28 | 29 | //************************************************************************************************* 30 | 31 | // compute test statistic 32 | template 33 | const T& DFGLS::statistic() 34 | { 35 | // setting type of lags (if a default type of lags value has been chosen) 36 | ur::set_lags_type(); 37 | // setting optimization method 38 | ur::set_method(); 39 | // setting number of lags 40 | ur::set_lags(); 41 | // setting regression trend 42 | ur::set_trend(); 43 | // if new trend or new data from bootstrap 44 | if (ur::new_trend || ur::new_data) 45 | { 46 | // if data do not come from bootstrap we set ptr back to the original data 47 | if (!ur::new_data) 48 | ur::set_data(); 49 | // detrending data by GLS 50 | ur::gls_detrend(); 51 | } 52 | // setting regression trend to no constant via npar 53 | ur::npar = 1; 54 | // computing ADF test 55 | ur::compute_adf(); 56 | 57 | return ur::stat; 58 | } 59 | 60 | //************************************************************************************************* 61 | 62 | // compute test p-value 63 | template 64 | const T& DFGLS::pvalue() 65 | { 66 | // computing test statistic 67 | this->statistic(); 68 | // setting critical values coefficients pointer 69 | ur::coeff_ptr = &coeff_dfgls.at(ur::trend); 70 | // computing p-value 71 | ur::pvalue(); 72 | 73 | return ur::pval; 74 | } 75 | 76 | //************************************************************************************************* 77 | 78 | // output test results 79 | template 80 | void DFGLS::show() 81 | { 82 | // in case user modified test type, for DFGLS it should always be empty 83 | ur::test_type = std::string(); 84 | // computing p-value 85 | this->pvalue(); 86 | // outputting results 87 | ur::show(); 88 | } 89 | 90 | //================================================================================================= 91 | 92 | } 93 | 94 | -------------------------------------------------------------------------------- /R/RcppURT/src/DFGLS.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | namespace urt { 8 | 9 | //================================================================================================= 10 | 11 | // parameter constructor for computing DF-GLS test for a given number of lags 12 | template 13 | DFGLS::DFGLS(const Vector& data, int lags, const std::string& trend, bool regression) : ur(data, lags, trend, regression) 14 | { 15 | ur::test_name = _test_name; 16 | ur::valid_trends = _valid_trends; 17 | } 18 | 19 | //************************************************************************************************* 20 | 21 | // parameter constructor for computing ADF test with lag length optimization 22 | template 23 | DFGLS::DFGLS(const Vector& data, const std::string& method, const std::string& trend, bool regression) : ur(data, method, trend, regression) 24 | { 25 | ur::test_name = _test_name; 26 | ur::valid_trends = _valid_trends; 27 | } 28 | 29 | //************************************************************************************************* 30 | 31 | // compute test statistic 32 | template 33 | const T& DFGLS::statistic() 34 | { 35 | // setting type of lags (if a default type of lags value has been chosen) 36 | ur::set_lags_type(); 37 | // setting optimization method 38 | ur::set_method(); 39 | // setting number of lags 40 | ur::set_lags(); 41 | // setting regression trend 42 | ur::set_trend(); 43 | // if new trend or new data from bootstrap 44 | if (ur::new_trend || ur::new_data) 45 | { 46 | // if data do not come from bootstrap we set ptr back to the original data 47 | if (!ur::new_data) 48 | ur::set_data(); 49 | // detrending data by GLS 50 | ur::gls_detrend(); 51 | } 52 | // setting regression trend to no constant via npar 53 | ur::npar = 1; 54 | // computing ADF test 55 | ur::compute_adf(); 56 | 57 | return ur::stat; 58 | } 59 | 60 | //************************************************************************************************* 61 | 62 | // compute test p-value 63 | template 64 | const T& DFGLS::pvalue() 65 | { 66 | // computing test statistic 67 | this->statistic(); 68 | // setting critical values coefficients pointer 69 | ur::coeff_ptr = &coeff_dfgls.at(ur::trend); 70 | // computing p-value 71 | ur::pvalue(); 72 | 73 | return ur::pval; 74 | } 75 | 76 | //************************************************************************************************* 77 | 78 | // output test results 79 | template 80 | void DFGLS::show() 81 | { 82 | // in case user modified test type, for DFGLS it should always be empty 83 | ur::test_type = std::string(); 84 | // computing p-value 85 | this->pvalue(); 86 | // outputting results 87 | ur::show(); 88 | } 89 | 90 | //================================================================================================= 91 | 92 | } 93 | 94 | -------------------------------------------------------------------------------- /R/RcppURT/src/RcppOLSreg.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | #include 8 | 9 | using namespace urt; 10 | 11 | //------------------------------------------------------------------------------------------------- 12 | 13 | // C++ class OLS as a function 14 | RcppExport SEXP OLSreg_d(SEXP y_, SEXP x_, SEXP stats_, SEXP output_) 15 | { 16 | Rcpp::NumericVector yr(y_); 17 | arma::vec y(yr.begin(), yr.size(), false); 18 | Rcpp::NumericMatrix xr(x_); 19 | arma::mat x(xr.begin(), xr.nrow(), xr.ncol(), false); 20 | bool stats = Rcpp::as(stats_); 21 | bool output = Rcpp::as(output_); 22 | 23 | OLS fit; 24 | 25 | try { 26 | fit = OLS(y, x, stats); 27 | if (output) fit.show(); 28 | } catch(std::exception &ex) { 29 | forward_exception_to_r(ex); 30 | } 31 | 32 | return Rcpp::List::create(Rcpp::Named("param") = fit.param, 33 | Rcpp::Named("resid") = fit.resid, 34 | Rcpp::Named("t_stat") = fit.resid, 35 | Rcpp::Named("var") = fit.resid, 36 | Rcpp::Named("MSE") = fit.MSE, 37 | Rcpp::Named("ndf") = fit.ndf, 38 | Rcpp::Named("R2") = fit.R2, 39 | Rcpp::Named("adj_R2") = fit.adj_R2, 40 | Rcpp::Named("F_stat") = fit.F_stat, 41 | Rcpp::Named("DW_stat") = fit.DW_stat); 42 | } 43 | 44 | //------------------------------------------------------------------------------------------------- 45 | //################################################################################################# 46 | //------------------------------------------------------------------------------------------------- 47 | 48 | // C++ class OLS as a function 49 | RcppExport SEXP OLSreg_f(SEXP y_, SEXP x_, SEXP stats_, SEXP output_) 50 | { 51 | const arma::fvec& y = Rcpp::as(y_); 52 | const arma::fmat& x = Rcpp::as(x_); 53 | 54 | // NB: unfortunately for single precision type we cannot use the same method as with double precision type for converting SEXP objects into Armadillo arrays as we would have to cast NumericVector and NumericMatrix into float and then generate another copy, moreover R do not support float type so we would get NaN values 55 | 56 | bool stats = Rcpp::as(stats_); 57 | bool output = Rcpp::as(output_); 58 | 59 | OLS fit; 60 | 61 | try { 62 | fit = OLS(y, x, stats); 63 | if (output) fit.show(); 64 | } catch(std::exception &ex) { 65 | forward_exception_to_r(ex); 66 | } 67 | 68 | return Rcpp::List::create(Rcpp::Named("param") = fit.param, 69 | Rcpp::Named("resid") = fit.resid, 70 | Rcpp::Named("t_stat") = fit.resid, 71 | Rcpp::Named("var") = fit.resid, 72 | Rcpp::Named("MSE") = fit.MSE, 73 | Rcpp::Named("ndf") = fit.ndf, 74 | Rcpp::Named("R2") = fit.R2, 75 | Rcpp::Named("adj_R2") = fit.adj_R2, 76 | Rcpp::Named("F_stat") = fit.F_stat, 77 | Rcpp::Named("DW_stat") = fit.DW_stat); 78 | 79 | } 80 | 81 | //------------------------------------------------------------------------------------------------- 82 | -------------------------------------------------------------------------------- /Python/CyBlaze.pxd: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | cimport numpy as np 3 | cimport cython 4 | 5 | from libc.string cimport memcpy 6 | from libcpp cimport bool 7 | from libcpp.memory cimport unique_ptr 8 | from cython.operator cimport dereference as deref 9 | 10 | #-------------------------------------------------------------------------------------------------- 11 | 12 | # Blaze C++ constant unaligned exposed to Cython 13 | cdef extern from "blaze/math/AlignmentFlag.h" namespace "blaze": 14 | ctypedef bool _unaligned "blaze::unaligned" 15 | 16 | # Blaze C++ constant unpadded exposed to Cython 17 | cdef extern from "blaze/math/PaddingFlag.h" namespace "blaze": 18 | ctypedef bool _unpadded "blaze::unpadded" 19 | 20 | # Blaze C++ constant columnVector exposed to Cython 21 | cdef extern from "blaze/math/TransposeFlag.h" namespace "blaze": 22 | ctypedef bool _columnVector "blaze::columnVector" 23 | 24 | # Blaze C++ constant columnMajor exposed to Cython 25 | cdef extern from "blaze/math/StorageOrder.h" namespace "blaze": 26 | ctypedef bool _columnMajor "blaze::columnMajor" 27 | 28 | #-------------------------------------------------------------------------------------------------- 29 | 30 | # Blaze C++ class CustomVector exposed to Cython 31 | cdef extern from "blaze/math/CustomVector.h" namespace "blaze" nogil: 32 | cdef cppclass CustomVector[T,_unaligned,_unpadded,_columnVector]: 33 | CustomVector() nogil 34 | CustomVector(T*, int ) nogil 35 | 36 | # convenient typedef 37 | ctypedef CustomVector[double,_unaligned,_unpadded,_columnVector] VectorCd 38 | ctypedef CustomVector[float,_unaligned,_unpadded,_columnVector] VectorCf 39 | 40 | #-------------------------------------------------------------------------------------------------- 41 | 42 | # Blaze C++ class CustomMatrix exposed to Cython 43 | cdef extern from "blaze/math/CustomMatrix.h" namespace "blaze" nogil: 44 | cdef cppclass CustomMatrix[T,_unaligned,_unpadded,_columnMajor]: 45 | CustomMatrix() nogil 46 | CustomMatrix(T*, int, int ) nogil 47 | 48 | # convenient typedef 49 | ctypedef CustomMatrix[double,_unaligned,_unpadded,_columnMajor] MatrixCd 50 | ctypedef CustomMatrix[float,_unaligned,_unpadded,_columnMajor] MatrixCf 51 | 52 | #-------------------------------------------------------------------------------------------------- 53 | 54 | # Blaze C++ class DynamicVector exposed to Cython 55 | cdef extern from "blaze/math/DynamicVector.h" namespace "blaze" nogil: 56 | cdef cppclass DynamicVector[T]: 57 | DynamicVector() nogil 58 | int size() nogil 59 | const T& operator[](int ) nogil 60 | 61 | # convenient typedef 62 | ctypedef DynamicVector[double] VectorDd 63 | ctypedef DynamicVector[float] VectorDf 64 | 65 | #-------------------------------------------------------------------------------------------------- 66 | 67 | # Blaze DynamicVector[double] wrapped into a Numpy 1-dim array without copying the data 68 | cdef inline np.ndarray[np.double_t,ndim=1] blaze_vec_to_numpy(VectorDd& xd): 69 | cdef np.npy_intp shape[1] 70 | shape[0] = xd.size() 71 | cdef np.ndarray[np.double_t,ndim=1] x = np.PyArray_SimpleNewFromData(1,shape,np.NPY_DOUBLE,&xd[0]) 72 | return x 73 | 74 | #-------------------------------------------------------------------------------------------------- 75 | 76 | # Blaze DynamicVector[float] wrapped into a Numpy 1-dim array without copying the data 77 | cdef inline np.ndarray[np.float32_t,ndim=1] blaze_fvec_to_numpy(VectorDf& xf): 78 | cdef np.npy_intp shape[1] 79 | shape[0] = xf.size() 80 | cdef np.ndarray[np.float32_t,ndim=1] x = np.PyArray_SimpleNewFromData(1,shape,np.NPY_FLOAT32,&xf[0]) 81 | return x 82 | 83 | #-------------------------------------------------------------------------------------------------- 84 | 85 | -------------------------------------------------------------------------------- /R/RcppURT/R/OLS.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | suppressMessages(library(R6)) 6 | 7 | #-------------------------------------------------------------------------------------------------- 8 | 9 | # C++ class OLS exposed to R 10 | OLS_d <- R6Class("OLS_d", 11 | private = list( 12 | ptr = NULL 13 | ), 14 | public = list( 15 | initialize = function(y = NA, x = NA, stats = FALSE) { 16 | private$ptr <- .Call('OLS_d__new', PACKAGE = 'RcppURT', y_ = y, x_ = x, stats_ = stats) 17 | }, 18 | get_stats = function(y = NA, x = NA) { 19 | .Call('OLS_d_get_stats', PACKAGE = 'RcppURT', ptr_ = private$ptr, y_ = y, x_ = x) 20 | }, 21 | show = function() { 22 | .Call('OLS_d_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 23 | } 24 | ), 25 | active = list( 26 | param = function() { 27 | .Call('OLS_d_get_param', PACKAGE = 'RcppURT', ptr_ = private$ptr) 28 | }, 29 | resid = function() { 30 | .Call('OLS_d_get_resid', PACKAGE = 'RcppURT', ptr_ = private$ptr) 31 | }, 32 | t_stat = function() { 33 | .Call('OLS_d_get_t_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 34 | }, 35 | var = function() { 36 | .Call('OLS_d_get_var', PACKAGE = 'RcppURT', ptr_ = private$ptr) 37 | }, 38 | MSE = function() { 39 | .Call('OLS_d_get_MSE', PACKAGE = 'RcppURT', ptr_ = private$ptr) 40 | }, 41 | ndf = function() { 42 | .Call('OLS_d_get_ndf', PACKAGE = 'RcppURT', ptr_ = private$ptr) 43 | }, 44 | R2 = function() { 45 | .Call('OLS_d_get_R2', PACKAGE = 'RcppURT', ptr_ = private$ptr) 46 | }, 47 | adj_R2 = function() { 48 | .Call('OLS_d_get_adj_R2', PACKAGE = 'RcppURT', ptr_ = private$ptr) 49 | }, 50 | F_stat = function() { 51 | .Call('OLS_d_get_F_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 52 | }, 53 | DW_stat = function() { 54 | .Call('OLS_d_get_DW_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 55 | } 56 | ) 57 | ) 58 | 59 | #-------------------------------------------------------------------------------------------------- 60 | ################################################################################################### 61 | #-------------------------------------------------------------------------------------------------- 62 | 63 | # C++ class OLS exposed to R 64 | OLS_f <- R6Class("OLS_f", 65 | private = list( 66 | ptr = NULL 67 | ), 68 | public = list( 69 | initialize = function(y = NA, x = NA, stats = FALSE) { 70 | private$ptr <- .Call('OLS_f__new', PACKAGE = 'RcppURT', y_ = y, x_ = x, stats_ = stats) 71 | }, 72 | get_stats = function(y = NA, x = NA) { 73 | .Call('OLS_f_get_stats', PACKAGE = 'RcppURT', ptr_ = private$ptr, y_ = y, x_ = x) 74 | }, 75 | show = function() { 76 | .Call('OLS_f_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 77 | } 78 | ), 79 | active = list( 80 | param = function() { 81 | .Call('OLS_f_get_param', PACKAGE = 'RcppURT', ptr_ = private$ptr) 82 | }, 83 | resid = function() { 84 | .Call('OLS_f_get_resid', PACKAGE = 'RcppURT', ptr_ = private$ptr) 85 | }, 86 | t_stat = function() { 87 | .Call('OLS_f_get_t_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 88 | }, 89 | var = function() { 90 | .Call('OLS_f_get_var', PACKAGE = 'RcppURT', ptr_ = private$ptr) 91 | }, 92 | MSE = function() { 93 | .Call('OLS_f_get_MSE', PACKAGE = 'RcppURT', ptr_ = private$ptr) 94 | }, 95 | ndf = function() { 96 | .Call('OLS_f_get_ndf', PACKAGE = 'RcppURT', ptr_ = private$ptr) 97 | }, 98 | R2 = function() { 99 | .Call('OLS_f_get_R2', PACKAGE = 'RcppURT', ptr_ = private$ptr) 100 | }, 101 | adj_R2 = function() { 102 | .Call('OLS_f_get_adj_R2', PACKAGE = 'RcppURT', ptr_ = private$ptr) 103 | }, 104 | F_stat = function() { 105 | .Call('OLS_f_get_F_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 106 | }, 107 | DW_stat = function() { 108 | .Call('OLS_f_get_DW_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 109 | } 110 | ) 111 | ) 112 | 113 | #-------------------------------------------------------------------------------------------------- 114 | 115 | -------------------------------------------------------------------------------- /include/URT.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef URT_HPP 6 | #define URT_HPP 7 | 8 | //================================================================================================= 9 | 10 | #ifdef USE_BLAZE 11 | 12 | // neutralizing OpenMP in Blaze if defined 13 | #ifdef _OPENMP 14 | #undef _OPENMP 15 | #define SET_OPENMP_BACK 16 | #endif 17 | 18 | #ifndef NDEBUG 19 | #define NDEBUG 20 | #endif 21 | 22 | #if defined(USE_MKL) || defined (USE_BLAS) 23 | #define BLAZE_BLAS_MODE 1 24 | #define BLAZE_USE_BLAS_MATRIX_VECTOR_MULTIPLICATION 1 25 | #define BLAZE_USE_BLAS_MATRIX_MATRIX_MULTIPLICATION 1 26 | #endif 27 | 28 | #ifndef BLAZE_BLAS_INCLUDE_FILE 29 | #ifdef USE_MKL 30 | #define BLAZE_BLAS_INCLUDE_FILE 31 | #elif USE_BLAS 32 | #define BLAZE_BLAS_INCLUDE_FILE 33 | #endif 34 | #endif 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | namespace urt { 42 | template 43 | using Matrix = blaze::DynamicMatrix; 44 | template 45 | using CMatrix = blaze::CustomMatrix; 46 | template 47 | using Vector = blaze::DynamicVector; 48 | template 49 | using CVector = blaze::CustomVector; 50 | } 51 | 52 | #ifdef SET_OPENMP_BACK 53 | #define _OPENMP 54 | #endif 55 | 56 | //************************************************************************************************* 57 | 58 | #elif USE_EIGEN 59 | 60 | #ifdef USE_MKL 61 | #define EIGEN_USE_MKL_ALL 62 | #elif USE_BLAS 63 | #define EIGEN_USE_BLAS 64 | #endif 65 | 66 | #define EIGEN_DONT_PARALLELIZE 67 | #define EIGEN_NO_DEBUG 68 | #define EIGEN_NO_STATIC_ASSERT 69 | 70 | #include 71 | 72 | namespace urt { 73 | template 74 | using Matrix = Eigen::Matrix; 75 | template 76 | using Vector = Eigen::Matrix; 77 | } 78 | 79 | //************************************************************************************************* 80 | 81 | #else 82 | 83 | #ifndef ARMA_DONT_USE_WRAPPER 84 | #if defined(USE_MKL) || defined (USE_BLAS) 85 | #define ARMA_DONT_USE_WRAPPER 86 | #endif 87 | #endif 88 | 89 | #ifndef ARMA_NO_DEBUG 90 | #define ARMA_NO_DEBUG 91 | #endif 92 | 93 | #ifndef ARMA_USE_CXX11 94 | #define ARMA_USE_CXX11 95 | #endif 96 | 97 | #include 98 | 99 | namespace urt { 100 | template 101 | using Matrix = arma::Mat; 102 | template 103 | using Vector = arma::Col; 104 | } 105 | 106 | #endif 107 | 108 | //************************************************************************************************* 109 | 110 | #include 111 | #include 112 | 113 | #include 114 | #include 115 | #include 116 | #include 117 | #include 118 | 119 | #include "./CsvManager.hpp" 120 | #include "./Tools.hpp" 121 | 122 | #include "./OLS.hpp" 123 | #include "./UnitRoot.hpp" 124 | #include "./ADF.hpp" 125 | #include "./DFGLS.hpp" 126 | #include "./PP.hpp" 127 | #include "./KPSS.hpp" 128 | 129 | // template class specializations 130 | template class urt::OLS; 131 | template class urt::OLS; 132 | template class urt::UnitRoot; 133 | template class urt::UnitRoot; 134 | template class urt::ADF; 135 | template class urt::ADF; 136 | template class urt::DFGLS; 137 | template class urt::DFGLS; 138 | template class urt::PP; 139 | template class urt::PP; 140 | template class urt::KPSS; 141 | template class urt::KPSS; 142 | 143 | //================================================================================================= 144 | 145 | #endif 146 | -------------------------------------------------------------------------------- /R/RcppURT/include/URT.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef URT_HPP 6 | #define URT_HPP 7 | 8 | //================================================================================================= 9 | 10 | #ifdef USE_BLAZE 11 | 12 | // neutralizing OpenMP in Blaze if defined 13 | #ifdef _OPENMP 14 | #undef _OPENMP 15 | #define SET_OPENMP_BACK 16 | #endif 17 | 18 | #ifndef NDEBUG 19 | #define NDEBUG 20 | #endif 21 | 22 | #if defined(USE_MKL) || defined (USE_BLAS) 23 | #define BLAZE_BLAS_MODE 1 24 | #define BLAZE_USE_BLAS_MATRIX_VECTOR_MULTIPLICATION 1 25 | #define BLAZE_USE_BLAS_MATRIX_MATRIX_MULTIPLICATION 1 26 | #endif 27 | 28 | #ifndef BLAZE_BLAS_INCLUDE_FILE 29 | #ifdef USE_MKL 30 | #define BLAZE_BLAS_INCLUDE_FILE 31 | #elif USE_BLAS 32 | #define BLAZE_BLAS_INCLUDE_FILE 33 | #endif 34 | #endif 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | namespace urt { 42 | template 43 | using Matrix = blaze::DynamicMatrix; 44 | template 45 | using CMatrix = blaze::CustomMatrix; 46 | template 47 | using Vector = blaze::DynamicVector; 48 | template 49 | using CVector = blaze::CustomVector; 50 | } 51 | 52 | #ifdef SET_OPENMP_BACK 53 | #define _OPENMP 54 | #endif 55 | 56 | //************************************************************************************************* 57 | 58 | #elif USE_EIGEN 59 | 60 | #ifdef USE_MKL 61 | #define EIGEN_USE_MKL_ALL 62 | #elif USE_BLAS 63 | #define EIGEN_USE_BLAS 64 | #endif 65 | 66 | #define EIGEN_DONT_PARALLELIZE 67 | #define EIGEN_NO_DEBUG 68 | #define EIGEN_NO_STATIC_ASSERT 69 | 70 | #include 71 | 72 | namespace urt { 73 | template 74 | using Matrix = Eigen::Matrix; 75 | template 76 | using Vector = Eigen::Matrix; 77 | } 78 | 79 | //************************************************************************************************* 80 | 81 | #else 82 | 83 | #ifndef ARMA_DONT_USE_WRAPPER 84 | #if defined(USE_MKL) || defined (USE_BLAS) 85 | #define ARMA_DONT_USE_WRAPPER 86 | #endif 87 | #endif 88 | 89 | #ifndef ARMA_NO_DEBUG 90 | #define ARMA_NO_DEBUG 91 | #endif 92 | 93 | #ifndef ARMA_USE_CXX11 94 | #define ARMA_USE_CXX11 95 | #endif 96 | 97 | #include 98 | 99 | namespace urt { 100 | template 101 | using Matrix = arma::Mat; 102 | template 103 | using Vector = arma::Col; 104 | } 105 | 106 | #endif 107 | 108 | //************************************************************************************************* 109 | 110 | #include 111 | #include 112 | 113 | #include 114 | #include 115 | #include 116 | #include 117 | #include 118 | 119 | #include "./CsvManager.hpp" 120 | #include "./Tools.hpp" 121 | 122 | #include "./OLS.hpp" 123 | #include "./UnitRoot.hpp" 124 | #include "./ADF.hpp" 125 | #include "./DFGLS.hpp" 126 | #include "./PP.hpp" 127 | #include "./KPSS.hpp" 128 | 129 | // template class specializations 130 | template class urt::OLS; 131 | template class urt::OLS; 132 | template class urt::UnitRoot; 133 | template class urt::UnitRoot; 134 | template class urt::ADF; 135 | template class urt::ADF; 136 | template class urt::DFGLS; 137 | template class urt::DFGLS; 138 | template class urt::PP; 139 | template class urt::PP; 140 | template class urt::KPSS; 141 | template class urt::KPSS; 142 | 143 | //================================================================================================= 144 | 145 | #endif 146 | -------------------------------------------------------------------------------- /include/CsvManager.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef CSVMANAGER_HPP 6 | #define CSVMANAGER_HPP 7 | 8 | namespace urt { 9 | 10 | //================================================================================================= 11 | 12 | // write Vector to csv file 13 | template 14 | void WriteToCSV(const std::string& filename, const Vector& x) 15 | { 16 | std::ofstream myfile(filename); 17 | 18 | #ifdef USE_ARMA 19 | int nrows = x.n_rows; 20 | #elif defined(USE_BLAZE) || defined(USE_EIGEN) 21 | int nrows = x.size(); 22 | #endif 23 | 24 | for (int i = 0; i < nrows; ++i) 25 | myfile << x[i] << "\n"; 26 | 27 | myfile.close(); 28 | } 29 | 30 | //************************************************************************************************* 31 | 32 | // write Matrix to csv file 33 | template 34 | void WriteToCSV(const std::string& filename, const Matrix& x) 35 | { 36 | std::ofstream myfile(filename); 37 | 38 | #ifdef USE_ARMA 39 | int nrows = x.n_rows; 40 | int ncols = x.n_cols; 41 | #elif USE_BLAZE 42 | int nrows = x.rows(); 43 | int ncols = x.columns(); 44 | #elif USE_EIGEN 45 | int nrows = x.rows(); 46 | int ncols = x.cols(); 47 | #endif 48 | 49 | for (int i = 0; i < nrows; ++i) { 50 | for (int j = 0; j < ncols; ++j) { 51 | 52 | myfile << x(i, j); 53 | 54 | if (j != ncols - 1) { 55 | myfile << ","; 56 | } else { 57 | myfile << "\n"; 58 | } 59 | } 60 | } 61 | 62 | myfile.close(); 63 | } 64 | 65 | //************************************************************************************************* 66 | 67 | // read data from csv file and store them into a Vector 68 | template 69 | void ReadFromCSV(const std::string& filename, Vector& x) 70 | { 71 | std::ifstream myfile(filename); 72 | 73 | if (!myfile.good()) { 74 | std::cerr << "\n Error: " << filename << " cannot be found!\n\n"; 75 | return; 76 | } 77 | 78 | std::string line; 79 | 80 | int nrows = 0; 81 | 82 | // reading data 83 | while (std::getline(myfile, line)) { 84 | #if defined(USE_ARMA) || defined(USE_BLAZE) 85 | x.resize(nrows + 1); 86 | #elif USE_EIGEN 87 | x.conservativeResize(nrows + 1, 1); 88 | #endif 89 | 90 | std::stringstream lineStream(line); 91 | std::string cell; 92 | std::getline(lineStream, cell, ','); 93 | 94 | x[nrows] = std::stod(cell); 95 | ++nrows; 96 | } 97 | myfile.close(); 98 | } 99 | 100 | //************************************************************************************************* 101 | 102 | // read data from csv file and store them into a Matrix 103 | template 104 | void ReadFromCSV(const std::string& filename, Matrix& x) 105 | { 106 | std::ifstream myfile(filename); 107 | 108 | if (!myfile.good()) { 109 | std::cerr << "\n Error: " << filename << " cannot be found!\n\n"; 110 | return; 111 | } 112 | 113 | std::string line; 114 | 115 | int nrows = 0; 116 | 117 | // reading data 118 | while (std::getline(myfile, line)) { 119 | 120 | std::stringstream lineStream(line); 121 | std::string cell; 122 | 123 | #ifdef USE_ARMA 124 | arma::Row z; 125 | #elif USE_BLAZE 126 | blaze::DynamicVector z; 127 | #elif USE_EIGEN 128 | Vector z; 129 | #endif 130 | 131 | int ncols = 0; 132 | 133 | while (std::getline(lineStream, cell, ',')) { 134 | #if defined(USE_ARMA) || defined(USE_BLAZE) 135 | z.resize(ncols + 1); 136 | #elif USE_EIGEN 137 | z.conservativeResize(ncols + 1, Eigen::NoChange); 138 | #endif 139 | z[ncols] = std::stod(cell); 140 | ++ncols; 141 | } 142 | #ifdef USE_ARMA 143 | x.insert_rows(nrows, z); 144 | #elif USE_BLAZE 145 | x.resize(nrows + 1, ncols); 146 | row(x, nrows) = z; 147 | #elif USE_EIGEN 148 | x.conservativeResize(nrows + 1, ncols); 149 | x.row(nrows) = z; 150 | #endif 151 | ++nrows; 152 | } 153 | myfile.close(); 154 | } 155 | 156 | //================================================================================================= 157 | 158 | } 159 | 160 | #endif 161 | -------------------------------------------------------------------------------- /R/RcppURT/include/CsvManager.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef CSVMANAGER_HPP 6 | #define CSVMANAGER_HPP 7 | 8 | namespace urt { 9 | 10 | //================================================================================================= 11 | 12 | // write Vector to csv file 13 | template 14 | void WriteToCSV(const std::string& filename, const Vector& x) 15 | { 16 | std::ofstream myfile(filename); 17 | 18 | #ifdef USE_ARMA 19 | int nrows = x.n_rows; 20 | #elif defined(USE_BLAZE) || defined(USE_EIGEN) 21 | int nrows = x.size(); 22 | #endif 23 | 24 | for (int i = 0; i < nrows; ++i) 25 | myfile << x[i] << "\n"; 26 | 27 | myfile.close(); 28 | } 29 | 30 | //************************************************************************************************* 31 | 32 | // write Matrix to csv file 33 | template 34 | void WriteToCSV(const std::string& filename, const Matrix& x) 35 | { 36 | std::ofstream myfile(filename); 37 | 38 | #ifdef USE_ARMA 39 | int nrows = x.n_rows; 40 | int ncols = x.n_cols; 41 | #elif USE_BLAZE 42 | int nrows = x.rows(); 43 | int ncols = x.columns(); 44 | #elif USE_EIGEN 45 | int nrows = x.rows(); 46 | int ncols = x.cols(); 47 | #endif 48 | 49 | for (int i = 0; i < nrows; ++i) { 50 | for (int j = 0; j < ncols; ++j) { 51 | 52 | myfile << x(i, j); 53 | 54 | if (j != ncols - 1) { 55 | myfile << ","; 56 | } else { 57 | myfile << "\n"; 58 | } 59 | } 60 | } 61 | 62 | myfile.close(); 63 | } 64 | 65 | //************************************************************************************************* 66 | 67 | // read data from csv file and store them into a Vector 68 | template 69 | void ReadFromCSV(const std::string& filename, Vector& x) 70 | { 71 | std::ifstream myfile(filename); 72 | 73 | if (!myfile.good()) { 74 | std::cerr << "\n Error: " << filename << " cannot be found!\n\n"; 75 | return; 76 | } 77 | 78 | std::string line; 79 | 80 | int nrows = 0; 81 | 82 | // reading data 83 | while (std::getline(myfile, line)) { 84 | #if defined(USE_ARMA) || defined(USE_BLAZE) 85 | x.resize(nrows + 1); 86 | #elif USE_EIGEN 87 | x.conservativeResize(nrows + 1, 1); 88 | #endif 89 | 90 | std::stringstream lineStream(line); 91 | std::string cell; 92 | std::getline(lineStream, cell, ','); 93 | 94 | x[nrows] = std::stod(cell); 95 | ++nrows; 96 | } 97 | myfile.close(); 98 | } 99 | 100 | //************************************************************************************************* 101 | 102 | // read data from csv file and store them into a Matrix 103 | template 104 | void ReadFromCSV(const std::string& filename, Matrix& x) 105 | { 106 | std::ifstream myfile(filename); 107 | 108 | if (!myfile.good()) { 109 | std::cerr << "\n Error: " << filename << " cannot be found!\n\n"; 110 | return; 111 | } 112 | 113 | std::string line; 114 | 115 | int nrows = 0; 116 | 117 | // reading data 118 | while (std::getline(myfile, line)) { 119 | 120 | std::stringstream lineStream(line); 121 | std::string cell; 122 | 123 | #ifdef USE_ARMA 124 | arma::Row z; 125 | #elif USE_BLAZE 126 | blaze::DynamicVector z; 127 | #elif USE_EIGEN 128 | Vector z; 129 | #endif 130 | 131 | int ncols = 0; 132 | 133 | while (std::getline(lineStream, cell, ',')) { 134 | #if defined(USE_ARMA) || defined(USE_BLAZE) 135 | z.resize(ncols + 1); 136 | #elif USE_EIGEN 137 | z.conservativeResize(ncols + 1, Eigen::NoChange); 138 | #endif 139 | z[ncols] = std::stod(cell); 140 | ++ncols; 141 | } 142 | #ifdef USE_ARMA 143 | x.insert_rows(nrows, z); 144 | #elif USE_BLAZE 145 | x.resize(nrows + 1, ncols); 146 | row(x, nrows) = z; 147 | #elif USE_EIGEN 148 | x.conservativeResize(nrows + 1, ncols); 149 | x.row(nrows) = z; 150 | #endif 151 | ++nrows; 152 | } 153 | myfile.close(); 154 | } 155 | 156 | //================================================================================================= 157 | 158 | } 159 | 160 | #endif 161 | -------------------------------------------------------------------------------- /include/Coeff_kpss.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef COEFF_KPSS_HPP 6 | #define COEFF_KPSS_HPP 7 | 8 | static const std::map>>> coeff_kpss = 9 | { 10 | {"c",{ 11 | {0.001,{ 12 | {0, {0.0167565, 0.1628078, -0.01441111}}, 13 | {1, {0.2036426, 0.1065976, -0.5134231}}}}, 14 | {0.005,{ 15 | {0, {0.02160067, 0.1649124, -0.005748196}}, 16 | {1, {0.1939746, 0.180736, -0.6535936}}}}, 17 | {0.01,{ 18 | {0, {0.02466286, 0.1635288, 0.04500264}}, 19 | {1, {0.1893402, 0.2089429, -0.6943796}}}}, 20 | {0.025,{ 21 | {0, {0.03026605, 0.1623029, 0.09417188}}, 22 | {1, {0.1847549, 0.2238625, -0.6818411}}}}, 23 | {0.05,{ 24 | {0, {0.03650665, 0.1643194, 0.118059}}, 25 | {1, {0.1819509, 0.2103519, -0.5991279}}}}, 26 | {0.1,{ 27 | {0, {0.04597858, 0.1695003, 0.1046518}}, 28 | {1, {0.1809212, 0.1596069, -0.4150671}}}}, 29 | {0.2,{ 30 | {0, {0.06222337, 0.1785042, 0.06728867}}, 31 | {1, {0.1794514, 0.1031811, -0.2438007}}}}, 32 | {0.5,{ 33 | {0, {0.1188952, 0.2136505, -0.1988974}}, 34 | {1, {0.1738762, -0.04517147, 0.1061083}}}}, 35 | {0.8,{ 36 | {0, {0.2413558, 0.2427114, -0.4925517}}, 37 | {1, {0.07161341, 0.3635483, -0.8456676}}}}, 38 | {0.9,{ 39 | {0, {0.347477, 0.1815891, -0.8897294}}, 40 | {1, {-0.1026364, 0.4255538, -0.6916551}}}}, 41 | {0.95,{ 42 | {0, {0.461564, -0.03241585, -0.4093091}}, 43 | {1, {-0.4579306, 1.037593, -0.9517771}}}}, 44 | {0.975,{ 45 | {0, {0.5808698, -0.4198131, 1.286591}}, 46 | {1, {-1.021283, 2.743161, -2.926945}}}}, 47 | {0.99,{ 48 | {0, {0.7434379, -1.282285, 7.296387}}, 49 | {1, {-2.025517, 6.584114, -8.361188}}}}, 50 | {0.995,{ 51 | {0, {0.8686703, -2.108955, 13.70689}}, 52 | {1, {-3.003148, 11.11568, -15.74966}}}}, 53 | {0.999,{ 54 | {0, {1.162377, -4.578843, 34.27324}}, 55 | {1, {-5.808165, 25.97152, -41.79132}}}} 56 | }}, 57 | {"ct",{ 58 | {0.001,{ 59 | {0, {0.01234275, 0.1655343, 0.01672944, -1.675843}}, 60 | {1, {0.2106948, 0.03970888, -0.3368414}}}}, 61 | {0.005,{ 62 | {0, {0.01526251, 0.1588324, 0.2212439, -3.176791}}, 63 | {1, {0.2015772, 0.1179992, -0.5024903}}}}, 64 | {0.01,{ 65 | {0, {0.01699618, 0.1544975, 0.3604586, -4.262545}}, 66 | {1, {0.1969925, 0.1538411, -0.5702144}}}}, 67 | {0.025,{ 68 | {0, {0.02005522, 0.1485785, 0.5907839, -6.211238}}, 69 | {1, {0.1880815, 0.2169062, -0.6777426}}}}, 70 | {0.05,{ 71 | {0, {0.02326679, 0.1414555, 0.8224264, -7.893967}}, 72 | {1, {0.1804144, 0.2639471, -0.7486399}}}}, 73 | {0.1,{ 74 | {0, {0.02781531, 0.1377224, 0.917243, -8.218599}}, 75 | {1, {0.170477, 0.3089857, -0.7857759}}}}, 76 | {0.2,{ 77 | {0, {0.03489574, 0.1345627, 0.9657117, -7.923499}}, 78 | {1, {0.1579158, 0.3369968, -0.7515043}}}}, 79 | {0.5,{ 80 | {0, {0.0556109, 0.1397416, 0.4620311, -1.559948}}, 81 | {1, {0.1300005, 0.2833583, -0.4283284}}}}, 82 | {0.8,{ 83 | {0, {0.09159799, 0.1345566, -0.619965, 9.817577}}, 84 | {1, {0.05585951, 0.2773908, -0.1216728}}}}, 85 | {0.9,{ 86 | {0, {0.1193112, 0.09514355, -1.079983, 16.87997}}, 87 | {1, {-0.03271029, 0.4379497, -0.2279858}}}}, 88 | {0.95,{ 89 | {0, {0.1478756, 0.03863841, -1.89105, 30.13683}}, 90 | {1, {-0.149998, 0.6506317, -0.2305911}}}}, 91 | {0.975,{ 92 | {0, {0.1773233, -0.03755152, -3.125617, 51.66143}}, 93 | {1, {-0.3091562, 1.057115, -0.4266199}}}}, 94 | {0.99,{ 95 | {0, {0.2172606, -0.2049983, -4.308647, 82.65278}}, 96 | {1, {-0.5765132, 1.915267, -1.057833}}}}, 97 | {0.995,{ 98 | {0, {0.2480191, -0.3967449, -4.07506, 101.2076}}, 99 | {1, {-0.8216993, 2.85189, -1.957697}}}}, 100 | {0.999,{ 101 | {0, {0.3203646, -1.046171, -0.8434172, 144.566}}, 102 | {1, {-1.515429, 6.124994, -6.22971}}}} 103 | }} 104 | }; 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /R/RcppURT/include/Coeff_kpss.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef COEFF_KPSS_HPP 6 | #define COEFF_KPSS_HPP 7 | 8 | static const std::map>>> coeff_kpss = 9 | { 10 | {"c",{ 11 | {0.001,{ 12 | {0, {0.0167565, 0.1628078, -0.01441111}}, 13 | {1, {0.2036426, 0.1065976, -0.5134231}}}}, 14 | {0.005,{ 15 | {0, {0.02160067, 0.1649124, -0.005748196}}, 16 | {1, {0.1939746, 0.180736, -0.6535936}}}}, 17 | {0.01,{ 18 | {0, {0.02466286, 0.1635288, 0.04500264}}, 19 | {1, {0.1893402, 0.2089429, -0.6943796}}}}, 20 | {0.025,{ 21 | {0, {0.03026605, 0.1623029, 0.09417188}}, 22 | {1, {0.1847549, 0.2238625, -0.6818411}}}}, 23 | {0.05,{ 24 | {0, {0.03650665, 0.1643194, 0.118059}}, 25 | {1, {0.1819509, 0.2103519, -0.5991279}}}}, 26 | {0.1,{ 27 | {0, {0.04597858, 0.1695003, 0.1046518}}, 28 | {1, {0.1809212, 0.1596069, -0.4150671}}}}, 29 | {0.2,{ 30 | {0, {0.06222337, 0.1785042, 0.06728867}}, 31 | {1, {0.1794514, 0.1031811, -0.2438007}}}}, 32 | {0.5,{ 33 | {0, {0.1188952, 0.2136505, -0.1988974}}, 34 | {1, {0.1738762, -0.04517147, 0.1061083}}}}, 35 | {0.8,{ 36 | {0, {0.2413558, 0.2427114, -0.4925517}}, 37 | {1, {0.07161341, 0.3635483, -0.8456676}}}}, 38 | {0.9,{ 39 | {0, {0.347477, 0.1815891, -0.8897294}}, 40 | {1, {-0.1026364, 0.4255538, -0.6916551}}}}, 41 | {0.95,{ 42 | {0, {0.461564, -0.03241585, -0.4093091}}, 43 | {1, {-0.4579306, 1.037593, -0.9517771}}}}, 44 | {0.975,{ 45 | {0, {0.5808698, -0.4198131, 1.286591}}, 46 | {1, {-1.021283, 2.743161, -2.926945}}}}, 47 | {0.99,{ 48 | {0, {0.7434379, -1.282285, 7.296387}}, 49 | {1, {-2.025517, 6.584114, -8.361188}}}}, 50 | {0.995,{ 51 | {0, {0.8686703, -2.108955, 13.70689}}, 52 | {1, {-3.003148, 11.11568, -15.74966}}}}, 53 | {0.999,{ 54 | {0, {1.162377, -4.578843, 34.27324}}, 55 | {1, {-5.808165, 25.97152, -41.79132}}}} 56 | }}, 57 | {"ct",{ 58 | {0.001,{ 59 | {0, {0.01234275, 0.1655343, 0.01672944, -1.675843}}, 60 | {1, {0.2106948, 0.03970888, -0.3368414}}}}, 61 | {0.005,{ 62 | {0, {0.01526251, 0.1588324, 0.2212439, -3.176791}}, 63 | {1, {0.2015772, 0.1179992, -0.5024903}}}}, 64 | {0.01,{ 65 | {0, {0.01699618, 0.1544975, 0.3604586, -4.262545}}, 66 | {1, {0.1969925, 0.1538411, -0.5702144}}}}, 67 | {0.025,{ 68 | {0, {0.02005522, 0.1485785, 0.5907839, -6.211238}}, 69 | {1, {0.1880815, 0.2169062, -0.6777426}}}}, 70 | {0.05,{ 71 | {0, {0.02326679, 0.1414555, 0.8224264, -7.893967}}, 72 | {1, {0.1804144, 0.2639471, -0.7486399}}}}, 73 | {0.1,{ 74 | {0, {0.02781531, 0.1377224, 0.917243, -8.218599}}, 75 | {1, {0.170477, 0.3089857, -0.7857759}}}}, 76 | {0.2,{ 77 | {0, {0.03489574, 0.1345627, 0.9657117, -7.923499}}, 78 | {1, {0.1579158, 0.3369968, -0.7515043}}}}, 79 | {0.5,{ 80 | {0, {0.0556109, 0.1397416, 0.4620311, -1.559948}}, 81 | {1, {0.1300005, 0.2833583, -0.4283284}}}}, 82 | {0.8,{ 83 | {0, {0.09159799, 0.1345566, -0.619965, 9.817577}}, 84 | {1, {0.05585951, 0.2773908, -0.1216728}}}}, 85 | {0.9,{ 86 | {0, {0.1193112, 0.09514355, -1.079983, 16.87997}}, 87 | {1, {-0.03271029, 0.4379497, -0.2279858}}}}, 88 | {0.95,{ 89 | {0, {0.1478756, 0.03863841, -1.89105, 30.13683}}, 90 | {1, {-0.149998, 0.6506317, -0.2305911}}}}, 91 | {0.975,{ 92 | {0, {0.1773233, -0.03755152, -3.125617, 51.66143}}, 93 | {1, {-0.3091562, 1.057115, -0.4266199}}}}, 94 | {0.99,{ 95 | {0, {0.2172606, -0.2049983, -4.308647, 82.65278}}, 96 | {1, {-0.5765132, 1.915267, -1.057833}}}}, 97 | {0.995,{ 98 | {0, {0.2480191, -0.3967449, -4.07506, 101.2076}}, 99 | {1, {-0.8216993, 2.85189, -1.957697}}}}, 100 | {0.999,{ 101 | {0, {0.3203646, -1.046171, -0.8434172, 144.566}}, 102 | {1, {-1.515429, 6.124994, -6.22971}}}} 103 | }} 104 | }; 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /include/Tools.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef TOOLS_HPP 6 | #define TOOLS_HPP 7 | 8 | // unseeded random number generator 9 | static boost::mt19937 rng; 10 | 11 | namespace urt { 12 | 13 | //================================================================================================= 14 | 15 | // generate Vector of normally distributed random data 16 | template 17 | Vector gaussian_noise(int n, T mu = 0.0, T sigma = 1.0) 18 | { 19 | boost::normal_distribution ndistrib(mu, sigma); 20 | boost::variate_generator> rnorm(rng, ndistrib); 21 | 22 | Vector x(n); 23 | 24 | for (int i = 0; i < n; ++i) 25 | x[i] = rnorm(); 26 | 27 | return x; 28 | } 29 | 30 | //************************************************************************************************* 31 | 32 | // generate Matrix of normally distributed random data 33 | template 34 | Matrix gaussian_noise(int nr, int nc, T mu = 0.0, T sigma = 1.0) 35 | { 36 | boost::normal_distribution ndistrib(mu, sigma); 37 | boost::variate_generator> rnorm(rng, ndistrib); 38 | 39 | Matrix x(nr, nc); 40 | 41 | for (int i = 0; i < nr; ++i) 42 | for (int j = 0; j < nc; ++j) 43 | x(i, j) = rnorm(); 44 | 45 | return x; 46 | } 47 | 48 | //************************************************************************************************* 49 | 50 | // generate Wiener process 51 | template 52 | Vector wiener_process(int n, T mu = 0.0, T sigma = 1.0) 53 | { 54 | Vector x(n); 55 | 56 | Vector z = gaussian_noise(n, mu, sigma); 57 | 58 | std::partial_sum(&z[0], &z[n], &x[0]); 59 | 60 | return x; 61 | } 62 | 63 | //************************************************************************************************* 64 | 65 | // generate Matrix of Wiener processes 66 | template 67 | Matrix wiener_process(int nr, int nc, T mu = 0.0, T sigma = 1.0) 68 | { 69 | Matrix x(nr, nc); 70 | 71 | Matrix z = gaussian_noise(nr, nc, mu, sigma); 72 | 73 | for (int j = 0; j < nc; ++j) 74 | std::partial_sum(&z(0, j), &z(nr, j), &x(0, j)); 75 | 76 | return x; 77 | } 78 | 79 | //************************************************************************************************* 80 | 81 | // << operator overload for outputting std::vector 82 | template 83 | std::ostream& operator<<(std::ostream& out, const std::vector& x) 84 | { 85 | for (auto it = x.cbegin(), end = x.cend(); it != end; ++it) 86 | std::cout << *it << " "; 87 | 88 | std::cout << "\n"; 89 | 90 | return out; 91 | } 92 | 93 | //************************************************************************************************* 94 | 95 | // modify matrix x by adding an intercept 96 | template 97 | void add_intercept(Matrix& x) 98 | { 99 | #ifdef USE_ARMA 100 | x.insert_cols(x.n_cols, arma::ones>(x.n_rows)); 101 | #elif USE_BLAZE 102 | x.resize(x.rows(), x.columns() + 1); 103 | column(x, x.columns() - 1) = forEach(column(x, x.columns() - 1), [](T val){ return 1; }); 104 | #elif USE_EIGEN 105 | x.conservativeResize(Eigen::NoChange, x.cols() + 1); 106 | x.col(x.cols() - 1) = Vector::Ones(x.rows()); 107 | #endif 108 | } 109 | 110 | //************************************************************************************************* 111 | 112 | // modify matrix x by adding a constant trend 113 | template 114 | void add_constant_trend(Matrix& x) 115 | { 116 | add_intercept(x); 117 | 118 | #ifdef USE_ARMA 119 | x.insert_cols(x.n_cols, arma::cumsum(x.col(x.n_cols - 1))); 120 | #elif USE_BLAZE 121 | x.resize(x.rows(), x.columns() + 1); 122 | std::partial_sum(&x(0, x.columns() - 2), &x(x.rows() - 1, x.columns() - 2), &x(0, x.columns() - 1)); 123 | #elif USE_EIGEN 124 | x.conservativeResize(Eigen::NoChange, x.cols() + 1); 125 | x.col(x.cols() - 1) = Vector::LinSpaced(x.rows(), 0, x.rows() - 1); 126 | #endif 127 | } 128 | 129 | //************************************************************************************************* 130 | 131 | // modify matrix x by adding a quadratic trend 132 | template 133 | void add_quadratic_trend(Matrix& x) 134 | { 135 | add_constant_trend(x); 136 | 137 | #ifdef USE_ARMA 138 | Vector z(x.col(x.n_cols - 1)); 139 | x.insert_cols(x.n_cols, z % z); 140 | #elif USE_BLAZE 141 | column(x, x.columns() - 1) = forEach(column(x, x.columns() - 2), [](T val){ return val * val; }); 142 | #elif USE_EIGEN 143 | Vector z(x.col(x.cols() - 1)); 144 | x.conservativeResize(Eigen::NoChange, x.cols() + 1); 145 | x.col(x.cols() - 1) = z.cwiseProduct(z); 146 | #endif 147 | } 148 | 149 | //================================================================================================= 150 | 151 | } 152 | 153 | #endif 154 | -------------------------------------------------------------------------------- /R/RcppURT/include/Tools.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef TOOLS_HPP 6 | #define TOOLS_HPP 7 | 8 | // unseeded random number generator 9 | static boost::mt19937 rng; 10 | 11 | namespace urt { 12 | 13 | //================================================================================================= 14 | 15 | // generate Vector of normally distributed random data 16 | template 17 | Vector gaussian_noise(int n, T mu = 0.0, T sigma = 1.0) 18 | { 19 | boost::normal_distribution ndistrib(mu, sigma); 20 | boost::variate_generator> rnorm(rng, ndistrib); 21 | 22 | Vector x(n); 23 | 24 | for (int i = 0; i < n; ++i) 25 | x[i] = rnorm(); 26 | 27 | return x; 28 | } 29 | 30 | //************************************************************************************************* 31 | 32 | // generate Matrix of normally distributed random data 33 | template 34 | Matrix gaussian_noise(int nr, int nc, T mu = 0.0, T sigma = 1.0) 35 | { 36 | boost::normal_distribution ndistrib(mu, sigma); 37 | boost::variate_generator> rnorm(rng, ndistrib); 38 | 39 | Matrix x(nr, nc); 40 | 41 | for (int i = 0; i < nr; ++i) 42 | for (int j = 0; j < nc; ++j) 43 | x(i, j) = rnorm(); 44 | 45 | return x; 46 | } 47 | 48 | //************************************************************************************************* 49 | 50 | // generate Wiener process 51 | template 52 | Vector wiener_process(int n, T mu = 0.0, T sigma = 1.0) 53 | { 54 | Vector x(n); 55 | 56 | Vector z = gaussian_noise(n, mu, sigma); 57 | 58 | std::partial_sum(&z[0], &z[n], &x[0]); 59 | 60 | return x; 61 | } 62 | 63 | //************************************************************************************************* 64 | 65 | // generate Matrix of Wiener processes 66 | template 67 | Matrix wiener_process(int nr, int nc, T mu = 0.0, T sigma = 1.0) 68 | { 69 | Matrix x(nr, nc); 70 | 71 | Matrix z = gaussian_noise(nr, nc, mu, sigma); 72 | 73 | for (int j = 0; j < nc; ++j) 74 | std::partial_sum(&z(0, j), &z(nr, j), &x(0, j)); 75 | 76 | return x; 77 | } 78 | 79 | //************************************************************************************************* 80 | 81 | // << operator overload for outputting std::vector 82 | template 83 | std::ostream& operator<<(std::ostream& out, const std::vector& x) 84 | { 85 | for (auto it = x.cbegin(), end = x.cend(); it != end; ++it) 86 | std::cout << *it << " "; 87 | 88 | std::cout << "\n"; 89 | 90 | return out; 91 | } 92 | 93 | //************************************************************************************************* 94 | 95 | // modify matrix x by adding an intercept 96 | template 97 | void add_intercept(Matrix& x) 98 | { 99 | #ifdef USE_ARMA 100 | x.insert_cols(x.n_cols, arma::ones>(x.n_rows)); 101 | #elif USE_BLAZE 102 | x.resize(x.rows(), x.columns() + 1); 103 | column(x, x.columns() - 1) = forEach(column(x, x.columns() - 1), [](T val){ return 1; }); 104 | #elif USE_EIGEN 105 | x.conservativeResize(Eigen::NoChange, x.cols() + 1); 106 | x.col(x.cols() - 1) = Vector::Ones(x.rows()); 107 | #endif 108 | } 109 | 110 | //************************************************************************************************* 111 | 112 | // modify matrix x by adding a constant trend 113 | template 114 | void add_constant_trend(Matrix& x) 115 | { 116 | add_intercept(x); 117 | 118 | #ifdef USE_ARMA 119 | x.insert_cols(x.n_cols, arma::cumsum(x.col(x.n_cols - 1))); 120 | #elif USE_BLAZE 121 | x.resize(x.rows(), x.columns() + 1); 122 | std::partial_sum(&x(0, x.columns() - 2), &x(x.rows() - 1, x.columns() - 2), &x(0, x.columns() - 1)); 123 | #elif USE_EIGEN 124 | x.conservativeResize(Eigen::NoChange, x.cols() + 1); 125 | x.col(x.cols() - 1) = Vector::LinSpaced(x.rows(), 0, x.rows() - 1); 126 | #endif 127 | } 128 | 129 | //************************************************************************************************* 130 | 131 | // modify matrix x by adding a quadratic trend 132 | template 133 | void add_quadratic_trend(Matrix& x) 134 | { 135 | add_constant_trend(x); 136 | 137 | #ifdef USE_ARMA 138 | Vector z(x.col(x.n_cols - 1)); 139 | x.insert_cols(x.n_cols, z % z); 140 | #elif USE_BLAZE 141 | column(x, x.columns() - 1) = forEach(column(x, x.columns() - 2), [](T val){ return val * val; }); 142 | #elif USE_EIGEN 143 | Vector z(x.col(x.cols() - 1)); 144 | x.conservativeResize(Eigen::NoChange, x.cols() + 1); 145 | x.col(x.cols() - 1) = z.cwiseProduct(z); 146 | #endif 147 | } 148 | 149 | //================================================================================================= 150 | 151 | } 152 | 153 | #endif 154 | -------------------------------------------------------------------------------- /R/RcppURT/R/KPSS.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | suppressMessages(library(R6)) 6 | 7 | #-------------------------------------------------------------------------------------------------- 8 | 9 | # C++ class KPSS exposed to R 10 | KPSS_d <- R6Class("KPSS_d", 11 | private = list( 12 | ptr = NULL 13 | ), 14 | public = list( 15 | initialize = function(data = NA, lags = "", lags_type = "long", trend = "c") { 16 | if (lags == "") { 17 | private$ptr <- .Call('KPSS_d__new2', PACKAGE = 'RcppURT', data_ = data, lags_type_ = lags_type, trend_ = trend) 18 | } else { 19 | private$ptr <- .Call('KPSS_d__new1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend) 20 | } 21 | }, 22 | statistic = function() { 23 | .Call('UnitRoot_d_statistic', PACKAGE = 'RcppURT', ptr_ = private$ptr) 24 | }, 25 | pvalue = function() { 26 | .Call('UnitRoot_d_pvalue', PACKAGE = 'RcppURT', ptr_ = private$ptr) 27 | }, 28 | show = function() { 29 | .Call('UnitRoot_d_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 30 | } 31 | ), 32 | active = list( 33 | stat = function() { 34 | .Call('UnitRoot_d_get_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 35 | }, 36 | pval = function() { 37 | .Call('UnitRoot_d_get_pval', PACKAGE = 'RcppURT', ptr_ = private$ptr) 38 | }, 39 | trends = function() { 40 | .Call('UnitRoot_d_get_trends', PACKAGE = 'RcppURT', ptr_ = private$ptr) 41 | }, 42 | lags = function(value) { 43 | if (missing(value)) { 44 | .Call('UnitRoot_d_get_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 45 | } else { 46 | .Call('UnitRoot_d_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_ = value) 47 | } 48 | }, 49 | lags_type = function(value) { 50 | if (missing(value)) { 51 | .Call('UnitRoot_d_get_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 52 | } else { 53 | Call('UnitRoot_d_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_type_ = value) 54 | } 55 | }, 56 | trend = function(value) { 57 | if (missing(value)) { 58 | .Call('UnitRoot_d_get_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr) 59 | } else { 60 | .Call('UnitRoot_d_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr, trend_ = value) 61 | } 62 | } 63 | ) 64 | ) 65 | 66 | #-------------------------------------------------------------------------------------------------- 67 | ################################################################################################### 68 | #-------------------------------------------------------------------------------------------------- 69 | 70 | # C++ class KPSS exposed to R 71 | KPSS_f <- R6Class("KPSS_f", 72 | private = list( 73 | ptr = NULL 74 | ), 75 | public = list( 76 | initialize = function(data = NA, lags = "", lags_type = "long", trend = "c") { 77 | if (lags == "") { 78 | private$ptr <- .Call('KPSS_f__new2', PACKAGE = 'RcppURT', data_ = data, lags_type_ = lags_type, trend_ = trend) 79 | } 80 | else { 81 | private$ptr <- .Call('KPSS_f__new1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend) 82 | } 83 | }, 84 | statistic = function() { 85 | .Call('UnitRoot_f_statistic', PACKAGE = 'RcppURT', ptr_ = private$ptr) 86 | }, 87 | pvalue = function() { 88 | .Call('UnitRoot_f_pvalue', PACKAGE = 'RcppURT', ptr_ = private$ptr) 89 | }, 90 | show = function() { 91 | .Call('UnitRoot_f_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 92 | } 93 | ), 94 | active = list( 95 | stat = function() { 96 | .Call('UnitRoot_f_get_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 97 | }, 98 | pval = function() { 99 | .Call('UnitRoot_f_get_pval', PACKAGE = 'RcppURT', ptr_ = private$ptr) 100 | }, 101 | trends = function() { 102 | .Call('UnitRoot_f_get_trends', PACKAGE = 'RcppURT', ptr_ = private$ptr) 103 | }, 104 | lags = function(value) { 105 | if (missing(value)) { 106 | .Call('UnitRoot_f_get_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 107 | } else { 108 | .Call('UnitRoot_f_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_ = value) 109 | } 110 | }, 111 | lags_type = function(value) { 112 | if (missing(value)) { 113 | .Call('UnitRoot_f_get_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 114 | } 115 | else { 116 | Call('UnitRoot_f_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_type_ = value) 117 | } 118 | }, 119 | trend = function(value) { 120 | if (missing(value)) { 121 | .Call('UnitRoot_f_get_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr) 122 | } else { 123 | .Call('UnitRoot_f_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr, trend_ = value) 124 | } 125 | } 126 | ) 127 | ) 128 | 129 | #-------------------------------------------------------------------------------------------------- 130 | -------------------------------------------------------------------------------- /src/KPSS.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | namespace urt { 8 | 9 | //================================================================================================= 10 | 11 | // parameter constructor for computing KPSS test for a given number of lags 12 | template 13 | KPSS::KPSS(const Vector& data, int lags, const std::string& trend) : ur(data, lags, trend) 14 | { 15 | ur::test_name = _test_name; 16 | ur::valid_trends = _valid_trends; 17 | } 18 | 19 | //************************************************************************************************* 20 | 21 | // parameter constructor for computing PP test for a default number of lags (long or short) 22 | template 23 | KPSS::KPSS(const Vector& data, const std::string lags_type, const std::string& trend) : ur(data, 0, trend) 24 | { 25 | ur::test_name = _test_name; 26 | ur::valid_trends = _valid_trends; 27 | ur::lags_type = lags_type; 28 | } 29 | 30 | //************************************************************************************************* 31 | 32 | // compute test statistic 33 | template 34 | void KPSS::compute_stat() 35 | { 36 | T factor = 1.0 / ur::nobs; 37 | 38 | #ifdef USE_ARMA 39 | Vector s = arma::cumsum(*ur::ptr); 40 | T eta = factor * factor * arma::as_scalar(s.t() * s); 41 | T s2 = factor * arma::as_scalar(ur::ptr->t() * (*ur::ptr)); 42 | #elif defined(USE_BLAZE) || defined(USE_EIGEN) 43 | Vector s(ur::nobs); 44 | std::partial_sum(&ur::ptr->operator[](0), &ur::ptr->operator[](ur::nobs), &s[0]); 45 | #ifdef USE_BLAZE 46 | T eta = factor * factor * blaze::trans(s) * s; 47 | T s2 = factor * blaze::trans(*ur::ptr) * (*ur::ptr); 48 | #elif USE_EIGEN 49 | T eta = factor * factor * s.dot(s); 50 | T s2 = factor * ur::ptr->dot(*ur::ptr); 51 | #endif 52 | #endif 53 | 54 | T tmp1 = 0; 55 | // estimating long run variance 56 | for (int i = 1; i <= ur::lags; ++i) { 57 | #ifdef USE_ARMA 58 | T tmp2 = arma::as_scalar(ur::ptr->subvec(i, ur::nobs - 1).t() * ur::ptr->subvec(0, ur::nobs - i - 1)); 59 | #elif USE_BLAZE 60 | T tmp2 = blaze::trans(subvector(*ur::ptr, i, ur::nobs - i)) * subvector(*ur::ptr, 0, ur::nobs - i); 61 | #elif USE_EIGEN 62 | T tmp2 = ur::ptr->tail(ur::nobs - i).transpose() * ur::ptr->head(ur::nobs - i); 63 | #endif 64 | // computing Bartlett weight 65 | T weight = 1.0 - i / (ur::lags + 1.0); 66 | tmp1 += weight * tmp2; 67 | } 68 | s2 += factor * 2.0 * tmp1; 69 | 70 | ur::stat = eta / s2; 71 | } 72 | 73 | //************************************************************************************************* 74 | 75 | // compute KPSS test statistic 76 | template 77 | const T& KPSS::statistic() 78 | { 79 | // setting type of lags (if a default type of lags value has been chosen) 80 | ur::set_lags_type(); 81 | // setting number of lags 82 | ur::set_lags(); 83 | // setting regression trend 84 | ur::set_trend(); 85 | // if new trend 86 | if (ur::new_trend) { 87 | // setting pointer back to original data for new detrending 88 | ur::set_data(); 89 | // detrending data by OLS 90 | ur::ols_detrend(); 91 | } 92 | // if new trend or new lags 93 | if (ur::new_trend || ur::new_lags) { 94 | // computing statistic 95 | this->compute_stat(); 96 | ur::new_trend = false; 97 | ur::new_lags = false; 98 | } 99 | 100 | return ur::stat; 101 | } 102 | 103 | //************************************************************************************************* 104 | 105 | // compute KPSS test p-value 106 | template 107 | const T& KPSS::pvalue() 108 | { 109 | // computing test statistic 110 | this->statistic(); 111 | // KPSS does not have bootstrap available 112 | if (ur::bootstrap) { 113 | std::cout << "\n Sorry, bootstrap is not available for KPSS test.\n\n"; 114 | ur::bootstrap = false; 115 | } 116 | // setting critical values coefficients pointer 117 | ur::coeff_ptr = &coeff_kpss.at(ur::trend); 118 | // computing p-value 119 | T pval = 1 - ur::pvalue(); 120 | // setting test p-value 121 | ur::pval = pval; 122 | 123 | return ur::pval; 124 | } 125 | 126 | //************************************************************************************************* 127 | 128 | // output KPSS test results 129 | template 130 | void KPSS::show() 131 | { 132 | // in case user modified test type, for KPSS it should always be empty 133 | ur::test_type = std::string(); 134 | // in case user modified method for KPSS it should always be empty 135 | ur::method = std::string(); 136 | // in case user turned ur::regression to true, KPSS did not use an OLS regression but an OLS detrending 137 | ur::regression = false; 138 | // computing p-value 139 | this->pvalue(); 140 | // outputting results 141 | ur::show(); 142 | } 143 | 144 | //================================================================================================= 145 | 146 | } 147 | 148 | -------------------------------------------------------------------------------- /R/RcppURT/src/KPSS.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | namespace urt { 8 | 9 | //================================================================================================= 10 | 11 | // parameter constructor for computing KPSS test for a given number of lags 12 | template 13 | KPSS::KPSS(const Vector& data, int lags, const std::string& trend) : ur(data, lags, trend) 14 | { 15 | ur::test_name = _test_name; 16 | ur::valid_trends = _valid_trends; 17 | } 18 | 19 | //************************************************************************************************* 20 | 21 | // parameter constructor for computing PP test for a default number of lags (long or short) 22 | template 23 | KPSS::KPSS(const Vector& data, const std::string lags_type, const std::string& trend) : ur(data, 0, trend) 24 | { 25 | ur::test_name = _test_name; 26 | ur::valid_trends = _valid_trends; 27 | ur::lags_type = lags_type; 28 | } 29 | 30 | //************************************************************************************************* 31 | 32 | // compute test statistic 33 | template 34 | void KPSS::compute_stat() 35 | { 36 | T factor = 1.0 / ur::nobs; 37 | 38 | #ifdef USE_ARMA 39 | Vector s = arma::cumsum(*ur::ptr); 40 | T eta = factor * factor * arma::as_scalar(s.t() * s); 41 | T s2 = factor * arma::as_scalar(ur::ptr->t() * (*ur::ptr)); 42 | #elif defined(USE_BLAZE) || defined(USE_EIGEN) 43 | Vector s(ur::nobs); 44 | std::partial_sum(&ur::ptr->operator[](0), &ur::ptr->operator[](ur::nobs), &s[0]); 45 | #ifdef USE_BLAZE 46 | T eta = factor * factor * blaze::trans(s) * s; 47 | T s2 = factor * blaze::trans(*ur::ptr) * (*ur::ptr); 48 | #elif USE_EIGEN 49 | T eta = factor * factor * s.dot(s); 50 | T s2 = factor * ur::ptr->dot(*ur::ptr); 51 | #endif 52 | #endif 53 | 54 | T tmp1 = 0; 55 | // estimating long run variance 56 | for (int i = 1; i <= ur::lags; ++i) { 57 | #ifdef USE_ARMA 58 | T tmp2 = arma::as_scalar(ur::ptr->subvec(i, ur::nobs - 1).t() * ur::ptr->subvec(0, ur::nobs - i - 1)); 59 | #elif USE_BLAZE 60 | T tmp2 = blaze::trans(subvector(*ur::ptr, i, ur::nobs - i)) * subvector(*ur::ptr, 0, ur::nobs - i); 61 | #elif USE_EIGEN 62 | T tmp2 = ur::ptr->tail(ur::nobs - i).transpose() * ur::ptr->head(ur::nobs - i); 63 | #endif 64 | // computing Bartlett weight 65 | T weight = 1.0 - i / (ur::lags + 1.0); 66 | tmp1 += weight * tmp2; 67 | } 68 | s2 += factor * 2.0 * tmp1; 69 | 70 | ur::stat = eta / s2; 71 | } 72 | 73 | //************************************************************************************************* 74 | 75 | // compute KPSS test statistic 76 | template 77 | const T& KPSS::statistic() 78 | { 79 | // setting type of lags (if a default type of lags value has been chosen) 80 | ur::set_lags_type(); 81 | // setting number of lags 82 | ur::set_lags(); 83 | // setting regression trend 84 | ur::set_trend(); 85 | // if new trend 86 | if (ur::new_trend) { 87 | // setting pointer back to original data for new detrending 88 | ur::set_data(); 89 | // detrending data by OLS 90 | ur::ols_detrend(); 91 | } 92 | // if new trend or new lags 93 | if (ur::new_trend || ur::new_lags) { 94 | // computing statistic 95 | this->compute_stat(); 96 | ur::new_trend = false; 97 | ur::new_lags = false; 98 | } 99 | 100 | return ur::stat; 101 | } 102 | 103 | //************************************************************************************************* 104 | 105 | // compute KPSS test p-value 106 | template 107 | const T& KPSS::pvalue() 108 | { 109 | // computing test statistic 110 | this->statistic(); 111 | // KPSS does not have bootstrap available 112 | if (ur::bootstrap) { 113 | std::cout << "\n Sorry, bootstrap is not available for KPSS test.\n\n"; 114 | ur::bootstrap = false; 115 | } 116 | // setting critical values coefficients pointer 117 | ur::coeff_ptr = &coeff_kpss.at(ur::trend); 118 | // computing p-value 119 | T pval = 1 - ur::pvalue(); 120 | // setting test p-value 121 | ur::pval = pval; 122 | 123 | return ur::pval; 124 | } 125 | 126 | //************************************************************************************************* 127 | 128 | // output KPSS test results 129 | template 130 | void KPSS::show() 131 | { 132 | // in case user modified test type, for KPSS it should always be empty 133 | ur::test_type = std::string(); 134 | // in case user modified method for KPSS it should always be empty 135 | ur::method = std::string(); 136 | // in case user turned ur::regression to true, KPSS did not use an OLS regression but an OLS detrending 137 | ur::regression = false; 138 | // computing p-value 139 | this->pvalue(); 140 | // outputting results 141 | ur::show(); 142 | } 143 | 144 | //================================================================================================= 145 | 146 | } 147 | 148 | -------------------------------------------------------------------------------- /R/RcppURT/R/UnitRootTests.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | #-------------------------------------------------------------------------------------------------- 6 | 7 | # C++ class ADF as a function 8 | ADFtest_d <- function(data, lags = "", method = "AIC", trend = "c", output = FALSE, bootstrap = FALSE, niter = 1000) { 9 | if (lags == "") { 10 | .Call('ADFtest_d_2', PACKAGE = 'RcppURT', data_ = data, method_ = method, trend_ = trend, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 11 | } else { 12 | .Call('ADFtest_d_1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 13 | } 14 | } 15 | 16 | #-------------------------------------------------------------------------------------------------- 17 | 18 | # C++ class DFGLS as a function 19 | DFGLStest_d <- function(data, lags = "", method = "AIC", trend = "c", output = FALSE, bootstrap = FALSE, niter = 1000) { 20 | if (lags == "") { 21 | .Call('DFGLStest_d_2', PACKAGE = 'RcppURT', data_ = data, method_ = method, trend_ = trend, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 22 | } else { 23 | .Call('DFGLStest_d_1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 24 | } 25 | } 26 | 27 | #-------------------------------------------------------------------------------------------------- 28 | 29 | # C++ class PP as a function 30 | PPtest_d <- function(data, lags = "", lags_type = "long", trend = "c", test_type = "tau", output = FALSE, bootstrap = FALSE, niter = 1000) { 31 | if (lags == "") { 32 | .Call('PPtest_d_2', PACKAGE = 'RcppURT', data_ = data, lags_type_ = lags_type, trend_ = trend, test_type_ = test_type, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 33 | } else { 34 | .Call('PPtest_d_1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, test_type_ = test_type, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 35 | } 36 | } 37 | 38 | #-------------------------------------------------------------------------------------------------- 39 | 40 | # C++ class KPSS as a function 41 | KPSStest_d <- function(data, lags = "", lags_type = "long", trend = "c", output = FALSE) { 42 | if (lags == "") { 43 | .Call('KPSStest_d_2', PACKAGE = 'RcppURT', data_ = data, lags_type_ = lags_type, trend_ = trend, output_ = output) 44 | } else { 45 | .Call('KPSStest_d_1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, output_ = output) 46 | } 47 | } 48 | 49 | #-------------------------------------------------------------------------------------------------- 50 | ################################################################################################### 51 | #-------------------------------------------------------------------------------------------------- 52 | 53 | # C++ class ADF as a function 54 | ADFtest_f <- function(data, lags = "", method = "AIC", trend = "c", output = FALSE, bootstrap = FALSE, niter = 1000) { 55 | if (lags == "") { 56 | .Call('ADFtest_f_2', PACKAGE = 'RcppURT', data_ = data, method_ = method, trend_ = trend, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 57 | } else { 58 | .Call('ADFtest_f_1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 59 | } 60 | } 61 | 62 | #-------------------------------------------------------------------------------------------------- 63 | 64 | # C++ class DFGLS as a function 65 | DFGLStest_f <- function(data, lags = "", method = "AIC", trend = "c", output = FALSE, bootstrap = FALSE, niter = 1000) { 66 | if (lags == "") { 67 | .Call('DFGLStest_f_2', PACKAGE = 'RcppURT', data_ = data, method_ = method, trend_ = trend, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 68 | } else { 69 | .Call('DFGLStest_f_1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 70 | } 71 | } 72 | 73 | #-------------------------------------------------------------------------------------------------- 74 | 75 | # C++ class PP as a function 76 | PPtest_f <- function(data, lags = "", lags_type = "long", trend = "c", test_type = "tau", output = FALSE, bootstrap = FALSE, niter = 1000) { 77 | if (lags == "") { 78 | .Call('PPtest_f_2', PACKAGE = 'RcppURT', data_ = data, lags_type_ = lags_type, trend_ = trend, test_type_ = test_type, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 79 | } else { 80 | .Call('PPtest_f_1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, test_type_ = test_type, output_ = output, bootstrap_ = bootstrap, niter_ = niter) 81 | } 82 | } 83 | 84 | #-------------------------------------------------------------------------------------------------- 85 | 86 | # C++ class KPSS as a function 87 | KPSStest_f <- function(data, lags = "", lags_type = "long", trend = "c", output = FALSE) { 88 | if (lags == "") { 89 | .Call('KPSStest_f_2', PACKAGE = 'RcppURT', data_ = data, lags_type_ = lags_type, trend_ = trend, output_ = output) 90 | } else { 91 | .Call('KPSStest_f_1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, output_ = output) 92 | } 93 | } 94 | 95 | #-------------------------------------------------------------------------------------------------- 96 | -------------------------------------------------------------------------------- /src/PP.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | namespace urt { 8 | 9 | //================================================================================================= 10 | 11 | // parameter constructor for computing PP test for a given number of lags 12 | template 13 | PP::PP(const Vector& data, int lags, const std::string& trend, const std::string& test_type, bool regression) : ur(data, lags, trend, regression) 14 | { 15 | ur::test_name = _test_name; 16 | this->lags = lags; 17 | ur::test_type = test_type; 18 | ur::valid_trends = _valid_trends; 19 | } 20 | 21 | //************************************************************************************************* 22 | 23 | // parameter constructor for computing PP test for a default number of lags (long or short) 24 | template 25 | PP::PP(const Vector& data, const std::string& lags_type, const std::string& trend, const std::string& test_type, bool regression) : ur(data, 0, trend, regression) 26 | { 27 | ur::test_name = _test_name; 28 | ur::test_type = test_type; 29 | ur::valid_trends = _valid_trends; 30 | ur::lags_type = lags_type; 31 | } 32 | 33 | //************************************************************************************************* 34 | 35 | // compute test statistic 36 | template 37 | void PP::compute_stat() 38 | { 39 | // number of residuals 40 | int n = ur::nobs - 1; 41 | // computing gamma vector for PP test statistics computation 42 | Vector G(lags + 1); 43 | 44 | #ifdef USE_ARMA 45 | // pointer to test residual elements memory 46 | T* ptr = ur::result->resid.memptr(); 47 | for (int j = 0; j <= lags; ++j) { 48 | G[j] = arma::as_scalar(Vector(ptr + j, n - j, false).t() * Vector(ptr, n - j, false)); 49 | } 50 | #elif USE_BLAZE 51 | for (int j = 0; j <= lags; ++j) { 52 | G[j] = blaze::trans(subvector(ur::result->resid, j, n - j)) * subvector(ur::result->resid, 0, n - j); 53 | } 54 | #elif USE_EIGEN 55 | for (int j = 0; j <= lags; ++j) { 56 | G[j] = ur::result->resid.segment(j, n - j).transpose() * ur::result->resid.segment(0, n - j); 57 | } 58 | #endif 59 | 60 | G /= n; 61 | 62 | T term = 0; 63 | // computing long-run variance estimator (Newey-West) 64 | for (int j = 1; j <= lags; ++j) { 65 | term += (1.0 - j / (lags + 1.0)) * G[j]; 66 | } 67 | T L2 = G[0] + 2 * term; 68 | T rho = ur::result->param[0]; 69 | // OLS mean square error 70 | T S2 = ur::result->MSE; 71 | // OLS variance of rho 72 | T sigma2 = ur::result->var[0]; 73 | // computing PP test statistic 74 | if (ur::test_type == "rho") { 75 | // normalized statistic 76 | ur::stat = n * rho - .5 * n * n * sigma2 * (L2 - G[0]) / S2; 77 | } else { 78 | // t-statistic 79 | ur::stat = sqrt(G[0] / L2) * rho / sqrt(sigma2) - .5 * (L2 - G[0]) * n * sqrt(sigma2 / (L2 * S2)); 80 | } 81 | } 82 | 83 | //************************************************************************************************* 84 | 85 | // compute PP test statistic 86 | template 87 | const T& PP::statistic() 88 | { 89 | // setting type of lags (if a default type of lags value has been chosen) 90 | ur::set_lags_type(); 91 | // setting number of lags 92 | ur::lags = this->lags; 93 | ur::set_lags(); 94 | this->lags = ur::lags; 95 | // this test is not augmented, ur::lags must always be 0 in ur::adf_regression() and ur::bootstrap() 96 | ur::lags = 0; 97 | // setting regression trend 98 | ur::set_trend(); 99 | // setting test type tau or rho 100 | ur::set_test_type(); 101 | // if new trend or bootstrap 102 | if (ur::new_trend || ur::new_data) { 103 | // computing ADF regression 104 | ur::adf_regression(); 105 | // computing test statistic 106 | this->compute_stat(); 107 | ur::new_trend = false; 108 | } 109 | // if new lags, new maxlags or new test type we only compute the statistic 110 | else if (ur::new_lags || ur::new_test_type) { 111 | // computing PP test statistic 112 | this->compute_stat(); 113 | ur::new_lags = false; 114 | ur::new_test_type = false; 115 | } 116 | return ur::stat; 117 | } 118 | 119 | //************************************************************************************************* 120 | 121 | // compute PP test p-value 122 | template 123 | const T& PP::pvalue() 124 | { 125 | // computing test statistic 126 | this->statistic(); 127 | // setting critical values coeff pointer 128 | if (ur::test_type == "tau") { 129 | ur::coeff_ptr = &coeff_pptau.at(ur::trend); 130 | } else { 131 | ur::coeff_ptr = &coeff_pprho.at(ur::trend); 132 | } 133 | // computing p-value 134 | ur::pvalue(); 135 | return ur::pval; 136 | } 137 | 138 | //************************************************************************************************* 139 | 140 | // output PP test results 141 | template 142 | void PP::show() 143 | { 144 | // in case user modified method for PP it should always be empty 145 | ur::method = std::string(); 146 | // computing p-value 147 | this->pvalue(); 148 | // setting ur::lags from 0 to back to lags 149 | ur::lags = this->lags; 150 | // outputting results 151 | ur::show(); 152 | // setting ur::lags back to 0 (in case bootstrap is run next) 153 | ur::lags = 0; 154 | } 155 | 156 | //================================================================================================= 157 | 158 | } 159 | 160 | 161 | -------------------------------------------------------------------------------- /R/RcppURT/src/PP.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | namespace urt { 8 | 9 | //================================================================================================= 10 | 11 | // parameter constructor for computing PP test for a given number of lags 12 | template 13 | PP::PP(const Vector& data, int lags, const std::string& trend, const std::string& test_type, bool regression) : ur(data, lags, trend, regression) 14 | { 15 | ur::test_name = _test_name; 16 | this->lags = lags; 17 | ur::test_type = test_type; 18 | ur::valid_trends = _valid_trends; 19 | } 20 | 21 | //************************************************************************************************* 22 | 23 | // parameter constructor for computing PP test for a default number of lags (long or short) 24 | template 25 | PP::PP(const Vector& data, const std::string& lags_type, const std::string& trend, const std::string& test_type, bool regression) : ur(data, 0, trend, regression) 26 | { 27 | ur::test_name = _test_name; 28 | ur::test_type = test_type; 29 | ur::valid_trends = _valid_trends; 30 | ur::lags_type = lags_type; 31 | } 32 | 33 | //************************************************************************************************* 34 | 35 | // compute test statistic 36 | template 37 | void PP::compute_stat() 38 | { 39 | // number of residuals 40 | int n = ur::nobs - 1; 41 | // computing gamma vector for PP test statistics computation 42 | Vector G(lags + 1); 43 | 44 | #ifdef USE_ARMA 45 | // pointer to test residual elements memory 46 | T* ptr = ur::result->resid.memptr(); 47 | for (int j = 0; j <= lags; ++j) { 48 | G[j] = arma::as_scalar(Vector(ptr + j, n - j, false).t() * Vector(ptr, n - j, false)); 49 | } 50 | #elif USE_BLAZE 51 | for (int j = 0; j <= lags; ++j) { 52 | G[j] = blaze::trans(subvector(ur::result->resid, j, n - j)) * subvector(ur::result->resid, 0, n - j); 53 | } 54 | #elif USE_EIGEN 55 | for (int j = 0; j <= lags; ++j) { 56 | G[j] = ur::result->resid.segment(j, n - j).transpose() * ur::result->resid.segment(0, n - j); 57 | } 58 | #endif 59 | 60 | G /= n; 61 | 62 | T term = 0; 63 | // computing long-run variance estimator (Newey-West) 64 | for (int j = 1; j <= lags; ++j) { 65 | term += (1.0 - j / (lags + 1.0)) * G[j]; 66 | } 67 | T L2 = G[0] + 2 * term; 68 | T rho = ur::result->param[0]; 69 | // OLS mean square error 70 | T S2 = ur::result->MSE; 71 | // OLS variance of rho 72 | T sigma2 = ur::result->var[0]; 73 | // computing PP test statistic 74 | if (ur::test_type == "rho") { 75 | // normalized statistic 76 | ur::stat = n * rho - .5 * n * n * sigma2 * (L2 - G[0]) / S2; 77 | } else { 78 | // t-statistic 79 | ur::stat = sqrt(G[0] / L2) * rho / sqrt(sigma2) - .5 * (L2 - G[0]) * n * sqrt(sigma2 / (L2 * S2)); 80 | } 81 | } 82 | 83 | //************************************************************************************************* 84 | 85 | // compute PP test statistic 86 | template 87 | const T& PP::statistic() 88 | { 89 | // setting type of lags (if a default type of lags value has been chosen) 90 | ur::set_lags_type(); 91 | // setting number of lags 92 | ur::lags = this->lags; 93 | ur::set_lags(); 94 | this->lags = ur::lags; 95 | // this test is not augmented, ur::lags must always be 0 in ur::adf_regression() and ur::bootstrap() 96 | ur::lags = 0; 97 | // setting regression trend 98 | ur::set_trend(); 99 | // setting test type tau or rho 100 | ur::set_test_type(); 101 | // if new trend or bootstrap 102 | if (ur::new_trend || ur::new_data) { 103 | // computing ADF regression 104 | ur::adf_regression(); 105 | // computing test statistic 106 | this->compute_stat(); 107 | ur::new_trend = false; 108 | } 109 | // if new lags, new maxlags or new test type we only compute the statistic 110 | else if (ur::new_lags || ur::new_test_type) { 111 | // computing PP test statistic 112 | this->compute_stat(); 113 | ur::new_lags = false; 114 | ur::new_test_type = false; 115 | } 116 | return ur::stat; 117 | } 118 | 119 | //************************************************************************************************* 120 | 121 | // compute PP test p-value 122 | template 123 | const T& PP::pvalue() 124 | { 125 | // computing test statistic 126 | this->statistic(); 127 | // setting critical values coeff pointer 128 | if (ur::test_type == "tau") { 129 | ur::coeff_ptr = &coeff_pptau.at(ur::trend); 130 | } else { 131 | ur::coeff_ptr = &coeff_pprho.at(ur::trend); 132 | } 133 | // computing p-value 134 | ur::pvalue(); 135 | return ur::pval; 136 | } 137 | 138 | //************************************************************************************************* 139 | 140 | // output PP test results 141 | template 142 | void PP::show() 143 | { 144 | // in case user modified method for PP it should always be empty 145 | ur::method = std::string(); 146 | // computing p-value 147 | this->pvalue(); 148 | // setting ur::lags from 0 to back to lags 149 | ur::lags = this->lags; 150 | // outputting results 151 | ur::show(); 152 | // setting ur::lags back to 0 (in case bootstrap is run next) 153 | ur::lags = 0; 154 | } 155 | 156 | //================================================================================================= 157 | 158 | } 159 | 160 | 161 | -------------------------------------------------------------------------------- /include/Coeff_dfgls.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef COEFF_DFGLS_HPP 6 | #define COEFF_DFGLS_HPP 7 | 8 | static const std::map>>> coeff_dfgls = 9 | { 10 | {"c",{ 11 | {0.001,{ 12 | {0, {-3.281721, -20.10785, 20.54557, 919.4611, -15768.36}}, 13 | {1, {1.214884, -9.190296, 465.547, -4800.389, 18494.58, -24523.48}}}}, 14 | {0.005,{ 15 | {0, {-2.798722, -19.87674, 158.6922, -1361.471, 649.8149}}, 16 | {1, {1.377745, -9.989878, 369.4152, -3615.128, 13677.44, -17888.14}}}}, 17 | {0.01,{ 18 | {0, {-2.566274, -20.06523, 218.4769, -2506.118, 9754.758}}, 19 | {1, {1.367608, -14.98339, 466.491, -4344.437, 16100.97, -20814.35}}}}, 20 | {0.025,{ 21 | {0, {-2.227436, -20.71955, 276.1964, -3313.464, 16034.91}}, 22 | {1, {1.075308, -5.706035, 307.2224, -3143.095, 12178.3, -16217.98}}}}, 23 | {0.05,{ 24 | {0, {-1.941807, -21.8198, 327.6527, -3983.442, 20397.75}}, 25 | {1, {0.9627055, -2.586653, 239.4981, -2548.091, 10013.66, -13436.96}}}}, 26 | {0.1,{ 27 | {0, {-1.618534, -23.81555, 403.463, -5111.848, 27837.04}}, 28 | {1, {0.8819745, -0.5482568, 184.5334, -2036.822, 8119.434, -10998.77}}}}, 29 | {0.2,{ 30 | {0, {-1.236467, -27.13579, 499.8872, -6332.546, 34623.67}}, 31 | {1, {0.7793006, 1.366651, 137.9713, -1615.072, 6560.24, -8970.884}}}}, 32 | {0.5,{ 33 | {0, {-0.5023128, -38.29761, 767.7794, -9009.913, 44651.65}}, 34 | {1, {0.5846996, 8.288074, 25.93693, -785.592, 3753.654, -5481.515}}}}, 35 | {0.8,{ 36 | {0, {0.4066535, -50.03626, 832.9085, -6572.743, 16628.01}}, 37 | {1, {0.6261542, 17.42774, -155.8765, 586.6406, -901.9577, 418.7792}}}}, 38 | {0.9,{ 39 | {0, {0.8933428, -51.3788, 692.0526, -2970.695, -9079.985}}, 40 | {1, {0.5300101, 20.07061, -220.2416, 1188.216, -3179.489, 3487.29}}}}, 41 | {0.95,{ 42 | {0, {1.290573, -51.06435, 508.2149, 1089.122, -36173.9}}, 43 | {1, {0.3381674, 23.35171, -277.6058, 1727.191, -5242.62, 6267.253}}}}, 44 | {0.975,{ 45 | {0, {1.63135, -50.43061, 335.0748, 4694.975, -59130.16}}, 46 | {1, {0.2820731, 20.09923, -232.4119, 1600.805, -5325.437, 6810.411}}}}, 47 | {0.99,{ 48 | {0, {2.02525, -49.52247, 131.545, 8734.051, -83906.98}}, 49 | {1, {0.121659, 17.82018, -184.9223, 1512.869, -5754.938, 7987.295}}}}, 50 | {0.995,{ 51 | {0, {2.289887, -48.58579, -33.96214, 12094.66, -104876.2}}, 52 | {1, {0.3138123, 4.254046, 43.79203, 106.5682, -1929.322, 4154.06}}}}, 53 | {0.999,{ 54 | {0, {2.832592, -45.2852, -454.6888, 20303.27, -152655.0}}, 55 | {1, {0.4460974, -11.79829, 343.2512, -1557.125, 1982.09, 866.629}}}} 56 | }}, 57 | {"ct",{ 58 | {0.001,{ 59 | {0, {-4.064679, -24.35303, -160.8093, 3417.97, -29721.44}}, 60 | {1, {2.988795, -50.01775, 1285.353, -11102.31, 39779.73, -50648.68}}}}, 61 | {0.005,{ 62 | {0, {-3.617084, -22.33611, 5.425624, 499.2113, -6856.833}}, 63 | {1, {2.212498, -22.46913, 777.0021, -7112.543, 26047.46, -33445.7}}}}, 64 | {0.01,{ 65 | {0, {-3.407817, -21.3686, 49.65953, -17.72955, -3288.767}}, 66 | {1, {2.233841, -25.78092, 773.3825, -6772.984, 24204.3, -30586.87}}}}, 67 | {0.025,{ 68 | {0, {-3.102192, -20.3775, 98.40342, -417.9263, -1395.028}}, 69 | {1, {1.784919, -11.76577, 523.3629, -4900.973, 18035.26, -23151.24}}}}, 70 | {0.05,{ 71 | {0, {-2.846181, -19.94484, 150.1849, -1202.047, 4403.494}}, 72 | {1, {1.496648, -4.331499, 373.3418, -3693.049, 13868.73, -18009.14}}}}, 73 | {0.1,{ 74 | {0, {-2.557861, -20.1136, 217.2022, -2172.267, 10348.15}}, 75 | {1, {1.263156, -0.2713012, 281.1032, -2933.85, 11254.27, -14803.17}}}}, 76 | {0.2,{ 77 | {0, {-2.221751, -20.61889, 276.7115, -2809.184, 13107.6}}, 78 | {1, {1.050483, 1.595909, 222.6227, -2416.858, 9407.894, -12460.15}}}}, 79 | {0.5,{ 80 | {0, {-1.61915, -23.2942, 406.8652, -4234.284, 18540.95}}, 81 | {1, {0.428236, 9.900402, 65.28261, -1217.764, 5435.676, -7654.235}}}}, 82 | {0.8,{ 83 | {0, {-1.069954, -27.7498, 576.8673, -6323.645, 27317.89}}, 84 | {1, {-0.3402695, 19.09789, -71.12733, -235.8644, 2200.882, -3720.224}}}}, 85 | {0.9,{ 86 | {0, {-0.7906876, -30.03265, 661.9614, -7425.294, 32124.67}}, 87 | {1, {-0.9360614, 27.63615, -188.8822, 563.4788, -311.7842, -782.7323}}}}, 88 | {0.95,{ 89 | {0, {-0.5575089, -30.92955, 687.6883, -7680.324, 33082.79}}, 90 | {1, {-1.682314, 34.56686, -263.5979, 1071.46, -1998.443, 1295.644}}}}, 91 | {0.975,{ 92 | {0, {-0.3564748, -31.35741, 695.8376, -7714.068, 33126.65}}, 93 | {1, {-2.585414, 41.35117, -301.7647, 1242.268, -2510.971, 1935.754}}}}, 94 | {0.99,{ 95 | {0, {-0.1274131, -31.69644, 693.3064, -7628.013, 33448.61}}, 96 | {1, {-3.924606, 52.94466, -361.8511, 1435.423, -2908.53, 2306.161}}}}, 97 | {0.995,{ 98 | {0, {0.02660655, -32.1516, 699.7033, -7685.206, 34284.54}}, 99 | {1, {-5.082007, 65.0375, -432.9132, 1686.095, -3478.321, 2928.382}}}}, 100 | {0.999,{ 101 | {0, {0.3354447, -33.57506, 740.3102, -8450.701, 40957.97}}, 102 | {1, {-7.850939, 99.3478, -677.2877, 2666.331, -5583.625, 4786.65}}}} 103 | }} 104 | }; 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /R/RcppURT/include/Coeff_dfgls.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef COEFF_DFGLS_HPP 6 | #define COEFF_DFGLS_HPP 7 | 8 | static const std::map>>> coeff_dfgls = 9 | { 10 | {"c",{ 11 | {0.001,{ 12 | {0, {-3.281721, -20.10785, 20.54557, 919.4611, -15768.36}}, 13 | {1, {1.214884, -9.190296, 465.547, -4800.389, 18494.58, -24523.48}}}}, 14 | {0.005,{ 15 | {0, {-2.798722, -19.87674, 158.6922, -1361.471, 649.8149}}, 16 | {1, {1.377745, -9.989878, 369.4152, -3615.128, 13677.44, -17888.14}}}}, 17 | {0.01,{ 18 | {0, {-2.566274, -20.06523, 218.4769, -2506.118, 9754.758}}, 19 | {1, {1.367608, -14.98339, 466.491, -4344.437, 16100.97, -20814.35}}}}, 20 | {0.025,{ 21 | {0, {-2.227436, -20.71955, 276.1964, -3313.464, 16034.91}}, 22 | {1, {1.075308, -5.706035, 307.2224, -3143.095, 12178.3, -16217.98}}}}, 23 | {0.05,{ 24 | {0, {-1.941807, -21.8198, 327.6527, -3983.442, 20397.75}}, 25 | {1, {0.9627055, -2.586653, 239.4981, -2548.091, 10013.66, -13436.96}}}}, 26 | {0.1,{ 27 | {0, {-1.618534, -23.81555, 403.463, -5111.848, 27837.04}}, 28 | {1, {0.8819745, -0.5482568, 184.5334, -2036.822, 8119.434, -10998.77}}}}, 29 | {0.2,{ 30 | {0, {-1.236467, -27.13579, 499.8872, -6332.546, 34623.67}}, 31 | {1, {0.7793006, 1.366651, 137.9713, -1615.072, 6560.24, -8970.884}}}}, 32 | {0.5,{ 33 | {0, {-0.5023128, -38.29761, 767.7794, -9009.913, 44651.65}}, 34 | {1, {0.5846996, 8.288074, 25.93693, -785.592, 3753.654, -5481.515}}}}, 35 | {0.8,{ 36 | {0, {0.4066535, -50.03626, 832.9085, -6572.743, 16628.01}}, 37 | {1, {0.6261542, 17.42774, -155.8765, 586.6406, -901.9577, 418.7792}}}}, 38 | {0.9,{ 39 | {0, {0.8933428, -51.3788, 692.0526, -2970.695, -9079.985}}, 40 | {1, {0.5300101, 20.07061, -220.2416, 1188.216, -3179.489, 3487.29}}}}, 41 | {0.95,{ 42 | {0, {1.290573, -51.06435, 508.2149, 1089.122, -36173.9}}, 43 | {1, {0.3381674, 23.35171, -277.6058, 1727.191, -5242.62, 6267.253}}}}, 44 | {0.975,{ 45 | {0, {1.63135, -50.43061, 335.0748, 4694.975, -59130.16}}, 46 | {1, {0.2820731, 20.09923, -232.4119, 1600.805, -5325.437, 6810.411}}}}, 47 | {0.99,{ 48 | {0, {2.02525, -49.52247, 131.545, 8734.051, -83906.98}}, 49 | {1, {0.121659, 17.82018, -184.9223, 1512.869, -5754.938, 7987.295}}}}, 50 | {0.995,{ 51 | {0, {2.289887, -48.58579, -33.96214, 12094.66, -104876.2}}, 52 | {1, {0.3138123, 4.254046, 43.79203, 106.5682, -1929.322, 4154.06}}}}, 53 | {0.999,{ 54 | {0, {2.832592, -45.2852, -454.6888, 20303.27, -152655.0}}, 55 | {1, {0.4460974, -11.79829, 343.2512, -1557.125, 1982.09, 866.629}}}} 56 | }}, 57 | {"ct",{ 58 | {0.001,{ 59 | {0, {-4.064679, -24.35303, -160.8093, 3417.97, -29721.44}}, 60 | {1, {2.988795, -50.01775, 1285.353, -11102.31, 39779.73, -50648.68}}}}, 61 | {0.005,{ 62 | {0, {-3.617084, -22.33611, 5.425624, 499.2113, -6856.833}}, 63 | {1, {2.212498, -22.46913, 777.0021, -7112.543, 26047.46, -33445.7}}}}, 64 | {0.01,{ 65 | {0, {-3.407817, -21.3686, 49.65953, -17.72955, -3288.767}}, 66 | {1, {2.233841, -25.78092, 773.3825, -6772.984, 24204.3, -30586.87}}}}, 67 | {0.025,{ 68 | {0, {-3.102192, -20.3775, 98.40342, -417.9263, -1395.028}}, 69 | {1, {1.784919, -11.76577, 523.3629, -4900.973, 18035.26, -23151.24}}}}, 70 | {0.05,{ 71 | {0, {-2.846181, -19.94484, 150.1849, -1202.047, 4403.494}}, 72 | {1, {1.496648, -4.331499, 373.3418, -3693.049, 13868.73, -18009.14}}}}, 73 | {0.1,{ 74 | {0, {-2.557861, -20.1136, 217.2022, -2172.267, 10348.15}}, 75 | {1, {1.263156, -0.2713012, 281.1032, -2933.85, 11254.27, -14803.17}}}}, 76 | {0.2,{ 77 | {0, {-2.221751, -20.61889, 276.7115, -2809.184, 13107.6}}, 78 | {1, {1.050483, 1.595909, 222.6227, -2416.858, 9407.894, -12460.15}}}}, 79 | {0.5,{ 80 | {0, {-1.61915, -23.2942, 406.8652, -4234.284, 18540.95}}, 81 | {1, {0.428236, 9.900402, 65.28261, -1217.764, 5435.676, -7654.235}}}}, 82 | {0.8,{ 83 | {0, {-1.069954, -27.7498, 576.8673, -6323.645, 27317.89}}, 84 | {1, {-0.3402695, 19.09789, -71.12733, -235.8644, 2200.882, -3720.224}}}}, 85 | {0.9,{ 86 | {0, {-0.7906876, -30.03265, 661.9614, -7425.294, 32124.67}}, 87 | {1, {-0.9360614, 27.63615, -188.8822, 563.4788, -311.7842, -782.7323}}}}, 88 | {0.95,{ 89 | {0, {-0.5575089, -30.92955, 687.6883, -7680.324, 33082.79}}, 90 | {1, {-1.682314, 34.56686, -263.5979, 1071.46, -1998.443, 1295.644}}}}, 91 | {0.975,{ 92 | {0, {-0.3564748, -31.35741, 695.8376, -7714.068, 33126.65}}, 93 | {1, {-2.585414, 41.35117, -301.7647, 1242.268, -2510.971, 1935.754}}}}, 94 | {0.99,{ 95 | {0, {-0.1274131, -31.69644, 693.3064, -7628.013, 33448.61}}, 96 | {1, {-3.924606, 52.94466, -361.8511, 1435.423, -2908.53, 2306.161}}}}, 97 | {0.995,{ 98 | {0, {0.02660655, -32.1516, 699.7033, -7685.206, 34284.54}}, 99 | {1, {-5.082007, 65.0375, -432.9132, 1686.095, -3478.321, 2928.382}}}}, 100 | {0.999,{ 101 | {0, {0.3354447, -33.57506, 740.3102, -8450.701, 40957.97}}, 102 | {1, {-7.850939, 99.3478, -677.2877, 2666.331, -5583.625, 4786.65}}}} 103 | }} 104 | }; 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /R/RcppURT/src/RcppOLS.cpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #include "../include/URT.hpp" 6 | 7 | #include 8 | 9 | using namespace urt; 10 | 11 | //------------------------------------------------------------------------------------------------- 12 | 13 | // C++ class OLS exposed to Rcpp 14 | 15 | // create an external pointer to an OLS object 16 | RcppExport SEXP OLS_d__new(SEXP y_, SEXP x_, SEXP stats_) 17 | { 18 | // convert inputs to appropriate C++ types 19 | Rcpp::NumericVector yr(y_); 20 | arma::vec y(yr.begin(), yr.size(), false); 21 | Rcpp::NumericMatrix xr(x_); 22 | arma::mat x(xr.begin(), xr.nrow(), xr.ncol(), false); 23 | bool stats = Rcpp::as(stats_); 24 | 25 | OLS* p = nullptr; 26 | 27 | try { 28 | p = new OLS(y, x, stats); 29 | } catch(std::exception &ex) { 30 | forward_exception_to_r(ex); 31 | } 32 | 33 | // create a pointer to an OLS object and wrap it as an external pointer 34 | Rcpp::XPtr> ptr(p, true); 35 | 36 | // return the external pointer to the R side 37 | return ptr; 38 | } 39 | 40 | RcppExport SEXP OLS_d_get_stats(SEXP ptr_, SEXP y_, SEXP x_) 41 | { 42 | Rcpp::XPtr> ptr(ptr_); 43 | 44 | Rcpp::NumericVector yr(y_); 45 | arma::vec y(yr.begin(), yr.size(), false); 46 | Rcpp::NumericMatrix xr(x_); 47 | arma::mat x(xr.begin(), xr.nrow(), xr.ncol(), false); 48 | 49 | ptr->get_stats(y, x); 50 | 51 | return R_NilValue; 52 | } 53 | 54 | RcppExport SEXP OLS_d_show(SEXP ptr_) 55 | { 56 | Rcpp::XPtr> ptr(ptr_); 57 | 58 | try { 59 | ptr->show(); 60 | } catch(std::exception &ex) { 61 | forward_exception_to_r(ex); 62 | } 63 | 64 | return R_NilValue; 65 | } 66 | 67 | RcppExport SEXP OLS_d_get_param(SEXP ptr_) 68 | { 69 | Rcpp::XPtr> ptr(ptr_); 70 | return Rcpp::wrap(ptr->param); 71 | } 72 | 73 | RcppExport SEXP OLS_d_get_resid(SEXP ptr_) 74 | { 75 | Rcpp::XPtr> ptr(ptr_); 76 | return Rcpp::wrap(ptr->resid); 77 | } 78 | 79 | RcppExport SEXP OLS_d_get_t_stat(SEXP ptr_) 80 | { 81 | Rcpp::XPtr> ptr(ptr_); 82 | return Rcpp::wrap(ptr->t_stat); 83 | } 84 | 85 | RcppExport SEXP OLS_d_get_var(SEXP ptr_) 86 | { 87 | Rcpp::XPtr> ptr(ptr_); 88 | return Rcpp::wrap(ptr->var); 89 | } 90 | 91 | RcppExport SEXP OLS_d_get_MSE(SEXP ptr_) 92 | { 93 | Rcpp::XPtr> ptr(ptr_); 94 | return Rcpp::wrap(ptr->MSE); 95 | } 96 | 97 | RcppExport SEXP OLS_d_get_ndf(SEXP ptr_) 98 | { 99 | Rcpp::XPtr> ptr(ptr_); 100 | return Rcpp::wrap(ptr->ndf); 101 | } 102 | 103 | RcppExport SEXP OLS_d_get_R2(SEXP ptr_) 104 | { 105 | Rcpp::XPtr> ptr(ptr_); 106 | return Rcpp::wrap(ptr->R2); 107 | } 108 | 109 | RcppExport SEXP OLS_d_get_adj_R2(SEXP ptr_) 110 | { 111 | Rcpp::XPtr> ptr(ptr_); 112 | return Rcpp::wrap(ptr->adj_R2); 113 | } 114 | 115 | RcppExport SEXP OLS_d_get_F_stat(SEXP ptr_) 116 | { 117 | Rcpp::XPtr> ptr(ptr_); 118 | return Rcpp::wrap(ptr->F_stat); 119 | } 120 | 121 | RcppExport SEXP OLS_d_get_DW_stat(SEXP ptr_) 122 | { 123 | Rcpp::XPtr> ptr(ptr_); 124 | return Rcpp::wrap(ptr->DW_stat); 125 | } 126 | 127 | RcppExport SEXP OLS_d_get_IC(SEXP ptr_) 128 | { 129 | Rcpp::XPtr> ptr(ptr_); 130 | return Rcpp::wrap(ptr->IC); 131 | } 132 | 133 | //------------------------------------------------------------------------------------------------- 134 | //################################################################################################# 135 | //------------------------------------------------------------------------------------------------- 136 | 137 | // C++ class OLS exposed to Rcpp 138 | 139 | // create an external pointer to an OLS object 140 | RcppExport SEXP OLS_f__new(SEXP y_, SEXP x_, SEXP stats_) 141 | { 142 | const arma::fvec& y = Rcpp::as(y_); 143 | const arma::fmat& x = Rcpp::as(x_); 144 | bool stats = Rcpp::as(stats_); 145 | 146 | OLS* p = nullptr; 147 | 148 | try { 149 | p = new OLS(y, x, stats); 150 | } catch(std::exception &ex) { 151 | forward_exception_to_r(ex); 152 | } 153 | 154 | Rcpp::XPtr> ptr(p, true); 155 | 156 | return ptr; 157 | } 158 | 159 | RcppExport SEXP OLS_f_get_stats(SEXP ptr_, SEXP y_, SEXP x_) 160 | { 161 | Rcpp::XPtr> ptr(ptr_); 162 | 163 | const arma::fvec& y = Rcpp::as(y_); 164 | const arma::fmat& x = Rcpp::as(x_); 165 | 166 | ptr->get_stats(y, x); 167 | 168 | return R_NilValue; 169 | } 170 | 171 | RcppExport SEXP OLS_f_show(SEXP ptr_) 172 | { 173 | Rcpp::XPtr> ptr(ptr_); 174 | 175 | try { 176 | ptr->show(); 177 | } catch(std::exception &ex) { 178 | forward_exception_to_r(ex); 179 | } 180 | 181 | return R_NilValue; 182 | } 183 | 184 | RcppExport SEXP OLS_f_get_param(SEXP ptr_) 185 | { 186 | Rcpp::XPtr> ptr(ptr_); 187 | return Rcpp::wrap(ptr->param); 188 | } 189 | 190 | RcppExport SEXP OLS_f_get_resid(SEXP ptr_) 191 | { 192 | Rcpp::XPtr> ptr(ptr_); 193 | return Rcpp::wrap(ptr->resid); 194 | } 195 | 196 | RcppExport SEXP OLS_f_get_t_stat(SEXP ptr_) 197 | { 198 | Rcpp::XPtr> ptr(ptr_); 199 | return Rcpp::wrap(ptr->t_stat); 200 | } 201 | 202 | RcppExport SEXP OLS_f_get_var(SEXP ptr_) 203 | { 204 | Rcpp::XPtr> ptr(ptr_); 205 | return Rcpp::wrap(ptr->var); 206 | } 207 | 208 | RcppExport SEXP OLS_f_get_MSE(SEXP ptr_) 209 | { 210 | Rcpp::XPtr> ptr(ptr_); 211 | return Rcpp::wrap(ptr->MSE); 212 | } 213 | 214 | RcppExport SEXP OLS_f_get_ndf(SEXP ptr_) 215 | { 216 | Rcpp::XPtr> ptr(ptr_); 217 | return Rcpp::wrap(ptr->ndf); 218 | } 219 | 220 | RcppExport SEXP OLS_f_get_R2(SEXP ptr_) 221 | { 222 | Rcpp::XPtr> ptr(ptr_); 223 | return Rcpp::wrap(ptr->R2); 224 | } 225 | 226 | RcppExport SEXP OLS_f_get_adj_R2(SEXP ptr_) 227 | { 228 | Rcpp::XPtr> ptr(ptr_); 229 | return Rcpp::wrap(ptr->adj_R2); 230 | } 231 | 232 | RcppExport SEXP OLS_f_get_F_stat(SEXP ptr_) 233 | { 234 | Rcpp::XPtr> ptr(ptr_); 235 | return Rcpp::wrap(ptr->F_stat); 236 | } 237 | 238 | RcppExport SEXP OLS_f_get_DW_stat(SEXP ptr_) 239 | { 240 | Rcpp::XPtr> ptr(ptr_); 241 | return Rcpp::wrap(ptr->DW_stat); 242 | } 243 | 244 | RcppExport SEXP OLS_f_get_IC(SEXP ptr_) 245 | { 246 | Rcpp::XPtr> ptr(ptr_); 247 | return Rcpp::wrap(ptr->IC); 248 | } 249 | 250 | //------------------------------------------------------------------------------------------------- 251 | 252 | 253 | 254 | 255 | 256 | -------------------------------------------------------------------------------- /include/UnitRoot.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef UNITROOT_HPP 6 | #define UNITROOT_HPP 7 | 8 | namespace urt { 9 | 10 | //================================================================================================= 11 | 12 | // Abstract base class from which all tests will derive 13 | template 14 | class UnitRoot 15 | { 16 | public: 17 | std::string lags_type; // default lags value long or short 18 | std::string method; // method for lag length optimization 19 | std::string test_type; // t-statistic or normalized (tau or rho) 20 | std::string trend; // regression trend 21 | 22 | float level = 1.64; // statistic (absolute) threshold for optimal lags selection 23 | 24 | int lags = 0; // number of lags in model 25 | int max_lags = 0; // maximum number of lags for lag length optimization 26 | int niter = 1000; // number of iterations, required when bootstrap is set to true 27 | 28 | bool bootstrap = false; // compute critical values and p-value by bootstrap 29 | bool regression = false; // output OLS regression results if set to true 30 | 31 | // get test statistic 32 | const T& get_stat() const; 33 | // get test pvalue 34 | const T& get_pval() const; 35 | // get test OLS regression results 36 | const OLS& get_ols() const; 37 | // get test valid trends 38 | const std::vector& get_trends() const; 39 | 40 | // compute test statistic 41 | virtual const T& statistic() = 0; 42 | // compute test statistic p-value 43 | virtual const T& pvalue() = 0; 44 | // output test results (can be overriden by derived classes) 45 | virtual void show() = 0; 46 | 47 | // virtual destructor 48 | virtual ~UnitRoot() {} 49 | 50 | protected: 51 | std::shared_ptr> result; // test OLS regression result 52 | std::vector valid_trends; // vector of test valid regression trends 53 | Vector* ptr = nullptr; // pointer to data serie 54 | 55 | // pointer to critical values coefficients 56 | const std::map>>* coeff_ptr = nullptr; 57 | 58 | std::string test_name; // test name 59 | 60 | T stat = 0.0; // test statistic 61 | T pval = 0.0; // test p-value 62 | 63 | int nobs; // number of observations 64 | int npar; // number of parameters excluding lag difference terms 65 | 66 | bool new_data = false; // control if bootstrap has modified original data 67 | bool new_lags = false; // control if a new number of lags has been chosen 68 | bool new_level = false; // control if new stat level has been chosen 69 | bool new_method = false; // control if new method has been chosen 70 | bool new_test_type = false; // control if new test type has been chosen 71 | bool new_trend = false; // control if a new trend has been chosen 72 | 73 | // constructor for running test for a given number of lags 74 | UnitRoot(const Vector& data, int lags, const std::string& trend, bool regression = false); 75 | // constructor for running test with lag length optmization 76 | UnitRoot(const Vector& data, const std::string& method, const std::string& trend, bool regression = false); 77 | 78 | // set pointer to the original data 79 | void set_data(); 80 | // set number of lags 81 | void set_lags(); 82 | // set lags type 83 | void set_lags_type(); 84 | // set statistic level for optimal lags selection (method T-STAT) 85 | void set_level(); 86 | // set method for lag length optimization 87 | void set_method(); 88 | // set number of iterations for bootstrap 89 | void set_niter(); 90 | // set test type (tau or rho) 91 | void set_test_type(); 92 | // set regression trend 93 | void set_trend(); 94 | // OLS demeaning or detrending 95 | void ols_detrend(); 96 | // GLS demeaning or detrending 97 | void gls_detrend(); 98 | // run ADF test for a given lag 99 | void adf_regression(); 100 | // compute ADF test 101 | void compute_adf(); 102 | 103 | private: 104 | Vector data; // original data used for the test 105 | Matrix x; // matrix of independent variables for multi-linear regression 106 | Vector y; // vector of dependent variable for multi-linear regression 107 | Vector z; // vector of detrended data (by OLS or GLS) 108 | 109 | std::string prev_lags_type; // previous type of lags 110 | std::string prev_method; // previous method for lag length optimization 111 | std::string prev_test_type; // previous test type (tau or rho) 112 | std::string prev_trend; // previous regression trend 113 | std::string trend_type; // regression trend for outputting test results 114 | 115 | T ICcc; // information criterion correction coefficient 116 | float prev_level; // previous statistic (absolute) value for lag selection 117 | 118 | int nrows; // number of rows in matrix x and elements in vector y 119 | int prev_lags = 0; // previous number of lags 120 | int prev_max_lags = 0; // previous maximum number of lags 121 | int prev_niter = 1000; // previous number of iteratons 122 | 123 | bool new_niter = false; // control if new number of iterations for bootstrap has been chosen 124 | bool optim = false; // control if lag length is optimized 125 | bool new_test = false; // control if a new test is run (true) or all parameters remain the same (false) 126 | bool prev_bootstrap = false; // previous bootstrap value 127 | bool prev_regression = false; // previous regression value 128 | 129 | // vector of test results for lag length optimization 130 | std::vector>> results; 131 | // vector of valid methods for lag length optimization 132 | const std::vector valid_methods{"AIC","BIC","HQC","MAIC","MBIC","MHQC","T-STAT"}; 133 | // array of probabilities for p-value computation 134 | const float probas[15] = {0.001,0.005,0.01,0.025,0.05,0.10,0.20,0.50,0.80,0.90,0.95,0.975,0.99,0.995,0.999}; 135 | // array containing tne test critical values 136 | float critical_values[15]; 137 | // Information Criterion functor 138 | T (UnitRoot::*ICfunc)(const std::shared_ptr>& res); 139 | // set Information Criterion function and correction coefficient 140 | void set_IC(); 141 | // initialize dependent and independent variables in ADF test regression 142 | void initialize_adf(); 143 | // optimize ADF test lag length using Information Criterion 144 | void optimize_lag(); 145 | // select optimal ADF test lag using lags difference terms t-statistics 146 | void select_lag(); 147 | // compute Information Criterion 148 | T IC(const std::shared_ptr>& res); 149 | // compute Modified Information Criterion 150 | T MIC(const std::shared_ptr>& res); 151 | // compute critical value from probabilities 152 | void compute_cv(); 153 | // compute p-value by linear interpolation from critical values 154 | void compute_pval(); 155 | // bootstrap test residuals for critical values and p-value computation 156 | void run_bootstrap(); 157 | }; 158 | 159 | //================================================================================================= 160 | 161 | } 162 | 163 | #endif 164 | 165 | 166 | 167 | 168 | 169 | -------------------------------------------------------------------------------- /R/RcppURT/include/UnitRoot.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef UNITROOT_HPP 6 | #define UNITROOT_HPP 7 | 8 | namespace urt { 9 | 10 | //================================================================================================= 11 | 12 | // Abstract base class from which all tests will derive 13 | template 14 | class UnitRoot 15 | { 16 | public: 17 | std::string lags_type; // default lags value long or short 18 | std::string method; // method for lag length optimization 19 | std::string test_type; // t-statistic or normalized (tau or rho) 20 | std::string trend; // regression trend 21 | 22 | float level = 1.64; // statistic (absolute) threshold for optimal lags selection 23 | 24 | int lags = 0; // number of lags in model 25 | int max_lags = 0; // maximum number of lags for lag length optimization 26 | int niter = 1000; // number of iterations, required when bootstrap is set to true 27 | 28 | bool bootstrap = false; // compute critical values and p-value by bootstrap 29 | bool regression = false; // output OLS regression results if set to true 30 | 31 | // get test statistic 32 | const T& get_stat() const; 33 | // get test pvalue 34 | const T& get_pval() const; 35 | // get test OLS regression results 36 | const OLS& get_ols() const; 37 | // get test valid trends 38 | const std::vector& get_trends() const; 39 | 40 | // compute test statistic 41 | virtual const T& statistic() = 0; 42 | // compute test statistic p-value 43 | virtual const T& pvalue() = 0; 44 | // output test results (can be overriden by derived classes) 45 | virtual void show() = 0; 46 | 47 | // virtual destructor 48 | virtual ~UnitRoot() {} 49 | 50 | protected: 51 | std::shared_ptr> result; // test OLS regression result 52 | std::vector valid_trends; // vector of test valid regression trends 53 | Vector* ptr = nullptr; // pointer to data serie 54 | 55 | // pointer to critical values coefficients 56 | const std::map>>* coeff_ptr = nullptr; 57 | 58 | std::string test_name; // test name 59 | 60 | T stat = 0.0; // test statistic 61 | T pval = 0.0; // test p-value 62 | 63 | int nobs; // number of observations 64 | int npar; // number of parameters excluding lag difference terms 65 | 66 | bool new_data = false; // control if bootstrap has modified original data 67 | bool new_lags = false; // control if a new number of lags has been chosen 68 | bool new_level = false; // control if new stat level has been chosen 69 | bool new_method = false; // control if new method has been chosen 70 | bool new_test_type = false; // control if new test type has been chosen 71 | bool new_trend = false; // control if a new trend has been chosen 72 | 73 | // constructor for running test for a given number of lags 74 | UnitRoot(const Vector& data, int lags, const std::string& trend, bool regression = false); 75 | // constructor for running test with lag length optmization 76 | UnitRoot(const Vector& data, const std::string& method, const std::string& trend, bool regression = false); 77 | 78 | // set pointer to the original data 79 | void set_data(); 80 | // set number of lags 81 | void set_lags(); 82 | // set lags type 83 | void set_lags_type(); 84 | // set statistic level for optimal lags selection (method T-STAT) 85 | void set_level(); 86 | // set method for lag length optimization 87 | void set_method(); 88 | // set number of iterations for bootstrap 89 | void set_niter(); 90 | // set test type (tau or rho) 91 | void set_test_type(); 92 | // set regression trend 93 | void set_trend(); 94 | // OLS demeaning or detrending 95 | void ols_detrend(); 96 | // GLS demeaning or detrending 97 | void gls_detrend(); 98 | // run ADF test for a given lag 99 | void adf_regression(); 100 | // compute ADF test 101 | void compute_adf(); 102 | 103 | private: 104 | Vector data; // original data used for the test 105 | Matrix x; // matrix of independent variables for multi-linear regression 106 | Vector y; // vector of dependent variable for multi-linear regression 107 | Vector z; // vector of detrended data (by OLS or GLS) 108 | 109 | std::string prev_lags_type; // previous type of lags 110 | std::string prev_method; // previous method for lag length optimization 111 | std::string prev_test_type; // previous test type (tau or rho) 112 | std::string prev_trend; // previous regression trend 113 | std::string trend_type; // regression trend for outputting test results 114 | 115 | T ICcc; // information criterion correction coefficient 116 | float prev_level; // previous statistic (absolute) value for lag selection 117 | 118 | int nrows; // number of rows in matrix x and elements in vector y 119 | int prev_lags = 0; // previous number of lags 120 | int prev_max_lags = 0; // previous maximum number of lags 121 | int prev_niter = 1000; // previous number of iteratons 122 | 123 | bool new_niter = false; // control if new number of iterations for bootstrap has been chosen 124 | bool optim = false; // control if lag length is optimized 125 | bool new_test = false; // control if a new test is run (true) or all parameters remain the same (false) 126 | bool prev_bootstrap = false; // previous bootstrap value 127 | bool prev_regression = false; // previous regression value 128 | 129 | // vector of test results for lag length optimization 130 | std::vector>> results; 131 | // vector of valid methods for lag length optimization 132 | const std::vector valid_methods{"AIC","BIC","HQC","MAIC","MBIC","MHQC","T-STAT"}; 133 | // array of probabilities for p-value computation 134 | const float probas[15] = {0.001,0.005,0.01,0.025,0.05,0.10,0.20,0.50,0.80,0.90,0.95,0.975,0.99,0.995,0.999}; 135 | // array containing tne test critical values 136 | float critical_values[15]; 137 | // Information Criterion functor 138 | T (UnitRoot::*ICfunc)(const std::shared_ptr>& res); 139 | // set Information Criterion function and correction coefficient 140 | void set_IC(); 141 | // initialize dependent and independent variables in ADF test regression 142 | void initialize_adf(); 143 | // optimize ADF test lag length using Information Criterion 144 | void optimize_lag(); 145 | // select optimal ADF test lag using lags difference terms t-statistics 146 | void select_lag(); 147 | // compute Information Criterion 148 | T IC(const std::shared_ptr>& res); 149 | // compute Modified Information Criterion 150 | T MIC(const std::shared_ptr>& res); 151 | // compute critical value from probabilities 152 | void compute_cv(); 153 | // compute p-value by linear interpolation from critical values 154 | void compute_pval(); 155 | // bootstrap test residuals for critical values and p-value computation 156 | void run_bootstrap(); 157 | }; 158 | 159 | //================================================================================================= 160 | 161 | } 162 | 163 | #endif 164 | 165 | 166 | 167 | 168 | 169 | -------------------------------------------------------------------------------- /R/RcppURT/R/PP.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | suppressMessages(library(R6)) 6 | 7 | #-------------------------------------------------------------------------------------------------- 8 | 9 | # C++ class PP exposed to R 10 | PP_d <- R6Class("PP_d", 11 | private = list( 12 | ptr = NULL 13 | ), 14 | public = list( 15 | initialize = function(data = NA, lags = "", lags_type = "long", trend = "c", test_type = "tau", regression = FALSE) { 16 | if (lags == "") { 17 | private$ptr <- .Call('PP_d__new2', PACKAGE = 'RcppURT', data_ = data, lags_type_ = lags_type, trend_ = trend, test_type_ = test_type, regression_ = regression) 18 | } else { 19 | private$ptr <- .Call('PP_d__new1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, test_type_ = test_type, regression_ = regression) 20 | } 21 | }, 22 | statistic = function() { 23 | .Call('UnitRoot_d_statistic', PACKAGE = 'RcppURT', ptr_ = private$ptr) 24 | }, 25 | pvalue = function() { 26 | .Call('UnitRoot_d_pvalue', PACKAGE = 'RcppURT', ptr_ = private$ptr) 27 | }, 28 | show = function() { 29 | .Call('UnitRoot_d_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 30 | } 31 | ), 32 | active = list( 33 | stat = function() { 34 | .Call('UnitRoot_d_get_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 35 | }, 36 | pval = function() { 37 | .Call('UnitRoot_d_get_pval', PACKAGE = 'RcppURT', ptr_ = private$ptr) 38 | }, 39 | ols = function() { 40 | return(OLSlist_d(private$ptr)) 41 | }, 42 | trends = function() { 43 | .Call('UnitRoot_d_get_trends', PACKAGE = 'RcppURT', ptr_ = private$ptr) 44 | }, 45 | bootstrap = function(value) { 46 | if (missing(value)) { 47 | .Call('UnitRoot_d_get_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr) 48 | } else { 49 | .Call('UnitRoot_d_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr, bootstrap_ = value) 50 | } 51 | }, 52 | lags = function(value) { 53 | if (missing(value)) { 54 | .Call('UnitRoot_d_get_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 55 | } else { 56 | .Call('UnitRoot_d_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_ = value) 57 | } 58 | }, 59 | lags_type = function(value) { 60 | if (missing(value)) { 61 | .Call('UnitRoot_d_get_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 62 | } else { 63 | Call('UnitRoot_d_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_type_ = value) 64 | } 65 | }, 66 | niter = function(value) { 67 | if (missing(value)) { 68 | .Call('UnitRoot_d_get_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr) 69 | } else { 70 | .Call('UnitRoot_d_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr, niter_ = value) 71 | } 72 | }, 73 | regression = function(value) { 74 | if (missing(value)) { 75 | .Call('UnitRoot_d_get_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr) 76 | } else { 77 | .Call('UnitRoot_d_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr, regression_ = value) 78 | } 79 | }, 80 | test_type = function(value) { 81 | if (missing(value)) { 82 | .Call('UnitRoot_d_get_test_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 83 | } else { 84 | .Call('UnitRoot_d_test_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, test_type_ = value) 85 | } 86 | }, 87 | trend = function(value) { 88 | if (missing(value)) { 89 | .Call('UnitRoot_d_get_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr) 90 | } else { 91 | .Call('UnitRoot_d_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr, trend_ = value) 92 | } 93 | } 94 | ) 95 | ) 96 | 97 | #-------------------------------------------------------------------------------------------------- 98 | ################################################################################################### 99 | #-------------------------------------------------------------------------------------------------- 100 | 101 | # C++ class PP exposed to R 102 | PP_f <- R6Class("PP_f", 103 | private = list( 104 | ptr = NULL 105 | ), 106 | public = list( 107 | initialize = function(data = NA, lags = "", lags_type = "long", trend = "c", test_type = "tau", regression = FALSE) { 108 | if (lags == "") { 109 | private$ptr <- .Call('PP_f__new2', PACKAGE = 'RcppURT', data_ = data, lags_type_ = lags_type, trend_ = trend, test_type_ = test_type, regression_ = regression) 110 | } 111 | else { 112 | private$ptr <- .Call('PP_f__new1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, test_type_ = test_type, regression_ = regression) 113 | } 114 | }, 115 | statistic = function() { 116 | .Call('UnitRoot_f_statistic', PACKAGE = 'RcppURT', ptr_ = private$ptr) 117 | }, 118 | pvalue = function() { 119 | .Call('UnitRoot_f_pvalue', PACKAGE = 'RcppURT', ptr_ = private$ptr) 120 | }, 121 | show = function() { 122 | .Call('UnitRoot_f_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 123 | } 124 | ), 125 | active = list( 126 | stat = function() { 127 | .Call('UnitRoot_f_get_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 128 | }, 129 | pval = function() { 130 | .Call('UnitRoot_f_get_pval', PACKAGE = 'RcppURT', ptr_ = private$ptr) 131 | }, 132 | ols = function() { 133 | return(OLSlist_f(private$ptr)) 134 | }, 135 | trends = function() { 136 | .Call('UnitRoot_f_get_trends', PACKAGE = 'RcppURT', ptr_ = private$ptr) 137 | }, 138 | bootstrap = function(value) { 139 | if (missing(value)) { 140 | .Call('UnitRoot_f_get_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr) 141 | } else { 142 | .Call('UnitRoot_f_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr, bootstrap_ = value) 143 | } 144 | }, 145 | lags = function(value) { 146 | if (missing(value)) { 147 | .Call('UnitRoot_f_get_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 148 | } else { 149 | .Call('UnitRoot_f_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_ = value) 150 | } 151 | }, 152 | lags_type = function(value) { 153 | if (missing(value)) { 154 | .Call('UnitRoot_f_get_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 155 | } 156 | else { 157 | Call('UnitRoot_f_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_type_ = value) 158 | } 159 | }, 160 | niter = function(value) { 161 | if (missing(value)) { 162 | .Call('UnitRoot_f_get_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr) 163 | } else { 164 | .Call('UnitRoot_f_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr, niter_ = value) 165 | } 166 | }, 167 | regression = function(value) { 168 | if (missing(value)) { 169 | .Call('UnitRoot_f_get_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr) 170 | } else { 171 | .Call('UnitRoot_f_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr, regression_ = value) 172 | } 173 | }, 174 | test_type = function(value) { 175 | if (missing(value)) { 176 | .Call('UnitRoot_f_get_test_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 177 | } else { 178 | .Call('UnitRoot_f_test_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, test_type_ = value) 179 | } 180 | }, 181 | trend = function(value) { 182 | if (missing(value)) { 183 | .Call('UnitRoot_f_get_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr) 184 | } else { 185 | .Call('UnitRoot_f_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr, trend_ = value) 186 | } 187 | } 188 | ) 189 | ) 190 | 191 | #-------------------------------------------------------------------------------------------------- 192 | -------------------------------------------------------------------------------- /R/RcppURT/R/ADF.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | suppressMessages(library(R6)) 6 | 7 | #-------------------------------------------------------------------------------------------------- 8 | 9 | # C++ class ADF exposed to R 10 | ADF_d <- R6Class('ADF_d', 11 | private = list( 12 | ptr = NULL 13 | ), 14 | public = list( 15 | initialize = function(data = NA, lags = '', method = 'AIC', trend = 'c', regression = FALSE) { 16 | if (lags == '') { 17 | private$ptr <- .Call('ADF_d__new2', PACKAGE = 'RcppURT', data_ = data, method_ = method, trend_ = trend, regression_ = regression) 18 | } else { 19 | private$ptr <- .Call('ADF_d__new1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, regression_ = regression) 20 | } 21 | }, 22 | statistic = function() { 23 | .Call('UnitRoot_d_statistic', PACKAGE = 'RcppURT', ptr_ = private$ptr) 24 | }, 25 | pvalue = function() { 26 | .Call('UnitRoot_d_pvalue', PACKAGE = 'RcppURT', ptr_ = private$ptr) 27 | }, 28 | show = function() { 29 | .Call('UnitRoot_d_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 30 | } 31 | ), 32 | active = list( 33 | stat = function() { 34 | .Call('UnitRoot_d_get_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 35 | }, 36 | pval = function() { 37 | .Call('UnitRoot_d_get_pval', PACKAGE = 'RcppURT', ptr_ = private$ptr) 38 | }, 39 | ols = function() { 40 | return(OLSlist_d(private$ptr)) 41 | }, 42 | trends = function() { 43 | .Call('UnitRoot_d_get_trends', PACKAGE = 'RcppURT', ptr_ = private$ptr) 44 | }, 45 | bootstrap = function(value) { 46 | if (missing(value)) { 47 | .Call('UnitRoot_d_get_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr) 48 | } else { 49 | .Call('UnitRoot_d_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr, bootstrap_ = value) 50 | } 51 | }, 52 | lags = function(value) { 53 | if (missing(value)) { 54 | .Call('UnitRoot_d_get_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 55 | } else { 56 | .Call('UnitRoot_d_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_ = value) 57 | } 58 | }, 59 | lags_type = function(value) { 60 | if (missing(value)) { 61 | .Call('UnitRoot_d_get_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 62 | } else { 63 | Call('UnitRoot_d_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_type_ = value) 64 | } 65 | }, 66 | level = function(value) { 67 | if (missing(value)) { 68 | .Call('UnitRoot_d_get_level', PACKAGE = 'RcppURT', ptr_ = private$ptr) 69 | } else { 70 | .Call('UnitRoot_d_level', PACKAGE = 'RcppURT', ptr_ = private$ptr, level_ = value) 71 | } 72 | }, 73 | max_lags = function(value) { 74 | if (missing(value)) { 75 | .Call('UnitRoot_d_get_max_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 76 | } else { 77 | .Call('UnitRoot_d_max_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, max_lags_ = value) 78 | } 79 | }, 80 | method = function(value) { 81 | if (missing(value)) { 82 | .Call('UnitRoot_d_get_method', PACKAGE = 'RcppURT', ptr_ = private$ptr) 83 | } else { 84 | .Call('UnitRoot_d_method', PACKAGE = 'RcppURT', ptr_ = private$ptr, method_ = value) 85 | } 86 | }, 87 | niter = function(value) { 88 | if (missing(value)) { 89 | .Call('UnitRoot_d_get_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr) 90 | } else { 91 | .Call('UnitRoot_d_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr, niter_ = value) 92 | } 93 | }, 94 | regression = function(value) { 95 | if (missing(value)) { 96 | .Call('UnitRoot_d_get_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr) 97 | } else { 98 | .Call('UnitRoot_d_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr, regression_ = value) 99 | } 100 | }, 101 | trend = function(value) { 102 | if (missing(value)) { 103 | .Call('UnitRoot_d_get_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr) 104 | } else { 105 | .Call('UnitRoot_d_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr, trend_ = value) 106 | } 107 | } 108 | ) 109 | ) 110 | 111 | #-------------------------------------------------------------------------------------------------- 112 | ################################################################################################### 113 | #-------------------------------------------------------------------------------------------------- 114 | 115 | # C++ class ADF exposed to R 116 | ADF_f <- R6Class('ADF_f', 117 | private = list( 118 | ptr = NULL 119 | ), 120 | public = list( 121 | initialize = function(data = NA, lags = '', method = 'AIC', trend = 'c', regression = FALSE) { 122 | if (lags == '') { 123 | private$ptr <- .Call('ADF_f__new2', PACKAGE = 'RcppURT', data_ = data, method_ = method, trend_ = trend, regression_ = regression) 124 | } else { 125 | private$ptr <- .Call('ADF_f__new1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, regression_ = regression) 126 | } 127 | }, 128 | statistic = function() { 129 | .Call('UnitRoot_f_statistic', PACKAGE = 'RcppURT', ptr_ = private$ptr) 130 | }, 131 | pvalue = function() { 132 | .Call('UnitRoot_f_pvalue', PACKAGE = 'RcppURT', ptr_ = private$ptr) 133 | }, 134 | show = function() { 135 | .Call('UnitRoot_f_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 136 | } 137 | ), 138 | active = list( 139 | stat = function() { 140 | .Call('UnitRoot_f_get_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 141 | }, 142 | pval = function() { 143 | .Call('UnitRoot_f_get_pval', PACKAGE = 'RcppURT', ptr_ = private$ptr) 144 | }, 145 | ols = function() { 146 | return(OLSlist_f(private$ptr)) 147 | }, 148 | trends = function() { 149 | .Call('UnitRoot_f_get_trends', PACKAGE = 'RcppURT', ptr_ = private$ptr) 150 | }, 151 | bootstrap = function(value) { 152 | if (missing(value)) { 153 | .Call('UnitRoot_f_get_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr) 154 | } else { 155 | .Call('UnitRoot_f_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr, bootstrap_ = value) 156 | } 157 | }, 158 | lags = function(value) { 159 | if (missing(value)) { 160 | .Call('UnitRoot_f_get_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 161 | } else { 162 | .Call('UnitRoot_f_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_ = value) 163 | } 164 | }, 165 | lags_type = function(value) { 166 | if (missing(value)) { 167 | .Call('UnitRoot_f_get_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 168 | } 169 | else { 170 | Call('UnitRoot_f_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_type_ = value) 171 | } 172 | }, 173 | level = function(value) { 174 | if (missing(value)) { 175 | .Call('UnitRoot_f_get_level', PACKAGE = 'RcppURT', ptr_ = private$ptr) 176 | } else { 177 | .Call('UnitRoot_f_level', PACKAGE = 'RcppURT', ptr_ = private$ptr, level_ = value) 178 | } 179 | }, 180 | max_lags = function(value) { 181 | if (missing(value)) { 182 | .Call('UnitRoot_f_get_max_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 183 | } else { 184 | .Call('UnitRoot_f_max_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, max_lags_ = value) 185 | } 186 | }, 187 | method = function(value) { 188 | if (missing(value)) { 189 | .Call('UnitRoot_f_get_method', PACKAGE = 'RcppURT', ptr_ = private$ptr) 190 | } else { 191 | .Call('UnitRoot_f_method', PACKAGE = 'RcppURT', ptr_ = private$ptr, method_ = value) 192 | } 193 | }, 194 | niter = function(value) { 195 | if (missing(value)) { 196 | .Call('UnitRoot_f_get_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr) 197 | } else { 198 | .Call('UnitRoot_f_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr, niter_ = value) 199 | } 200 | }, 201 | regression = function(value) { 202 | if (missing(value)) { 203 | .Call('UnitRoot_f_get_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr) 204 | } else { 205 | .Call('UnitRoot_f_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr, regression_ = value) 206 | } 207 | }, 208 | trend = function(value) { 209 | if (missing(value)) { 210 | .Call('UnitRoot_f_get_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr) 211 | } else { 212 | .Call('UnitRoot_f_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr, trend_ = value) 213 | } 214 | } 215 | ) 216 | ) 217 | 218 | #-------------------------------------------------------------------------------------------------- 219 | 220 | -------------------------------------------------------------------------------- /R/RcppURT/R/DFGLS.R: -------------------------------------------------------------------------------- 1 | #================================================================================================== 2 | # Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | #================================================================================================== 4 | 5 | suppressMessages(library(R6)) 6 | 7 | #-------------------------------------------------------------------------------------------------- 8 | 9 | # C++ class DFGLS exposed to R 10 | DFGLS_d <- R6Class("DFGLS_d", 11 | private = list( 12 | ptr = NULL 13 | ), 14 | public = list( 15 | initialize = function(data = NA, lags = "", method = "AIC", trend = "c", regression = FALSE) { 16 | if (lags == "") { 17 | private$ptr <- .Call('DFGLS_d__new2', PACKAGE = 'RcppURT', data_ = data, method_ = method, trend_ = trend, regression_ = regression) 18 | } else { 19 | private$ptr <- .Call('DFGLS_d__new1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, regression_ = regression) 20 | } 21 | }, 22 | statistic = function() { 23 | .Call('UnitRoot_d_statistic', PACKAGE = 'RcppURT', ptr_ = private$ptr) 24 | }, 25 | pvalue = function() { 26 | .Call('UnitRoot_d_pvalue', PACKAGE = 'RcppURT', ptr_ = private$ptr) 27 | }, 28 | show = function() { 29 | .Call('UnitRoot_d_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 30 | } 31 | ), 32 | active = list( 33 | stat = function() { 34 | .Call('UnitRoot_d_get_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 35 | }, 36 | pval = function() { 37 | .Call('UnitRoot_d_get_pval', PACKAGE = 'RcppURT', ptr_ = private$ptr) 38 | }, 39 | ols = function() { 40 | return(OLSlist_d(private$ptr)) 41 | }, 42 | trends = function() { 43 | .Call('UnitRoot_d_get_trends', PACKAGE = 'RcppURT', ptr_ = private$ptr) 44 | }, 45 | bootstrap = function(value) { 46 | if (missing(value)) { 47 | .Call('UnitRoot_d_get_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr) 48 | } else { 49 | .Call('UnitRoot_d_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr, bootstrap_ = value) 50 | } 51 | }, 52 | lags = function(value) { 53 | if (missing(value)) { 54 | .Call('UnitRoot_d_get_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 55 | } else { 56 | .Call('UnitRoot_d_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_ = value) 57 | } 58 | }, 59 | lags_type = function(value) { 60 | if (missing(value)) { 61 | .Call('UnitRoot_d_get_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 62 | } else { 63 | Call('UnitRoot_d_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_type_ = value) 64 | } 65 | }, 66 | level = function(value) { 67 | if (missing(value)) { 68 | .Call('UnitRoot_d_get_level', PACKAGE = 'RcppURT', ptr_ = private$ptr) 69 | } else { 70 | .Call('UnitRoot_d_level', PACKAGE = 'RcppURT', ptr_ = private$ptr, level_ = value) 71 | } 72 | }, 73 | max_lags = function(value) { 74 | if (missing(value)) { 75 | .Call('UnitRoot_d_get_max_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 76 | } else { 77 | .Call('UnitRoot_d_max_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, max_lags_ = value) 78 | } 79 | }, 80 | method = function(value) { 81 | if (missing(value)) { 82 | .Call('UnitRoot_d_get_method', PACKAGE = 'RcppURT', ptr_ = private$ptr) 83 | } else { 84 | .Call('UnitRoot_d_method', PACKAGE = 'RcppURT', ptr_ = private$ptr, method_ = value) 85 | } 86 | }, 87 | niter = function(value) { 88 | if (missing(value)) { 89 | .Call('UnitRoot_d_get_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr) 90 | } else { 91 | .Call('UnitRoot_d_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr, niter_ = value) 92 | } 93 | }, 94 | regression = function(value) { 95 | if (missing(value)) { 96 | .Call('UnitRoot_d_get_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr) 97 | } else { 98 | .Call('UnitRoot_d_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr, regression_ = value) 99 | } 100 | }, 101 | trend = function(value) { 102 | if (missing(value)) { 103 | .Call('UnitRoot_d_get_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr) 104 | } else { 105 | .Call('UnitRoot_d_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr, trend_ = value) 106 | } 107 | } 108 | ) 109 | ) 110 | 111 | #-------------------------------------------------------------------------------------------------- 112 | ################################################################################################### 113 | #-------------------------------------------------------------------------------------------------- 114 | 115 | # C++ class DFGLS exposed to R 116 | DFGLS_f <- R6Class("DFGLS_f", 117 | private = list( 118 | ptr = NULL 119 | ), 120 | public = list( 121 | initialize = function(data = NA, lags = "", method = "AIC", trend = "c", regression = FALSE) { 122 | if (lags == "") { 123 | private$ptr <- .Call('DFGLS_f__new2', PACKAGE = 'RcppURT', data_ = data, method_ = method, trend_ = trend, regression_ = regression) 124 | } else { 125 | private$ptr <- .Call('DFGLS_f__new1', PACKAGE = 'RcppURT', data_ = data, lags_ = lags, trend_ = trend, regression_ = regression) 126 | } 127 | }, 128 | statistic = function() { 129 | .Call('UnitRoot_f_statistic', PACKAGE = 'RcppURT', ptr_ = private$ptr) 130 | }, 131 | pvalue = function() { 132 | .Call('UnitRoot_f_pvalue', PACKAGE = 'RcppURT', ptr_ = private$ptr) 133 | }, 134 | show = function() { 135 | .Call('UnitRoot_f_show', PACKAGE = 'RcppURT', ptr_ = private$ptr) 136 | } 137 | ), 138 | active = list( 139 | stat = function() { 140 | .Call('UnitRoot_f_get_stat', PACKAGE = 'RcppURT', ptr_ = private$ptr) 141 | }, 142 | pval = function() { 143 | .Call('UnitRoot_f_get_pval', PACKAGE = 'RcppURT', ptr_ = private$ptr) 144 | }, 145 | ols = function() { 146 | return(OLSlist_f(private$ptr)) 147 | }, 148 | trends = function() { 149 | .Call('UnitRoot_f_get_trends', PACKAGE = 'RcppURT', ptr_ = private$ptr) 150 | }, 151 | bootstrap = function(value) { 152 | if (missing(value)) { 153 | .Call('UnitRoot_f_get_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr) 154 | } else { 155 | .Call('UnitRoot_f_bootstrap', PACKAGE = 'RcppURT', ptr_ = private$ptr, bootstrap_ = value) 156 | } 157 | }, 158 | lags = function(value) { 159 | if (missing(value)) { 160 | .Call('UnitRoot_f_get_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 161 | } else { 162 | .Call('UnitRoot_f_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_ = value) 163 | } 164 | }, 165 | lags_type = function(value) { 166 | if (missing(value)) { 167 | .Call('UnitRoot_f_get_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr) 168 | } 169 | else { 170 | Call('UnitRoot_f_lags_type', PACKAGE = 'RcppURT', ptr_ = private$ptr, lags_type_ = value) 171 | } 172 | }, 173 | level = function(value) { 174 | if (missing(value)) { 175 | .Call('UnitRoot_f_get_level', PACKAGE = 'RcppURT', ptr_ = private$ptr) 176 | } else { 177 | .Call('UnitRoot_f_level', PACKAGE = 'RcppURT', ptr_ = private$ptr, level_ = value) 178 | } 179 | }, 180 | max_lags = function(value) { 181 | if (missing(value)) { 182 | .Call('UnitRoot_f_get_max_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr) 183 | } else { 184 | .Call('UnitRoot_f_max_lags', PACKAGE = 'RcppURT', ptr_ = private$ptr, max_lags_ = value) 185 | } 186 | }, 187 | method = function(value) { 188 | if (missing(value)) { 189 | .Call('UnitRoot_f_get_method', PACKAGE = 'RcppURT', ptr_ = private$ptr) 190 | } else { 191 | .Call('UnitRoot_f_method', PACKAGE = 'RcppURT', ptr_ = private$ptr, method_ = value) 192 | } 193 | }, 194 | niter = function(value) { 195 | if (missing(value)) { 196 | .Call('UnitRoot_f_get_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr) 197 | } else { 198 | .Call('UnitRoot_f_niter', PACKAGE = 'RcppURT', ptr_ = private$ptr, niter_ = value) 199 | } 200 | }, 201 | regression = function(value) { 202 | if (missing(value)) { 203 | .Call('UnitRoot_f_get_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr) 204 | } else { 205 | .Call('UnitRoot_f_regression', PACKAGE = 'RcppURT', ptr_ = private$ptr, regression_ = value) 206 | } 207 | }, 208 | trend = function(value) { 209 | if (missing(value)) { 210 | .Call('UnitRoot_f_get_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr) 211 | } else { 212 | .Call('UnitRoot_f_trend', PACKAGE = 'RcppURT', ptr_ = private$ptr, trend_ = value) 213 | } 214 | } 215 | ) 216 | ) 217 | 218 | #-------------------------------------------------------------------------------------------------- 219 | -------------------------------------------------------------------------------- /include/Coeff_adf.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef COEFF_ADF_HPP 6 | #define COEFF_ADF_HPP 7 | 8 | // critical values response surface coefficients 9 | // for each probability or significance level the associated quantile is estimated via the following regression: 10 | // C(k,T)(p) = B0 + B1/T + B2/T^2 + B3*(k/T) + B4*(k/T)^2 + e(k,T) 11 | // with T = N - k - 1 where N is the sample size, k is the number of lags, T is the effective number of observations and e(k,T) the residuals 12 | // B0 is the asymptotic critical value of the test for the p significance level 13 | // this is an example, in some cases we will have more term in 1/T^i or (k/T)^i 14 | // in the example above, index 0 would contain B0, B1 and B2 and index 1 B3 and B4 15 | // The choice of the number of regressors has been made looking at the precision of each model estimation by comparing with Monte-Carlo simulated critical values, and taking into account the regression quality that is the parameters heteroskedasticity consistent standard errors and t values, the regression standard error, the Akaike information criterion, the goodness of fit R and the residuals auto-correlation. 16 | // We have taken care especially of the quality of the 1%, 5% and 10% confidence levels regressions. 17 | 18 | // We have used the methodology explained in "Lag Order and Critical Values of the Augmented Dickey-Fuller Test" by Cheung and Lai (1995), we have extended the sample sizes and number of lags and have used more replications to get more accurate results. 19 | 20 | static const std::map>>> coeff_adf = 21 | { 22 | {"nc",{ 23 | {0.001,{ 24 | {0, {-3.281702, -4.768726, -272.9072, 5579.295, -45226.96}}, 25 | {1, {0.5493214, -2.698764}}}}, 26 | {0.005,{ 27 | {0, {-2.794503, -3.1995, -146.9075, 3099.156, -24497.04}}, 28 | {1, {0.5505589, -2.085242}}}}, 29 | {0.01,{ 30 | {0, {-2.561287, -2.142564, -121.1985, 2584.976, -20410.33}}, 31 | {1, {0.4760427, -1.539142}}}}, 32 | {0.025,{ 33 | {0, {-2.223917, -0.6914316, -111.5947, 2411.508, -17908.34}}, 34 | {1, {0.4181673, -1.186685}}}}, 35 | {0.05,{ 36 | {0, {-1.936403, -1.176172, -4.937049}}, 37 | {1, {0.4133739, -1.106667}}}}, 38 | {0.10,{ 39 | {0, {-1.61316, -0.3324831, -1.810886}}, 40 | {1, {0.346874, -0.7603873}}}}, 41 | {0.20,{ 42 | {0, {-1.232539, 0.6746855, -2.034739}}, 43 | {1, {0.2687615, -0.4501587}}}}, 44 | {0.50,{ 45 | {0, {-0.5019654, 4.022296, -26.36976}}, 46 | {1, {0.2869943, 0.03450908}}}}, 47 | {0.8,{ 48 | {0, {0.4032327, 4.540675, -33.07985}}, 49 | {1, {0.5776706, 0.2456931}}}}, 50 | {0.9,{ 51 | {0, {0.8867146, 4.419886, -29.49461}}, 52 | {1, {0.741675, -0.1049857}}}}, 53 | {0.95,{ 54 | {0, {1.281964, 4.550459, -25.75607}}, 55 | {1, {0.8509155, -0.4257711}}}}, 56 | {0.975,{ 57 | {0, {1.621407, 4.919284, -21.08875}}, 58 | {1, {0.9096531, -0.6824688}}}}, 59 | {0.99,{ 60 | {0, {2.013246, 5.729489, -14.25646}}, 61 | {1, {0.9222194, -0.6803509}}}}, 62 | {0.995,{ 63 | {0, {2.278707, 6.459439, -6.844265}}, 64 | {1, {0.9229393, -0.58905}}}}, 65 | {0.999,{ 66 | {0, {2.82238, 8.798691, 15.4077}}, 67 | {1, {0.8097516, 0.076346}}}} 68 | }}, 69 | {"c",{ 70 | {0.001,{ 71 | {0, {-4.093629, -10.95619, -201.1707, 4059.445, -47421.5}}, 72 | {1, {1.135068, -5.529418}}}}, 73 | {0.005,{ 74 | {0, {-3.644943, -7.32321, -133.5845, 3116.307, -34229.66}}, 75 | {1, {0.9485738, -3.219351}}}}, 76 | {0.01,{ 77 | {0, {-3.431418, -5.638492, -117.5981, 2807.583, -28966.07}}, 78 | {1, {0.8777557, -2.478217}}}}, 79 | {0.025,{ 80 | {0, {-3.122512, -4.084893, -56.41564, 1374.696, -15404.28}}, 81 | {1, {0.7851985, -1.540707}}}}, 82 | {0.05,{ 83 | {0, {-2.861234, -3.352301, 14.44003, -393.0152}}, 84 | {1, {0.7401921, -1.162586}}}}, 85 | {0.1,{ 86 | {0, {-2.56724, -1.456952, -11.77928}}, 87 | {1, {0.6391723, -0.5805052}}}}, 88 | {0.2,{ 89 | {0, {-2.21775, -0.4380315, -5.553964}}, 90 | {1, {0.5146035, 0.07913832}}}}, 91 | {0.5,{ 92 | {0, {-1.565026, 0.5520032, 0.3320911}}, 93 | {1, {0.2884884, 1.558016}}}}, 94 | {0.8,{ 95 | {0, {-0.8648469, 1.248728, 0.2664044}}, 96 | {1, {1.179858, 1.250165}}}}, 97 | {0.9,{ 98 | {0, {-0.4406348, 1.649804, -3.58948}}, 99 | {1, {1.939274, 0.2523149}}}}, 100 | {0.95,{ 101 | {0, {-0.07812974, 1.893762, -6.304656}}, 102 | {1, {2.639824, -0.7719409}}}}, 103 | {0.975,{ 104 | {0, {0.239394, 2.14986, -8.572655}}, 105 | {1, {3.330538, -1.912599}}}}, 106 | {0.99,{ 107 | {0, {0.6091726, 2.496982, -7.439446}}, 108 | {1, {4.225463, -3.616533}}}}, 109 | {0.995,{ 110 | {0, {0.8623198, 2.874015, -5.580965}}, 111 | {1, {4.839039, -4.653141}}}}, 112 | {0.999,{ 113 | {0, {1.38689, 3.415222, 13.42629}}, 114 | {1, {6.166719, -6.678131}}}} 115 | }}, 116 | {"ct",{ 117 | {0.001,{ 118 | {0, {-4.602904, -12.17787, -527.9003, 13769.63, -149247.9}}, 119 | {1, {1.738192, -8.439729}}}}, 120 | {0.005,{ 121 | {0, {-4.168873, -9.095683, -266.1883, 6786.044, -75171.46}}, 122 | {1, {1.449617, -4.963447}}}}, 123 | {0.01,{ 124 | {0, {-3.961001, -7.941104, -164.6046, 4154.17, -48022.67}}, 125 | {1, {1.33377, -3.942321}}}}, 126 | {0.025,{ 127 | {0, {-3.662298, -5.911063, -88.6424, 2221.722, -26744.54}}, 128 | {1, {1.16549, -2.516979}}}}, 129 | {0.05,{ 130 | {0, {-3.410894, -4.149194, -64.03544, 1624.534, -18656.29}}, 131 | {1, {1.022964, -1.462953}}}}, 132 | {0.1,{ 133 | {0, {-3.127902, -2.344014, -22.26126}}, 134 | {1, {0.8885139, -0.7515388}}}}, 135 | {0.2,{ 136 | {0, {-2.7924, -0.9703418, -9.753819}}, 137 | {1, {0.6833622, 0.4110066}}}}, 138 | {0.5,{ 139 | {0, {-2.17854, 0.6154379, 2.143893}}, 140 | {1, {0.323513, 2.654173}}}}, 141 | {0.8,{ 142 | {0, {-1.584759, 1.35046, 9.264756}}, 143 | {1, {0.8602757, 4.07367}}}}, 144 | {0.9,{ 145 | {0, {-1.250804, 2.20269, 4.417211}}, 146 | {1, {1.987795, 2.371877}}}}, 147 | {0.95,{ 148 | {0, {-0.9430168, 2.91489, -3.161068}}, 149 | {1, {2.961466, 0.795468}}}}, 150 | {0.975,{ 151 | {0, {-0.6617862, 3.388312, -7.564776}}, 152 | {1, {3.852376, -0.7378108}}}}, 153 | {0.99,{ 154 | {0, {-0.3247998, 3.833996, -9.750533}}, 155 | {1, {4.934732, -2.514723}}}}, 156 | {0.995,{ 157 | {0, {-0.09078475, 3.95775, -6.916539}}, 158 | {1, {5.73554, -3.762424}}}}, 159 | {0.999,{ 160 | {0, {0.3995264, 4.650644, -2.198471}}, 161 | {1, {7.366291, -5.53531}}}} 162 | }}, 163 | {"ctt",{ 164 | {0.001,{ 165 | {0, {-5.013231, -12.32148, -931.3062, 26178.47, -281197.4}}, 166 | {1, {2.373687, -13.23884}}}}, 167 | {0.005,{ 168 | {0, {-4.579845, -10.82918, -406.3291, 10976.8, -123523.2}}, 169 | {1, {1.821085, -7.467968}}}}, 170 | {0.01,{ 171 | {0, {-4.376393, -9.20233, -288.6418, 7529.634, -84447.02}}, 172 | {1, {1.637286, -5.610608}}}}, 173 | {0.025,{ 174 | {0, {-4.082349, -7.139103, -167.3334, 4387.772, -50031.24}}, 175 | {1, {1.43226, -3.575693}}}}, 176 | {0.05,{ 177 | {0, {-3.833339, -5.531, -87.00663, 2242.58, -27241.12}}, 178 | {1, {1.226892, -2.0091}}}}, 179 | {0.1,{ 180 | {0, {-3.551958, -4.452909, 26.14951, -675.7979}}, 181 | {1, {1.021034, -0.6615268}}}}, 182 | {0.2,{ 183 | {0, {-3.223184, -1.522704, -13.55332}}, 184 | {1, {0.77266, 0.8243226}}}}, 185 | {0.5,{ 186 | {0, {-2.617229, 0.5046602, 7.156626}}, 187 | {1, {0.3347223, 3.860026}}}}, 188 | {0.8,{ 189 | {0, {-2.043999, 1.426968, 20.87687}}, 190 | {1, {0.7072516, 6.213741}}}}, 191 | {0.9,{ 192 | {0, {-1.743752, 2.260416, 20.26852}}, 193 | {1, {1.886585, 4.803952}}}}, 194 | {0.95,{ 195 | {0, {-1.474189, 3.330163, 11.66451}}, 196 | {1, {3.088977, 2.70093}}}}, 197 | {0.975,{ 198 | {0, {-1.219848, 4.087149, 4.610631}}, 199 | {1, {4.181104, 0.7281762}}}}, 200 | {0.99,{ 201 | {0, {-0.908479, 4.874732, -3.408405}}, 202 | {1, {5.469988, -1.455318}}}}, 203 | {0.995,{ 204 | {0, {-0.6891048, 5.363189, -7.792514}}, 205 | {1, {6.365469, -2.765692}}}}, 206 | {0.999,{ 207 | {0, {-0.2228572, 5.742693, -1.09101}}, 208 | {1, {8.303857, -5.030229}}}} 209 | }} 210 | }; 211 | 212 | #endif 213 | -------------------------------------------------------------------------------- /R/RcppURT/include/Coeff_adf.hpp: -------------------------------------------------------------------------------- 1 | //================================================================================================= 2 | // Copyright (C) 2016 Olivier Mallet - All Rights Reserved 3 | //================================================================================================= 4 | 5 | #ifndef COEFF_ADF_HPP 6 | #define COEFF_ADF_HPP 7 | 8 | // critical values response surface coefficients 9 | // for each probability or significance level the associated quantile is estimated via the following regression: 10 | // C(k,T)(p) = B0 + B1/T + B2/T^2 + B3*(k/T) + B4*(k/T)^2 + e(k,T) 11 | // with T = N - k - 1 where N is the sample size, k is the number of lags, T is the effective number of observations and e(k,T) the residuals 12 | // B0 is the asymptotic critical value of the test for the p significance level 13 | // this is an example, in some cases we will have more term in 1/T^i or (k/T)^i 14 | // in the example above, index 0 would contain B0, B1 and B2 and index 1 B3 and B4 15 | // The choice of the number of regressors has been made looking at the precision of each model estimation by comparing with Monte-Carlo simulated critical values, and taking into account the regression quality that is the parameters heteroskedasticity consistent standard errors and t values, the regression standard error, the Akaike information criterion, the goodness of fit R and the residuals auto-correlation. 16 | // We have taken care especially of the quality of the 1%, 5% and 10% confidence levels regressions. 17 | 18 | // We have used the methodology explained in "Lag Order and Critical Values of the Augmented Dickey-Fuller Test" by Cheung and Lai (1995), we have extended the sample sizes and number of lags and have used more replications to get more accurate results. 19 | 20 | static const std::map>>> coeff_adf = 21 | { 22 | {"nc",{ 23 | {0.001,{ 24 | {0, {-3.281702, -4.768726, -272.9072, 5579.295, -45226.96}}, 25 | {1, {0.5493214, -2.698764}}}}, 26 | {0.005,{ 27 | {0, {-2.794503, -3.1995, -146.9075, 3099.156, -24497.04}}, 28 | {1, {0.5505589, -2.085242}}}}, 29 | {0.01,{ 30 | {0, {-2.561287, -2.142564, -121.1985, 2584.976, -20410.33}}, 31 | {1, {0.4760427, -1.539142}}}}, 32 | {0.025,{ 33 | {0, {-2.223917, -0.6914316, -111.5947, 2411.508, -17908.34}}, 34 | {1, {0.4181673, -1.186685}}}}, 35 | {0.05,{ 36 | {0, {-1.936403, -1.176172, -4.937049}}, 37 | {1, {0.4133739, -1.106667}}}}, 38 | {0.10,{ 39 | {0, {-1.61316, -0.3324831, -1.810886}}, 40 | {1, {0.346874, -0.7603873}}}}, 41 | {0.20,{ 42 | {0, {-1.232539, 0.6746855, -2.034739}}, 43 | {1, {0.2687615, -0.4501587}}}}, 44 | {0.50,{ 45 | {0, {-0.5019654, 4.022296, -26.36976}}, 46 | {1, {0.2869943, 0.03450908}}}}, 47 | {0.8,{ 48 | {0, {0.4032327, 4.540675, -33.07985}}, 49 | {1, {0.5776706, 0.2456931}}}}, 50 | {0.9,{ 51 | {0, {0.8867146, 4.419886, -29.49461}}, 52 | {1, {0.741675, -0.1049857}}}}, 53 | {0.95,{ 54 | {0, {1.281964, 4.550459, -25.75607}}, 55 | {1, {0.8509155, -0.4257711}}}}, 56 | {0.975,{ 57 | {0, {1.621407, 4.919284, -21.08875}}, 58 | {1, {0.9096531, -0.6824688}}}}, 59 | {0.99,{ 60 | {0, {2.013246, 5.729489, -14.25646}}, 61 | {1, {0.9222194, -0.6803509}}}}, 62 | {0.995,{ 63 | {0, {2.278707, 6.459439, -6.844265}}, 64 | {1, {0.9229393, -0.58905}}}}, 65 | {0.999,{ 66 | {0, {2.82238, 8.798691, 15.4077}}, 67 | {1, {0.8097516, 0.076346}}}} 68 | }}, 69 | {"c",{ 70 | {0.001,{ 71 | {0, {-4.093629, -10.95619, -201.1707, 4059.445, -47421.5}}, 72 | {1, {1.135068, -5.529418}}}}, 73 | {0.005,{ 74 | {0, {-3.644943, -7.32321, -133.5845, 3116.307, -34229.66}}, 75 | {1, {0.9485738, -3.219351}}}}, 76 | {0.01,{ 77 | {0, {-3.431418, -5.638492, -117.5981, 2807.583, -28966.07}}, 78 | {1, {0.8777557, -2.478217}}}}, 79 | {0.025,{ 80 | {0, {-3.122512, -4.084893, -56.41564, 1374.696, -15404.28}}, 81 | {1, {0.7851985, -1.540707}}}}, 82 | {0.05,{ 83 | {0, {-2.861234, -3.352301, 14.44003, -393.0152}}, 84 | {1, {0.7401921, -1.162586}}}}, 85 | {0.1,{ 86 | {0, {-2.56724, -1.456952, -11.77928}}, 87 | {1, {0.6391723, -0.5805052}}}}, 88 | {0.2,{ 89 | {0, {-2.21775, -0.4380315, -5.553964}}, 90 | {1, {0.5146035, 0.07913832}}}}, 91 | {0.5,{ 92 | {0, {-1.565026, 0.5520032, 0.3320911}}, 93 | {1, {0.2884884, 1.558016}}}}, 94 | {0.8,{ 95 | {0, {-0.8648469, 1.248728, 0.2664044}}, 96 | {1, {1.179858, 1.250165}}}}, 97 | {0.9,{ 98 | {0, {-0.4406348, 1.649804, -3.58948}}, 99 | {1, {1.939274, 0.2523149}}}}, 100 | {0.95,{ 101 | {0, {-0.07812974, 1.893762, -6.304656}}, 102 | {1, {2.639824, -0.7719409}}}}, 103 | {0.975,{ 104 | {0, {0.239394, 2.14986, -8.572655}}, 105 | {1, {3.330538, -1.912599}}}}, 106 | {0.99,{ 107 | {0, {0.6091726, 2.496982, -7.439446}}, 108 | {1, {4.225463, -3.616533}}}}, 109 | {0.995,{ 110 | {0, {0.8623198, 2.874015, -5.580965}}, 111 | {1, {4.839039, -4.653141}}}}, 112 | {0.999,{ 113 | {0, {1.38689, 3.415222, 13.42629}}, 114 | {1, {6.166719, -6.678131}}}} 115 | }}, 116 | {"ct",{ 117 | {0.001,{ 118 | {0, {-4.602904, -12.17787, -527.9003, 13769.63, -149247.9}}, 119 | {1, {1.738192, -8.439729}}}}, 120 | {0.005,{ 121 | {0, {-4.168873, -9.095683, -266.1883, 6786.044, -75171.46}}, 122 | {1, {1.449617, -4.963447}}}}, 123 | {0.01,{ 124 | {0, {-3.961001, -7.941104, -164.6046, 4154.17, -48022.67}}, 125 | {1, {1.33377, -3.942321}}}}, 126 | {0.025,{ 127 | {0, {-3.662298, -5.911063, -88.6424, 2221.722, -26744.54}}, 128 | {1, {1.16549, -2.516979}}}}, 129 | {0.05,{ 130 | {0, {-3.410894, -4.149194, -64.03544, 1624.534, -18656.29}}, 131 | {1, {1.022964, -1.462953}}}}, 132 | {0.1,{ 133 | {0, {-3.127902, -2.344014, -22.26126}}, 134 | {1, {0.8885139, -0.7515388}}}}, 135 | {0.2,{ 136 | {0, {-2.7924, -0.9703418, -9.753819}}, 137 | {1, {0.6833622, 0.4110066}}}}, 138 | {0.5,{ 139 | {0, {-2.17854, 0.6154379, 2.143893}}, 140 | {1, {0.323513, 2.654173}}}}, 141 | {0.8,{ 142 | {0, {-1.584759, 1.35046, 9.264756}}, 143 | {1, {0.8602757, 4.07367}}}}, 144 | {0.9,{ 145 | {0, {-1.250804, 2.20269, 4.417211}}, 146 | {1, {1.987795, 2.371877}}}}, 147 | {0.95,{ 148 | {0, {-0.9430168, 2.91489, -3.161068}}, 149 | {1, {2.961466, 0.795468}}}}, 150 | {0.975,{ 151 | {0, {-0.6617862, 3.388312, -7.564776}}, 152 | {1, {3.852376, -0.7378108}}}}, 153 | {0.99,{ 154 | {0, {-0.3247998, 3.833996, -9.750533}}, 155 | {1, {4.934732, -2.514723}}}}, 156 | {0.995,{ 157 | {0, {-0.09078475, 3.95775, -6.916539}}, 158 | {1, {5.73554, -3.762424}}}}, 159 | {0.999,{ 160 | {0, {0.3995264, 4.650644, -2.198471}}, 161 | {1, {7.366291, -5.53531}}}} 162 | }}, 163 | {"ctt",{ 164 | {0.001,{ 165 | {0, {-5.013231, -12.32148, -931.3062, 26178.47, -281197.4}}, 166 | {1, {2.373687, -13.23884}}}}, 167 | {0.005,{ 168 | {0, {-4.579845, -10.82918, -406.3291, 10976.8, -123523.2}}, 169 | {1, {1.821085, -7.467968}}}}, 170 | {0.01,{ 171 | {0, {-4.376393, -9.20233, -288.6418, 7529.634, -84447.02}}, 172 | {1, {1.637286, -5.610608}}}}, 173 | {0.025,{ 174 | {0, {-4.082349, -7.139103, -167.3334, 4387.772, -50031.24}}, 175 | {1, {1.43226, -3.575693}}}}, 176 | {0.05,{ 177 | {0, {-3.833339, -5.531, -87.00663, 2242.58, -27241.12}}, 178 | {1, {1.226892, -2.0091}}}}, 179 | {0.1,{ 180 | {0, {-3.551958, -4.452909, 26.14951, -675.7979}}, 181 | {1, {1.021034, -0.6615268}}}}, 182 | {0.2,{ 183 | {0, {-3.223184, -1.522704, -13.55332}}, 184 | {1, {0.77266, 0.8243226}}}}, 185 | {0.5,{ 186 | {0, {-2.617229, 0.5046602, 7.156626}}, 187 | {1, {0.3347223, 3.860026}}}}, 188 | {0.8,{ 189 | {0, {-2.043999, 1.426968, 20.87687}}, 190 | {1, {0.7072516, 6.213741}}}}, 191 | {0.9,{ 192 | {0, {-1.743752, 2.260416, 20.26852}}, 193 | {1, {1.886585, 4.803952}}}}, 194 | {0.95,{ 195 | {0, {-1.474189, 3.330163, 11.66451}}, 196 | {1, {3.088977, 2.70093}}}}, 197 | {0.975,{ 198 | {0, {-1.219848, 4.087149, 4.610631}}, 199 | {1, {4.181104, 0.7281762}}}}, 200 | {0.99,{ 201 | {0, {-0.908479, 4.874732, -3.408405}}, 202 | {1, {5.469988, -1.455318}}}}, 203 | {0.995,{ 204 | {0, {-0.6891048, 5.363189, -7.792514}}, 205 | {1, {6.365469, -2.765692}}}}, 206 | {0.999,{ 207 | {0, {-0.2228572, 5.742693, -1.09101}}, 208 | {1, {8.303857, -5.030229}}}} 209 | }} 210 | }; 211 | 212 | #endif 213 | --------------------------------------------------------------------------------