コード例 #1
0
    def setUp(self):
        # "persistent idiosyncratic shocks" model
        PrstIncCorr = 0.98  # Serial correlation coefficient for persistent income
        persistent_shocks = copy(GenIncDictionary)
        persistent_shocks["PrstIncCorr"] = PrstIncCorr

        # "persistent idisyncratic shocks" consumer
        self.agent = PersistentShockConsumerType(cycles=1, **persistent_shocks)
        self.agent.solve()
コード例 #2
0
class testPersistentShockConsumerType(unittest.TestCase):
    def setUp(self):
        # "persistent idiosyncratic shocks" model
        PrstIncCorr = 0.98  # Serial correlation coefficient for persistent income
        persistent_shocks = copy(GenIncDictionary)
        persistent_shocks["PrstIncCorr"] = PrstIncCorr

        # "persistent idisyncratic shocks" consumer
        self.agent = PersistentShockConsumerType(cycles=1, **persistent_shocks)
        self.agent.solve()

    def test_solution(self):
        pLvlGrid = self.agent.pLvlGrid[0]

        self.assertAlmostEqual(
            self.agent.solution[0].cFunc(10, pLvlGrid[1]).tolist(),
            5.6030075768585075)

    def test_simulation(self):
        self.agent.T_sim = 25

        # why does ,"bLvlNow" not work here?
        self.agent.track_vars = ["aLvl", "mLvl", "cLvl", "pLvl"]
        self.agent.initialize_sim()
        self.agent.simulate()

        self.assertAlmostEqual(np.mean(self.agent.history["mLvl"]),
                               1.2043946738813716)
コード例 #3
0
class testPersistentShockConsumerType(unittest.TestCase):
    def setUp(self):
        # "persistent idiosyncratic shocks" model
        PrstIncCorr = 0.98  # Serial correlation coefficient for persistent income
        persistent_shocks = copy(GenIncDictionary)
        persistent_shocks['PrstIncCorr'] = PrstIncCorr

        # "persistent idisyncratic shocks" consumer
        self.agent = PersistentShockConsumerType(cycles=1, **persistent_shocks)
        self.agent.solve()

    def test_solution(self):
        pLvlGrid = self.agent.pLvlGrid[0]

        self.assertAlmostEqual(
            self.agent.solution[0].cFunc(10, pLvlGrid[1]).tolist(),
            5.6030075768585075)

    def test_simulation(self):
        self.agent.T_sim = 25
        self.agent.track_vars = ['mLvlNow', 'cLvlNow', 'pLvlNow']
        self.agent.initializeSim()
        self.agent.simulate()

        self.assertAlmostEqual(np.mean(self.agent.history['mLvlNow']),
                               1.2043701902476343)
コード例 #4
0
class testPersistentShockConsumerType(unittest.TestCase):
    def setUp(self):
        # "persistent idiosyncratic shocks" model
        PrstIncCorr = 0.98  # Serial correlation coefficient for persistent income
        persistent_shocks = copy(GenIncDictionary)
        persistent_shocks['PrstIncCorr'] = PrstIncCorr

        # "persistent idisyncratic shocks" consumer
        self.agent = PersistentShockConsumerType(**persistent_shocks)
        self.agent.solve()

    def test_solution(self):
        pLvlGrid = self.agent.pLvlGrid[0]

        self.assertAlmostEqual(
            self.agent.solution[0].cFunc(10, pLvlGrid[1]).tolist(),
            0.9141970598224893)

    def test_simulation(self):
        self.agent.T_sim = 25
        self.agent.track_vars = ['mLvlNow', 'cLvlNow', 'pLvlNow']
        self.agent.initializeSim()
        self.agent.simulate()

        self.assertAlmostEqual(np.mean(self.agent.mLvlNow_hist),
                               2.372861738538392)
コード例 #5
0
# * For this model, we overwrite the method $\texttt{updatepLvlNextFunc}$ to create the input $\texttt{pLvlNextFunc}$ as a sequence of AR1-style functions. The method uses now the attributes $\texttt{PermGroFac}$ and $\texttt{PrstIncCorr}$. If cycles=0, the product of $\texttt{PermGroFac}$ across all periods must be 1.0, otherwise this method is invalid.
#

# %% {"code_folding": []}
# Make a dictionary for the "persistent idiosyncratic shocks" model
PrstIncCorr = 0.98  # Serial correlation coefficient for persistent income

persistent_shocks = copy(GenIncDictionary)
persistent_shocks['PrstIncCorr'] = PrstIncCorr

# %% [markdown]
# The `PersistentShockConsumerType` class solves the problem of a consumer facing idiosyncratic shocks to his persistent and transitory income, and for which the (log) persistent income follows an AR1 process rather than random walk.

# %%
# Make and solve an example of "persistent idisyncratic shocks" consumer
PersistentExample = PersistentShockConsumerType(**persistent_shocks)
PersistentExample.solve()

# %%
# Plot the consumption function at various levels of persistent income pLvl
print(
    'Consumption function by persistent income level pLvl for a consumer with AR1 coefficient of '
    + str(PersistentExample.PrstIncCorr) + ':')
pLvlGrid = PersistentExample.pLvlGrid[0]
mLvlGrid = np.linspace(0, 20, 300)
for p in pLvlGrid:
    M_temp = mLvlGrid + PersistentExample.solution[0].mLvlMin(p)
    C = PersistentExample.solution[0].cFunc(M_temp, p * np.ones_like(M_temp))
    plt.plot(M_temp, C)
plt.xlim(0., 20.)
plt.xlabel('Market resource level mLvl')
コード例 #6
0
# %%
# Simulate some data
if do_simulation:
    ExplicitExample.T_sim = 500
    ExplicitExample.track_vars = ["mLvlNow", "cLvlNow", "pLvlNow"]
    ExplicitExample.makeShockHistory()  # This is optional
    ExplicitExample.initializeSim()
    ExplicitExample.simulate()
    plt.plot(np.mean(ExplicitExample.mLvlNow_hist, axis=1))
    plt.xlabel("Simulated time period")
    plt.ylabel("Average market resources mLvl")
    plt.show()

# %%
# Make and solve an example "persistent idisyncratic shocks" consumer
PersistentExample = PersistentShockConsumerType()
t_start = process_time()
PersistentExample.solve()
t_end = process_time()
print(
    "Solving a persistent income shocks consumer took "
    + mystr(t_end - t_start)
    + " seconds."
)

# %%
# Plot the consumption function at various levels of persistent income pLvl
print(
    "Consumption function by persistent income level pLvl for a consumer with AR1 coefficient of "
    + str(PersistentExample.PrstIncCorr)
    + ":"