Skip to content

Instantly share code, notes, and snippets.

@dirmeier
Last active May 19, 2020 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirmeier/73b23fa21c8dfbcf9d041b15f980d96f to your computer and use it in GitHub Desktop.
Save dirmeier/73b23fa21c8dfbcf9d041b15f980d96f to your computer and use it in GitHub Desktop.
Khatri-Rao product for any kind of matrices.
def khatri_rao(J, X):
# as needed to compute random effects model matrices
# references:
# - http://dx.doi.org/10.18637/jss.v067.i01
# - https://stat.ethz.ch/R-manual/R-patched/library/Matrix/html/KhatriRao.html
Z = J[..., :, np.newaxis, :] * X[..., np.newaxis, :, :]
return Z.reshape((-1,) + Z.shape[2:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment