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

[WIP] Vector-valued interpolation in econforgeinterp #1242

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Mv77
Copy link
Contributor

@Mv77 Mv77 commented Mar 17, 2023

econforgeinterp (and all other interpolators in HARK) currently support interpolation of scalar valued functions, $f:\mathbb{R}^n\rightarrow \mathbb{R}$. I would like to work on adding the capability to interpolate vector-valued functions, $f:\mathbb{R}^n\rightarrow \mathbb{R^m}$.

The current version of this PR makes this possible in the barebones LinearFast. I still need to adjust the extra-stuff: derivatives and decay-interpolation.

Why do I think this would be useful?

In a model with, say, a 2-dimensional state space, we often will construct a bunch of interpolators for vFunc(m,n), dvdmFunc(m,n), dvdnFunc(m,n), cFunc(m,n)... over the same (m,n) grids.

Often, in the expectation step of backward induction, we will need to evaluate, say dvdmFunc(m,n), dvdnFunc(m,n) in a bunch of points. We can do this with two different interpolators, one for each function. But this does the slow calculation of which indices and weights to use for interpolation twice for every point (once in each interpolator). If we instead had an
$\mathbb{R}^2\rightarrow \mathbb{R^2}$ interpolator of (m,n) -> [dvdmFunc(m,n), dvdnFunc(m,n)] the calculation of indices and weights would be done only once.

  • Tests for new functionality/models or Tests to reproduce the bug-fix in code.
  • Updated documentation of features that add new functionality.
  • Update CHANGELOG.md with major/minor changes.

@Mv77
Copy link
Contributor Author

Mv77 commented Mar 17, 2023

@alanlujan91 not sure how this interferes or interacts with your interpolation rework. Does that change the econforgeinterp module?

@codecov-commenter
Copy link

codecov-commenter commented Mar 17, 2023

Codecov Report

Patch coverage: 97.14% and project coverage change: +0.21 🎉

Comparison is base (a479a8d) 73.29% compared to head (08fc316) 73.51%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1242      +/-   ##
==========================================
+ Coverage   73.29%   73.51%   +0.21%     
==========================================
  Files          75       75              
  Lines       12535    12632      +97     
==========================================
+ Hits         9188     9286      +98     
+ Misses       3347     3346       -1     
Impacted Files Coverage Δ
HARK/econforgeinterp.py 93.63% <91.17%> (+3.16%) ⬆️
HARK/tests/test_econforgeinterp.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@alanlujan91
Copy link
Member

@Mv77 this is a great idea! No worries about my interpolation overhaul. I'll merge your changes and resolve any conflicts.

The only changes I made to econforgeinterp was moving the file to a HARK.interpolation subdirectory.

@llorracc
Copy link
Collaborator

@Mv77, yes, this would be great!

But I think a good preliminary starting point would be for you do a review of Alan's interpolation notebook to revise it in whatever ways might be appropriate (probably mainly just notation) to make it possible to seamlessly add an example (or examples) using your tool when your tool is in a usable state. That would also get you a bit more into his mindspace which might be useful as you go along. In particular, it is still frustrating to me that we have not figured out how to deal generically (using open source tools) with the "structured but warped" case. The different triangulation methods, and Gaussian Process Regression, I believe are enormously slower than an efficient "structured but warped" tool would be (in which there is a known affine mapping between quadrangles in the warped space and regular rectangles in the unwarped space.

@Mv77
Copy link
Contributor Author

Mv77 commented Mar 17, 2023

Derivatives and decay extrapolators now work :)

@Mv77
Copy link
Contributor Author

Mv77 commented Mar 17, 2023

@Mv77, yes, this would be great!

But I think a good preliminary starting point would be for you do a review of Alan's interpolation notebook to revise it in whatever ways might be appropriate (probably mainly just notation) to make it possible to seamlessly add an example (or examples) using your tool when your tool is in a usable state. That would also get you a bit more into his mindspace which might be useful as you go along. In particular, it is still frustrating to me that we have not figured out how to deal generically (using open source tools) with the "structured but warped" case. The different triangulation methods, and Gaussian Process Regression, I believe are enormously slower than an efficient "structured but warped" tool would be (in which there is a known affine mapping between quadrangles in the warped space and regular rectangles in the unwarped space.

@llorracc I'm not sure this relates to warped interpolation. @alanlujan91 just confirmed that his rework only interacts with econforgeinterp to the extent that it changes the location of the file. This is a pretty contained change that is now pretty much ready to go and which does not change the behavior of any of the currently existing code.

I'd be happy to contribute to the warped interpolation discussion but I would not want to tie this PR to that goal.

@Mv77
Copy link
Contributor Author

Mv77 commented Mar 20, 2023

I've run into some strange potential bug in econforge/interpolation.py that affects this PR.
EconForge/interpolation.py#104

Don't merge in yet.

@sbenthall sbenthall mentioned this pull request Mar 22, 2023
3 tasks
@llorracc
Copy link
Collaborator

Just to codify a point from our discussion today: I'd really love to find some framework that is designed to represent a function AND ITS DERIVATIVES at a point, all in a tidy bundle.

So, for the consumption function, at point $m_{i}$ we would want to measure at least the level and the derivative of consumption with respect to m:

$$[c(m_{i}),c^{\prime}(m_{i})]$$

or in Python

[cFunc(m[i]),cFunc.deriv('m')(m[i])]

and using the Envelope result that $v^{\prime}(m)=u'(c(m))$ we would be able to represent the value function consistent with that consumption function by

$$ [v(m_i),v^{m}(m_i) = u^{c}(c(m_i)),v^{mm}(m_i) = u^{cc}(c(m_i))c^{m}(m_{i})] $$

with corresponding python something like

[vFunc(m[i]),uFunc.deriv('c')(m[i]),uFunc.deriv('c').deriv('c')(m[i])*cFunc.deriv('m')(m[i])

Somehow I can't shake the suspicion that surely someone, somewhere, has developed some infrastructure for constructing objects like this, rather than simply representing each item as a completely standalone thing as in Mateo's example.

(I know that, if you have an interpolating function that has already been constructed, there's a syntax for obtaining its derivatives at a point; the natural syntax for representing the values of these derivatives at given points, to be fed to the interpolator constructor, would be to set the values for each point using exactly the same syntax, and then instruct your interpolation constructor to use the points it has to construct a piecewise function using some specified method. For example, piecewise Hermite interpolators will generate a function that matches the inputs (in levels and derivatives) at the specified points.)

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

Successfully merging this pull request may close these issues.

4 participants