This page was generated from examples/ConsPortfolioModel/RiskyAssetConsumerType.ipynb.
Interactive online version: Binder badge. Download notebook.

Example Implementations of HARK.ConsumptionSaving.ConsRiskyAssetModel#

[5]:
from time import time

from HARK.ConsumptionSaving.ConsIndShockModel import (
    IndShockConsumerType,
    IndShockConsumerType_constructors_default,
)
from HARK.ConsumptionSaving.ConsPortfolioModel import (
    PortfolioConsumerType,
    init_portfolio,
)
from HARK.ConsumptionSaving.ConsRiskyAssetModel import (
    FixedPortfolioShareRiskyAssetConsumerType,
    RiskyAssetConsumerType,
    IndShockRiskyAssetConsumerType_constructor_default,
)
from HARK.utilities import plot_funcs, plot_funcs_der
[6]:
mystr = lambda number: f"{number:.4f}"

Idiosyncratic Income Shocks Consumer Type#

[7]:
# Make a dictionary for the idiosyncratic shocks type that uses common values from the portfolio type
common_dict = init_portfolio.copy()
common_dict["constructors"] = IndShockConsumerType_constructors_default
[8]:
# Make and solve an example consumer with idiosyncratic income shocks
# Use init_portfolio parameters to compare to results of PortfolioConsumerType
IndShockExample = IndShockConsumerType(**common_dict)
IndShockExample.cycles = 0  # Make this type have an infinite horizon
[9]:
start_time = time()
IndShockExample.solve()
end_time = time()
print(
    "Solving a consumer with idiosyncratic shocks took "
    + mystr(end_time - start_time)
    + " seconds.",
)
IndShockExample.unpack("cFunc")
Solving a consumer with idiosyncratic shocks took 0.4473 seconds.
[10]:
# Plot the consumption function and MPC for the infinite horizon consumer
print("Concave consumption function:")
plot_funcs(IndShockExample.cFunc[0], 0.0, 5.0)
print("Marginal consumption function:")
plot_funcs_der(IndShockExample.cFunc[0], 0.0, 5.0)
Concave consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_7_1.png
Marginal consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_7_3.png

Risky Return Consumer Type#

[11]:
# Make and solve an example consumer with risky returns to savings
# Use init_portfolio parameters to compare to results of PortfolioConsumerType
temp_dict = init_portfolio.copy()
temp_dict["constructors"] = IndShockRiskyAssetConsumerType_constructor_default
RiskyReturnExample = RiskyAssetConsumerType(**temp_dict)
RiskyReturnExample.cycles = 0  # Make this type have an infinite horizon
[12]:
start_time = time()
RiskyReturnExample.solve()
end_time = time()
print(
    "Solving a consumer with risky returns took "
    + mystr(end_time - start_time)
    + " seconds.",
)
RiskyReturnExample.unpack("cFunc")
Solving a consumer with risky returns took 0.4987 seconds.
[13]:
# Plot the consumption function and MPC for the risky asset consumer
print("Concave consumption function:")
plot_funcs(RiskyReturnExample.cFunc[0], 0.0, 5.0)
print("Marginal consumption function:")
plot_funcs_der(RiskyReturnExample.cFunc[0], 0.0, 5.0)
Concave consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_11_1.png
Marginal consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_11_3.png

Compare Idiosyncratic Income Shocks with Risky Return#

[14]:
# Compare the consumption functions for the various agents in this notebook.
print("Consumption functions for idiosyncratic shocks vs risky returns:")
plot_funcs(
    [
        IndShockExample.cFunc[0],  # blue
        RiskyReturnExample.cFunc[0],  # orange
    ],
    0.0,
    20.0,
)
Consumption functions for idiosyncratic shocks vs risky returns:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_13_1.png

Risky Return Consumer Type with Portfolio Choice#

[15]:
# Make and solve an example risky consumer with a portfolio choice
temp_dict["PortfolioBool"] = True
PortfolioChoiceExample = RiskyAssetConsumerType(**temp_dict)
PortfolioChoiceExample.cycles = 0  # Make this type have an infinite horizon
[16]:
start_time = time()
PortfolioChoiceExample.solve()
end_time = time()
print(
    "Solving a consumer with risky returns and portfolio choice took "
    + mystr(end_time - start_time)
    + " seconds.",
)
PortfolioChoiceExample.unpack("cFunc")
PortfolioChoiceExample.unpack("ShareFunc")
Solving a consumer with risky returns and portfolio choice took 4.1953 seconds.
[17]:
# Plot the consumption function and MPC for the portfolio choice consumer
print("Concave consumption function:")
plot_funcs(PortfolioChoiceExample.cFunc[0], 0.0, 5.0)
print("Marginal consumption function:")
plot_funcs_der(PortfolioChoiceExample.cFunc[0], 0.0, 5.0)
Concave consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_17_1.png
Marginal consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_17_3.png

Compare Income Shocks, Risky Return, and RR w/ Portfolio Choice#

