from _api_doc_utils import *OLS
Ordinary least squares with robust covariance options
1 Where it fits
Group: Regression
OLS estimates the linear projection
\[ y_i = \alpha + x_i'\beta + u_i \]
by least squares. The class stores the fitted intercept and coefficient vector and exposes the common linear-model covariance surface: classical, HC1, Newey-West, and cluster-robust standard errors.
2 Python API
Constructor: cm.OLS
Use fit(x, y) for the standard estimator, fit_weighted(x, y, sample_weight) for weighted least squares, and fit_sketch(x, y, sketch_size, seed=None) when a randomized row sketch is acceptable for a large tall design. predict(x) returns fitted values. bootstrap(B, seed=None) returns bootstrap draws with the intercept in the first column.
print(inspect.signature(cm.OLS))()
cls = cm.OLS
display(HTML(html_table(["Public method"], public_methods(cls))))| Public method |
|---|
bootstrap(self, /, n_bootstrap, seed=None) |
fit(self, /, x, y) |
fit_sketch(self, /, x, y, sketch_size, seed=None) |
fit_weighted(self, /, x, y, sample_weight) |
predict(self, /, x) |
summary(self, /, vcov='hc1', lags=None, clusters=None) |
3 Minimal example
rng = np.random.default_rng(1)
x = rng.normal(size=(200, 3))
y = 0.5 + x @ np.array([1.0, -0.7, 0.25]) + rng.normal(scale=0.3, size=200)
model = cm.OLS()
model.fit(x, y)
clusters = np.repeat(np.arange(20, dtype=np.int64), 10)
print(model.summary(vcov="hc1")["coef"])
print(model.summary(vcov="cluster", clusters=clusters)["coef_se"])
print(model.predict(x[:3]))[ 1.00664118 -0.70425283 0.224367 ]
[0.02367873 0.0222344 0.0228217 ]
[ 0.29427264 -1.39837835 -0.41709421]
4 summary() contract
The table below is generated by fitting the live class in this repository and then inspecting summary(). Shapes are shown because most values are plain NumPy arrays or scalars.
rng = np.random.default_rng(101)
x = rng.normal(size=(80, 3))
y = 0.5 + x @ np.array([1.0, -0.7, 0.25]) + rng.normal(scale=0.3, size=80)
model = cm.OLS(); model.fit(x, y)
summary = model.summary()
display(HTML(html_table(["summary() key", "shape"], summary_shape_rows(summary))))| summary() key | shape |
|---|---|
intercept |
() |
coef |
(3,) |
intercept_se |
() |
coef_se |
(3,) |
vcov_type |
() |