Skip to content

Commit 70a154c

Browse files
HAC: fix bug with indexes plus vectorization (#1058)
* fix bug with vectorization * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ba2e243 commit 70a154c

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

pyfixest/estimation/vcov_utils.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def _get_panel_idx(
191191
return order, units, starts, counts, panel_arr_sorted, time_arr_sorted
192192

193193

194-
@nb.njit(parallel=False)
194+
# @nb.njit(parallel=False)
195195
def _nw_meat_panel(
196196
scores: np.ndarray,
197197
time_arr: np.ndarray,
@@ -235,23 +235,20 @@ def _nw_meat_panel(
235235
gamma_l = np.zeros((k, k))
236236
gamma_l_sum = np.zeros((k, k))
237237

238+
# start: first entry per panel i = 1, ..., N
239+
# counts: number of counts for panel i
238240
for start, count in zip(starts, counts):
239241
end = start + count
240-
gamma0 = np.zeros((k, k))
241-
for t in range(start, end):
242-
score_t = scores[t, :]
243-
gamma0 += np.outer(score_t, score_t)
242+
243+
score_i = scores[start:end, :]
244+
gamma0 = score_i.T @ score_i
244245

245246
gamma_l_sum.fill(0.0)
246247
Lmax = min(lag, count - 1)
247248
for lag_value in range(1, Lmax + 1):
248-
gamma_l.fill(0.0)
249-
for t in range(lag_value, count):
250-
curr_t = start + t
251-
prev_t = start + t - lag_value
252-
score_curr = scores[curr_t, :]
253-
score_prev = scores[prev_t, :]
254-
gamma_l += np.outer(score_curr, score_prev)
249+
score_curr = scores[start + lag_value : end, :]
250+
score_prev = scores[start : end - lag_value, :]
251+
gamma_l = score_curr.T @ score_prev
255252
gamma_l_sum += weights[lag_value] * (gamma_l + gamma_l.T)
256253

257254
meat_nw_panel += gamma0 + gamma_l_sum

0 commit comments

Comments
 (0)