[18]:
# Compare the consumption functions for the various agents in this notebook.
print(
    "Consumption functions for idiosyncratic shocks vs risky returns vs portfolio choice:",
)
plot_funcs(
    [
        IndShockExample.cFunc[0],  # blue
        RiskyReturnExample.cFunc[0],  # orange
        PortfolioChoiceExample.cFunc[0],  # green
    ],
    0.0,
    20.0,
)
Consumption functions for idiosyncratic shocks vs risky returns vs portfolio choice:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_19_1.png

Portfolio Consumer Type#

[19]:
# Make and solve an example portfolio choice consumer
PortfolioTypeExample = PortfolioConsumerType(**init_portfolio)  # default parameters
PortfolioTypeExample.cycles = 0  # Make this type has an infinite horizon
[20]:
start_time = time()
PortfolioTypeExample.solve()
end_time = time()
print(
    "Solving a consumer with portfolio choice took "
    + mystr(end_time - start_time)
    + " seconds.",
)
PortfolioTypeExample.unpack("cFuncAdj")
PortfolioTypeExample.unpack("ShareFuncAdj")
Solving a consumer with portfolio choice took 10.7557 seconds.
[21]:
# Plot the consumption function and MPC for the portfolio choice consumer
print("Concave consumption function:")
plot_funcs(PortfolioTypeExample.cFuncAdj[0], 0.0, 5.0)
print("Marginal consumption function:")
plot_funcs_der(PortfolioTypeExample.cFuncAdj[0], 0.0, 5.0)
Concave consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_23_1.png
Marginal consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_23_3.png

Compare RR w/ Portfolio Choice with Portfolio Choice Type#

[22]:
# Compare the consumption functions for the various portfolio choice types.
print(
    "Consumption functions for portfolio choice type vs risky asset with portfolio choice:",
)
plot_funcs(
    [
        PortfolioTypeExample.cFuncAdj[0],  # blue
        PortfolioChoiceExample.cFunc[0],  # orange
    ],
    0.0,
    20.0,
)
Consumption functions for portfolio choice type vs risky asset with portfolio choice:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_25_1.png
[23]:
# Compare the share functions for the various portfolio choice types.
print("Share functions for portfolio choice type vs risky asset with portfolio choice:")
plot_funcs(
    [
        PortfolioTypeExample.ShareFuncAdj[0],  # blue
        PortfolioChoiceExample.ShareFunc[0],  # orange
    ],
    0,
    200,
)
Share functions for portfolio choice type vs risky asset with portfolio choice:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_26_1.png

Risky Return Given Fixed Portfolio Share#

[24]:
FixedShareExample = FixedPortfolioShareRiskyAssetConsumerType(**temp_dict)
FixedShareExample.cycles = 0
[25]:
start_time = time()
FixedShareExample.solve()
end_time = time()
print(
    "Solving a consumer with fixed portfolio share took "
    + mystr(end_time - start_time)
    + " seconds.",
)
FixedShareExample.unpack("cFunc")
Solving a consumer with fixed portfolio share took 0.7053 seconds.
[26]:
# Plot the consumption function and MPC for the infinite horizon consumer
print("Concave consumption function:")
plot_funcs(FixedShareExample.cFunc[0], 0.0, 5.0)
print("Marginal consumption function:")
plot_funcs_der(FixedShareExample.cFunc[0], 0.0, 5.0)
Concave consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_30_1.png
Marginal consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_30_3.png

Compare Idiosyncratic Shock Type with Fixed Share at 0.0 Type#

[27]:
# Compare the consumption functions for the various idiosyncratic shocks
print("Consumption functions for idiosyncratic shocks vs fixed share at 0.0:")
plot_funcs(
    [
        IndShockExample.cFunc[0],  # blue
        FixedShareExample.cFunc[0],  # orange
    ],
    0.0,
    20.0,
)
Consumption functions for idiosyncratic shocks vs fixed share at 0.0:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_32_1.png

Fixed Share at 1.0 Type#

[28]:
temp_dict["RiskyShareFixed"] = [1.0]
RiskyFixedExample = FixedPortfolioShareRiskyAssetConsumerType(**temp_dict)
RiskyFixedExample.cycles = 0
[29]:
start_time = time()
RiskyFixedExample.solve()
end_time = time()
print(
    "Solving a consumer with share fixed at 1.0 took "
    + mystr(end_time - start_time)
    + " seconds.",
)
RiskyFixedExample.unpack("cFunc")
Solving a consumer with share fixed at 1.0 took 0.6070 seconds.
[30]:
# Plot the consumption function and MPC for the portfolio choice consumer
print("Concave consumption function:")
plot_funcs(RiskyFixedExample.cFunc[0], 0.0, 5.0)
print("Marginal consumption function:")
plot_funcs_der(RiskyFixedExample.cFunc[0], 0.0, 5.0)
Concave consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_36_1.png
Marginal consumption function:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_36_3.png

Compare Fixed Share at 1.0 Type with Risky Return Type#

[31]:
# Compare the consumption functions for the various risky shocks
print("Consumption functions for risky asset vs fixed share at 1.0:")
plot_funcs(
    [
        RiskyReturnExample.cFunc[0],  # blue
        RiskyFixedExample.cFunc[0],  # orange
    ],
    0.0,
    200.0,
)
Consumption functions for risky asset vs fixed share at 1.0:
../../_images/examples_ConsPortfolioModel_RiskyAssetConsumerType_38_1.png