Skip to content

Commit f7d74dc

Browse files
committed
Address code review feedback from PR #9
- Add Optional type hint to cluster parameter in DifferenceInDifferences - Use significance_stars property in DiDResults.__repr__ instead of inline logic - Add clarifying comments for vcov computation using solve() - Import LinAlgError directly for cleaner exception handling
1 parent cdc944d commit f7d74dc

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

diff_diff/estimators.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any, Dict, List, Optional, Tuple, Union
66

77
import numpy as np
8+
from numpy.linalg import LinAlgError
89
import pandas as pd
910
from scipy import stats
1011

@@ -90,7 +91,7 @@ class DifferenceInDifferences:
9091
def __init__(
9192
self,
9293
robust: bool = True,
93-
cluster: str = None,
94+
cluster: Optional[str] = None,
9495
alpha: float = 0.05
9596
):
9697
self.robust = robust
@@ -248,6 +249,7 @@ def fit(
248249
k = X.shape[1]
249250
mse = np.sum(residuals ** 2) / (n - k)
250251
# Use solve() instead of inv() for numerical stability
252+
# solve(A, B) computes X where AX=B, so this yields (X'X)^{-1} * mse
251253
vcov = np.linalg.solve(X.T @ X, mse * np.eye(k))
252254

253255
# Extract ATT (coefficient on interaction term)
@@ -963,6 +965,7 @@ def fit(
963965
k = X.shape[1]
964966
mse = np.sum(residuals ** 2) / (n - k)
965967
# Use solve() instead of inv() for numerical stability
968+
# solve(A, B) computes X where AX=B, so this yields (X'X)^{-1} * mse
966969
vcov = np.linalg.solve(X.T @ X, mse * np.eye(k))
967970

968971
# Degrees of freedom
@@ -1528,7 +1531,7 @@ def _bootstrap_se(
15281531
)
15291532
bootstrap_estimates.append(tau)
15301533

1531-
except (ValueError, np.linalg.LinAlgError, KeyError):
1534+
except (ValueError, LinAlgError, KeyError):
15321535
# Skip failed bootstrap iterations (e.g., singular matrices,
15331536
# missing data in resampled units, or invalid weight computations)
15341537
continue

diff_diff/results.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ class DiDResults:
5656

5757
def __repr__(self) -> str:
5858
"""Concise string representation."""
59-
sig = "***" if self.p_value < 0.001 else "**" if self.p_value < 0.01 else "*" if self.p_value < 0.05 else ""
6059
return (
61-
f"DiDResults(ATT={self.att:.4f}{sig}, "
60+
f"DiDResults(ATT={self.att:.4f}{self.significance_stars}, "
6261
f"SE={self.se:.4f}, "
6362
f"p={self.p_value:.4f})"
6463
)

0 commit comments

Comments
 (0)