Skip to content

Econometrics

Ordinary least squares

OLS over statsmodels, with classical, HC0-HC3 and HAC standard errors, and the Durbin-Watson, Breusch-Pagan, Jarque-Bera, omnibus, RESET and VIF diagnostics.

Model id
econometrics.ols, version 1
Domain
Econometrics
Evidence
standard
Cost class
light: A short numerical method; a hosted run may take up to 10 seconds.
Needs
pip install "pyeconomics[econometrics]"

Formula

β^=(X′X)−1X′y,e=y−Xβ^\hat\beta = (X'X)^{-1}X'y, \quad e = y - X\hat\betaVHC=(X′X)−1X′ diag(ωiei2) X(X′X)−1V_{\mathrm{HC}} = (X'X)^{-1}X'\,\mathrm{diag}(\omega_i e_i^2)\,X(X'X)^{-1}ωi∈{1,nn−k,11−hi,1(1−hi)2}\omega_i \in \{1, \tfrac{n}{n-k}, \tfrac{1}{1-h_i}, \tfrac{1}{(1-h_i)^2}\}VHAC=(X′X)−1[∑l=−LL(1−∣l∣L+1)×∑txtetet−lxt−l′](X′X)−1\begin{aligned} V_{\mathrm{HAC}} &= (X'X)^{-1}\Big[\sum_{l=-L}^{L}\Big(1 - \tfrac{|l|}{L+1}\Big) \\ &\quad \times \sum_t x_t e_t e_{t-l} x_{t-l}'\Big](X'X)^{-1} \end{aligned}R2=1−SSRTSS,d=∑(et−et−1)2∑et2R^2 = 1 - \frac{SSR}{TSS}, \quad d = \frac{\sum (e_t - e_{t-1})^2}{\sum e_t^2}VIFj=11−Rj2VIF_j = \frac{1}{1 - R_j^2}JB=n6(S2+(K−3)24),LMBP=nRe22JB = \frac{n}{6}\left(S^2 + \frac{(K - 3)^2}{4}\right), \quad LM_{\mathrm{BP}} = n R^2_{e^2}

Where

β^\hat\beta
The estimated coefficients coefficients
XX
The design matrix: one row for each observation, one column for each regressor design
yy
The response response
ee
The residuals residuals
VHCV_{\mathrm{HC}}
A heteroskedasticity-consistent covariance matrix of the coefficients
ωi\omega_i
The weight on observation i's squared residual: HC0, HC1, HC2 and HC3 in turn
hih_i
The leverage of observation i
nn
Number of observations observations
kk
Number of coefficients
VHACV_{\mathrm{HAC}}
The heteroskedasticity- and autocorrelation-consistent covariance matrix (Newey-West)
LL
Number of lags in the HAC estimate hac_lags
R2R^2
The share of the response's variation the regression explains r_squared
SSRSSR
Sum of squared residuals
TSSTSS
Total sum of squares of the response about its mean
dd
The Durbin-Watson statistic durbin_watson
VIFjVIF_j
Variance inflation factor of regressor j vif
Rj2R_j^2
R-squared of regressor j on the other regressors
JBJB
The Jarque-Bera statistic jarque_bera
SS
Skewness of the residuals
KK
Kurtosis of the residuals
LMBPLM_{\mathrm{BP}}
The Breusch-Pagan statistic breusch_pagan
Re22R^2_{e^2}
R-squared of the squared residuals on the regressors

Each symbol's field is named beside it; the inputs and outputs, with their units and bounds, are under Variables, and the citations give each formula's source.

Variables

Rates, returns and yields are decimals: 0.05 is five percent. Bounds in brackets include their end; in parentheses they exclude it.

Inputs

Inputs
NameDescription
Responseresponseratio (array) · length min 2, max 10000; each [-1e+15, 1e+15]The dependent variable, y
Designdesignratio (array) (array) · length min 2, max 10000; each length min 1, max 20; each [-1e+15, 1e+15]Regressors, one row per observation (a table or a 2-D array)
Namesnamestext (array) · length max 20; each at most 64 charactersRegressor names; a table's columns, or x1, x2, ... when null
Add constantadd_constantflagAdd an intercept named const as the first term
CovariancecovariancetextCovariance of the coefficients
HAC lagshac_lagscount · [0, 100]HAC lags; null for Newey-West's floor(4 (n/100)^(2/9))
Alphaalphaprobability · [0.001, 0.5]Size of tests and 1 - interval coverage
Reset powerreset_powercount · [2, 4]Highest power of the fitted values in RESET

Outputs

Outputs
NameDescription
Namesnamestext (array) · length max 21; each at most 64 charactersTerm names, const first when added
Coefficientscoefficientsratio (array) · length max 21; each [-1e+300, 1e+300]Estimated coefficients
Standard errorsstandard_errorsratio (array) · length max 21; each [0, 1e+300]Standard errors, from the covariance
T statisticst_statisticsratio (array) · each [-1e+300, 1e+300]t statistics; null for a perfect fit
P valuesp_valuesprobability (array) · each [0, 1]Two-sided p-values from Student's t; null for a perfect fit
Conf lowerconf_lowerratio (array) · length max 21; each [-1e+300, 1e+300]Lower ends of the 1 - alpha confidence intervals
Conf upperconf_upperratio (array) · length max 21; each [-1e+300, 1e+300]Upper ends of the 1 - alpha confidence intervals
VIFvifratio (array) · length max 20; each [1, 1e+300]Variance inflation factor per regressor
Observationsobservationscount · [2, 10000]Observations, n
Degrees of freedomdegrees_of_freedomcount · [1, 10000]Residual degrees of freedom, n - k
HAC lagshac_lagscount · [0, 100]Lags in the HAC covariance; 0 for the others
R squaredr_squaredratio · [0, 1]R-squared, uncentered without a constant; null if y does not vary about its mean (or, without a constant, is all zero)
Adjusted r squaredadjusted_r_squaredratio · [-10000, 1]R-squared adjusted for degrees of freedom
F statisticf_statisticratio · [0, 1e+300]Wald F statistic that every slope is zero, under the chosen covariance
F p valuef_p_valueprobability · [0, 1]P-value of the F statistic
Residual std errorresidual_std_errorratio · [0, 1e+300]sqrt(SSR / (n - k))
Durbin watsondurbin_watsonratio · [0, 4]Durbin-Watson statistic of the residuals
Breusch paganbreusch_paganratio · [0, 1e+300]Breusch-Pagan LM statistic (Koenker's studentized form)
Breusch pagan p valuebreusch_pagan_p_valueprobability · [0, 1]Breusch-Pagan p-value
Jarque berajarque_beraratio · [0, 1e+300]Jarque-Bera statistic of the residuals
Jarque bera p valuejarque_bera_p_valueprobability · [0, 1]Jarque-Bera p-value
Omnibusomnibusratio · [0, 1e+300]D'Agostino-Pearson omnibus K-squared statistic of the residuals
Omnibus p valueomnibus_p_valueprobability · [0, 1]Omnibus p-value
Reset freset_fratio · [0, 1e+300]Ramsey RESET F statistic
Reset p valuereset_p_valueprobability · [0, 1]Ramsey RESET p-value
Fittedfittedratio (array) · length max 10000; each [-1e+300, 1e+300]Fitted values
Residualsresidualsratio (array) · length max 10000; each [-1e+300, 1e+300]Residuals, y - fitted

Calculator

Run the model on your own inputs, starting from its example. Rates are decimals: 0.05 is five percent. The run goes to the pyeconomics API on this site: your values travel only in the request, and nothing is stored.

Loading the calculator…

Worked example

The first eight observations of NIST's Norris dataset.

Example nist_norris_head. Inputs:

{
  "add_constant": true,
  "alpha": 0.05,
  "covariance": "nonrobust",
  "design": [
    [0.2],
    [337.4],
    [118.2],
    [884.6],
    [10.1],
    [226.5],
    [666.3],
    [996.3]
  ],
  "hac_lags": null,
  "names": ["x"],
  "reset_power": 2,
  "response": [0.1, 338.8, 118.1, 888, 9.2, 228.1, 668.5, 998.5]
}

Outputs:

{
  "adjusted_r_squared": 0.9999965635112051,
  "breusch_pagan": 0.37663069293153395,
  "breusch_pagan_p_value": 0.5394119713596573,
  "coefficients": [-0.1111519359435531, 1.0032686799257773],
  "conf_lower": [-1.0529363546274488, 1.001548615694831],
  "conf_upper": [0.8306324827403426, 1.0049887441567236],
  "degrees_of_freedom": 6,
  "durbin_watson": 2.7688126100195967,
  "f_p_value": 7.986471183484879e-18,
  "f_statistic": 2036956.84725361,
  "fitted": [
    0.0895018000416024,
    338.39170067101367,
    118.47520603128334,
    887.3803223263991,
    10.021861731306796,
    227.129204067245,
    668.3667694986018,
    999.4454338741084
  ],
  "hac_lags": 0,
  "jarque_bera": 0.49418869504387686,
  "jarque_bera_p_value": 0.7810669983191916,
  "names": ["const", "x"],
  "observations": 8,
  "omnibus": null,
  "omnibus_p_value": null,
  "p_values": [0.7824591357109563, 7.986471183484876e-18],
  "r_squared": 0.9999970544381759,
  "reset_f": 4.243652031217087,
  "reset_p_value": 0.09443169349728608,
  "residual_std_error": 0.7326999800485193,
  "residuals": [
    0.01049819995839761,
    0.40829932898634225,
    -0.37520603128333985,
    0.6196776736008879,
    -0.8218617313067967,
    0.9707959327549779,
    0.13323050139820536,
    -0.9454338741084467
  ],
  "standard_errors": [0.3848869415721733, 0.0007029530835536349],
  "t_statistics": [-0.2887911330260346, 1427.2199715718702],
  "vif": [1]
}

Warnings:

  • small_sample: the omnibus test needs at least 20 observations

Assumptions

  • statsmodels fits by QR decomposition; inference uses Student's t with n - k degrees of freedom for every covariance type.
  • HAC uses Bartlett weights and no small-sample correction; with no lag count, Newey and West's (1994) rule of thumb picks one.
  • R-squared is centered with a constant and uncentered without; the F statistic tests every slope (every coefficient without a constant) under the chosen covariance.
  • Breusch-Pagan is Koenker's studentized n R^2, regressing e^2 on the regressors and a constant; RESET is an F test on the fitted values' powers 2 to reset_power; each VIF regresses one regressor on the others.

Limitations

  • A perfectly collinear design, or one whose equilibrated condition number exceeds 1e7, has no fit: DomainError.
  • HC2 and HC3 are undefined when an observation has leverage one: DomainError.
  • A fit exact to rounding has no t statistics, F statistic or residual diagnostics (each null, with a warning); the omnibus test needs 20 observations; RESET and Breusch-Pagan are null when their auxiliary regression is collinear, fits exactly or leaves no degrees of freedom.
  • An output beyond 1e300 in magnitude (a near-collinear design) raises DomainError.
  • Needs the econometrics extra: pip install 'pyeconomics[econometrics]'.

Evidence

Status: standard. Textbook consensus, such as bond duration or the CAPM formula.

Every model declares one status: standard (textbook consensus), practitioner (a widely used convention), contested (under active debate), rejected (failed out of sample) or historical (a superseded methodology).

Syllabus

The 2027 CFA® Program's topic areas this model relates to, as factual tags. Each links to CFA Institute's page for its level; pyeconomics holds none of the curriculum's text.

Citations

  1. Seabold, S. and Perktold, J. (2010). statsmodels: Econometric and statistical modeling with Python. Proceedings of the 9th Python in Science Conference, 92-96. doi:10.25080/Majora-92bf1922-011 (The OLS model, its covariance types and statsmodels.stats.diagnostic)
  2. White, H. (1980). A heteroskedasticity-consistent covariance matrix estimator and a direct test for heteroskedasticity. Econometrica, 48(4), 817-838. doi:10.2307/1912934 (Theorem 1 (the HC0 estimator))
  3. MacKinnon, J. G. and White, H. (1985). Some heteroskedasticity-consistent covariance matrix estimators with improved finite sample properties. Journal of Econometrics, 29(3), 305-325. doi:10.1016/0304-4076(85)90158-7 (Section 2 (HC1, HC2 and HC3))
  4. Newey, W. K. and West, K. D. (1987). A simple, positive semi-definite, heteroskedasticity and autocorrelation consistent covariance matrix. Econometrica, 55(3), 703-708. doi:10.2307/1913610 (Bartlett weights 1 - j / (L + 1))
  5. Newey, W. K. and West, K. D. (1994). Automatic lag selection in covariance matrix estimation. The Review of Economic Studies, 61(4), 631-653. doi:10.2307/2297912 (The Bartlett-kernel rule of thumb, 4 (n / 100)^(2/9))
  6. Durbin, J. and Watson, G. S. (1950). Testing for serial correlation in least squares regression. I. Biometrika, 37(3-4), 409-428. doi:10.1093/biomet/37.3-4.409 (The statistic d)
  7. Breusch, T. S. and Pagan, A. R. (1979). A simple test for heteroscedasticity and random coefficient variation. Econometrica, 47(5), 1287-1294. doi:10.2307/1911963 (The Lagrange multiplier test)
  8. Koenker, R. (1981). A note on studentizing a test for heteroscedasticity. Journal of Econometrics, 17(1), 107-112. doi:10.1016/0304-4076(81)90062-2 (The studentized statistic n R^2)
  9. Jarque, C. M. and Bera, A. K. (1980). Efficient tests for normality, homoscedasticity and serial independence of regression residuals. Economics Letters, 6(3), 255-259. doi:10.1016/0165-1765(80)90024-5 (The normality statistic from skewness and kurtosis)
  10. D'Agostino, R. and Pearson, E. S. (1973). Tests for departure from normality. Biometrika, 60(3), 613-622. doi:10.1093/biomet/60.3.613 (The omnibus K-squared statistic)
  11. Ramsey, J. B. (1969). Tests for specification errors in classical linear least-squares regression analysis. Journal of the Royal Statistical Society: Series B, 31(2), 350-371. doi:10.1111/j.2517-6161.1969.tb00796.x (RESET with powers of the fitted values)
  12. Marquardt, D. W. (1970). Generalized inverses, ridge regression, biased linear estimation, and nonlinear estimation. Technometrics, 12(3), 591-612. doi:10.2307/1267205 (The variance inflation factor)
  13. NIST (2003). Statistical Reference Datasets: linear least squares regression. National Institute of Standards and Technology. https://www.itl.nist.gov/div898/strd/lls/lls.shtml (Norris, Pontius, Longley, Wampler1 and Wampler2: certified values)

Changelog

  • Version 1: First version.

Use it elsewhere

Documentation
The model's reference page
REST API
GET or POST /v1/models/econometrics.ols/run
MCP tool
run_model with the model's id, econometrics.ols. Connect an assistant
Command line
pyeconomics models run econometrics.ols --help

The worked example, from Python and from the command line:

Python
import pyeconomics

inputs = {
    "add_constant": True,
    "alpha": 0.05,
    "covariance": "nonrobust",
    "design": [
        [0.2],
        [337.4],
        [118.2],
        [884.6],
        [10.1],
        [226.5],
        [666.3],
        [996.3],
    ],
    "hac_lags": None,
    "names": ["x"],
    "reset_power": 2,
    "response": [0.1, 338.8, 118.1, 888, 9.2, 228.1, 668.5, 998.5],
}
result = pyeconomics.run("econometrics.ols", inputs)
print(result.to_json())
Command line
pyeconomics models run econometrics.ols --inputs - <<'JSON'
{
  "add_constant": true,
  "alpha": 0.05,
  "covariance": "nonrobust",
  "design": [
    [0.2],
    [337.4],
    [118.2],
    [884.6],
    [10.1],
    [226.5],
    [666.3],
    [996.3]
  ],
  "hac_lags": null,
  "names": ["x"],
  "reset_power": 2,
  "response": [0.1, 338.8, 118.1, 888, 9.2, 228.1, 668.5, 998.5]
}
JSON