Metadata-Version: 2.4
Name: co-eco
Version: 0.0.1
Summary: Quasi-likelihood ratio tests for cointegration, cobreaking, and cotrending
Author-email: Dr Merwan Roudane <merwanroudane920@gmail.com>
Maintainer-email: Dr Merwan Roudane <merwanroudane920@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/merwanroudane/coeco
Project-URL: Repository, https://github.com/merwanroudane/coeco
Project-URL: Documentation, https://github.com/merwanroudane/coeco#readme
Project-URL: Bug Tracker, https://github.com/merwanroudane/coeco/issues
Keywords: econometrics,cointegration,cobreaking,cotrending,structural breaks,time series,unit root,quasi-likelihood ratio,DOLS
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Education
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.20.0
Requires-Dist: scipy>=1.7.0
Requires-Dist: pandas>=1.3.0
Requires-Dist: tabulate>=0.8.9
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov>=3.0.0; extra == "dev"
Requires-Dist: black>=22.0.0; extra == "dev"
Requires-Dist: isort>=5.10.0; extra == "dev"
Requires-Dist: flake8>=4.0.0; extra == "dev"
Requires-Dist: mypy>=0.950; extra == "dev"
Provides-Extra: docs
Requires-Dist: sphinx>=4.5.0; extra == "docs"
Requires-Dist: sphinx-rtd-theme>=1.0.0; extra == "docs"
Requires-Dist: numpydoc>=1.4.0; extra == "docs"
Provides-Extra: plotting
Requires-Dist: matplotlib>=3.5.0; extra == "plotting"
Dynamic: license-file

# co-eco

**Quasi-likelihood ratio tests for cointegration, cobreaking, and cotrending**

