Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixef: Implement Iterative Least Squares Solver #469

Closed
s3alfisc opened this issue May 30, 2024 · 3 comments · Fixed by #546
Closed

fixef: Implement Iterative Least Squares Solver #469

s3alfisc opened this issue May 30, 2024 · 3 comments · Fixed by #546
Labels
enhancement New feature or request

Comments

@s3alfisc
Copy link
Member

s3alfisc commented May 30, 2024

Context

Currently, pyfixest can compute fixed effect estimates in the fixef() method by using a sparse solver.

A potentially faster alternative is to implement an iterative solver. See section 3 in the lfe paper.

@s3alfisc s3alfisc added the enhancement New feature or request label May 30, 2024
@s3alfisc s3alfisc changed the title fixef(): Implement Kacmarz method fixef: Implement Kacmarz method May 30, 2024
@s3alfisc s3alfisc changed the title fixef: Implement Kacmarz method fixef: Implement Iterative Least Squares Solver Jul 6, 2024
@s3alfisc
Copy link
Member Author

s3alfisc commented Jul 6, 2024

Hi Ben (@b-knight) - I have been playing around with scipy's solver for linear systems and it's much faster than the sparse solver I have implemented by hand:

import numpy as np
from scipy.sparse import rand
from scipy.sparse.linalg import lsqr, spsolve
import time

m, n = 2_000_000, 500
A = rand(m, n, density=0.1, format='csr')
x_true = np.random.rand(n)
b = A @ x_true

tic = time.time()
result = lsqr(A, b)
toc = time.time()
print(f"Elapsed time: {toc - tic:.2f} seconds")
x_lsqr = result[0]
x_lsqr[0:5]
#Elapsed time: 1.35 seconds
#array([0.5335062 , 0.99837454, 0.69382102, 0.5373236 , 0.45653057])

tic = time.time()
alpha = spsolve(A.transpose() @ A, A.transpose() @ b)
toc = time.time()
print(f"Elapsed time: {toc - tic:.2f} seconds")
alpha[0:5]
# Elapsed time: 30.38 seconds
# array([0.53350756, 0.99837705, 0.69382127, 0.53732394, 0.45653019])

Hence to speed up the fixef and the predict method, we'd have to change this line

alpha = spsolve(D2.transpose() @ D2, D2.transpose() @ uhat)
in .fixef() to using the lsqr solver.

Would you like to implement the change? Else I'm happy to do it myself =)

Best, Alex

@s3alfisc
Copy link
Member Author

@greenguy33 can I assign this to you? I think this should be a minimal change =)

@greenguy33
Copy link
Contributor

@s3alfisc sure, I'll take a look asap (ideally later this week or early next)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants