reghdfe icon indicating copy to clipboard operation
reghdfe copied to clipboard

[BUG] Ensure vcov matrix is symmetric to avoid numerical precision issues

Open mcaceresb opened this issue 1 year ago • 1 comments

Bug report

Please complete the following information:

  • Stata version: 17.0 MP
  • OS: Rocky Linux 9.2 (kernel 5.14.0)
  • which reghdfe gives version 6.12.4 12sep2023

Behavior

  • Expected behavior: Symmetric vcov matrix
  • Actual behavior: The variance-covariance matrix computed in reghdfe_solve_ols may not be symmetric (I get this error under some circumstances with reghdfe_vce_cluster specifically, but I imagine it might be generic). This causes EreturnPost/ereturn pots to give a warning: warning: variance matrix is nonsymmetric or highly singular and instead of the vcov matrix it posts a matrix that is all 0s, resulting in missing SEs. The matrix is approximately symmetric, so it's a numerical precision issue.

Steps to reproduce the problem

Unfortunately I am unable to provide a MWE. This is a numerical precision issue that arises when using reghdfe with administrative individual-level data I cannot share and have not reproduced with basic simulations. However, I can show the output from the HDFE object saved by my regression:

. mata issymmetric(HDFE.solution.V)
  0

. mata max(reldif(lowertriangle(HDFE.solution.V), uppertriangle(HDFE.solution.V)'))
  4.74795e-09

I propose forcing symmetry at the end of reghdfe_solve_ols, which solves it for me; e.g.

sol.V = makesymmetric((lowertriangle(sol.V) + uppertriangle(sol.V)')/2)

mcaceresb avatar Oct 23 '23 00:10 mcaceresb

PS: Actually I just realized the code I suggested is oddly convoluted. This does the same: sol.v = (sol.v + sol.v')/2

mcaceresb avatar Oct 23 '23 11:10 mcaceresb