-
-
Notifications
You must be signed in to change notification settings - Fork 199
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
Replace loop with vectorized calculation of optimal share #1079
Conversation
replace optimization loop with vectorized calculation to speed up process
crossing = np.logical_and(FOC_s[:, 1:] <= 0.0, FOC_s[:, :-1] >= 0.0) | ||
share_idx = np.argmax(crossing, axis=1) | ||
a_idx = np.arange(self.aNrmCount) | ||
bot_s = self.ShareGrid[share_idx] | ||
top_s = self.ShareGrid[share_idx + 1] | ||
bot_f = FOC_s[a_idx, share_idx] | ||
top_f = FOC_s[a_idx, share_idx + 1] | ||
bot_c = self.EndOfPrddvdaNvrs[a_idx, share_idx] | ||
top_c = self.EndOfPrddvdaNvrs[a_idx, share_idx + 1] | ||
alpha = 1.0 - top_f / (top_f - bot_f) | ||
|
||
self.Share_now = (1.0 - alpha) * bot_s + alpha * top_s | ||
self.cNrmAdj_now = (1.0 - alpha) * bot_c + alpha * top_c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alpha
and other variables here can be saved and added to save_points
.
Good candidate for benchmark #1131 |
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #1079 +/- ##
==========================================
- Coverage 72.56% 72.55% -0.01%
==========================================
Files 78 78
Lines 13012 13009 -3
==========================================
- Hits 9442 9439 -3
Misses 3570 3570
☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
This is not about this PR but I don't really like how the optimal share and consumption are found.
I think it'd be clearer (and maybe more accurate?) to do the stage thing where the optimal share associated with every a is found and then the a is decided later.
But that's for another day. Not what this PR is about.
Happy to have it merged.
Here are some thoughts.
|
I agree. There are some shortcuts here that are meant to be "close enough" and maybe pedagogical. But maybe we should have the "best" portfolio choice solver we can come up with and the more pedagogical one as separate models. |
There's long been a comment on
ConsPortfolioModel
that says:This is my attempt at replacing the loop with vectorized calculations to test just how much faster this could be. Working out how to do this in vectorized form can provide insights into how to do it in higher dimensions, where loops might be much slower than in this instance.