Skip to content

Commit

Permalink
rename pmf and X
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlujan91 committed Aug 16, 2022
1 parent b344850 commit c3262ef
Show file tree
Hide file tree
Showing 21 changed files with 553 additions and 546 deletions.
296 changes: 167 additions & 129 deletions HARK/ConsumptionSaving/ConsAggShockModel.py

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions HARK/ConsumptionSaving/ConsIndShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,8 @@ def set_and_update_values(self, solution_next, IncShkDstn, LivPrb, DiscFac):
solution_next : ConsumerSolution
The solution to next period's one period problem.
IncShkDstn : distribution.DiscreteDistribution
A DiscreteDistribution with a pmf
and two point value arrays in X, order:
A DiscreteDistribution with a pmv
and two point value arrays in atoms, order:
permanent shocks, transitory shocks.
LivPrb : float
Survival probability; likelihood of being alive at the beginning of
Expand All @@ -702,9 +702,9 @@ def set_and_update_values(self, solution_next, IncShkDstn, LivPrb, DiscFac):
"""
self.DiscFacEff = DiscFac * LivPrb # "effective" discount factor
self.IncShkDstn = IncShkDstn
self.ShkPrbsNext = IncShkDstn.pmf
self.PermShkValsNext = IncShkDstn.X[0]
self.TranShkValsNext = IncShkDstn.X[1]
self.ShkPrbsNext = IncShkDstn.pmv
self.PermShkValsNext = IncShkDstn.atoms[0]
self.TranShkValsNext = IncShkDstn.atoms[1]
self.PermShkMinNext = np.min(self.PermShkValsNext)
self.TranShkMinNext = np.min(self.TranShkValsNext)
self.vPfuncNext = solution_next.vPfunc
Expand Down Expand Up @@ -2283,9 +2283,9 @@ def get_shocks(self):
# Get random draws of income shocks from the discrete distribution
EventDraws = IncShkDstnNow.draw_events(N)
PermShkNow[these] = (
IncShkDstnNow.X[0][EventDraws] * PermGroFacNow
IncShkDstnNow.atoms[0][EventDraws] * PermGroFacNow
) # permanent "shock" includes expected growth
TranShkNow[these] = IncShkDstnNow.X[1][EventDraws]
TranShkNow[these] = IncShkDstnNow.atoms[1][EventDraws]
# PermShkNow[newborn] = 1.0
# Whether Newborns have transitory shock. The default is False.
if not NewbornTransShk:
Expand Down Expand Up @@ -2863,11 +2863,11 @@ def __init__(self, sigma, n_approx, neutral_measure=False, seed=0):
logn_approx = MeanOneLogNormal(sigma).approx(
n_approx if sigma > 0.0 else 1, tail_N=0
)
# Change the pmf if necessary
# Change the pmv if necessary
if neutral_measure:
logn_approx.pmf = (logn_approx.X * logn_approx.pmf).flatten()
logn_approx.pmv = (logn_approx.atoms * logn_approx.pmv).flatten()

super().__init__(pmf=logn_approx.pmf, X=logn_approx.X, seed=seed)
super().__init__(pmv=logn_approx.pmv, atoms=logn_approx.atoms, seed=seed)


class MixtureTranIncShk(DiscreteDistribution):
Expand Down Expand Up @@ -2904,7 +2904,7 @@ def __init__(self, sigma, UnempPrb, IncUnemp, n_approx, seed=0):
dstn_approx, p=UnempPrb, x=IncUnemp
)

super().__init__(pmf=dstn_approx.pmf, X=dstn_approx.X, seed=seed)
super().__init__(pmv=dstn_approx.pmv, atoms=dstn_approx.atoms, seed=seed)


class BufferStockIncShkDstn(DiscreteDistribution):
Expand Down Expand Up @@ -2966,7 +2966,7 @@ def __init__(

joint_dstn = combine_indep_dstns(perm_dstn, tran_dstn)

super().__init__(pmf=joint_dstn.pmf, X=joint_dstn.X, seed=seed)
super().__init__(pmv=joint_dstn.pmv, atoms=joint_dstn.atoms, seed=seed)


# Make a dictionary to specify a "kinked R" idiosyncratic shock consumer
Expand Down
6 changes: 3 additions & 3 deletions HARK/ConsumptionSaving/ConsIndShockModelFast.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,9 @@ def prepare_to_solve(self):
none
"""

