Panel Cointegration with Common Factors — CupFM, CupBC, Bai FM, LSDV estimators (Bai, Kao & Ng 2009)
Project description
PyCupFM — Panel Cointegration with Common Factors
Python implementation of all 5 panel cointegration estimators from
Bai, Kao & Ng (2009, Journal of Econometrics) and Bai & Kao (2005, SSRN)
✨ Features
| Feature | Description |
|---|---|
| 5 Estimators | LSDV, Bai FM, CupFM ★, CupFM-bar, CupBC |
| Auto Factor Selection | Bai & Ng (2002) information criterion |
| 3 Kernels | Bartlett, Parzen, Quadratic Spectral |
| Auto Bandwidth | Newey-West and Andrews plug-in rules |
| Beautiful Plots | 9 publication-quality visualization types |
| Export | LaTeX, Excel, CSV, HTML tables |
| Monte Carlo | Built-in DGP simulation (BKN 2009 design) |
| Pandas Native | DataFrame input/output |
📦 Installation
pip install pycupfm
🚀 Quick Start
from pycupfm import CupFM
from pycupfm.datasets import load_grunfeld
# Load classic Grunfeld investment data (N=10 firms, T=20 years)
df = load_grunfeld()
# Fit all 5 estimators
model = CupFM(n_factors=1, bandwidth=3, max_iter=25)
results = model.fit(
y=df['linvest'],
X=df[['lmvalue', 'lkstock']],
panel_id=df['firm'],
time_id=df['year'],
var_names=['lmvalue', 'lkstock'],
dep_var='linvest'
)
# Publication-quality summary table
results.summary()
# Beautiful visualizations
model.plot(kind='all')
📊 Output Example
==============================================================================
cupfm — Panel Cointegration with Common Factors v1.0.0
Bai, Kao & Ng (2009, JoE 149:82-99) | Bai & Kao (2005, SSRN-1815227)
==============================================================================
Panel Information
--------------------------------------------------------------------------
Dependent variable : linvest Regressors : lmvalue, lkstock
Cross-sections (N) : 10 Time periods (T) : 20
Observations (N×T) : 200 Panel type : Balanced
Common factors (r) : 1 Bandwidth (M) : 3 (bartlett)
--------------------------------------------------------------------------
Estimation Results
--------------------------------------------------------------------------
Variable | LSDV Bai FM CupFM CupFM-bar CupBC
--------------+----------------------------------------------------------
lmvalue | 0.1234*** 0.1189*** 0.1156*** 0.1180*** 0.1145***
| ( 5.67) ( 5.23) ( 5.01) ( 5.12) ( 4.95)
lkstock | 0.3456*** 0.3312*** 0.3289*** 0.3301*** 0.3278***
| ( 8.91) ( 8.34) ( 8.22) ( 8.27) ( 8.15)
--------------------------------------------------------------------------
📐 Model
Panel cointegrating regression with common factor structure:
$$y_{it} = \alpha_i + \beta' x_{it} + \lambda_i' F_t + u_{it}$$
where $F_t$ are $r$ common I(1) stochastic trends, $\lambda_i$ are heterogeneous factor loadings, and $\beta$ is the homogeneous cointegrating vector.
Estimators
| Estimator | Method | Iterates | Reference |
|---|---|---|---|
| LSDV | Within/FE | No | Biased baseline |
| Bai FM | FM correction (1-step) | No | Bai & Kao (2005) |
| CupFM ★ | FM + continuous updating | Yes | BKN (2009) Thm 3 |
| CupFM-bar | FM + Z-bar instrument | Yes | BKN (2009) |
| CupBC | Bias-corrected + updating | Yes | BKN (2009) Thm 2 |
🎨 Visualization Gallery
# Individual plots
model.plot(kind='coefficients') # Forest plot with 95% CIs
model.plot(kind='factors') # Estimated common factors
model.plot(kind='loadings') # Factor loadings scatter/bar
model.plot(kind='convergence') # CupFM iteration path
model.plot(kind='omega') # Long-run covariance heatmap
model.plot(kind='ic') # Bai-Ng IC values vs r
🔬 Monte Carlo Simulation
from pycupfm import simulate_panel, monte_carlo
# Simulate BKN (2009) DGP
sim = simulate_panel(N=20, T=40, k=1, r=2, beta=2.0, seed=42)
# Run Monte Carlo experiment
mc = monte_carlo(n_reps=100, N=20, T=40, verbose=True)
📤 Export Results
# Export to multiple formats
results.to_latex() # LaTeX table
results.to_excel('out.xlsx') # Excel workbook
results.to_csv('out.csv') # CSV file
results.to_dataframe() # Pandas DataFrame
# Or all at once:
from pycupfm import export_results
export_results(results, 'cupfm_results', fmt='all')
🔗 Also Available for Stata
This package is the Python companion to the Stata cupfm package:
ssc install cupfm
cupfm y x1 x2, nfactors(2) bandwidth(5) plot
📚 References
- Bai, J., Kao, C. & Ng, S. (2009). Panel cointegration with global stochastic trends. Journal of Econometrics, 149(1), 82-99. DOI
- Bai, J. & Kao, C. (2005). On the estimation and inference of a panel cointegration model with cross-sectional dependence. CPR Working Paper No. 75. SSRN
- Bai, J. & Ng, S. (2002). Determining the number of factors in approximate factor models. Econometrica, 70(1), 191-221.
👤 Author
Dr. Merwan Roudane
- Email: merwanroudane920@gmail.com
- GitHub: @merwanroudane
📄 License
MIT License — see LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pycupfm-1.0.0.tar.gz.
File metadata
- Download URL: pycupfm-1.0.0.tar.gz
- Upload date:
- Size: 28.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d38697133344ed55659b0605602e77b7d111b996435c3105f6b162000a343dd3
|
|
| MD5 |
88a30f60b397b4017db02f6ff66c16f4
|
|
| BLAKE2b-256 |
01475e9386f8474bc80d3ed016dcca188eb1624d85581f51e5ae02b1ddbc23f8
|
File details
Details for the file pycupfm-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pycupfm-1.0.0-py3-none-any.whl
- Upload date:
- Size: 30.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e201b243b30d2f97f2135b42657a68018ffe77231b40185a74747e12272e7b5
|
|
| MD5 |
7c1bf0e2846be6d55624504d108cbc91
|
|
| BLAKE2b-256 |
7beaf0645a9e75d63922c4872df77803ebca2a1aca62dab81f7ff9c249a0b192
|