Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

10.4 KB
Raw Blame

Statsmodels Wrapper

Comprehensive Python wrapper for the statsmodels library, providing access to advanced statistical modeling, time series analysis, and econometric methods.

Overview

This wrapper provides 100% coverage of the statsmodels library with 200+ functions organized into logical modules:

  • Regression Models: OLS, WLS, GLS, GLSAR, Recursive LS, Rolling OLS, Rolling WLS (9 functions)
  • Time Series Models: ARIMA, SARIMAX, VARMAX, AutoReg, ARDL, Exponential Smoothing, Holt, Simple ES, Markov Autoregression, STL Forecast (16 functions)
  • Time Series Functions: ACF, PACF, ADF, KPSS, Cointegration tests (12 functions)
  • GLM Models: Logit, Probit, Poisson, Negative Binomial, Conditional Logit, Conditional Poisson, Generalized Poisson, Zero-Inflated models (16 functions)
  • Statistical Tests: t-tests, ANOVA, Chi-square, Diagnostic tests (20 functions)
  • Power Analysis: Sample size and power calculations (8 functions)
  • Multivariate Analysis: PCA, Factor Analysis, MANOVA, Canonical Correlation (4 functions)
  • Robust Regression: RLM, MAD, Huber scale (4 functions)
  • Survival Analysis: Cox PH, Kaplan-Meier, Log-rank test (3 functions)
  • Nonparametric Methods: KDE, Kernel Regression, LOWESS (4 functions)
  • GAM: Generalized Additive Models (2 functions)
  • Extended Stats: 100+ additional statistical functions in stats_extended module covering all of statsmodels.stats.api

Installation

The statsmodels library is already installed as part of the Fincept Terminal dependencies.

pip install statsmodels==0.14.4

Module Structure

statsmodels_wrapper/
├── __init__.py              # Main exports
├── regression.py            # Regression models (9 functions)
├── timeseries_models.py     # Time series models (16 functions)
├── timeseries_funcs.py      # Time series functions (12 functions)
├── glm.py                   # Generalized linear models (16 functions)
├── stats_tests.py           # Statistical tests (20 functions)
├── power_analysis.py        # Power and sample size (8 functions)
├── multivariate.py          # Multivariate analysis (4 functions)
├── robust.py                # Robust regression (4 functions)
├── survival.py              # Survival analysis (3 functions)
├── nonparametric.py         # Nonparametric methods (4 functions)
├── gam.py                   # Generalized additive models (2 functions)
├── stats_extended.py        # Extended stats module (100+ functions)
└── README.md                # This file

Quick Start

Regression Analysis

from statsmodels_wrapper import fit_ols, regression_diagnostics

result = fit_ols(y, X, add_constant=True)
print(f"R-squared: {result['rsquared']:.4f}")

diagnostics = regression_diagnostics(y, X)
print(f"Durbin-Watson: {diagnostics['durbin_watson']:.4f}")

Time Series Analysis

from statsmodels_wrapper import fit_arima, forecast_arima, adf_test

model = fit_arima(data, order=(1, 1, 1))
print(f"AIC: {model['aic']:.4f}")

forecast = forecast_arima(data, order=(1, 1, 1), steps=10)

adf_result = adf_test(data)
print(f"ADF p-value: {adf_result['pvalue']:.4f}")

Classification Models

from statsmodels_wrapper import fit_logit, predict_logit

result = fit_logit(y_binary, X)
print(f"Pseudo R-squared: {result['pseudo_rsquared']:.4f}")

predictions = predict_logit(y_binary, X, X_new)
print(predictions['probabilities'])

Statistical Tests

from statsmodels_wrapper import ttest_ind, proportions_ztest, het_breuschpagan

t_result = ttest_ind(x1, x2)
print(f"T-test p-value: {t_result['pvalue']:.4f}")

prop_result = proportions_ztest(count, nobs, value=0.5)

bp_result = het_breuschpagan(residuals, X)
print(f"Breusch-Pagan p-value: {bp_result['lm_pvalue']:.4f}")

Power Analysis

from statsmodels_wrapper import ttest_power, anova_power

power = ttest_power(effect_size=0.5, nobs=50, alpha=0.05)
print(f"Power: {power['power']:.4f}")

sample_size = ttest_power(effect_size=0.5, power=0.8, alpha=0.05)
print(f"Required n: {sample_size['nobs']:.0f}")

Function Reference

Regression (regression.py)

Function Description
fit_ols Ordinary Least Squares regression
fit_wls Weighted Least Squares regression
fit_gls Generalized Least Squares
fit_glsar GLS with AR errors
fit_recursive_ls Recursive Least Squares
predict_ols OLS prediction on new data
regression_diagnostics Comprehensive diagnostics

Time Series Models (timeseries_models.py)

Function Description
fit_arima ARIMA model
forecast_arima ARIMA forecast
fit_sarimax Seasonal ARIMA with exogenous variables
fit_varmax Vector Autoregression Moving Average
fit_autoreg Autoregressive model
fit_ardl Autoregressive Distributed Lag
fit_exponential_smoothing Exponential Smoothing
stl_decompose STL decomposition
fit_dynamic_factor Dynamic Factor model
fit_unobserved_components Unobserved Components model
fit_markov_regression Markov Switching Regression
fit_vecm Vector Error Correction Model

Time Series Functions (timeseries_funcs.py)

Function Description
calculate_acf Autocorrelation function
calculate_pacf Partial autocorrelation function
calculate_ccf Cross-correlation function
adf_test Augmented Dickey-Fuller test
kpss_test KPSS stationarity test
coint_test Engle-Granger cointegration test
bds_test BDS test for independence
acorr_ljungbox Ljung-Box test
seasonal_decompose_additive Seasonal decomposition
arma_order_select ARMA order selection

GLM Models (glm.py)

Function Description
fit_glm Generalized Linear Model
fit_logit Logistic Regression
predict_logit Logit predictions
fit_probit Probit Regression
fit_poisson Poisson Regression
fit_negative_binomial Negative Binomial model
fit_mnlogit Multinomial Logit
fit_quantreg Quantile Regression
fit_gee Generalized Estimating Equations
fit_zero_inflated_poisson Zero-Inflated Poisson

Statistical Tests (stats_tests.py)

Function Description
ttest_ind Independent samples t-test
ttest_1samp One sample t-test
ztest Z-test for mean
proportions_ztest Proportions test
jarque_bera_test Normality test
het_breuschpagan Breusch-Pagan test
het_white White test
multipletests_correction Multiple testing correction

Power Analysis (power_analysis.py)

Function Description
ttest_power T-test power calculation
ftest_power F-test power calculation
anova_power ANOVA power calculation
calculate_effect_size_cohens_d Cohen's d effect size
sample_size_proportion_confint Sample size for CI

Multivariate (multivariate.py)

Function Description
perform_pca Principal Component Analysis
perform_factor_analysis Factor Analysis
perform_manova Multivariate ANOVA
canonical_correlation Canonical Correlation Analysis

Robust (robust.py)

Function Description
fit_rlm Robust Linear Model
mad Median Absolute Deviation
huber_scale Huber scale estimator

Survival (survival.py)

Function Description
fit_cox_ph Cox Proportional Hazards model
survdiff Log-rank test
kaplan_meier Kaplan-Meier survival function

Nonparametric (nonparametric.py)

Function Description
kernel_density_estimation Univariate KDE
kernel_regression Kernel Regression
lowess_smoothing LOWESS smoothing
multivariate_kde Multivariate KDE

GAM (gam.py)

Function Description
fit_glm_gam Generalized Additive Model
fit_gam_formula GAM with formula interface

Extended Stats (stats_extended.py)

The stats_extended module provides 100+ additional statistical functions covering the complete statsmodels.stats.api. Access functions via:

from statsmodels_wrapper import stats_extended

# Example usage
result = stats_extended.binom_test([60], [100], prop=0.5)
fdr = stats_extended.fdrcorrection([0.01, 0.04, 0.1, 0.3])
het_test = stats_extended.het_breuschpagan(residuals, X)

Categories included:

  • Binomial tests (binom_test, binom_test_reject_interval, binom_tost_reject_interval)
  • ANOVA tests (anova_oneway, anova_generic)
  • Confidence intervals (confint_poisson, confint_mvmean, confint_effectsize_oneway)
  • Effect sizes (effectsize_2proportions, effectsize_oneway, effectsize_smd)
  • Equivalence tests (equivalence_oneway, etest_poisson_2indep, nonequivalence_poisson_2indep)
  • FDR corrections (fdrcorrection, fdrcorrection_twostage, multipletests, local_fdr)
  • Heteroscedasticity tests (het_arch, het_breuschpagan, het_white, het_goldfeldquandt)
  • Linearity tests (linear_harvey_collier, linear_lm, linear_rainbow, linear_reset)
  • Normality tests (lilliefors, omni_normtest, jarque_bera, normal_ad)
  • Poisson tests (test_poisson, test_poisson_2indep, tost_poisson_2indep)
  • Power calculations (power_binom_tost, power_equivalence_poisson_2indep, power_proportions_2indep)
  • Proportion tests (proportions_chisquare, proportions_ztest, proportion_confint, test_proportions_2indep)
  • Runs tests (runstest_1samp, runstest_2samp)
  • t-tests & z-tests (ttest_ind, ttost_ind, ttost_paired, ztest, ztost)
  • Covariance functions (cov_nearest_factor_homog, cov_nw_panel, cov_white_simple)
  • Correlation functions (corr_clipped, corr_nearest)
  • Model comparison (compare_cox, compare_encompassing, compare_j)
  • And many more...

See stats_extended.py source code for complete list of 100+ functions.

Testing

All modules include self-tests. Run individual tests:

python regression.py
python timeseries_models.py
python glm.py
python stats_extended.py  # Test extended stats module

Version

  • Statsmodels: 0.14.4
  • Wrapper Version: 2.0.0 (Complete Coverage)
  • Total Functions: 200+
  • Coverage: 100% of statsmodels API
  • Last Updated: 2026-01-23

License

MIT License - Same as Fincept Terminal