Skip to content

Commit

Permalink
Merge pull request #974 from sbenthall/v1.0.0-cleanup
Browse files Browse the repository at this point in the history
PEP8 updates to examples for v1.0.0
  • Loading branch information
sbenthall authored Feb 28, 2021
2 parents 76051a3 + 6c7f40f commit 799943e
Show file tree
Hide file tree
Showing 22 changed files with 167 additions and 167 deletions.
10 changes: 5 additions & 5 deletions examples/ConsIndShockModel/Finite Cyclical Test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"source": [
"# Initial imports and notebook setup, click arrow to show\n",
"from HARK.ConsumptionSaving.ConsIndShockModel import IndShockConsumerType\n",
"from HARK.utilities import plotFuncsDer, plotFuncs\n",
"from HARK.utilities import plot_funcs_der, plot_funcs\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"mystr = lambda number : \"{:.4f}\".format(number)"
Expand Down Expand Up @@ -74,7 +74,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"unpackcFunc is deprecated and it will soon be removed, please use unpack('cFunc') instead.\n"
"unpack_cFunc is deprecated and it will soon be removed, please use unpack('cFunc') instead.\n"
]
},
{
Expand Down Expand Up @@ -102,10 +102,10 @@
"CyclicalExample.cycles = 0 # Make this consumer type have an infinite horizon\n",
"CyclicalExample.solve()\n",
"\n",
"CyclicalExample.unpackcFunc()\n",
"CyclicalExample.unpack_cFunc()\n",
"print('Quarterly consumption functions:')\n",
"mMin = min([X.mNrmMin for X in CyclicalExample.solution])\n",
"plotFuncs(CyclicalExample.cFunc,mMin,5)"
"plot_funcs(CyclicalExample.cFunc,mMin,5)"
]
},
{
Expand Down Expand Up @@ -144,7 +144,7 @@
"FiniteCyclicalExample.unpack('cFunc')\n",
"print('Quarterly consumption functions:')\n",
"mMin = min([X.mNrmMin for X in FiniteCyclicalExample.solution])\n",
"plotFuncs(FiniteCyclicalExample.cFunc,mMin,5)"
"plot_funcs(FiniteCyclicalExample.cFunc,mMin,5)"
]
},
{
Expand Down
22 changes: 11 additions & 11 deletions examples/ConsumptionSaving/example_ConsAggShockModel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@
"\n",
" # Make a Cobb-Douglas economy for the agents\n",
" EconomyExample = CobbDouglasEconomy(agents=[AggShockExample])\n",
" EconomyExample.makeAggShkHist() # Simulate a history of aggregate shocks\n",
" EconomyExample.make_AggShkHist() # Simulate a history of aggregate shocks\n",
"\n",
" # Have the consumers inherit relevant objects from the economy\n",
" AggShockExample.getEconomyData(EconomyExample)"
" AggShockExample.get_economy_data(EconomyExample)"
]
},
{
Expand Down Expand Up @@ -136,13 +136,13 @@
},
{
"ename": "NameError",
"evalue": "name 'plotFuncs' is not defined",
"evalue": "name 'plot_funcs' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-8f9086566e0b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Aggregate savings as a function of aggregate market resources:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mplotFuncs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mEconomyExample\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mAFunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mEconomyExample\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkSS\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m print(\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\"Consumption function at each aggregate market resources gridpoint (in general equilibrium):\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'plotFuncs' is not defined"
"\u001b[0;32m<ipython-input-5-8f9086566e0b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Aggregate savings as a function of aggregate market resources:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mplot_funcs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mEconomyExample\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mAFunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mEconomyExample\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkSS\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m print(\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\"Consumption function at each aggregate market resources gridpoint (in general equilibrium):\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'plot_funcs' is not defined"
]
}
],
Expand Down Expand Up @@ -199,8 +199,8 @@
" # Make a Cobb-Douglas economy for the agents\n",
" MrkvEconomyExample = CobbDouglasMarkovEconomy(agents=[AggShockMrkvExample])\n",
" MrkvEconomyExample.DampingFac = 0.2 # Turn down damping\n",
" MrkvEconomyExample.makeAggShkHist() # Simulate a history of aggregate shocks\n",
" AggShockMrkvExample.getEconomyData(\n",
" MrkvEconomyExample.make_AggShkHist() # Simulate a history of aggregate shocks\n",
" AggShockMrkvExample.get_economy_data(\n",
" MrkvEconomyExample\n",
" ) # Have the consumers inherit relevant objects from the economy"
]
Expand Down Expand Up @@ -309,8 +309,8 @@
" )\n",
" ]\n",
" KSeconomy.PermGroFacAgg = [1.0, 1.0]\n",
" KSexampleType.getEconomyData(KSeconomy)\n",
" KSeconomy.makeAggShkHist()\n",
" KSexampleType.get_economy_data(KSeconomy)\n",
" KSeconomy.make_AggShkHist()\n",
"\n",
" # Solve the K-S model\n",
" t_start = process_time()\n",
Expand Down Expand Up @@ -366,8 +366,8 @@
" PolyStateEconomy.intercept_prev = StateCount * [0.0]\n",
" PolyStateEconomy.update()\n",
" PolyStateEconomy.makeAggShkDstn()\n",
" PolyStateEconomy.makeAggShkHist() # Simulate a history of aggregate shocks\n",
" PolyStateExample.getEconomyData(\n",
" PolyStateEconomy.make_AggShkHist() # Simulate a history of aggregate shocks\n",
" PolyStateExample.get_economy_data(\n",
" PolyStateEconomy\n",
" ) # Have the consumers inherit relevant objects from the economy\n",
"\n",
Expand Down
16 changes: 8 additions & 8 deletions examples/ConsumptionSaving/example_ConsAggShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def mystr(number):

