Skip to content

Instantly share code, notes, and snippets.

@galenseilis
Created October 16, 2022 00:08
Show Gist options
  • Select an option

  • Save galenseilis/5147606b002df38f64045b3677834580 to your computer and use it in GitHub Desktop.

Select an option

Save galenseilis/5147606b002df38f64045b3677834580 to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.stats import t
import statsmodels.api as sm
np.random.seed(2022)
# Prepare data
N = 51
k = 1
x = np.random.normal(size=N)
y = 2 * x + np.random.normal(size=N)
X = sm.add_constant(x)
# Define and train
model = sm.OLS(y, X)
results = model.fit()
# t-test
dof = results.df_resid
tstat = (results.params[k] - 2) / results.bse[k]
print(1 - t.sf(tstat, dof))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment