# Dataism Lab for Quantatative Finance (DLQF) Python Library

##  Summary - The goal of this library provide a collection of methods that can be implemented by clients to test for linearity and non-linearity on univariate and multivariate data sets.

# 0. Abstract

In quantitative finance, accurately modeling the dynamics of asset prices is critical for risk management, portfolio optimization, and the development of trading strategies. Many financial time series exhibit complex, potentially nonlinear behavior that cannot be adequately captured by traditional linear models. Detecting nonlinearity in time series can signal the need for more advanced modeling techniques such as regime-switching models, nonlinear autoregressive models, or machine learning methods. This document proposes the development of an expanded Python library for nonlinearity testing by integrating and extending the functionality of an existing package called nln-test.py. The new library is designed to support both univariate and multivariate nonlinearity tests with specific focus on financial time series (e.g., equities, indices, and market volumes). In addition to implementing conventional tests such as those proposed by Ramsey, Keenan, Tsay, and Terasvirta, the package will incorporate an artificial neural network (ANN) approach to further enhance testing power. Ultimately, the library aims to aid quantitative finance practitioners and researchers in determining the appropriateness of linear versus nonlinear modeling in financial applications.

# 1. Introduction

Many financial time series exhibit nonlinear dynamics that standard linear models do not capture. For quantitative finance practitioners, nonlinearity tests are crucial to decide if more advanced models such as nonlinear autoregressive models, regime-switching models, or machine learning techniques are necessary. The expanded library, hereafter referred to as fin_nln, is designed to build on the existing nln-test.py package by incorporating modules for both univariate and multivariate nonlinearity tests, as well as tests based on artificial neural networks (ANNs). Additionally, the library will include integrated support for financial data acquisition and preprocessing.

# 2. Surface-Level

1. **fin_nln.univariate**: 
    + This module builds on the existing nlntstuniv functionality. 
    + It implements tests such as Ramsey’s RESET, Keenan’s test, Tsay’s test, and Terasvirta’s test for univariate time series.
    + Enhancements will be provided to support financial time series, including flexibility in selecting lag structures.

2. **fin_nln.multivariate**: 
    + This module extends the functionality of the nlntstmultv module for multivariate nonlinearity testing. 
    + It is designed to accept and analyze multiple related financial time series. 
    + Applications include testing for nonlinear interactions among equity returns or asset classes.

3. **fin_nln.ann**: 
    + This module enhances the annnlntst module, implementing and optimizing ANN based tests. 
    + Modifications may include additional neural network architectures or hyperparameter tuning to improve test power, particularly for financial data.

4. **fin_nln.data**: 
    + A new module to handle data acquisition and preprocessing. 
    + Functions to download financial data from sources such as Yahoo Finance or FRED using packages like pandas-datareader or yfinance. 
    + Includes typical preprocessing steps such as computing log returns, detrending, and scaling.

5. **fin_nln.visualize**: 
    + An optional module for creating diagnostic plots and visualizations. 
    + Will include autocorrelation plots, evolution of test statistics, and plots illustrating nonlinear dynamics.

# 3. Deeper Breakdown

### Univariate Linearity Tests

- **Ljung-Box Test** *from univariate.linearity.ljung_box_test import ljung_box_test*
    - Tests for autocorrelation in residuals (linear dependence).

- **Augmented Dickey-Fuller (ADF)**	*from univariate.linearity.adf_test import adf_test*
    - Stationarity check — helps determine linear time series behavior.

- **AR Model Fit & Residual Analysis** *from univariate.linearity.fit_ar_resid import fit_ar_resid*
    - Fit AR(p) and inspect residuals for linear structure.

### Univariate **Non**-Linearity Tests

- **Hurst Exponent (R/S Analysis)**	*from univariate.non-linearity.hurst_exponent import hurst_exponent*
    - Measures long memory (persistence or anti-persistence).

- **BDS Test** *from univariate.non-linearity.bds_test import bds_test*
    - Detects general nonlinearity in residuals of a time series.

- **Largest Lyapunov Exponent**	*from univariate.non-linearity.lyapunov_exponent import lyapunov_exponent*
    - Detects chaotic behavior via sensitivity to initial conditions.

- **Tsay Test**	*from univariate.non-linearity.tsay_test import tsay_test*
    - A test for threshold nonlinearity in autoregressive processes.

### Multivariate Linearity Tests

- **VAR Model Fit**	*from multivariate.linearity.var_model_fit*
    - Tests for linear interdependencies among variables.

- **Granger Causality Test** *from multivariate.linearity.granger_test import granger_test*
    - Determines if one time series linearly predicts another.

- **Cointegration Tests (Johansen)** *from multivariate.linearity.johansen_cointegration import johansen_cointegration*
    - Tests for linear long-run equilibrium relationships.

### Multivariate **Non**-Linearity Tests

- **Multivariate BDS Test (mBDS)** *from multivariate.non-linearity.mBDS_test import mBDS_test*
    - Extension of BDS to multivariate context.

- **Mutual Information** *from multivariate.non-linearity.mutual_info import mutual_info*
    - Measures nonlinear dependence among variables.

- **Nonlinear PCA / Kernel PCA** *from multivariate.non-linearity.nonlinear_pca.py import nonlinear_pca / from multivariate.non-linearity.kernel_pca import kernel_pca*
    - Uncovers nonlinear structures in multivariate data.

- **Cross-Lyapunov Exponents**	*from multivariate.non-linearity.lyapunov_exponent import lyapunov_exponent*
    - Tests chaotic synchronization across variables.
