Skip to content

Integration Example [WIP]#1434

Open
AMonninger wants to merge 2 commits intoecon-ark:mainfrom
AMonninger:IntegrationMethod
Open

Integration Example [WIP]#1434
AMonninger wants to merge 2 commits intoecon-ark:mainfrom
AMonninger:IntegrationMethod

Conversation

@AMonninger
Copy link
Collaborator

Example notebook of Constructing End of Period (Marginal) Value Function using Transitionmatrices instead of Expectations.

This example notebook tests the idea of @mnwhite . So far, the two methods do NOT construct the same function. Could @mnwhite check for conceptual/ coding errors?

Please ensure your pull request adheres to the following guidelines:

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

@mnwhite
Copy link
Contributor

mnwhite commented May 28, 2024

I'll take a look at this later today or early tomorrow.

Made various changes to Adrian's notebook to make the method work as intended. Most of it had to do with selecting the grids properly, with some work on function representation. Should run without issue now.
@mnwhite
Copy link
Contributor

mnwhite commented May 28, 2024

@AMonninger I just pushed a commit that revises the notebook. The method works as intended now, and displays comparisons at the bottom. Important changes that I put in (there might be more):

  • You either need to implement an artificial borrowing constraint OR include a tiny probability of zero income. I put in UnempPrb=1e-10. Without this, the minimum allowable bNrm will vary between the two methods.
  • The mNrmGrid and bNrmGrid had incorrect offsets. This is critical.
  • Small, but helped with debugging: use different grid sizes for b and m so you can verify dimensions
  • Did some variable relabeling to make it more accurate
  • You were post-multiplying, not pre-multiplying
  • Need to use the "pseudo-inverse trick" when representing the interim marginal value function
  • And needs to have a point added at 0 (or fix this to generalize it).

The consumption functions constructed by the two integration methods now match as expected. The one using the new integration method is always a little lower than the old one, especially in the highly concave region of the cFunc. This is because the new method incorporates more uncertainty than the old one: as long as the theoretical probability of getting to some m from some b is positive, it is included in the expectation. That's not the case with a fixed set of integration nodes.

Take a look at the notebook and let me know if you want to meet to discuss. I didn't fix all typos or style issues.

@llorracc
Copy link
Collaborator

llorracc commented May 29, 2024 via email

@AMonninger
Copy link
Collaborator Author

@mnwhite Thank you so much. Those changes are brilliant!
But if I understood you correctly, the method only works without Unemployment Probability (eg no pointmass of the pdf at 0/'IncUnemp'). Is there a way to circumvent that limitation?

@llorracc
Copy link
Collaborator

llorracc commented May 29, 2024 via email

@mnwhite
Copy link
Contributor

mnwhite commented May 29, 2024 via email

@mnwhite
Copy link
Contributor

mnwhite commented Dec 4, 2024

I am going to work on this. We liked it, and then it fell off the map.

@mnwhite mnwhite moved this from Not started to Stale in Issues & PRs Jan 3, 2026
@alanlujan91 alanlujan91 requested a review from Copilot January 28, 2026 19:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new example notebook demonstrating how to construct the end-of-period marginal value function for IndShockConsumerType using a transition-matrix-based integration method, and compares it to the standard expectation-based method.

Changes:

  • Introduces IndShockConsumerType_IntegrationMethod_Example.ipynb, setting up a simple consumption-saving model with only transitory shocks.
  • Implements both the traditional expectation-based end-of-period marginal value function and an alternative integration/transition-matrix approach.
  • Compares the implied consumption functions from both methods via plots to highlight differences.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

"\n",
"def updateBM_TranMatrix(TranShkStd, bGrid, mGrid):\n",
" \"\"\"\n",
" Calculates the Probabillity of a transioty shock for the b times m matrix\n",
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

In updateBM_TranMatrix, the docstring has spelling mistakes ("Probabillity" and "transioty"), which can be confusing in user-facing example documentation. Please correct these to "probability" and "transitory".

Suggested change
" Calculates the Probabillity of a transioty shock for the b times m matrix\n",
" Calculates the probability of a transitory shock for the b times m matrix\n",

Copilot uses AI. Check for mistakes.
Comment on lines +256 to +268
" # ### getting the probability for each transitory shock with the size b - m (remember m = b * transitory shock)\n",
"\n",
" ### Integration 1: No unemployment probability\n",
" s = TranShkStd\n",
" mu = -0.5 * s**2\n",
" lognorm_dist = sp.stats.lognorm(s, scale=np.exp(mu))\n",
"\n",
" ### Create matrix\n",
" # Construct meshgrid of bNrmGrid_income and mNrmGrid_income\n",
" b, m = np.meshgrid(bGrid, mGrid, indexing=\"ij\")\n",
"\n",
" # Calculate differences between corresponding elements\n",
" probGrid = lognorm_dist.pdf(m - b)\n",
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

Within updateBM_TranMatrix, the comment says "remember m = b * transitory shock", but the code computes probGrid = lognorm_dist.pdf(m - b), which corresponds to an additive rather than multiplicative transitory shock and is inconsistent with the stated relationship. This mismatch is likely a conceptual error in constructing the transition matrix and should be resolved by aligning the PDF argument with the intended mapping between m and b.

Copilot uses AI. Check for mistakes.
Comment on lines +379 to +390
"\n",
" Parameters\n",
" ----------\n",
" shocks: [float]\n",
" Permanent and transitory income shock levels.\n",
" a_nrm: float\n",
" Normalized market assets this period\n",
"\n",
" Returns\n",
" -------\n",
" float\n",
" normalized market resources in the next period\n",
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

The b_nrm_next docstring states that it returns "normalized market resources in the next period", but the implementation only computes next-period normalized bank balances (before adding the transitory shock), which is inconsistent with the description. Please update the docstring to describe bank balances (or "b") rather than full market resources, or adjust the implementation to match the documented behavior.

Suggested change
"\n",
" Parameters\n",
" ----------\n",
" shocks: [float]\n",
" Permanent and transitory income shock levels.\n",
" a_nrm: float\n",
" Normalized market assets this period\n",
"\n",
" Returns\n",
" -------\n",
" float\n",
" normalized market resources in the next period\n",
"\n",
" Parameters\n",
" ----------\n",
" shocks: [float]\n",
" Permanent and transitory income shock levels.\n",
" a_nrm: float\n",
" Normalized market assets this period\n",
"\n",
" Returns\n",
" -------\n",
" float\n",
" normalized bank balances in the next period\n",

Copilot uses AI. Check for mistakes.
Comment on lines +217 to +218
"### Let ist start from minimum of aNrm\n",
"# NO, WRONG MINIMUM!\n",
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

The comment # NO, WRONG MINIMUM! indicates that shifting mGrid and bGrid by mNrmMinNext and aNrmNow[0] is incorrect, but the code still applies these shifts and uses the resulting grids. Either fix the lower bounds used for mGrid/bGrid or remove/clarify the comment so that the implementation and commentary are consistent.

Suggested change
"### Let ist start from minimum of aNrm\n",
"# NO, WRONG MINIMUM!\n",
"### Shift grids so that mGrid starts at mNrmMinNext and bGrid at aNrmNow[0]\n",

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Stale

Development

Successfully merging this pull request may close these issues.

5 participants