Documentation¶
grgrlib.plots¶
A collection of handy plotting functions.
-
grgrlib.plots.axformater(ax, mode='rotate')¶ Rotate ax as in autofmt_xdate
-
grgrlib.plots.bifplot(y, X=None, plot_dots=None, ax=None, markersize=0.02, color='k', ylabel=None, xlabel=None)¶ A bifurcation diagram
(add further documentation)
-
grgrlib.plots.figurator(nrows=2, ncols=2, nfigs=1, tight_layout=True, format_date=True, **args)¶ Create list of figures and axes with (potentially) more than one graph
Parameters: - nrows (int, optional) – Number of rows per figure, defaults to 2
- ncols (int, optional) – Number of cols per figure, defaults to 2
- nfigs (int, optional) – Number of figures, defaults to 1
- args (keyword arguments, optional) – keyword arguments that will be forwarded to matplotlib.pyplot.subplots
Returns: fig, ax – A tuple of two lists: the first list are all figure handlers, the second is a list of all the axis
Return type: list, list
-
grgrlib.plots.grbar3d(x, bounds=None, xedges=None, yedges=None, width=1, depth=1, colors=None, cmap=None, ax=None, figsize=None, unset_pane_color=True, **kwargs)¶
-
grgrlib.plots.grheat(X, bounds, xlabel=None, ylabel=None, zlabel=None, ax=None, draw_colorbar=None, cmap=None, **kwargs)¶ Simple interface to a heatmap (uses matplotlib’s imshow).
Parameters: - X (numpy.array) – a matrix-like object
- bounds (float or tuple) – the bounds of the grid. If a float, -/+ this value is taken as the bounds
- xlabel (str (optional)) –
- ylabel (str (optional)) –
- zlabel (str (optional)) –
- ax (array (optional)) –
- draw_colorbar (bool (optional)) –
- cmap (matplotlib colormap (optional)) –
- kwargs (any argument passet forward to imshow) –
-
grgrlib.plots.grhist2d(x, y=None, bins=10, ax=None, alpha=None)¶
-
grgrlib.plots.grplot(X, xscale=None, labels=None, title='', styles=None, colors=None, legend=None, bulk_plot=False, ax=None, fig=None, figsize=None, nlocbins=None, sigma=0.05, alpha=None, tol=1e-08, stat=<sphinx.ext.autodoc.importer._MockObject object>, **plotargs)¶
-
grgrlib.plots.pplot(X, xscale=None, labels=None, title='', styles=None, colors=None, legend=None, bulk_plot=False, ax=None, fig=None, figsize=None, nlocbins=None, sigma=0.05, alpha=None, tol=1e-08, stat=<sphinx.ext.autodoc.importer._MockObject object>, **plotargs)¶
-
grgrlib.plots.save_png2pdf(fig, path, **args)¶ Save as a .png and use unix convert to convert to PDF.
-
grgrlib.plots.spy(M, ax=None, cmap='inferno')¶ Visualize a matrix nicely
-
grgrlib.plots.wunstify(figs, axs)¶
grgrlib.jaxed¶
grgrlib.njitted¶
-
grgrlib.njitted.cholesky(A)¶ Performs a Cholesky decomposition of on symmetric, pos-def A. Returns lower-triangular L (full sized, zeroed above diag)
-
grgrlib.njitted.histogram_weights(a, bins, weights)¶
-
grgrlib.njitted.householder_reflection(x)¶
-
grgrlib.njitted.householder_reflection_right(x)¶
-
grgrlib.njitted.mvn_logpdf(x, mean, cov)¶ log-PDF of multivariate normal
-
grgrlib.njitted.normal_cdf(x, mu, sig)¶
-
grgrlib.njitted.normal_pdf(x, mu, sig)¶
-
grgrlib.njitted.numba_rand_norm(loc=0, scale=1, size=1)¶ A numba interface to create the user experience of np.random.normal with the size argument.
-
grgrlib.njitted.psd_func(M)¶ Compute the symmetric eigendecomposition
Note that eigh takes care of array conversion, chkfinite, and assertion that the matrix is square.
-
grgrlib.njitted.shredder(M, pivoting=None, tol=1e-11, verbose=False)¶ The QS decomposition from “Efficient Solution of Models with Occasionally Binding Constraints” (Gregor Boehl)
The QS decomposition uses Householder reflections to bring a system in the row-echolon form. This is a dispatcher for the sub-functions.
-
grgrlib.njitted.shredder_basic(M, tol, verbose)¶
-
grgrlib.njitted.shredder_non_pivoting(M, tol, verbose)¶ The DS-decomposition with anti-pivoting
-
grgrlib.njitted.shredder_pivoting(M, tol, verbose)¶ The DS-decomposition with pivoting
-
grgrlib.njitted.subt(A, B)¶
grgrlib.datatools¶
-
grgrlib.datatools.sdw_parser(x)¶ Tries to encode the date format used by the ECB statistical data warehouse (SDW). To be used for the argument date_parser in pandas read_csv
grgrlib.multiprocessing¶
-
class
grgrlib.multiprocessing.JoblibPoolDummy(func=None, njobs=None, **kwargs)¶ Bases:
objectjoblib Parallel workers pool pretending to behave like a multiprocessing pool
-
close()¶
-
map(func, iterable)¶
-
vectorized_func(iterable)¶
-
-
grgrlib.multiprocessing.serializer(*functions)¶ Serialize functions to use multiprocessing
-
grgrlib.multiprocessing.serializer_unix(func)¶ Dirty hack that transforms the non-serializable function to a serializable one (when using dill) … Don’t try that at home!
grgrlib.optimize¶
Hand-written optimizers. Currently only CMA-ES. Look for the documentation of cmaes (the UI) to understand its usage.
-
class
grgrlib.optimize.BestSolution(x=None, f=None, evals=None)¶ Bases:
objectContainer to keep track of the best solution seen.
-
all¶ (x, f, evals)of the best seen solution.
-
update(x, f, evals=None)¶ Update the best solution if
f < self.f.
-
-
class
grgrlib.optimize.CMAES(xstart, sigma, popsize, biject, verbose, **args)¶ Bases:
object-
disp(verb_modulo=1)¶ print well-formated iteration info
-
result¶
-
run()¶ update the evolution paths and the distribution parameters m, sigma, and C within CMA-ES.
-
stop()¶ Check termination criteria and return string.
-
-
class
grgrlib.optimize.CMAESParameters(ndim, popsize, mu=None, mu_mean=None, cc=None, cs=None, c1=None, cmu=None, fatol=None, frtol=None, xtol=None, maxfev=None, active=None, scaled=False, ld_rule=None, verbose=True, debug=False)¶ Bases:
objectstatic “internal” parameter setting for CMAES
-
finalize_weights()¶
-
recombination_weights()¶
-
-
class
grgrlib.optimize.GPP(func, bounds)¶ Bases:
objectGeneric PYGMO problem. Assumes minimization.
-
fitness(x)¶
-
get_bounds()¶
-
name= 'GPP'¶
-
-
grgrlib.optimize.broyden2d(f, x0, J_inv=None, eps=0.0001, maxiter=500, transpose_jac=False, record_history=False, verbose=True)¶ A simple 2D implementation of Broyden’s method.
This is not optimized for performance, but intended to better understand the target function.
Parameters: - f (callable) –
- x0 (array) –
- eps (float, optional) –
- maxiter (int, optional) –
- transpose_jac (bool, optional) –
- record_history (bool, optional) –
- verbose (bool, optional) –
-
grgrlib.optimize.broyden4d(f, x0, J_inv=None, eps=0.0001, maxiter=500, transpose_jac=False, record_history=False, verbose=True)¶ A simple 4D implementation of Broyden’s method.
This is not optimized for performance and instead intended to help better understanding the target function.
Parameters: - f (callable) –
- x0 (array) –
- eps (float, optional) –
- maxiter (int, optional) –
- transpose_jac (bool, optional) –
- record_history (bool, optional) –
- verbose (bool, optional) –
-
grgrlib.optimize.cmaes(objective_fct, xstart, sigma, popsize=None, mapper=None, biject=False, verbose=100, **args)¶ UI access point to CMAES to minimize objective_fct
…
This is in parts taken from the pycma package, but generally reworked and cleaned up. Arguably, this interface is relatively slim and the code more readible. Thougout this code it is assumed that the problem is expected to lie within the unit hypercube.
Parameters: - objective_fct (callable) – A function that takes as input a numpy array and returns a (numpy) float. If you want to use additional aruments to the function, just use lambda before you pass it on.
- xstart (array) – Initial mean/solution vector.
- sigma (float) – Initial step-size, standard deviation in any coordinate.
- popsize (int) – Population size. By default will be determined from the dimensionality of the problem.
- mapper (callable, optional) – A funciton mapper. This could just be map (default), but is meant to work with a multiprocessing pool such as pathos.pools.ProcessPool().imap. Defaults to map.
- biject (bool, optional) – Whether or not to employ a bijective mapping to enforce that only values within the hypercube are called. Defaults to False.
- verbose (int, optional) – Number of iterations to wait until update is printed. Defaults to 100.
- args (keyword arguments, optional) – Additional arguments, in particular stopping criteria and parameters. See the CMAESParameters class.
Notes
The CMA-ES algoritm is not optimized for large populations. In particular, it might get stuck or just not converge. One way to avoid explosion of the covariance for large populations is to manually set the parameter cs (very) close to zero. Another way is to reduce mu to only update using a few top-samples. This is automatically done if you set the scaled option to True. Note that doing so might have other disadvantages. If you prefer tighter selecton of only the best sample without reducing the size of the cov update, you can reduce mu_mean, which is the size of the offspring vector for the mean only.
All in all, this is a powerful method which, due to its heuristic nature, requires quite some tweaking at times.