Skip to content

Commit

Permalink
test HC1 SE
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvalal committed Aug 8, 2024
1 parent 77ea9a0 commit 2667779
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions tests/test_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from tests.utils import generate_sample_data, create_duckdb_database
import duckdb
import pandas as pd
import pyfixest as pf


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -39,6 +40,14 @@ def get_numpy_coefficients(db_path, formula):
return coeffs[1:]


def get_pyfixest_estimates(db_path, formula):
conn = duckdb.connect(db_path)
df = conn.execute("SELECT * FROM data").df()
conn.close()
m_pf = pf.feols(formula, data=df, vcov="hetero")
return m_pf


@pytest.mark.parametrize(
"fml",
[
Expand All @@ -50,8 +59,6 @@ def get_numpy_coefficients(db_path, formula):
def test_fitters(database, fml):
db_path = database

uncompressed_coeffs = get_numpy_coefficients(db_path, fml)

m_duck = DuckRegression(
db_name=db_path,
table_name="data",
Expand All @@ -61,14 +68,28 @@ def test_fitters(database, fml):
seed=42,
)
m_duck.fit()

m_duck.fit_vcov()
# nobs
np.testing.assert_allclose(
m_duck.df_compressed["count"].sum(), 1_000_000, rtol=1e-4
), "Number of observations are not equal"

results = m_duck.summary()
compressed_coeffs = results["point_estimate"][1:]
compressed_coeffs, compressed_se = (
results["point_estimate"][1:],
results["standard_error"][1:],
)
uncompressed_coeffs = get_numpy_coefficients(db_path, fml)
uncompressed_coeffs2 = get_pyfixest_estimates(db_path, fml)

np.testing.assert_allclose(
compressed_coeffs, uncompressed_coeffs, rtol=1e-4
), f"Coefficients are not equal for formula {fml}"

np.testing.assert_allclose(
compressed_coeffs, uncompressed_coeffs2.coef().values[1:], rtol=1e-4
), f"Coefficients are not equal to pyfixest version for formula {fml}"

np.testing.assert_allclose(
compressed_se, uncompressed_coeffs2.se().values[1:], rtol=1e-4
), f"Standard errors are not equal to pyfixest version for formula {fml}"

0 comments on commit 2667779

Please sign in to comment.