crabbymetrics
  • Home
  • API
  • Binding Crash Course
  • Examples
    • OLS
    • Fixed Effects OLS
    • ElasticNet
    • Synthetic Control
    • Logit
    • Multinomial Logit
    • Poisson
    • TwoSLS
    • GMM
    • FTRL
    • MEstimator Poisson
  • Ablations
    • Variance Estimators
  • Optimization
    • Optimizers
    • GMM With Optimizers

OLS Example

This page mirrors examples/ols_example.py.

1 Fit A Basic Linear Model

import numpy as np
from pprint import pprint

from crabbymetrics import OLS

np.set_printoptions(precision=4, suppress=True)
rng = np.random.default_rng(0)
n = 500
k = 3
beta = np.array([1.5, -2.0, 0.5])
intercept = 0.7

x = rng.normal(size=(n, k))
y = intercept + x @ beta + rng.normal(scale=0.5, size=n)

model = OLS()
model.fit(x, y)

print("true intercept:", intercept)
print("true coef:", beta)
pprint(model.summary())
true intercept: 0.7
true coef: [ 1.5 -2.   0.5]
{'coef': array([ 1.5206, -1.9933,  0.5314]),
 'coef_se': array([0.0224, 0.0249, 0.0223]),
 'intercept': 0.6686882817624616,
 'intercept_se': 0.023367266952062416,
 'vcov_type': 'hc1'}