Skip to content

Commit

Permalink
Implement feature and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mv77 committed Apr 4, 2023
1 parent 5a21876 commit 909bd16
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
19 changes: 15 additions & 4 deletions HARK/ConsumptionSaving/ConsRiskyAssetModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def __init__(self, verbose=False, quiet=False, **kwds):
if not hasattr(self, "PortfolioBisect"):
self.PortfolioBisect = False

# Boolean determines whether, when simulating a given time period,
# all agents will draw the same risky return factor (true by default)
if not hasattr(self, "sim_common_Rriksy"):
self.sim_common_Rriksy = True

# Initialize a basic consumer type
IndShockConsumerType.__init__(self, verbose=verbose, quiet=quiet, **kwds)

Expand Down Expand Up @@ -319,9 +324,15 @@ def get_Risky(self):
RiskyAvg = self.RiskyAvg
RiskyStd = self.RiskyStd

self.shocks["Risky"] = Lognormal.from_mean_std(
RiskyAvg, RiskyStd, seed=self.RNG.integers(0, 2**31 - 1)
).draw(1)
# Draw either a common economy-wide return, or one for each agent
if self.sim_common_Rriksy:
self.shocks["Risky"] = Lognormal.from_mean_std(
RiskyAvg, RiskyStd, seed=self.RNG.integers(0, 2**31 - 1)
).draw(1)
else:
self.shocks["Risky"] = Lognormal.from_mean_std(
RiskyAvg, RiskyStd, seed=self.RNG.integers(0, 2**31 - 1)
).draw(self.AgentCount)

def get_Adjust(self):
"""
Expand Down Expand Up @@ -1307,7 +1318,7 @@ def v_next(shocks, a_nrm):
"AdjustPrb": 1.0,
# When simulating the model, should all agents get the same risky return in
# a given period?
"sim_common_Rriksy": True,
"sim_common_Rriksy": True,
}

# Make a dictionary to specify a risky asset consumer type
Expand Down
33 changes: 33 additions & 0 deletions HARK/ConsumptionSaving/tests/test_ConsPortfolioModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,36 @@ def test_discrete_and_joint(self):

# Solve model under given parameters
self.discrete_and_joint.solve()


class testRiskyReturnDim(PortfolioConsumerTypeTestCase):
def test_simulation(self):
# Setup
self.pcct.T_sim = 30
self.pcct.AgentCount = 10
self.pcct.track_vars += [
"mNrm",
"cNrm",
"Risky",
]
# Common (default) simulation
self.pcct.initialize_sim()
self.pcct.simulate()
# Assety that all columns of Risky are the same
self.assertTrue(
np.all(
self.pcct.history["Risky"]
== self.pcct.history["Risky"][:, 0][:, np.newaxis]
)
)
# Agent specific simulation
self.pcct.sim_common_Rriksy = False
self.pcct.initialize_sim()
self.pcct.simulate()
# Assety that all columns of Risky are not the same
self.assertFalse(
np.all(
self.pcct.history["Risky"]
== self.pcct.history["Risky"][:, 0][:, np.newaxis]
)
)

0 comments on commit 909bd16

Please sign in to comment.