Beispiel #1
0
class testPrefShockConsumerType(unittest.TestCase):
    def setUp(self):

        self.agent = PrefShockConsumerType()
        self.agent.cycles = 0
        self.agent.solve()

    def test_solution(self):

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

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

        self.assertAlmostEqual(
            self.agent.solution[0].cFunc(m, np.ones_like(m))[35],
            0.8123891603954809)

        self.assertAlmostEqual(
            self.agent.solution[0].cFunc.derivativeX(m, np.ones_like(m))[35],
            0.44973706445183886)

    def test_simulation(self):
        self.agent.T_sim = 10
        self.agent.track_vars = ["cNrmNow"]
        self.agent.makeShockHistory()  # This is optional
        self.agent.initializeSim()
        self.agent.simulate()
Beispiel #2
0
class testPrefShockConsumerType(unittest.TestCase):
    def setUp(self):

        self.agent = PrefShockConsumerType()
        self.agent.cycles = 0
        self.agent.solve()

    def test_solution(self):

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

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

        self.assertAlmostEqual(
            self.agent.solution[0].cFunc(m, np.ones_like(m))[35],
            0.8123891603954809)

        self.assertAlmostEqual(
            self.agent.solution[0].cFunc.derivativeX(m, np.ones_like(m))[35],
            0.44973706445183886,
        )

    def test_simulation(self):
        self.agent.T_sim = 10
        self.agent.track_vars = ['cNrm', "PrefShk"]
        self.agent.make_shock_history()  # This is optional
        self.agent.initialize_sim()
        self.agent.simulate()

        self.assertAlmostEqual(self.agent.history['cNrm'][0][5],
                               0.7366020536567589)

        self.assertEqual(
            self.agent.shock_history["PrefShk"][0][5],
            self.agent.history["PrefShk"][0][5],
        )

        self.assertEqual(self.agent.history["PrefShk"][0][5],
                         0.4909415933881665)
Beispiel #3
0
    def setUp(self):

        self.agent = PrefShockConsumerType()
        self.agent.cycles = 0
        self.agent.solve()
import HARK.ConsumptionSaving.ConsumerParameters as Params
import matplotlib.pyplot as plt
from HARK.utilities import plotFuncs
from time import process_time
import numpy as np
from HARK.ConsumptionSaving.ConsPrefShockModel import (
    PrefShockConsumerType,
    KinkyPrefConsumerType,
)
mystr = lambda number: "{:.4f}".format(number)
do_simulation = True

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

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

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