# Make a Cobb-Douglas economy for the agents
EconomyExample = CobbDouglasEconomy(agents=[AggShockExample])
EconomyExample.makeAggShkHist() # Simulate a history of aggregate shocks
EconomyExample.make_AggShkHist() # Simulate a history of aggregate shocks

# Have the consumers inherit relevant objects from the economy
AggShockExample.getEconomyData(EconomyExample)
AggShockExample.get_economy_data(EconomyExample)

if solve_agg_shocks_micro:
# Solve the microeconomic model for the aggregate shocks example type (and display results)
Expand Down Expand Up @@ -103,8 +103,8 @@ def mystr(number):
# 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.make_AggShkHist() # Simulate a history of aggregate shocks
AggShockMrkvExample.get_economy_data(
MrkvEconomyExample
) # Have the consumers inherit relevant objects from the economy

Expand Down Expand Up @@ -192,8 +192,8 @@ def mystr(number):
)
]
KSeconomy.PermGroFacAgg = [1.0, 1.0]
KSexampleType.getEconomyData(KSeconomy)
KSeconomy.makeAggShkHist()
KSexampleType.get_economy_data(KSeconomy)
KSeconomy.make_AggShkHist()

# Solve the K-S model
t_start = process_time()
Expand Down Expand Up @@ -242,8 +242,8 @@ def mystr(number):
PolyStateEconomy.intercept_prev = StateCount * [0.0]
PolyStateEconomy.update()
PolyStateEconomy.makeAggShkDstn()
PolyStateEconomy.makeAggShkHist() # Simulate a history of aggregate shocks
PolyStateExample.getEconomyData(
PolyStateEconomy.make_AggShkHist() # Simulate a history of aggregate shocks
PolyStateExample.get_economy_data(
PolyStateEconomy
) # Have the consumers inherit relevant objects from the economy

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"from HARK.utilities import plotFuncs\n",
"from HARK.utilities import plot_funcs\n",
"from time import process_time\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
Expand Down Expand Up @@ -224,7 +224,7 @@
" \"Consumption function for normalized problem (without explicit permanent income):\"\n",
")\n",
"mNrmMin = NormalizedExample.solution[0].mNrmMin\n",
"plotFuncs(NormalizedExample.solution[0].cFunc, mNrmMin, mNrmMin + 20)"
"plot_funcs(NormalizedExample.solution[0].cFunc, mNrmMin, mNrmMin + 20)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/ConsumptionSaving/example_ConsGenIncProcessModel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# %%
from HARK.utilities import plotFuncs
from HARK.utilities import plot_funcs
from time import process_time
import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -95,7 +95,7 @@ def mystr(number):
"Consumption function for normalized problem (without explicit permanent income):"
)
mNrmMin = NormalizedExample.solution[0].mNrmMin
plotFuncs(NormalizedExample.solution[0].cFunc, mNrmMin, mNrmMin + 20)
plot_funcs(NormalizedExample.solution[0].cFunc, mNrmMin, mNrmMin + 20)

