Skip to content

Commit

Permalink
Merge pull request #1239 from alanlujan91/distance_metric
Browse files Browse the repository at this point in the history
Update MetricObject
  • Loading branch information
alanlujan91 authored Apr 3, 2023
2 parents 3f6ce46 + e0c9c5d commit 88eb36c
Show file tree
Hide file tree
Showing 17 changed files with 351 additions and 354 deletions.
3 changes: 2 additions & 1 deletion HARK/ConsumptionSaving/ConsAggShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
import scipy.stats as stats

from HARK import AgentType, Market, MetricObject
from HARK import AgentType, Market
from HARK.ConsumptionSaving.ConsIndShockModel import (
ConsumerSolution,
IndShockConsumerType,
Expand All @@ -34,6 +34,7 @@
UpperEnvelope,
VariableLowerBoundFunc2D,
)
from HARK.metric import MetricObject
from HARK.rewards import (
CRRAutility,
CRRAutility_inv,
Expand Down
3 changes: 2 additions & 1 deletion HARK/ConsumptionSaving/ConsGenIncProcessModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
import numpy as np

from HARK import AgentType, MetricObject, make_one_period_oo_solver
from HARK import AgentType, make_one_period_oo_solver
from HARK.ConsumptionSaving.ConsIndShockModel import (
ConsIndShockSetup,
ConsumerSolution,
Expand All @@ -26,6 +26,7 @@
ValueFuncCRRA,
VariableLowerBoundFunc2D,
)
from HARK.metric import MetricObject
from HARK.rewards import (
CRRAutility,
CRRAutility_inv,
Expand Down
26 changes: 10 additions & 16 deletions HARK/ConsumptionSaving/ConsIndShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from HARK import (
AgentType,
MetricObject,
NullFunc,
_log,
make_one_period_oo_solver,
Expand Down Expand Up @@ -53,6 +52,7 @@
MargValueFuncCRRA,
ValueFuncCRRA,
)
from HARK.metric import MetricObject
from HARK.rewards import (
CRRAutility,
CRRAutility_inv,
Expand Down Expand Up @@ -447,9 +447,8 @@ def add_mNrmTrg(self, solution):

# mNrmTrg solves Rcalbar*(m - c(m)) + E[inc_next] = m. Define a
# rearranged version.
Ex_m_tp1_minus_m_t = (
lambda m: Ex_RNrmFac * (m - solution.cFunc(m)) + self.Ex_IncNext - m
)
def Ex_m_tp1_minus_m_t(m):
return Ex_RNrmFac * (m - solution.cFunc(m)) + self.Ex_IncNext - m

# Minimum market resources plus next income is okay starting guess
m_init_guess = self.mNrmMinNow + self.Ex_IncNext
Expand Down Expand Up @@ -507,9 +506,8 @@ def add_mNrmStE(self, solution):
solution.mNrmTrg = float("inf")
return solution

Ex_PermShk_tp1_times_m_tp1_minus_m_t = (
lambda mStE: PF_RNrm * (mStE - solution.cFunc(mStE)) + 1.0 - mStE
)
def Ex_PermShk_tp1_times_m_tp1_minus_m_t(mStE):
return PF_RNrm * (mStE - solution.cFunc(mStE)) + 1.0 - mStE

# Minimum market resources plus next income is okay starting guess
m_init_guess = self.mNrmMinNow + self.Ex_IncNext
Expand Down Expand Up @@ -1755,7 +1753,7 @@ def sim_birth(self, which_agents):
): # If PerfMITShk not specified, let it be False
self.PerfMITShk = False
if (
self.PerfMITShk == False
self.PerfMITShk is False
): # If True, Newborns inherit t_cycle of agent they replaced (i.e. t_cycles are not reset).
self.t_cycle[
which_agents
Expand Down Expand Up @@ -2018,10 +2016,6 @@ def test(agent):
False: "The given type violates the Finite Human Wealth Condition; the Finite Human wealth factor value is {0.FHWF}",
}

verbose_messages = {
True: " Therefore, the limiting consumption function is not c(m)=Infinity\nand human wealth normalized by permanent income is {0.hNrm}\nand the PDV of future consumption growth is {0.cNrmPDV}",
False: " Therefore, the limiting consumption function is c(m)=Infinity for all m unless the RIC is also violated. If both FHWC and RIC fail and the consumer faces a liquidity constraint, the limiting consumption function is nondegenerate but has a limiting slope of 0. (https://econ-ark.github.io/BufferStockTheory#PFGICRawHoldsFHWCFailsRICFailsDiscuss)",
}
verbose = self.verbose if verbose is None else verbose
self.check_condition(name, test, messages, verbose)

Expand Down Expand Up @@ -2393,7 +2387,7 @@ def define_distribution_grid(
if not hasattr(self, "neutral_measure"):
self.neutral_measure = False

if num_pointsM == None:
if num_pointsM is None:
m_points = self.mCount
else:
m_points = num_pointsM
Expand Down Expand Up @@ -2446,7 +2440,7 @@ def define_distribution_grid(
self.dist_pGrid = dist_pGrid

if (
self.neutral_measure == True
self.neutral_measure is True
): # If true Use Harmenberg 2021's Neutral Measure. For more information, see https://econ-ark.org/materials/harmenberg-aggregation?launch
self.dist_pGrid = np.array([1])

Expand All @@ -2456,7 +2450,7 @@ def define_distribution_grid(
)

elif self.T_cycle != 0:
if num_pointsM == None:
if num_pointsM is None:
m_points = self.mCount
else:
m_points = num_pointsM
Expand Down Expand Up @@ -2510,7 +2504,7 @@ def define_distribution_grid(
self.dist_pGrid = dist_pGrid

if (
self.neutral_measure == True
self.neutral_measure is True
): # If true Use Harmenberg 2021's Neutral Measure. For more information, see https://econ-ark.org/materials/harmenberg-aggregation?launch
self.dist_pGrid = self.T_cycle * [np.array([1])]

Expand Down
3 changes: 2 additions & 1 deletion HARK/ConsumptionSaving/ConsIndShockModelFast.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from numba import njit
from quantecon.optimize import newton_secant

from HARK import MetricObject, make_one_period_oo_solver
from HARK import make_one_period_oo_solver
from HARK.ConsumptionSaving.ConsIndShockModel import (
ConsIndShockSolverBasic,
ConsPerfForesightSolver,
Expand All @@ -36,6 +36,7 @@
MargValueFuncCRRA,
ValueFuncCRRA,
)
from HARK.metric import MetricObject
from HARK.numba_tools import (
CRRAutility,
CRRAutility_inv,
Expand Down
3 changes: 2 additions & 1 deletion HARK/ConsumptionSaving/ConsLabeledModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
init_risky_asset,
init_risky_share_fixed,
)
from HARK.core import MetricObject, make_one_period_oo_solver
from HARK.core import make_one_period_oo_solver
from HARK.distribution import DiscreteDistributionLabeled
from HARK.metric import MetricObject
from HARK.rewards import UtilityFuncCRRA


Expand Down
2 changes: 1 addition & 1 deletion HARK/ConsumptionSaving/ConsLaborModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
IndShockConsumerType,
init_idiosyncratic_shocks,
)
from HARK.core import MetricObject
from HARK.interpolation import (
BilinearInterp,
ConstantFunction,
Expand All @@ -28,6 +27,7 @@
ValueFuncCRRA,
VariableLowerBoundFunc2D,
)
from HARK.metric import MetricObject
from HARK.rewards import CRRAutilityP, CRRAutilityP_inv


Expand Down
14 changes: 8 additions & 6 deletions HARK/ConsumptionSaving/ConsMedModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
from scipy.optimize import brentq

from HARK import AgentType, MetricObject, make_one_period_oo_solver
from HARK import AgentType, make_one_period_oo_solver
from HARK.ConsumptionSaving.ConsGenIncProcessModel import (
ConsGenIncProcessSolver,
PersistentShockConsumerType,
Expand All @@ -29,6 +29,7 @@
ValueFuncCRRA,
VariableLowerBoundFunc3D,
)
from HARK.metric import MetricObject
from HARK.rewards import (
CRRAutility,
CRRAutility_inv,
Expand Down Expand Up @@ -113,11 +114,12 @@ def __init__(
elif MedShk == 0: # All consumption when MedShk = 0
cLvl = xLvl
else:
optMedZeroFunc = (
lambda c: (MedShk / MedPrice) ** (-1.0 / CRRAcon)
* ((xLvl - c) / MedPrice) ** (CRRAmed / CRRAcon)
- c
)

def optMedZeroFunc(c):
return (MedShk / MedPrice) ** (-1.0 / CRRAcon) * (
(xLvl - c) / MedPrice
) ** (CRRAmed / CRRAcon) - c

# Find solution to FOC
cLvl = brentq(optMedZeroFunc, 0.0, xLvl)
cLvlGrid[i, j] = cLvl
Expand Down
52 changes: 17 additions & 35 deletions HARK/ConsumptionSaving/ConsPortfolioModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,28 @@

import numpy as np

from HARK import ( # Basic HARK features
AgentType,
MetricObject,
NullFunc,
make_one_period_oo_solver,
)
from HARK.ConsumptionSaving.ConsIndShockModel import (
IndShockConsumerType, # PortfolioConsumerType inherits from it
)
from HARK.ConsumptionSaving.ConsIndShockModel import (
init_idiosyncratic_shocks, # Baseline dictionary to build on
)
from HARK.ConsumptionSaving.ConsIndShockModel import utility # CRRA utility function
from HARK.ConsumptionSaving.ConsIndShockModel import (
utility_inv, # Inverse CRRA utility function
)
from HARK import AgentType, NullFunc, make_one_period_oo_solver
from HARK.ConsumptionSaving.ConsIndShockModel import (
utility_invP, # Derivative of inverse CRRA utility function
)
from HARK.ConsumptionSaving.ConsIndShockModel import (
utilityP, # CRRA marginal utility function
)
from HARK.ConsumptionSaving.ConsIndShockModel import (
utilityP_inv, # Inverse CRRA marginal utility function
IndShockConsumerType,
init_idiosyncratic_shocks,
utility,
utility_inv,
utility_invP,
utilityP,
utilityP_inv,
)
from HARK.ConsumptionSaving.ConsRiskyAssetModel import RiskyAssetConsumerType
from HARK.distribution import calc_expectation
from HARK.interpolation import BilinearInterp # 2D interpolator
from HARK.interpolation import (
ConstantFunction, # Interpolator-like class that returns constant value
)
from HARK.interpolation import CubicInterp # Piecewise cubic interpolation
from HARK.interpolation import (
IdentityFunction, # Interpolator-like class that returns one of its arguments
)
from HARK.interpolation import LinearInterp # Piecewise linear interpolation
from HARK.interpolation import (
LinearInterpOnInterp1D, # Interpolator over 1D interpolations
BilinearInterp,
ConstantFunction,
CubicInterp,
IdentityFunction,
LinearInterp,
LinearInterpOnInterp1D,
MargValueFuncCRRA,
ValueFuncCRRA,
)
from HARK.interpolation import MargValueFuncCRRA, ValueFuncCRRA
from HARK.metric import MetricObject


# Define a class to represent the single period solution of the portfolio choice problem
Expand Down
3 changes: 2 additions & 1 deletion HARK/ConsumptionSaving/ConsRiskyContribModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import numpy as np

from HARK import MetricObject, NullFunc # Basic HARK features
from HARK import NullFunc # Basic HARK features
from HARK.ConsumptionSaving.ConsIndShockModel import utility # CRRA utility function
from HARK.ConsumptionSaving.ConsIndShockModel import (
utility_inv, # Inverse CRRA utility function
Expand Down Expand Up @@ -53,6 +53,7 @@
from HARK.interpolation import LinearInterp # Piecewise linear interpolation
from HARK.interpolation import TrilinearInterp # 3D interpolator
from HARK.interpolation import DiscreteInterp, MargValueFuncCRRA, ValueFuncCRRA
from HARK.metric import MetricObject
from HARK.utilities import make_grid_exp_mult


Expand Down
Loading

0 comments on commit 88eb36c

Please sign in to comment.