From c3a30d9fb812ff135f871ad40572e22b141da552 Mon Sep 17 00:00:00 2001 From: MateoVG Date: Thu, 2 Jul 2020 15:07:23 -0400 Subject: [PATCH 1/2] Deal with cases when the agent wants to short --- HARK/ConsumptionSaving/ConsPortfolioModel.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/HARK/ConsumptionSaving/ConsPortfolioModel.py b/HARK/ConsumptionSaving/ConsPortfolioModel.py index 60b926b91..20329dc64 100644 --- a/HARK/ConsumptionSaving/ConsPortfolioModel.py +++ b/HARK/ConsumptionSaving/ConsPortfolioModel.py @@ -778,18 +778,20 @@ def solveConsPortfolio(solution_next,ShockDstn,IncomeDstn,RiskyDstn, FOC_s = EndOfPrddvds Share_now = np.zeros_like(aNrmGrid) # Initialize to putting everything in safe asset cNrmAdj_now = np.zeros_like(aNrmGrid) - constrained = FOC_s[:,-1] > 0. # If agent wants to put more than 100% into risky asset, he is constrained - Share_now[constrained] = 1.0 + constrained_top = FOC_s[:,-1] > 0. # If agent wants to put more than 100% into risky asset, he is constrained + constrained_bot = FOC_s[:,0] < 0. # Likewise if he wants to put less than 0% into risky asset + Share_now[constrained_top] = 1.0 if not zero_bound: Share_now[0] = 1. # aNrm=0, so there's no way to "optimize" the portfolio cNrmAdj_now[0] = EndOfPrddvdaNvrs[0,-1] # Consumption when aNrm=0 does not depend on Share - cNrmAdj_now[constrained] = EndOfPrddvdaNvrs[constrained,-1] # Get consumption when share-constrained - + constrained_top[0] = True # Mark as constrained so that there is no attempt at optimization + cNrmAdj_now[constrained_top] = EndOfPrddvdaNvrs[constrained_top,-1] # Get consumption when share-constrained + cNrmAdj_now[constrained_bot] = EndOfPrddvdaNvrs[constrained_bot,0] # For each value of aNrm, find the value of Share such that FOC-Share == 0. # This loop can probably be eliminated, but it's such a small step that it won't speed things up much. crossing = np.logical_and(FOC_s[:,1:] <= 0., FOC_s[:,:-1] >= 0.) for j in range(aNrm_N): - if Share_now[j] == 0.: + if not (constrained_top[j] or constrained_bot[j]): try: idx = np.argwhere(crossing[j,:])[0][0] bot_s = ShareGrid[idx] From 05cfd8e5d6d5d9ef88abbace3acd63b66898fb3a Mon Sep 17 00:00:00 2001 From: MateoVG Date: Thu, 2 Jul 2020 15:28:10 -0400 Subject: [PATCH 2/2] Get rid of the try-except --- HARK/ConsumptionSaving/ConsPortfolioModel.py | 23 +++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/HARK/ConsumptionSaving/ConsPortfolioModel.py b/HARK/ConsumptionSaving/ConsPortfolioModel.py index 20329dc64..934a0e8ef 100644 --- a/HARK/ConsumptionSaving/ConsPortfolioModel.py +++ b/HARK/ConsumptionSaving/ConsPortfolioModel.py @@ -792,19 +792,16 @@ def solveConsPortfolio(solution_next,ShockDstn,IncomeDstn,RiskyDstn, crossing = np.logical_and(FOC_s[:,1:] <= 0., FOC_s[:,:-1] >= 0.) for j in range(aNrm_N): if not (constrained_top[j] or constrained_bot[j]): - try: - idx = np.argwhere(crossing[j,:])[0][0] - bot_s = ShareGrid[idx] - top_s = ShareGrid[idx+1] - bot_f = FOC_s[j,idx] - top_f = FOC_s[j,idx+1] - bot_c = EndOfPrddvdaNvrs[j,idx] - top_c = EndOfPrddvdaNvrs[j,idx+1] - alpha = 1. - top_f/(top_f-bot_f) - Share_now[j] = (1.-alpha)*bot_s + alpha*top_s - cNrmAdj_now[j] = (1.-alpha)*bot_c + alpha*top_c - except: - print('No optimal controls found for a=' + str(aNrmGrid[j])) + idx = np.argwhere(crossing[j,:])[0][0] + bot_s = ShareGrid[idx] + top_s = ShareGrid[idx+1] + bot_f = FOC_s[j,idx] + top_f = FOC_s[j,idx+1] + bot_c = EndOfPrddvdaNvrs[j,idx] + top_c = EndOfPrddvdaNvrs[j,idx+1] + alpha = 1. - top_f/(top_f-bot_f) + Share_now[j] = (1.-alpha)*bot_s + alpha*top_s + cNrmAdj_now[j] = (1.-alpha)*bot_c + alpha*top_c # Calculate the endogenous mNrm gridpoints when the agent adjusts his portfolio mNrmAdj_now = aNrmGrid + cNrmAdj_now