# %% [markdown]
# The "explicit permanent income" solution deviates from the solution to the normalized problem because
Expand Down
20 changes: 10 additions & 10 deletions examples/ConsumptionSaving/example_ConsIndShock.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
" init_lifecycle,\n",
" init_cyclical\n",
")\n",
"from HARK.utilities import plotFuncsDer, plotFuncs\n",
"from HARK.utilities import plot_funcs_der, plot_funcs\n",
"from time import time"
]
},
Expand Down Expand Up @@ -103,7 +103,7 @@
"# Plot the perfect foresight consumption function\n",
"print(\"Perfect foresight consumption function:\")\n",
"mMin = PFexample.solution[0].mNrmMin\n",
"plotFuncs(PFexample.cFunc[0], mMin, mMin + 10)"
"plot_funcs(PFexample.cFunc[0], mMin, mMin + 10)"
]
},
{
Expand Down Expand Up @@ -218,9 +218,9 @@
"source": [
"# Plot the consumption function and MPC for the infinite horizon consumer\n",
"print(\"Concave consumption function:\")\n",
"plotFuncs(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)\n",
"plot_funcs(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)\n",
"print(\"Marginal consumption function:\")\n",
"plotFuncsDer(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)"
"plot_funcs_der(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)"
]
},
{
Expand Down Expand Up @@ -252,7 +252,7 @@
"# Compare the consumption functions for the perfect foresight and idiosyncratic\n",
"# shock types. Risky income cFunc asymptotically approaches perfect foresight cFunc.\n",
"print(\"Consumption functions for perfect foresight vs idiosyncratic shocks:\")\n",
"plotFuncs(\n",
"plot_funcs(\n",
" [PFexample.cFunc[0], IndShockExample.cFunc[0]],\n",
" IndShockExample.solution[0].mNrmMin,\n",
" 100,\n",
Expand All @@ -268,7 +268,7 @@
"# Compare the value functions for the two types\n",
"if IndShockExample.vFuncBool:\n",
" print(\"Value functions for perfect foresight vs idiosyncratic shocks:\")\n",
" plotFuncs(\n",
" plot_funcs(\n",
" [PFexample.solution[0].vFunc, IndShockExample.solution[0].vFunc],\n",
" IndShockExample.solution[0].mNrmMin + 0.5,\n",
" 10,\n",
Expand Down Expand Up @@ -355,7 +355,7 @@
"mMin = min(\n",
" [LifecycleExample.solution[t].mNrmMin for t in range(LifecycleExample.T_cycle)]\n",
")\n",
"plotFuncs(LifecycleExample.cFunc[: LifecycleExample.T_retire], mMin, 5)"
"plot_funcs(LifecycleExample.cFunc[: LifecycleExample.T_retire], mMin, 5)"
]
},
{
Expand Down Expand Up @@ -386,7 +386,7 @@
"source": [
"# Plot the consumption functions during retirement\n",
"print(\"Consumption functions while retired:\")\n",
"plotFuncs(LifecycleExample.cFunc[LifecycleExample.T_retire :], 0, 5)"
"plot_funcs(LifecycleExample.cFunc[LifecycleExample.T_retire :], 0, 5)"
]
},
{
Expand Down Expand Up @@ -474,7 +474,7 @@
"# Plot the consumption functions for the cyclical consumer type\n",
"print(\"Quarterly consumption functions:\")\n",
"mMin = min([X.mNrmMin for X in CyclicalExample.solution])\n",
"plotFuncs(CyclicalExample.cFunc, mMin, 5)"
"plot_funcs(CyclicalExample.cFunc, mMin, 5)"
]
},
{
Expand Down Expand Up @@ -514,7 +514,7 @@
"print(\"Solving a kinky consumer took \" + mystr(end_time - start_time) + \" seconds.\")\n",
"KinkyExample.unpack('cFunc')\n",
"print(\"Kinky consumption function:\")\n",
"plotFuncs(KinkyExample.cFunc[0], KinkyExample.solution[0].mNrmMin, 5)"
"plot_funcs(KinkyExample.cFunc[0], KinkyExample.solution[0].mNrmMin, 5)"
]
},
{
Expand Down
20 changes: 10 additions & 10 deletions examples/ConsumptionSaving/example_ConsIndShock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
init_lifecycle,
init_cyclical
)
from HARK.utilities import plotFuncsDer, plotFuncs
from HARK.utilities import plot_funcs_der, plot_funcs
from time import time

mystr = lambda number: "{:.4f}".format(number)
Expand All @@ -31,7 +31,7 @@
# Plot the perfect foresight consumption function
print("Perfect foresight consumption function:")
mMin = PFexample.solution[0].mNrmMin
plotFuncs(PFexample.cFunc[0], mMin, mMin + 10)
plot_funcs(PFexample.cFunc[0], mMin, mMin + 10)

if do_simulation:
PFexample.T_sim = 120 # Set number of simulation periods
Expand All @@ -55,14 +55,14 @@

# Plot the consumption function and MPC for the infinite horizon consumer
print("Concave consumption function:")
plotFuncs(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)
plot_funcs(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)
print("Marginal consumption function:")
plotFuncsDer(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)
plot_funcs_der(IndShockExample.cFunc[0], IndShockExample.solution[0].mNrmMin, 5)

# Compare the consumption functions for the perfect foresight and idiosyncratic
# shock types. Risky income cFunc asymptotically approaches perfect foresight cFunc.
print("Consumption functions for perfect foresight vs idiosyncratic shocks:")
plotFuncs(
plot_funcs(
[PFexample.cFunc[0], IndShockExample.cFunc[0]],
IndShockExample.solution[0].mNrmMin,
100,
Expand All @@ -71,7 +71,7 @@
# Compare the value functions for the two types
if IndShockExample.vFuncBool:
print("Value functions for perfect foresight vs idiosyncratic shocks:")
plotFuncs(
plot_funcs(
[PFexample.solution[0].vFunc, IndShockExample.solution[0].vFunc],
IndShockExample.solution[0].mNrmMin + 0.5,
10,
Expand Down Expand Up @@ -102,11 +102,11 @@
mMin = min(
[LifecycleExample.solution[t].mNrmMin for t in range(LifecycleExample.T_cycle)]
)
plotFuncs(LifecycleExample.cFunc[: LifecycleExample.T_retire], mMin, 5)
plot_funcs(LifecycleExample.cFunc[: LifecycleExample.T_retire], mMin, 5)

# Plot the consumption functions during retirement
print("Consumption functions while retired:")
plotFuncs(LifecycleExample.cFunc[LifecycleExample.T_retire :], 0, 5)
plot_funcs(LifecycleExample.cFunc[LifecycleExample.T_retire :], 0, 5)

# Simulate some data; results stored in mNrmNow_hist, cNrmNow_hist, pLvlNow_hist, and t_age_hist
if do_simulation:
Expand All @@ -129,7 +129,7 @@
# Plot the consumption functions for the cyclical consumer type
print("Quarterly consumption functions:")
mMin = min([X.mNrmMin for X in CyclicalExample.solution])
plotFuncs(CyclicalExample.cFunc, mMin, 5)
plot_funcs(CyclicalExample.cFunc, mMin, 5)

# Simulate some data; results stored in cHist, mHist, bHist, aHist, MPChist, and pHist
if do_simulation:
Expand All @@ -148,7 +148,7 @@
print("Solving a kinky consumer took " + mystr(end_time - start_time) + " seconds.")
KinkyExample.unpack('cFunc')
print("Kinky consumption function:")
plotFuncs(KinkyExample.cFunc[0], KinkyExample.solution[0].mNrmMin, 5)
plot_funcs(KinkyExample.cFunc[0], KinkyExample.solution[0].mNrmMin, 5)

if do_simulation:
KinkyExample.T_sim = 120
Expand Down
58 changes: 29 additions & 29 deletions examples/ConsumptionSaving/example_ConsMarkovModel.ipynb

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions examples/ConsumptionSaving/example_ConsMarkovModel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# %%
from HARK.utilities import plotFuncs
from HARK.utilities import plot_funcs
from time import process_time
from copy import deepcopy, copy
import numpy as np
Expand Down Expand Up @@ -132,7 +132,7 @@
]

# %% [markdown]
# Note that $\texttt{MarkovConsumerType}$ currently has no method to automatically construct a valid IncomeDstn - $\texttt{IncomeDstn}$ is manually constructed in each case. Writing a method to supersede $\texttt{IndShockConsumerType.updateIncomeProcess}$ for the “Markov model” would be a welcome contribution!
# Note that $\texttt{MarkovConsumerType}$ currently has no method to automatically construct a valid IncomeDstn - $\texttt{IncomeDstn}$ is manually constructed in each case. Writing a method to supersede $\texttt{IndShockConsumerType.update_income_process}$ for the “Markov model” would be a welcome contribution!

# %%
# Interest factor, permanent growth rates, and survival probabilities are constant arrays
Expand All @@ -153,10 +153,10 @@
+ " seconds."
)
print("Consumption functions for each discrete state:")
plotFuncs(SerialUnemploymentExample.solution[0].cFunc, 0, 50)
plot_funcs(SerialUnemploymentExample.solution[0].cFunc, 0, 50)
if SerialUnemploymentExample.vFuncBool:
print("Value functions for each discrete state:")
plotFuncs(SerialUnemploymentExample.solution[0].vFunc, 5, 50)
plot_funcs(SerialUnemploymentExample.solution[0].vFunc, 5, 50)

# %%
# Simulate some data; results stored in cHist, mNrmNow_hist, cNrmNow_hist, and MrkvNow_hist
Expand Down Expand Up @@ -241,7 +241,7 @@
)
print("Consumption functions for each discrete state:")
mNrmMin = np.min([ImmunityExample.solution[0].mNrmMin[j] for j in range(StateCount)])
plotFuncs(ImmunityExample.solution[0].cFunc, mNrmMin, 10)
plot_funcs(ImmunityExample.solution[0].cFunc, mNrmMin, 10)

# %% [markdown]
# ### 3. Serial permanent income growth
Expand Down Expand Up @@ -305,7 +305,7 @@
+ " seconds."
)
print("Consumption functions for each discrete state:")
plotFuncs(SerialGroExample.solution[0].cFunc, 0, 10)
plot_funcs(SerialGroExample.solution[0].cFunc, 0, 10)

# %%
# Make a consumer with serially correlated interest factors
Expand All @@ -328,4 +328,4 @@
+ " seconds."
)
print("Consumption functions for each discrete state:")
plotFuncs(SerialRExample.solution[0].cFunc, 0, 10)
plot_funcs(SerialRExample.solution[0].cFunc, 0, 10)
Loading

0 comments on commit 799943e

Please sign in to comment.