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

Added solver to feols and created new test file for it #513

Merged
merged 11 commits into from
Jul 2, 2024

Conversation

saidamir
Copy link
Contributor

Hi I added a solver to feols class and created a test file for it. Once approved, I will add the solver to other classes as well, as I want to complete one class at this stage.

tests/test_solver.py Outdated Show resolved Hide resolved
self._tZX = _Z.T @ _X
self._tZy = _Z.T @ _Y

# self._tZXinv = np.linalg.inv(self._tZX)
self._beta_hat = np.linalg.solve(self._tZX, self._tZy).flatten()
if _solver == "np.linalg.lstsq":
Copy link
Member

@s3alfisc s3alfisc Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! For other solvers, maybe we can add a dedicated function, e.g.

def _solve_ols(tZX, tZY, solver):
   
    if solver== "np.linalg.lstsq":
        return np.linalg.lstsq(tZX, tZy, rcond=None)[0]
    elif solver == "np.linalg.solve": 
        return np.linalg.solve(tZX, tZy).flatten()
    else: 
        raise ValueError(f"Solver {solver} not supported.")
        

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want me to do it for other classes, and leave this calls feols as it is now?

@s3alfisc s3alfisc added the enhancement New feature or request label Jun 17, 2024
@saidamir
Copy link
Contributor Author

@s3alfisc would you please review my pr at your free time? Thanks!

@s3alfisc
Copy link
Member

Hi @saidamir, sorry, I did not understand that this was already ready for review. This looks good, thank you! =) What next step do you have in mind? Will you add solver arguments to Fepois and Feiv or add arguments for the feols() and fepois() APIs?

@saidamir
Copy link
Contributor Author

saidamir commented Jun 25, 2024

The test is failing per logs, as I can see, and the error:
"FAILED tests/test_solver.py::test_solver_equivalence - TypeError: Y must be a numpy array."
I could not figure out why this was happening, it looks to me that this bug is not cause by my changes but it was in the original design of the get_data() method and feols class. The output of get_data() is not compatible with Feols methods. What do you think?

tests/test_solver.py Outdated Show resolved Hide resolved
@s3alfisc
Copy link
Member

Hi Aziz (@saidamir) - if you move the solve_ols method to Feols and delete it in the Feiv and Fepois classes, then this looks good to merge (pending CI tests not failing, which I don't expect).

@all-contributors please add @saidamir for code.

Copy link
Contributor

@s3alfisc

I've put up a pull request to add @saidamir! 🎉

Copy link
Contributor Author

@saidamir saidamir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

self._tZX = _Z.T @ _X
self._tZy = _Z.T @ _Y

# self._tZXinv = np.linalg.inv(self._tZX)
self._beta_hat = np.linalg.solve(self._tZX, self._tZy).flatten()
if _solver == "np.linalg.lstsq":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want me to do it for other classes, and leave this calls feols as it is now?

tests/test_solver.py Outdated Show resolved Hide resolved
tests/test_solver.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@saidamir saidamir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

Copy link
Contributor Author

@saidamir saidamir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

@saidamir
Copy link
Contributor Author

saidamir commented Jul 1, 2024

@s3alfisc When running test_solver.py the following error:

FAILED tests/test_solver.py::test_solver_fepois - ValueError: operands could not be broadcast together with shapes (997,997) (997,998).
Screenshot 2024-07-01 at 5 25 07 PM The proposed fix would involve changes to fepois_.py, git_fit() method as follows: Ensure mu is reshaped to (n_samples, 1) for correct broadcasting:
- WX = np.sqrt(mu) * X_resid
+ WX = np.sqrt(mu.reshape(-1, 1)) * X_resid

I am not sure if I can change the git fit method. What do you think?

@s3alfisc
Copy link
Member

s3alfisc commented Jul 1, 2024

Nice catch! I think the error stems from

delta_new = self.solve_ols(XWX, XWZ, _solver) 

now evaluating to a "flat" array whereas in the old version, it was of dim N x 1. In the second run of the for loop, we then get the shape error you report.

I think changing delta_new back to a N x 1 array should do the trick, e.g.

delta_new = self.solve_ols(XWX, XWZ, _solver).reshape((-1,1))

@s3alfisc
Copy link
Member

s3alfisc commented Jul 2, 2024

I think everything should be fixed now. I also added support for scipy.sparse.linalg.lsqr and added a test =)

Copy link

codecov bot commented Jul 2, 2024

Codecov Report

Attention: Patch coverage is 98.63014% with 1 line in your changes missing coverage. Please review.

Files Coverage Δ
pyfixest/estimation/feiv_.py 97.67% <100.00%> (ø)
pyfixest/estimation/fepois_.py 89.18% <100.00%> (ø)
tests/test_solver.py 100.00% <100.00%> (ø)
pyfixest/estimation/feols_.py 87.52% <91.66%> (ø)

... and 26 files with indirect coverage changes

@s3alfisc s3alfisc merged commit 56c4c6e into py-econometrics:master Jul 2, 2024
7 checks passed
@s3alfisc
Copy link
Member

s3alfisc commented Jul 2, 2024

And it's merged! Congrats @saidamir and thanks for your help! =)

@saidamir
Copy link
Contributor Author

saidamir commented Jul 2, 2024

@s3alfisc would you recommend me the next issue? I have to admit, still struggling a bit with the basics, as I have to combine work, jobs search and this very exciting project, but I love it!

@s3alfisc
Copy link
Member

s3alfisc commented Jul 3, 2024

Of course! The immediate follow up would consist of adding the solver argument to the feols and fepois functions. There's no PR for this yet but I can write one tomorrow evening =)

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
Development

Successfully merging this pull request may close these issues.

Feols, Fepois, Feiv: Implement Option to use different Solver for Normal Equation
2 participants