[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A Python implementation of the econometric tests proposed by Carrion-i-Silvestre and Kim (2019) for testing cointegration, cobreaking, and cotrending in time series data with structural breaks.

## Overview

This package provides three types of tests:

1. **Robust Cointegration Test (Q_r)**: Tests the null hypothesis of cointegration regardless of whether structural breaks are present (robust to the presence or absence of breaks).

2. **Joint CI/CB Test (Q_cb)**: Tests the joint null hypothesis of cointegration AND cobreaking (breaks cancel out).

3. **Joint CI/CT Test (Q_ct)**: Tests the joint null hypothesis of cointegration AND cotrending (both breaks and linear trends cancel out). Only applicable for Model II.

Additionally, **Dmax tests** are provided for determining the number of breaks when testing cobreaking or cotrending.

## Installation

```bash
pip install co-eco
```

Or install from source:

```bash
git clone https://github.com/merwanroudane/coeco.git
cd coeco
pip install -e .
```

## Quick Start

```python
import numpy as np
from co_eco import CKTest

# Generate example data
np.random.seed(42)
T = 200
x = np.cumsum(np.random.randn(T, 1), axis=0)  # I(1) regressor
y = 0.5 * x.flatten() + np.cumsum(np.random.randn(T))  # Cointegrated

# Run tests
test = CKTest(y, x, model=2, klags=2, kleads=2)
results = test.run(num_breaks=1)

# Display results
print(results)
```

## Detailed Usage

### Model Specifications

- **Model I**: Mean shifts only (no linear trend)
- **Model II**: Linear trend with intercept shifts

### Known Break Dates

```python
from co_eco import ck_test_known_breaks

Q_r, Q_cb, Q_ct = ck_test_known_breaks(
    y, x, 
    model=2, 
    break_dates=[100],  # Known break at t=100
    klags=2, 
    kleads=2
)
```

### Unknown Break Dates

```python
from co_eco import ck_test_unknown_1break, ck_test_unknown_2breaks

# One unknown break
Q_r, Q_cb, Q_ct, Tbhat = ck_test_unknown_1break(y, x, model=2, klags=2, kleads=2)

# Two unknown breaks
Q_r, Q_cb, Q_ct, Tbhat = ck_test_unknown_2breaks(y, x, model=2, klags=2, kleads=2)
```

### Using the CKTest Class

```python
from co_eco import CKTest

test = CKTest(y, x, model=2, klags=2, kleads=2)

# Run for specific number of breaks
results_1break = test.run(num_breaks=1)
print(results_1break.is_cointegrated(0.05))  # True if CI holds at 5% level

# Run for all break specifications (0, 1, 2)
all_results = test.run_all(max_breaks=2)

# Run Dmax tests
dmax_cb, dmax_ct = test.run_dmax(max_breaks=2)
print(dmax_cb)
```

### Output Formats

```python
# Get summary
print(results.summary())

# Get LaTeX table
latex_table = results.to_latex()

# Get dictionary
results_dict = results.to_dict()
```

## Example: US Budget Sustainability

```python
from co_eco import CKTest
from co_eco.data import load_us_budget

# Load data
R, E, B = load_us_budget()

# Test for cointegration between expenditures and revenues
test = CKTest(E, R.reshape(-1, 1), model=2, klags=3, kleads=3)

# Run tests with 1 break
results = test.run(num_breaks=1)
print(results)

# Run tests with 2 breaks
results_2 = test.run(num_breaks=2)
print(results_2)
```

## Critical Values

Critical values are provided for:
- Significance levels: 1%, 5%, 10%
- Number of breaks: 0, 1, 2
- Number of regressors: 1-5
- Both Model I and Model II

```python
from co_eco import get_critical_values, get_lambda_bar

# Get critical value
cv = get_critical_values('robust_ci', m=1, model=2, px=1, significance_level=0.05)

# Get lambda bar parameter
lbar = get_lambda_bar(m=1, model=2, px=1)
```

## API Reference

### Main Functions

| Function | Description |
|----------|-------------|
| `ck_test_known_breaks()` | Tests with known break dates |
| `ck_test_unknown_1break()` | Tests with one unknown break |
| `ck_test_unknown_2breaks()` | Tests with two unknown breaks |
| `dmax_cobreaking_test()` | Dmax test for cobreaking |
| `dmax_cotrending_test()` | Dmax test for cotrending |

### Utility Functions

| Function | Description |
|----------|-------------|
| `long_run_variance()` | HAC estimator with Andrews bandwidth |
| `dols_reg_maker()` | DOLS regressor matrix construction |
| `create_step_dummies()` | Step dummy variable creation |

### Classes

| Class | Description |
|-------|-------------|
| `CKTest` | Main class for running all tests |
| `CKTestResults` | Results container with formatting |
| `DmaxTestResults` | Dmax test results container |

## Mathematical Background

The tests are based on quasi-likelihood ratio principles. Under the null hypothesis of cointegration:

$$y_t = \beta' x_t + \alpha' d_t + v_t$$

where:
- $y_t$ is the dependent variable
- $x_t$ are stochastic regressors (I(1))
- $d_t$ are deterministic components (intercepts, trends, step dummies)
- $v_t$ is I(0) under cointegration, I(1) under no cointegration

The test statistics are:

- **Q_r**: Robust CI test (invariant to break parameters)
- **Q_cb**: Joint test with $m \log(T)$ adjustment
- **Q_ct**: Joint test with additional adjustment for trend

## Compatibility

This package is an exact translation of the original MATLAB code by Carrion-i-Silvestre and Kim (2019). Results should match the MATLAB implementation up to numerical precision.

## Citation

If you use this package in your research, please cite:

```bibtex
@article{carrion2019quasi,
  title={Quasi-likelihood ratio tests for cointegration, cobreaking, and cotrending},
  author={Carrion-i-Silvestre, Josep Llu{\'\i}s and Kim, Dukpa},
  journal={Econometric Reviews},
  volume={38},
  number={6},
  pages={681--709},
  year={2019},
  publisher={Taylor \& Francis},
  doi={10.1080/07474938.2018.1528416}
}
```

And this Python implementation:

```bibtex
@software{coeco2024,
  author = {Roudane, Merwan},
  title = {co-eco: Python implementation of Carrion-i-Silvestre and Kim (2019) cointegration tests},
  year = {2024},
  url = {https://github.com/merwanroudane/coeco}
}
```

## References

- Carrion-i-Silvestre, J.L. and Kim, D. (2019). Quasi-likelihood ratio tests for cointegration, cobreaking, and cotrending. *Econometric Reviews*, 38(6), 681-709.
- Andrews, D.W.K. (1991). Heteroskedasticity and Autocorrelation Consistent Covariance Matrix Estimation. *Econometrica*, 59(3), 817-858.
- Saikkonen, P. (1991). Asymptotically Efficient Estimation of Cointegration Regressions. *Econometric Theory*, 7(1), 1-21.

## Author

**Dr Merwan Roudane**  
Email: merwanroudane920@gmail.com  
GitHub: [https://github.com/merwanroudane/coeco](https://github.com/merwanroudane/coeco)

## License

MIT License - see [LICENSE](LICENSE) for details.
