tsls

weak_instruments.tsls.TSLS(Y: ndarray[tuple[int, ...], dtype[float64]], X: ndarray[tuple[int, ...], dtype[float64]], Z: ndarray[tuple[int, ...], dtype[float64]], W: ndarray[tuple[int, ...], dtype[float64]] | None = None, talk: bool = False) TSLSResult

Two-Stage Least Squares (2SLS) estimator for weak instruments.

Parameters:
  • Y (NDArray[np.float64]) – A 1-D numpy array of the dependent variable (N,).

  • X (NDArray[np.float64]) – A 2-D numpy array of the endogenous regressors (N, L). Do not include the constant.

  • Z (NDArray[np.float64]) – A 2-D numpy array of the instruments (N, K), where K >= L. Do not include the constant.

  • W (NDArray[np.float64], optional) – A 2-D numpy array of the exogenous controls (N, G). Do not include the constant. Default is None.

  • talk (bool, optional) – If True, provides detailed output for teaching / debugging purposes. Default is False.

Returns:

An object containing the following attributes:
  • beta (NDArray[np.float64]): The estimated coefficients for the model.

  • r_squared (float): The R-squared value for the model.

  • adjusted_r_squared (float): The adjusted R-squared value for the model.

  • f_stat (float): The F-statistic for the model.

  • standard_errors (NDArray[np.float64]): The robust standard errors for the estimated coefficients.

  • root_mse (float): The root mean squared error.

  • pvals (list of float): p-values for coefficients.

  • tstats (list of float): t-statistics for coefficients.

  • cis (list of tuple): Confidence intervals for coefficients.

Return type:

TSLSResult

Raises:
  • ValueError – If the dimensions of Y, X, or Z are inconsistent or invalid.

  • RuntimeWarning – If the number of instruments (columns in Z) is not greater than the number of regressors (columns in X).

Notes

  • The TSLS estimator is a classic instrumental variable estimator.

  • The function performs two stages:
    1. The first stage projects X onto the space spanned by Z (and W if provided).

    2. The second stage regresses Y on the fitted values from the first stage.

  • Additional statistics such as R-squared, adjusted R-squared, and F-statistics are calculated for model evaluation.

  • If the number of endogenous regressors is 1, first-stage statistics (R-squared and F-statistic) are also computed.

Example

>>> import numpy as np
>>> from weak_instruments.tsls import TSLS
>>> Y = np.array([1, 2, 3])
>>> X = np.array([[1], [2], [3]])
>>> Z = np.array([[1, 0], [0, 1], [1, 1]])
>>> result = TSLS(Y, X, Z)
>>> print(result.summary())
class weak_instruments.tsls.TSLSResult(beta: ndarray[tuple[int, ...], dtype[float64]], r_squared: float = None, adjusted_r_squared: float = None, f_stat: float = None, standard_errors: ndarray[tuple[int, ...], dtype[float64]] = None, root_mse: float = None, pvals: ndarray[tuple[int, ...], dtype[float64]] | None = None, tstats: ndarray[tuple[int, ...], dtype[float64]] | None = None, cis: ndarray[tuple[int, ...], dtype[float64]] = None)

Bases: object

Stores results for the TSLS estimator.

beta

Estimated coefficients from the TSLS regression.

Type:

NDArray[np.float64]

r_squared

R-squared value.

Type:

float

adjusted_r_squared

Adjusted R-squared value.

Type:

float

f_stat

F-statistic for the model.

Type:

float

standard_errors

Robust standard errors.

Type:

NDArray[np.float64]

root_mse

Root mean squared error.

Type:

float

pvals

p-values for coefficients.

Type:

NDArray[np.float64] or None

tstats

t-statistics for coefficients.

Type:

NDArray[np.float64] or None

cis

Confidence intervals for coefficients.

Type:

NDArray[np.float64] or None

summary()

Prints a summary of the TSLS results in a tabular format similar to statsmodels OLS.