Repository navigation
ENH: Improved performance of the ConditionalMNLogit class - #9036
Conversation
…p with numpy vector operations. In 'score' method, the score of the variable 'v' is calculated at a time for the selected values (exp_sum). The number 'p' is used to access them.
jseabold
left a comment
There was a problem hiding this comment.
Have recently been using these models. In general this looks good to me, if you could change to logsumexp that would be helpful.
Updated for better stability(sum and exp replaced with scipy.special.logsumexp).
|
Hello @quant12345! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found: There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻 Comment last updated at 2025-01-07 19:32:22 UTC |
|
Below is the test_conditional_new code. The conditional_models_new code is not posted due to line limitations, I am attaching it as an attachment(with using scipy.special.logsumexp). test_conditional_new - codeThe performance is the same as in my previous version. |
…p with numpy vector operations. In 'score' method, the score of the variable 'v' is calculated at a time for the selected values (exp_sum). The number 'p' is used to access them.
Updated for better stability(sum and exp replaced with scipy.special.logsumexp).
# Conflicts: # statsmodels/discrete/conditional_models.py
|
Thanks for the PR I put it on my list of PRs to look at and merge over the next few weeks. (for soonish 0.15) |
|
I added another improvement and comments (commit 1ee8e71), but the tests do not run. So the PR is quite old, I had to tinker with merges and therefore, due to ignorance, I created many commits. Now a merge conflict has appeared, but before that it was not there (it is not clear what is wrong) Line: --------------------------------------------------------------performance------------------------------------------------------------------------------------------ test_conditional_new - codePerformance is better than previous commits. comparison resultsif you run the tests themselves before and after the changes. before: after: |
|
do you know how to do an interactive rebase in git? The formatting of the import statements has changed in statsmodels |
|
I haven't worked with interactive rebase in git yet. I couldn't send a push. In PyCharm terminal it said that I need to merge before pushing. I tried several times. Then I tried to merge and after that I was able to send a push). |
|
I fixed your merge problem You need to pull this change before making more changes. |
|
@quant12345 gentle ping, are you planning to open new PR for this, as i can see it was in good direction and in priority as per review from members and @bashtage helped to solve some merge conflicts
Or i can open a new pr and add you co-author so that we can proceed with your work. please let me know |
|
@Aniketsy opened it again, fixed linting, removed merge conflicts. |
ci- fail looks unrelated @quant12345 thanks for re-opening this PR, hoping to moving this forward... i'll review this once from my end
is this separate PR ? if related to this please link |
|
|
||
| grad = np.zeros((q, c)) | ||
| for ii in self._grp_ix: | ||
| for ii in np.array(self._grp_ix): |
There was a problem hiding this comment.
how will this behave when group size will be different ?
if this breaks, then we should revert this.
There was a problem hiding this comment.
@Aniketsy do I understand correctly that you mean a non-homogeneous array, for example: [[1,3],[4,5,6,7],[1]] ?
There was a problem hiding this comment.
@Aniketsy do I understand correctly that you mean a non-homogeneous array, for example: [[1,3],[4,5,6,7],[1]] ?
@quant12345 yes could you please verify
There was a problem hiding this comment.
returned the row to its original state: for ii in self._grp_ix:. I also checked all three tests with the change (all three tests were successful).
I tried print('!!!!!!!!!!!!!!') the loglike and score functions from ConditionalMNLogit. When running the test: PR 9264 is independent of this. |
hmm, yes 👍 |
|
Thanks. |
ENH - ConditionalMNLogit class: in
loglikemethod replaces the nested loop to numpy vector operations. Inscoremethod, variablevis calculated at a time for the selected values (exp_sum). The number 'p' is used to access them.As a result,
test_conditional_mnlogit_gradis calculated 2.7 times faster, andtest_conditional_mnlogit_2d,test_conditional_mnlogit_3d1.7 times faster.Below is a description of how to compare the old and new options.
I separated
test_conditional_,conditional_models_into separate files so that I could run the old and new options locally together for comparison (located them in a common directory). In conditional_models there are two classesConditionalMNLogitandConditionalMNLogit_new- in which I made improvements. In tests, the module is connected directly to use both options for tests.n = 90is passedto gen_mnlogit(n)Below is the
test_conditional_code. Theconditional_models_code is not posted due to line limitations, I am attaching it as an attachment.test_conditional_ - code
conditional_models_.py.tar.gz
Output:
**Notes**
needed for doc changes.
then show that it is fixed with the new code.
verify you changes are well formatted by running
flake8is installed. This command is also available on Windowsusing the Windows System for Linux once
flake8is installed in thelocal Linux environment. While passing this test is not required, it is good practice and it help
improve code quality in
statsmodels.