self.ShkPrbsNext = self.IncShkDstn.pmf
self.PermShkValsNext = self.IncShkDstn.X[0]
self.TranShkValsNext = self.IncShkDstn.X[1]
self.ShkPrbsNext = self.IncShkDstn.pmv
self.PermShkValsNext = self.IncShkDstn.atoms[0]
self.TranShkValsNext = self.IncShkDstn.atoms[1]

(
self.DiscFacEff,
Expand Down
47 changes: 25 additions & 22 deletions HARK/ConsumptionSaving/ConsLaborModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
VariableLowerBoundFunc2D,
BilinearInterp,
ConstantFunction,
ValueFuncCRRA, MargValueFuncCRRA
ValueFuncCRRA,
MargValueFuncCRRA,
)
from HARK.ConsumptionSaving.ConsIndShockModel import (
IndShockConsumerType,
Expand Down Expand Up @@ -158,10 +159,10 @@ def solve_ConsLaborIntMarg(

# Unpack next period's solution and the productivity shock distribution, and define the inverse (marginal) utilty function
vPfunc_next = solution_next.vPfunc
TranShkPrbs = TranShkDstn.pmf
TranShkVals = TranShkDstn.X.flatten()
PermShkPrbs = PermShkDstn.pmf
PermShkVals = PermShkDstn.X.flatten()
TranShkPrbs = TranShkDstn.pmv
TranShkVals = TranShkDstn.atoms.flatten()
PermShkPrbs = PermShkDstn.pmv
PermShkVals = PermShkDstn.atoms.flatten()
TranShkCount = TranShkPrbs.size
PermShkCount = PermShkPrbs.size
uPinv = lambda X: CRRAutilityP_inv(X, gam=CRRA)
Expand Down Expand Up @@ -232,7 +233,7 @@ def solve_ConsLaborIntMarg(
TranShkScaleFac_temp = (
frac
* (WageRte * TranShkGrid) ** (LbrCost * frac)
* (LbrCost ** (-LbrCost * frac) + LbrCost ** frac)
* (LbrCost ** (-LbrCost * frac) + LbrCost**frac)
)

# Flip it to be a row vector
Expand All @@ -245,7 +246,7 @@ def solve_ConsLaborIntMarg(
TranShkGrid_rep = np.tile(
np.reshape(TranShkGrid, (1, TranShkGrid.size)), (aXtraCount, 1)
)
xNowPow = xNow ** frac # Will use this object multiple times in math below
xNowPow = xNow**frac # Will use this object multiple times in math below

# Find optimal consumption from optimal composite good
cNrmNow = (((WageRte * TranShkGrid_rep) / LbrCost) ** (LbrCost * frac)) * xNowPow
Expand Down Expand Up @@ -406,7 +407,7 @@ def update_LbrCost(self):
age_vec = np.arange(self.T_cycle)
LbrCostBase = np.zeros(self.T_cycle)
for n in range(N):
LbrCostBase += Coeffs[n] * age_vec ** n
LbrCostBase += Coeffs[n] * age_vec**n
LbrCost = np.exp(LbrCostBase)
self.LbrCost = LbrCost.tolist()
self.add_to_time_vary("LbrCost")
Expand Down Expand Up @@ -479,7 +480,7 @@ def get_states(self):
None
"""
IndShockConsumerType.get_states(self)
self.state_now['mNrm'][:] = np.nan # Delete market resource calculation
self.state_now["mNrm"][:] = np.nan # Delete market resource calculation

def get_controls(self):
"""
Expand All @@ -500,17 +501,17 @@ def get_controls(self):
for t in range(self.T_cycle):
these = t == self.t_cycle
cNrmNow[these] = self.solution[t].cFunc(
self.state_now['bNrm'][these], self.shocks['TranShk'][these]
self.state_now["bNrm"][these], self.shocks["TranShk"][these]
) # Assign consumption values
MPCnow[these] = self.solution[t].cFunc.derivativeX(
self.state_now['bNrm'][these], self.shocks['TranShk'][these]
self.state_now["bNrm"][these], self.shocks["TranShk"][these]
) # Assign marginal propensity to consume values (derivative)
LbrNow[these] = self.solution[t].LbrFunc(
self.state_now['bNrm'][these], self.shocks['TranShk'][these]
self.state_now["bNrm"][these], self.shocks["TranShk"][these]
) # Assign labor supply
self.controls['cNrm'] = cNrmNow
self.controls["cNrm"] = cNrmNow
self.MPCnow = MPCnow
self.controls['Lbr'] = LbrNow
self.controls["Lbr"] = LbrNow

def get_poststates(self):
"""
Expand All @@ -529,12 +530,14 @@ def get_poststates(self):
for t in range(self.T_cycle):
these = t == self.t_cycle
mNrmNow[these] = (
self.state_now['bNrm'][these]
+ self.controls['Lbr'][these] * self.shocks['TranShk'][these]
self.state_now["bNrm"][these]
+ self.controls["Lbr"][these] * self.shocks["TranShk"][these]
) # mNrm = bNrm + yNrm
aNrmNow[these] = mNrmNow[these] - self.controls['cNrm'][these] # aNrm = mNrm - cNrm
self.state_now['mNrm'] = mNrmNow
self.state_now['aNrm'] = aNrmNow
aNrmNow[these] = (
mNrmNow[these] - self.controls["cNrm"][these]
) # aNrm = mNrm - cNrm
self.state_now["mNrm"] = mNrmNow
self.state_now["aNrm"] = aNrmNow

# moves now to prev
super().get_poststates()
Expand All @@ -557,7 +560,7 @@ def update_TranShkGrid(self):
TranShkGrid = [] # Create an empty list for TranShkGrid that will be updated
for t in range(self.T_cycle):
TranShkGrid.append(
self.TranShkDstn[t].X.flatten()
self.TranShkDstn[t].atoms.flatten()
) # Update/ Extend the list of TranShkGrid with the TranShkVals for each TranShkPrbs
self.TranShkGrid = TranShkGrid # Save that list in self (time-varying)
self.add_to_time_vary(
Expand Down Expand Up @@ -612,12 +615,12 @@ def update_solution_terminal(self):
cFunc_terminal = BilinearInterp(cNrmTerm, bNrmGrid, TranShkGrid)

# Compute the effective consumption value using consumption value and labor value at the terminal solution
xEffTerm = LsrTerm ** LbrCost * cNrmTerm
xEffTerm = LsrTerm**LbrCost * cNrmTerm
vNvrsFunc_terminal = BilinearInterp(xEffTerm, bNrmGrid, TranShkGrid)
vFunc_terminal = ValueFuncCRRA(vNvrsFunc_terminal, self.CRRA)

# Using the envelope condition at the terminal solution to estimate the marginal value function
vPterm = LsrTerm ** LbrCost * CRRAutilityP(xEffTerm, gam=self.CRRA)
vPterm = LsrTerm**LbrCost * CRRAutilityP(xEffTerm, gam=self.CRRA)
vPnvrsTerm = CRRAutilityP_inv(
vPterm, gam=self.CRRA
) # Evaluate the inverse of the CRRA marginal utility function at a given marginal value, vP
Expand Down
8 changes: 4 additions & 4 deletions HARK/ConsumptionSaving/ConsMarkovModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ def def_boundary(self):
self.BoroCnstNatAll = np.zeros(self.StateCount) + np.nan
# Find the natural borrowing constraint conditional on next period's state
for j in range(self.StateCount):
PermShkMinNext = np.min(self.IncShkDstn_list[j].X[0])
TranShkMinNext = np.min(self.IncShkDstn_list[j].X[1])
PermShkMinNext = np.min(self.IncShkDstn_list[j].atoms[0])
TranShkMinNext = np.min(self.IncShkDstn_list[j].atoms[1])
self.BoroCnstNatAll[j] = (
(self.solution_next.mNrmMin[j] - TranShkMinNext)
* (self.PermGroFac_list[j] * PermShkMinNext)
Expand Down Expand Up @@ -1117,9 +1117,9 @@ def get_shocks(self):
# Get random draws of income shocks from the discrete distribution
EventDraws = IncShkDstnNow.draw_events(N)
PermShkNow[these] = (
IncShkDstnNow.X[0][EventDraws] * PermGroFacNow
IncShkDstnNow.atoms[0][EventDraws] * PermGroFacNow
) # permanent "shock" includes expected growth
TranShkNow[these] = IncShkDstnNow.X[1][EventDraws]
TranShkNow[these] = IncShkDstnNow.atoms[1][EventDraws]
newborn = self.t_age == 0
PermShkNow[newborn] = 1.0
TranShkNow[newborn] = 1.0
Expand Down
32 changes: 18 additions & 14 deletions HARK/ConsumptionSaving/ConsMedModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import numpy as np
from scipy.optimize import brentq
from HARK import AgentType, MetricObject, make_one_period_oo_solver
from HARK import AgentType, MetricObject, make_one_period_oo_solver
from HARK.distribution import add_discrete_outcome_constant_mean, Lognormal
from HARK.utilities import (
CRRAutilityP_inv,
Expand All @@ -27,7 +27,7 @@
VariableLowerBoundFunc3D,
ValueFuncCRRA,
MargValueFuncCRRA,
MargMargValueFuncCRRA
MargMargValueFuncCRRA,
)
from HARK.ConsumptionSaving.ConsGenIncProcessModel import (
ConsGenIncProcessSolver,
Expand Down Expand Up @@ -569,7 +569,7 @@ class MedShockConsumerType(PersistentShockConsumerType):
"""

shock_vars_ = PersistentShockConsumerType.shock_vars_ + ["MedShk"]
state_vars = PersistentShockConsumerType.state_vars + ['mLvl']
state_vars = PersistentShockConsumerType.state_vars + ["mLvl"]

def __init__(self, **kwds):
params = init_medical_shocks.copy()
Expand Down Expand Up @@ -622,7 +622,7 @@ def update_med_shock_process(self):
MedShkAvgNow = self.MedShkAvg[t] # get shock distribution parameters
MedShkStdNow = self.MedShkStd[t]
MedShkDstnNow = Lognormal(
mu=np.log(MedShkAvgNow) - 0.5 * MedShkStdNow ** 2, sigma=MedShkStdNow
mu=np.log(MedShkAvgNow) - 0.5 * MedShkStdNow**2, sigma=MedShkStdNow
).approx(
N=self.MedShkCount, tail_N=self.MedShkCountTail, tail_bound=[0, 0.9]
)
Expand All @@ -649,8 +649,8 @@ def update_solution_terminal(self):
"""
# Take last period data, whichever way time is flowing
MedPrice = self.MedPrice[-1]
MedShkVals = self.MedShkDstn[-1].X.flatten()
MedShkPrbs = self.MedShkDstn[-1].pmf
MedShkVals = self.MedShkDstn[-1].atoms.flatten()
MedShkPrbs = self.MedShkDstn[-1].pmv

# Initialize grids of medical need shocks, market resources, and optimal consumption
MedShkGrid = MedShkVals
Expand Down Expand Up @@ -840,12 +840,12 @@ def get_controls(self):
for t in range(self.T_cycle):
these = t == self.t_cycle
cLvlNow[these], MedNow[these] = self.solution[t].policyFunc(
self.state_now['mLvl'][these],
self.state_now['pLvl'][these],
self.state_now["mLvl"][these],
self.state_now["pLvl"][these],
self.shocks["MedShk"][these],
)
self.controls['cLvl'] = cLvlNow
self.controls['Med'] = MedNow
self.controls["cLvl"] = cLvlNow
self.controls["Med"] = MedNow
return None

def get_poststates(self):
Expand All @@ -860,7 +860,11 @@ def get_poststates(self):
-------
None
"""
self.state_now['aLvl'] = self.state_now['mLvl'] - self.controls['cLvl'] - self.shocks["MedPrice"] * self.controls['Med']
self.state_now["aLvl"] = (
self.state_now["mLvl"]
- self.controls["cLvl"]
- self.shocks["MedPrice"] * self.controls["Med"]
)

# moves now to prev
AgentType.get_poststates(self)
Expand All @@ -887,7 +891,7 @@ class ConsMedShockSolver(ConsGenIncProcessSolver):
and the one immediately following (in solution_next).
MedShkDstn : distribution.Distribution
Discrete distribution of the multiplicative utility shifter for med-
ical care.
ical care.
LivPrb : float
Survival probability; likelihood of being alive at the beginning of
the succeeding period.
Expand Down Expand Up @@ -992,8 +996,8 @@ def set_and_update_values(self, solution_next, IncShkDstn, LivPrb, DiscFac):
)

# Also unpack the medical shock distribution
self.MedShkPrbs = self.MedShkDstn.pmf
self.MedShkVals = self.MedShkDstn.X.flatten()
self.MedShkPrbs = self.MedShkDstn.pmv
self.MedShkVals = self.MedShkDstn.atoms.flatten()

def def_utility_funcs(self):
"""
Expand Down
10 changes: 5 additions & 5 deletions HARK/ConsumptionSaving/ConsPortfolioModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def set_and_update_values(self):
self.vFuncFxd_next = self.solution_next.vFuncFxd

# Unpack the shock distribution
TranShks_next = self.IncShkDstn.X[1]
TranShks_next = self.IncShkDstn.atoms[1]

# Flag for whether the natural borrowing constraint is zero
self.zero_bound = np.min(TranShks_next) == 0.0
Expand All @@ -500,7 +500,7 @@ def prepare_to_calc_EndOfPrdvP(self):
"""

# Unpack the shock distribution
Risky_next = self.RiskyDstn.X
Risky_next = self.RiskyDstn.atoms
RiskyMax = np.max(Risky_next)
RiskyMin = np.min(Risky_next)

Expand Down Expand Up @@ -583,7 +583,7 @@ def dvds_dist(shocks, b_nrm, Share_next):
dvdb_intermed = calc_expectation(
self.IncShkDstn, dvdb_dist, self.bNrmNext, self.ShareNext
)

dvdbNvrs_intermed = self.uPinv(dvdb_intermed)
dvdbNvrsFunc_intermed = BilinearInterp(
dvdbNvrs_intermed, self.bNrmGrid, self.ShareGrid
Expand All @@ -594,7 +594,7 @@ def dvds_dist(shocks, b_nrm, Share_next):
dvds_intermed = calc_expectation(
self.IncShkDstn, dvds_dist, self.bNrmNext, self.ShareNext
)

dvdsFunc_intermed = BilinearInterp(dvds_intermed, self.bNrmGrid, self.ShareGrid)

# Make tiled arrays to calculate future realizations of bNrm and Share when integrating over RiskyDstn
Expand Down Expand Up @@ -1019,7 +1019,7 @@ def set_and_update_values(self):

# If the distributions are NOT independent...
# Unpack the shock distribution
self.TranShks_next = self.ShockDstn.X[1]
self.TranShks_next = self.ShockDstn.atoms[1]
# Flag for whether the natural borrowing constraint is zero
self.zero_bound = np.min(self.TranShks_next) == 0.0

Expand Down
Loading

0 comments on commit c3262ef

Please sign in to comment.