Skip to content

Commit 31ca7ca

Browse files
committed
Fix CRRA=1.0 issues in additional consumption models
- ConsIndShockModelFast.py: Fixed vFuncNvrsSlope and vNvrsP expressions - ConsBequestModel.py: Fixed vNvrsP_temp and MPCminNvrs expressions - ConsGenIncProcessModel.py: Fixed MPCminNvrs expression All fixes use the same epsilon approach (CRRA_safe = 1.0 + 1e-8) for consistency.
1 parent c01a227 commit 31ca7ca

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

HARK/ConsumptionSaving/ConsBequestModel.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,19 @@ def calc_vPPnext(S, a, R):
419419
mNrm_temp = np.insert(mNrm_temp, 0, mNrmMinNow)
420420
vNvrs_temp = np.insert(vNvrs_temp, 0, 0.0)
421421
vNvrsP_temp = np.insert(vNvrsP_temp, 0, MPCmaxEff ** (-CRRA / (1.0 - CRRA)))
422-
MPCminNvrs = MPCminNow ** (-CRRA / (1.0 - CRRA))
422+
# Handle CRRA=1.0 case to avoid division by zero
423+
424+
if CRRA == 1.0:
425+
426+
# When CRRA=1.0, use a small epsilon to avoid division by zero
427+
428+
CRRA_safe = 1.0 + 1e-8
429+
430+
MPCminNvrs = MPCminNow ** (-CRRA_safe / (1.0 - CRRA_safe))
431+
432+
else:
433+
434+
MPCminNvrs = MPCminNow ** (-CRRA / (1.0 - CRRA))
423435
vNvrsFuncNow = CubicInterp(
424436
mNrm_temp, vNvrs_temp, vNvrsP_temp, MPCminNvrs * hNrmNow, MPCminNvrs
425437
)

HARK/ConsumptionSaving/ConsGenIncProcessModel.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,19 @@ def calc_vPP_next(S, a, p):
488488
)
489489

490490
# Add data at the lower bound of p
491-
MPCminNvrs = MPCminNow ** (-CRRA / (1.0 - CRRA))
491+
# Handle CRRA=1.0 case to avoid division by zero
492+
493+
if CRRA == 1.0:
494+
495+
# When CRRA=1.0, use a small epsilon to avoid division by zero
496+
497+
CRRA_safe = 1.0 + 1e-8
498+
499+
MPCminNvrs = MPCminNow ** (-CRRA_safe / (1.0 - CRRA_safe))
500+
501+
else:
502+
503+
MPCminNvrs = MPCminNow ** (-CRRA / (1.0 - CRRA))
492504
m_temp = np.reshape(mLvl_temp[:, 0], (aNrmCount + 1, 1))
493505
mLvl_temp = np.concatenate((m_temp, mLvl_temp), axis=1)
494506
vNvrs_temp = np.concatenate((MPCminNvrs * m_temp, vNvrs_temp), axis=1)

HARK/ConsumptionSaving/ConsIndShockModelFast.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,19 @@ def _solveConsPerfForesightNumba(
341341
mNrmMinNow = mNrmNow[0]
342342

343343
# See the PerfForesightConsumerType.ipynb documentation notebook for the derivations
344-
vFuncNvrsSlope = MPCmin ** (-CRRA / (1.0 - CRRA))
344+
# Handle CRRA=1.0 case to avoid division by zero
345+
346+
if CRRA == 1.0:
347+
348+
# When CRRA=1.0, use a small epsilon to avoid division by zero
349+
350+
CRRA_safe = 1.0 + 1e-8
351+
352+
vFuncNvrsSlope = MPCmin ** (-CRRA_safe / (1.0 - CRRA_safe))
353+
354+
else:
355+
356+
vFuncNvrsSlope = MPCmin ** (-CRRA / (1.0 - CRRA))
345357

346358
return (
347359
mNrmNow,
@@ -869,7 +881,19 @@ def _add_vFuncNumba(
869881
mNrmGrid = _np_insert(mNrmGrid, 0, mNrmMinNow)
870882
vNvrs = _np_insert(vNvrs, 0, 0.0)
871883
vNvrsP = _np_insert(vNvrsP, 0, MPCmaxEff ** (-CRRA / (1.0 - CRRA)))
872-
MPCminNvrs = MPCminNow ** (-CRRA / (1.0 - CRRA))
884+
# Handle CRRA=1.0 case to avoid division by zero
885+
886+
if CRRA == 1.0:
887+
888+
# When CRRA=1.0, use a small epsilon to avoid division by zero
889+
890+
CRRA_safe = 1.0 + 1e-8
891+
892+
MPCminNvrs = MPCminNow ** (-CRRA_safe / (1.0 - CRRA_safe))
893+
894+
else:
895+
896+
MPCminNvrs = MPCminNow ** (-CRRA / (1.0 - CRRA))
873897

874898
return (
875899
mNrmGrid,

0 commit comments

Comments
 (0)