Skip to content

Commit

Permalink
ruff format the example notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
sbenthall committed Nov 13, 2023
1 parent 1a33be6 commit 62e8d5f
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions examples/MonteCarlo/Generic Monte Carlo Perfect Foresight.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
" \"AgentCount\": 3, # Number of agents of this type\n",
" \"T_sim\": 120, # Number of periods to simulate\n",
" \"aNrmInitMean\": -6.0, # Mean of log initial assets\n",
" \"aNrmInitStd\": 0, #1.0, # Standard deviation of log initial assets\n",
" \"aNrmInitStd\": 0, # 1.0, # Standard deviation of log initial assets\n",
" \"pLvlInitMean\": 0.0, # Mean of log initial permanent income\n",
" \"pLvlInitStd\": 0.0, # Standard deviation of log initial permanent income\n",
" \"PermGroFacAgg\": 1.0, # Aggregate permanent income growth factor\n",
" \"T_age\": None, # Age after which simulated agents are automatically killed,\n",
" \"LivPrb\": [0.98]\n",
" \"LivPrb\": [0.98],\n",
"}\n",
"\n",
"PFexample.assign_parameters(**SimulationParams)"
Expand Down Expand Up @@ -594,7 +594,7 @@
}
],
"source": [
"PFexample.track_vars = [\"who_dies\",\"mNrm\",\"pLvl\",\"aNrm\"]\n",
"PFexample.track_vars = [\"who_dies\", \"mNrm\", \"pLvl\", \"aNrm\"]\n",
"PFexample.make_shock_history()\n",
"\n",
"PFexample.initialize_sim()\n",
Expand Down Expand Up @@ -681,7 +681,12 @@
}
],
"source": [
"plt.plot(np.log(np.mean(PFexample.history[\"mNrm\"], axis=1) - np.min(np.mean(PFexample.history[\"mNrm\"], axis=1))))\n",
"plt.plot(\n",
" np.log(\n",
" np.mean(PFexample.history[\"mNrm\"], axis=1)\n",
" - np.min(np.mean(PFexample.history[\"mNrm\"], axis=1))\n",
" )\n",
")\n",
"plt.xlabel(\"Time\")\n",
"plt.ylabel(\"Mean normalized market resources\")\n",
"plt.show()"
Expand All @@ -705,7 +710,7 @@
}
],
"source": [
"PFexample.newborn_init_history['pLvl'][1,PFexample.history[\"who_dies\"][1,:] > 0].shape"
"PFexample.newborn_init_history[\"pLvl\"][1, PFexample.history[\"who_dies\"][1, :] > 0].shape"
]
},
{
Expand All @@ -726,7 +731,7 @@
}
],
"source": [
"PFexample.newborn_init_history['aNrm'][2, PFexample.history[\"who_dies\"][2,:] > 0].shape"
"PFexample.newborn_init_history[\"aNrm\"][2, PFexample.history[\"who_dies\"][2, :] > 0].shape"
]
},
{
Expand Down Expand Up @@ -791,19 +796,17 @@
"outputs": [],
"source": [
"pfn_simulator = AgentTypeMonteCarloSimulator(\n",
" pfn.model['parameters'],\n",
" pfn.model['shocks'],\n",
" pfn.model['dynamics'],\n",
" {\n",
" 'c_nrm' : lambda m_nrm: PFexample.solution[0].cFunc(m_nrm)\n",
" },\n",
" { # initial states\n",
" 'a_nrm' : Lognormal(-6, 0),\n",
" pfn.model[\"parameters\"],\n",
" pfn.model[\"shocks\"],\n",
" pfn.model[\"dynamics\"],\n",
" {\"c_nrm\": lambda m_nrm: PFexample.solution[0].cFunc(m_nrm)},\n",
" { # initial states\n",
" \"a_nrm\": Lognormal(-6, 0),\n",
" #'live' : 1,\n",
" 'p' : 1.0\n",
" \"p\": 1.0,\n",
" },\n",
" agent_count = 3,\n",
" T_sim = 120\n",
" agent_count=3,\n",
" T_sim=120,\n",
")"
]
},
Expand All @@ -815,9 +818,9 @@
"outputs": [],
"source": [
"pfn_simulator.read_shocks = True\n",
"#pfn_simulator.shock_history['live'] = 1 - np.roll(PFexample.history[\"who_dies\"], -1)\n",
"# pfn_simulator.shock_history['live'] = 1 - np.roll(PFexample.history[\"who_dies\"], -1)\n",
"\n",
"pfn_simulator.shock_history['live'] = 1 - PFexample.history[\"who_dies\"]"
"pfn_simulator.shock_history[\"live\"] = 1 - PFexample.history[\"who_dies\"]"
]
},
{
Expand All @@ -827,9 +830,9 @@
"metadata": {},
"outputs": [],
"source": [
"pfn_simulator.newborn_init_history['a_nrm'] = PFexample.newborn_init_history['aNrm']\n",
"pfn_simulator.newborn_init_history['p'] = PFexample.newborn_init_history['pLvl']\n",
"#pfn_simulator.newborn_init_history['live'] = PFexample.newborn_init_history['pLvl']"
"pfn_simulator.newborn_init_history[\"a_nrm\"] = PFexample.newborn_init_history[\"aNrm\"]\n",
"pfn_simulator.newborn_init_history[\"p\"] = PFexample.newborn_init_history[\"pLvl\"]\n",
"# pfn_simulator.newborn_init_history['live'] = PFexample.newborn_init_history['pLvl']"
]
},
{
Expand Down Expand Up @@ -1689,7 +1692,7 @@
}
],
"source": [
"#pf_simulator.track_vars = [\"mNrm\"]\n",
"# pf_simulator.track_vars = [\"mNrm\"]\n",
"pfn_simulator.initialize_sim()\n",
"pfn_simulator.simulate(sim_periods=120)"
]
Expand All @@ -1714,7 +1717,7 @@
}
],
"source": [
"plt.plot(np.mean(pfn_simulator.history['m_nrm'], axis=1))\n",
"plt.plot(np.mean(pfn_simulator.history[\"m_nrm\"], axis=1))\n",
"plt.xlabel(\"Time\")\n",
"plt.ylabel(\"Mean normalized market resources\")\n",
"plt.show()"
Expand Down Expand Up @@ -1748,7 +1751,12 @@
}
],
"source": [
"plt.plot(np.log(np.mean(pfn_simulator.history[\"m_nrm\"], axis=1) - np.min(np.mean(pfn_simulator.history[\"m_nrm\"], axis=1))))\n",
"plt.plot(\n",
" np.log(\n",
" np.mean(pfn_simulator.history[\"m_nrm\"], axis=1)\n",
" - np.min(np.mean(pfn_simulator.history[\"m_nrm\"], axis=1))\n",
" )\n",
")\n",
"plt.xlabel(\"Time\")\n",
"plt.ylabel(\"Mean normalized market resources\")\n",
"plt.show()"
Expand Down Expand Up @@ -1784,7 +1792,7 @@
}
],
"source": [
"plt.plot(pfn_simulator.history['live'].sum(axis=1))"
"plt.plot(pfn_simulator.history[\"live\"].sum(axis=1))"
]
},
{
Expand All @@ -1807,8 +1815,11 @@
}
],
"source": [
"plt.plot(np.mean(pfn_simulator.history['a_nrm'], axis=1), label = 'Generic monte carlo')\n",
"plt.plot(np.mean(PFexample.history[\"aNrm\"], axis=1), label = 'HARK 0.13 PerfForesightConsumerType')\n",
"plt.plot(np.mean(pfn_simulator.history[\"a_nrm\"], axis=1), label=\"Generic monte carlo\")\n",
"plt.plot(\n",
" np.mean(PFexample.history[\"aNrm\"], axis=1),\n",
" label=\"HARK 0.13 PerfForesightConsumerType\",\n",
")\n",
"plt.xlabel(\"Time\")\n",
"plt.ylabel(\"Mean normalized market resources\")\n",
"plt.legend()\n",
Expand Down Expand Up @@ -1840,7 +1851,9 @@
}
],
"source": [
"np.mean(pfn_simulator.history['a_nrm'], axis=1) - np.mean(PFexample.history[\"aNrm\"], axis=1)"
"np.mean(pfn_simulator.history[\"a_nrm\"], axis=1) - np.mean(\n",
" PFexample.history[\"aNrm\"], axis=1\n",
")"
]
},
{
Expand All @@ -1862,7 +1875,7 @@
}
],
"source": [
"np.where(pfn_simulator.history['live'] < 1)"
"np.where(pfn_simulator.history[\"live\"] < 1)"
]
},
{
Expand All @@ -1884,7 +1897,7 @@
}
],
"source": [
"np.where(1 - PFexample.history['who_dies'] < 1)"
"np.where(1 - PFexample.history[\"who_dies\"] < 1)"
]
},
{
Expand Down

0 comments on commit 62e8d5f

Please sign in to comment.