Utilities¶
General purpose / miscellaneous functions. Includes functions to approximate continuous distributions with discrete ones, utility functions (and their derivatives), manipulation of discrete distributions, and basic plotting tools.
- class HARK.utilities.NullFunc¶
Bases:
object
A trivial class that acts as a placeholder “do nothing” function.
- distance(other)¶
Trivial distance metric that only cares whether the other object is also an instance of NullFunc. Intentionally does not inherit from HARKobject as this might create dependency problems.
- Parameters:
other (any) – Any object for comparison to this instance of NullFunc.
- Returns:
(unnamed) – The distance between self and other. Returns 0 if other is also a NullFunc; otherwise returns an arbitrary high number.
- Return type:
- HARK.utilities.benchmark(agent_type, sort_by='tottime', max_print=10, filename='restats', return_output=False)¶
Profiling tool for HARK models. Calling benchmark on agents calls the solver for the agents and provides time to solve as well as the top max_print function calls in terms of sort_by. Optionally allows for saving a text copy of the profile as well as returning the Stats object for further inspection.
For more details on the python profilers, see https://docs.python.org/3/library/profile.html#the-stats-class
- Parameters:
- Returns:
stats – Profiling object with call statistics.
- Return type:
Stats (optional)
- HARK.utilities.calc_subpop_avg(data, reference, cutoffs, weights=None)¶
Calculates the average of (weighted) data between cutoff percentiles of a reference variable.
- Parameters:
- Returns:
The (weighted) average of data that falls within the cutoff percentiles of reference.
- Return type:
slice_avg
- HARK.utilities.calc_weighted_avg(data, weights)¶
Generates a weighted average of simulated data. The Nth row of data is averaged and then weighted by the Nth element of weights in an aggregate average.
- Parameters:
data (numpy.array) – An array of data with N rows of J floats
weights (numpy.array) – A length N array of weights for the N rows of data.
- Returns:
weighted_sum – The weighted sum of the data.
- Return type:
- HARK.utilities.construct_assets_grid(parameters)¶
Constructs the base grid of post-decision states, representing end-of-period assets above the absolute minimum.
All parameters are passed as attributes of the single input parameters. The input can be an instance of a ConsumerType, or a custom Parameters class.
- Parameters:
- Returns:
aXtraGrid – Base array of values for the post-decision-state grid.
- Return type:
np.ndarray
- HARK.utilities.determine_platform()¶
Untility function to return the platform currenlty in use.
- Returns:
pf – ‘darwin’ (MacOS), ‘debian’(debian Linux) or ‘win’ (windows)
- Return type:
- HARK.utilities.epanechnikov_kernel(x, ref_x, h=1.0)¶
The Epanechnikov kernel.
- HARK.utilities.files_in_dir(mypath)¶
- HARK.utilities.find_gui()¶
Quick fix to check if matplotlib is running in a GUI environment.
- Returns:
bool – True if it’s a GUI environment, False if not.
- Return type:
Boolean
- HARK.utilities.gen_tran_matrix_1D(dist_mGrid, bNext, shk_prbs, perm_shks, tran_shks, LivPrb, NewBornDist)¶
Computes Transition Matrix across normalized market resources. This function is built to non-stochastic simulate the IndShockConsumerType. This function is used exclusively when Harmenberg Neutral Measure is applied and/or if permanent income is not a state variable For more information, see https://econ-ark.org/materials/harmenberg-aggregation?launch
- Parameters:
dist_mGrid (np.array) – Grid over normalized market resources
bNext (np.array) – Grid over bank balances
shk_prbs (np.array) – Array of shock probabilities over combinations of permanent and transitory shocks
perm_shks (np.array) – Array of shocks to permanent income. Shocks should follow Harmenberg neutral measure
tran_shks (np.array) – Array of shocks to transitory
LivPrb (float) – Probability of not dying
NewBornDist (np.array) – array representing distribution of newborns across grid of normalized market resources and grid of permanent income.
- Returns:
TranMatrix – Transition Matrix over normalized market resources grid.
- Return type:
np.array
- HARK.utilities.gen_tran_matrix_2D(dist_mGrid, dist_pGrid, bNext, shk_prbs, perm_shks, tran_shks, LivPrb, NewBornDist)¶
Computes Transition Matrix over normalized market resources and permanent income. This function is built to non-stochastic simulate the IndShockConsumerType.
- Parameters:
dist_mGrid (np.array) – Grid over normalized market resources
dist_pGrid (np.array) – Grid over permanent income
bNext (np.array) – Grid over bank balances
shk_prbs (np.array) – Array of shock probabilities over combinations of perm and tran shocks
perm_shks (np.array) – Array of shocks to permanent income
tran_shks (np.array) – Array of shocks to transitory income
LivPrb (float) – Probability of not dying
NewBornDist (np.array) – array representing distribution of newborns across grid of normalized market resources and grid of permanent income.
- Returns:
TranMatrix – Transition Matrix over normalized market resources grid and permanent income grid
- Return type:
np.array
- HARK.utilities.get_arg_names(function)¶
Returns a list of strings naming all of the arguments for the passed function.
- Parameters:
function (function) – A function whose argument names are wanted.
- Returns:
argNames – The names of the arguments of function.
- Return type:
[string]
Calculates the Lorenz curve at the requested percentiles of (weighted) data. Median by default.
- Parameters:
data (numpy.array) – A 1D array of float data.
weights (numpy.array) – A weighting vector for the data.
percentiles ([float]) – A list or numpy.array of percentiles to calculate for the data. Each element should be in (0,1).
presorted (boolean) – Indicator for whether data has already been sorted.
- Returns:
lorenz_out – The requested Lorenz curve points of the data.
- Return type:
numpy.array
- HARK.utilities.get_percentiles(data, weights=None, percentiles=None, presorted=False)¶
Calculates the requested percentiles of (weighted) data. Median by default.
- Parameters:
data (numpy.array) – A 1D array of float data.
weights (np.array) – A weighting vector for the data.
percentiles ([float]) – A list or numpy.array of percentiles to calculate for the data. Each element should be in (0,1).
presorted (boolean) – Indicator for whether data has already been sorted.
- Returns:
pctl_out – The requested percentiles of the data.
- Return type:
numpy.array
- HARK.utilities.in_ipynb()¶
If the ipython process contains ‘terminal’ assume not in a notebook.
- Returns:
bool – True if called from a jupyter notebook, else False
- Return type:
Boolean
- HARK.utilities.jump_to_grid_1D(m_vals, probs, Dist_mGrid)¶
Distributes values onto a predefined grid, maintaining the means.
- Parameters:
m_vals (np.array) – Market resource values
probs (np.array) – Shock probabilities associated with combinations of m_vals. Can be thought of as the probability mass function of (m_vals).
dist_mGrid (np.array) – Grid over normalized market resources
- Returns:
probGrid.flatten() – Probabilities of each gridpoint on the combined grid of market resources
- Return type:
np.array
- HARK.utilities.jump_to_grid_2D(m_vals, perm_vals, probs, dist_mGrid, dist_pGrid)¶
Distributes values onto a predefined grid, maintaining the means. m_vals and perm_vals are realizations of market resources and permanent income while dist_mGrid and dist_pGrid are the predefined grids of market resources and permanent income, respectively. That is, m_vals and perm_vals do not necesarily lie on their respective grids. Returns probabilities of each gridpoint on the combined grid of market resources and permanent income.
- Parameters:
m_vals (np.array) – Market resource values
perm_vals (np.array) – Permanent income values
probs (np.array) – Shock probabilities associated with combinations of m_vals and perm_vals. Can be thought of as the probability mass function of (m_vals, perm_vals).
dist_mGrid (np.array) – Grid over normalized market resources
dist_pGrid (np.array) – Grid over permanent income
- Returns:
probGrid.flatten() – Probabilities of each gridpoint on the combined grid of market resources and permanent income
- Return type:
np.array
- HARK.utilities.kernel_regression(x, y, bot=None, top=None, N=500, h=None)¶
Performs a non-parametric Nadaraya-Watson 1D kernel regression on given data with optionally specified range, number of points, and kernel bandwidth.
- Parameters:
x (np.array) – The independent variable in the kernel regression.
y (np.array) – The dependent variable in the kernel regression.
bot (float) – Minimum value of interest in the regression; defaults to min(x).
top (float) – Maximum value of interest in the regression; defaults to max(y).
N (int) – Number of points to compute.
h (float) – The bandwidth of the (Epanechnikov) kernel. To-do: GENERALIZE.
- Returns:
regression – A piecewise locally linear kernel regression: y = f(x).
- Return type:
- HARK.utilities.make_figs(figure_name, saveFigs, drawFigs, target_dir='Figures')¶
Utility function to save figure in multiple formats and display the image.
- Parameters:
- HARK.utilities.make_grid_exp_mult(ming, maxg, ng, timestonest=20)¶
Make a multi-exponentially spaced grid.
- Parameters:
- Returns:
points (np.array) – A multi-exponentially spaced grid
Original Matab code can be found in Chris Carroll’s
[Solution Methods for Microeconomic Dynamic Optimization Problems]
(http (//www.econ2.jhu.edu/people/ccarroll/solvingmicrodsops/) toolkit.)
Latest update (01 May 2015)
- HARK.utilities.memoize(obj)¶
A decorator to (potentially) make functions more efficient.
With this decorator, functions will “remember” if they have been evaluated with given inputs before. If they have, they will “remember” the outputs that have already been calculated for those inputs, rather than calculating them again.
- HARK.utilities.mround(match)¶
- HARK.utilities.plot_funcs(functions, bottom, top, N=1000, legend_kwds=None)¶
Plots 1D function(s) over a given range.
- Parameters:
functions ([function] or function) – A single function, or a list of functions, to be plotted.
bottom (float) – The lower limit of the domain to be plotted.
top (float) – The upper limit of the domain to be plotted.
N (int) – Number of points in the domain to evaluate.
legend_kwds (None, or dictionary) – If not None, the keyword dictionary to pass to plt.legend
- Return type:
none
- HARK.utilities.plot_funcs_der(functions, bottom, top, N=1000, legend_kwds=None)¶
Plots the first derivative of 1D function(s) over a given range.
- Parameters:
function (function) – A function or list of functions, the derivatives of which are to be plotted.
bottom (float) – The lower limit of the domain to be plotted.
top (float) – The upper limit of the domain to be plotted.
N (int) – Number of points in the domain to evaluate.
legend_kwds (None, or dictionary) – If not None, the keyword dictionary to pass to plt.legend
- Return type:
none
- HARK.utilities.round_in_file(filename)¶
- HARK.utilities.setup_latex_env_notebook(pf, latexExists)¶
This is needed for use of the latex_envs notebook extension which allows the use of environments in Markdown.
- Parameters:
pf (str (platform)) – output of determine_platform()
- HARK.utilities.test_latex_installation(pf)¶
Test to check if latex is installed on the machine.
- Parameters:
pf (str (platform)) – output of determine_platform()
- Returns:
bool – True if latex found, else installed in the case of debian otherwise ImportError raised to direct user to install latex manually
- Return type:
Boolean