In [1]:
from time import process_time
import numpy as np
import matplotlib.pyplot as plt
from HARK.utilities import plotFuncs
from HARK.distribution import DiscreteDistribution
from HARK.ConsumptionSaving.ConsAggShockModel import (
AggShockConsumerType,
CobbDouglasEconomy,
AggShockMarkovConsumerType,
CobbDouglasMarkovEconomy,
)
from copy import deepcopy
def mystr(number):
return "{:.4f}".format(number)
In [2]:
# Solve an AggShockConsumerType's microeconomic problem
solve_agg_shocks_micro = False
# Solve for the equilibrium aggregate saving rule in a CobbDouglasEconomy
solve_agg_shocks_market = True
# Solve an AggShockMarkovConsumerType's microeconomic problem
solve_markov_micro = False
# Solve for the equilibrium aggregate saving rule in a CobbDouglasMarkovEconomy
solve_markov_market = True
# Solve a simple Krusell-Smith-style two state, two shock model
solve_krusell_smith = True
# Solve a CobbDouglasEconomy with many states, potentially utilizing the "state jumper"
solve_poly_state = False
In [3]:
if solve_agg_shocks_micro or solve_agg_shocks_market:
# Make an aggregate shocks consumer type
AggShockExample = AggShockConsumerType()
AggShockExample.cycles = 0
# Make a Cobb-Douglas economy for the agents
EconomyExample = CobbDouglasEconomy(agents=[AggShockExample])
EconomyExample.makeAggShkHist() # Simulate a history of aggregate shocks
# Have the consumers inherit relevant objects from the economy
AggShockExample.getEconomyData(EconomyExample)
In [4]:
if solve_agg_shocks_micro:
# Solve the microeconomic model for the aggregate shocks example type (and display results)
t_start = process_time()
AggShockExample.solve()
t_end = process_time()
print(
"Solving an aggregate shocks consumer took "
+ mystr(t_end - t_start)
+ " seconds."
)
print(
"Consumption function at each aggregate market resources-to-labor ratio gridpoint:"
)
m_grid = np.linspace(0, 10, 200)
AggShockExample.unpackcFunc()
for M in AggShockExample.Mgrid.tolist():
mMin = AggShockExample.solution[0].mNrmMin(M)
c_at_this_M = AggShockExample.cFunc[0](m_grid + mMin, M * np.ones_like(m_grid))
plt.plot(m_grid + mMin, c_at_this_M)
plt.ylim(0.0, None)
plt.show()
In [5]:
if solve_agg_shocks_market:
# Solve the "macroeconomic" model by searching for a "fixed point dynamic rule"
t_start = process_time()
print(
"Now solving for the equilibrium of a Cobb-Douglas economy. This might take a few minutes..."
)
EconomyExample.solve()
t_end = process_time()
print(
'Solving the "macroeconomic" aggregate shocks model took '
+ str(t_end - t_start)
+ " seconds."
)
print("Aggregate savings as a function of aggregate market resources:")
plotFuncs(EconomyExample.AFunc, 0, 2 * EconomyExample.kSS)
print(
"Consumption function at each aggregate market resources gridpoint (in general equilibrium):"
)
AggShockExample.unpackcFunc()
m_grid = np.linspace(0, 10, 200)
AggShockExample.unpackcFunc()
for M in AggShockExample.Mgrid.tolist():
mMin = AggShockExample.solution[0].mNrmMin(M)
c_at_this_M = AggShockExample.cFunc[0](m_grid + mMin, M * np.ones_like(m_grid))
plt.plot(m_grid + mMin, c_at_this_M)
plt.ylim(0.0, None)
plt.show()
In [6]:
if solve_markov_micro or solve_markov_market or solve_krusell_smith:
# Make a Markov aggregate shocks consumer type
AggShockMrkvExample = AggShockMarkovConsumerType()
AggShockMrkvExample.IncomeDstn[0] = 2 * [AggShockMrkvExample.IncomeDstn[0]]
AggShockMrkvExample.cycles = 0
# Make a Cobb-Douglas economy for the agents
MrkvEconomyExample = CobbDouglasMarkovEconomy(agents=[AggShockMrkvExample])
MrkvEconomyExample.DampingFac = 0.2 # Turn down damping
MrkvEconomyExample.makeAggShkHist() # Simulate a history of aggregate shocks
AggShockMrkvExample.getEconomyData(
MrkvEconomyExample
) # Have the consumers inherit relevant objects from the economy
In [7]:
if solve_markov_micro:
# Solve the microeconomic model for the Markov aggregate shocks example type (and display results)
t_start = process_time()
AggShockMrkvExample.solve()
t_end = process_time()
print(
"Solving an aggregate shocks Markov consumer took "
+ mystr(t_end - t_start)
+ " seconds."
)
print(
"Consumption function at each aggregate market \
resources-to-labor ratio gridpoint (for each macro state):"
)
m_grid = np.linspace(0, 10, 200)
AggShockMrkvExample.unpackcFunc()
for i in range(2):
for M in AggShockMrkvExample.Mgrid.tolist():
mMin = AggShockMrkvExample.solution[0].mNrmMin[i](M)
c_at_this_M = AggShockMrkvExample.cFunc[0][i](
m_grid + mMin, M * np.ones_like(m_grid)
)
plt.plot(m_grid + mMin, c_at_this_M)
plt.ylim(0.0, None)
plt.show()
In [8]:
if solve_markov_market:
# Solve the "macroeconomic" model by searching for a "fixed point dynamic rule"
t_start = process_time()
print("Now solving a two-state Markov economy. This should take a few minutes...")
MrkvEconomyExample.solve()
t_end = process_time()
print(
'Solving the "macroeconomic" aggregate shocks model took '
+ str(t_end - t_start)
+ " seconds."
)
print(
"Consumption function at each aggregate market \
resources-to-labor ratio gridpoint (for each macro state):"
)
m_grid = np.linspace(0, 10, 200)
AggShockMrkvExample.unpackcFunc()
for i in range(2):
for M in AggShockMrkvExample.Mgrid.tolist():
mMin = AggShockMrkvExample.solution[0].mNrmMin[i](M)
c_at_this_M = AggShockMrkvExample.cFunc[0][i](
m_grid + mMin, M * np.ones_like(m_grid)
)
plt.plot(m_grid + mMin, c_at_this_M)
plt.ylim(0.0, None)
plt.show()
In [9]:
if solve_krusell_smith:
# Make a Krusell-Smith agent type
# NOTE: These agents aren't exactly like KS, as they don't have serially correlated unemployment
KSexampleType = deepcopy(AggShockMrkvExample)
KSexampleType.IncomeDstn[0] = [
DiscreteDistribution(
np.array([0.96, 0.04]),
[ np.array([1.0, 1.0]), np.array([1.0 / 0.96, 0.0])]
),
DiscreteDistribution(
np.array([0.90, 0.10]),
[np.array([1.0, 1.0]), np.array([1.0 / 0.90, 0.0])],
)
]
# Make a KS economy
KSeconomy = deepcopy(MrkvEconomyExample)
KSeconomy.agents = [KSexampleType]
KSeconomy.AggShkDstn = [
DiscreteDistribution(
np.array([1.0]),
[np.array([1.0]), np.array([1.05])],
),
DiscreteDistribution(
np.array([1.0]),
[np.array([1.0]), np.array([0.95])],
)
]
KSeconomy.PermGroFacAgg = [1.0, 1.0]
KSexampleType.getEconomyData(KSeconomy)
KSeconomy.makeAggShkHist()
# Solve the K-S model
t_start = process_time()
print(
"Now solving a Krusell-Smith-style economy. This should take about a minute..."
)
KSeconomy.solve()
t_end = process_time()
print("Solving the Krusell-Smith model took " + str(t_end - t_start) + " seconds.")
In [10]:
if solve_poly_state:
StateCount = 15 # Number of Markov states
GrowthAvg = 1.01 # Average permanent income growth factor
GrowthWidth = 0.02 # PermGroFacAgg deviates from PermGroFacAgg in this range
Persistence = 0.90 # Probability of staying in the same Markov state
PermGroFacAgg = np.linspace(
GrowthAvg - GrowthWidth, GrowthAvg + GrowthWidth, num=StateCount
)
# Make the Markov array with chosen states and persistence
PolyMrkvArray = np.zeros((StateCount, StateCount))
for i in range(StateCount):
for j in range(StateCount):
if i == j:
PolyMrkvArray[i, j] = Persistence
elif (i == (j - 1)) or (i == (j + 1)):
PolyMrkvArray[i, j] = 0.5 * (1.0 - Persistence)
PolyMrkvArray[0, 0] += 0.5 * (1.0 - Persistence)
PolyMrkvArray[StateCount - 1, StateCount - 1] += 0.5 * (1.0 - Persistence)
# Make a consumer type to inhabit the economy
PolyStateExample = AggShockMarkovConsumerType()
PolyStateExample.MrkvArray = PolyMrkvArray
PolyStateExample.PermGroFacAgg = PermGroFacAgg
PolyStateExample.IncomeDstn[0] = StateCount * [PolyStateExample.IncomeDstn[0]]
PolyStateExample.cycles = 0
# Make a Cobb-Douglas economy for the agents
# Use verbose=False to remove printing of intercept
PolyStateEconomy = CobbDouglasMarkovEconomy(agents=[PolyStateExample], verbose=False)
PolyStateEconomy.MrkvArray = PolyMrkvArray
PolyStateEconomy.PermGroFacAgg = PermGroFacAgg
PolyStateEconomy.PermShkAggStd = StateCount * [0.006]
PolyStateEconomy.TranShkAggStd = StateCount * [0.003]
PolyStateEconomy.slope_prev = StateCount * [1.0]
PolyStateEconomy.intercept_prev = StateCount * [0.0]
PolyStateEconomy.update()
PolyStateEconomy.makeAggShkDstn()
PolyStateEconomy.makeAggShkHist() # Simulate a history of aggregate shocks
PolyStateExample.getEconomyData(
PolyStateEconomy
) # Have the consumers inherit relevant objects from the economy
# Solve the many state model
t_start = process_time()
print(
"Now solving an economy with "
+ str(StateCount)
+ " Markov states. This might take a while..."
)
PolyStateEconomy.solve()
t_end = process_time()
print(
"Solving a model with "
+ str(StateCount)
+ " states took "
+ str(t_end - t_start)
+ " seconds."
)
In [ ]: