Esempio n. 1
0
class testKinkyPrefConsumerType(unittest.TestCase):
    def setUp(self):

        self.agent = KinkyPrefConsumerType()
        self.agent.cycles = 0  # Infinite horizon
        self.agent.solve()

    def test_solution(self):
        self.assertAlmostEqual(self.agent.solution[0].mNrmMin,
                               -0.7555156106287383)

        m = np.linspace(self.agent.solution[0].mNrmMin, 5, 200)

        self.assertAlmostEqual(self.agent.PrefShkDstn[0].X[5],
                               0.6904681186891202)

        c = self.agent.solution[0].cFunc(m, np.ones_like(m))
        self.assertAlmostEqual(c[5], 0.13237946)

        k = self.agent.solution[0].cFunc.derivativeX(m, np.ones_like(m))
        self.assertAlmostEqual(k[5], 0.91443463)

        self.agent.solution[0].vFunc
        self.agent.solution[0].mNrmMin

    def test_simulation(self):
        self.agent.T_sim = 10
        self.agent.track_vars = ["cNrmNow", "PrefShkNow"]
        self.agent.initializeSim()
        self.agent.simulate()
Esempio n. 2
0
    def setUp(self):

        self.agent = KinkyPrefConsumerType()
        self.agent.cycles = 0  # Infinite horizon
        self.agent.solve()
Esempio n. 3
0
#
# An example notebook which has some basic content which a REMARK may include.
#
# The REMARK notebook shouldn't just contain the code, use the markdown cells to explain the code and models.

# In[2]:

# required imports
from HARK.ConsumptionSaving.ConsPrefShockModel import KinkyPrefConsumerType
import numpy as np
import matplotlib.pyplot as plt

# In[3]:

# Make and solve a preference shock consumer
PrefShockExample = KinkyPrefConsumerType()
PrefShockExample.cycles = 0  # Infinite horizon
PrefShockExample.solve()

# In[4]:

# Unpack the consumption function for easier access.
PrefShockExample.unpack('cFunc')
PrefShockExample.unpack('mNrmMin')

# In[5]:

# Plot the consumption function at each discrete shock
m = np.linspace(PrefShockExample.mNrmMin[0], 5, 200)
for j in range(PrefShockExample.PrefShkDstn[0].pmf.size):
    PrefShk = PrefShockExample.PrefShkDstn[0].X[j]
    plotFuncs(
        PrefShockExample.solution[0].vFunc,
        PrefShockExample.solution[0].mNrmMin + 0.5,
        5,
    )

# Test the simulator for the pref shock class
if do_simulation:
    PrefShockExample.T_sim = 120
    PrefShockExample.track_vars = ["cNrmNow"]
    PrefShockExample.makeShockHistory()  # This is optional
    PrefShockExample.initializeSim()
    PrefShockExample.simulate()

# Make and solve a "kinky preferece" consumer, whose model combines KinkedR and PrefShock
KinkyPrefExample = KinkyPrefConsumerType()
KinkyPrefExample.cycles = 0  # Infinite horizon

t_start = process_time()
KinkyPrefExample.solve()
t_end = process_time()
print("Solving a kinky preference consumer took " + str(t_end - t_start) +
      " seconds.")

# Plot the consumption function at each discrete shock
m = np.linspace(KinkyPrefExample.solution[0].mNrmMin, 5, 200)
print("Consumption functions at each discrete shock:")
for j in range(KinkyPrefExample.PrefShkDstn[0][1].size):
    PrefShk = KinkyPrefExample.PrefShkDstn[0][1][j]
    c = KinkyPrefExample.solution[0].cFunc(m, PrefShk * np.ones_like(m))
    plt.plot(m, c)