chippr includes a number of modules containing helper functions.

Default Settings

defaults.check_basic_setup(params)[source]

Sets parameter values pertaining to basic constants of simulation

Parameters:params (dict) – dictionary containing key/value pairs for simulation
Returns:params – dictionary containing key/value pairs for simulation
Return type:dict
defaults.check_bias_params(params)[source]

Sets parameter values pertaining to presence of a systematic bias

Parameters:params (dict) – dictionary containing key/value pairs for simulation
Returns:params – dictionary containing key/value pairs for simulation
Return type:dict
defaults.check_catastrophic_outliers(params)[source]

Sets parameter values pertaining to presence of a catastrophic outlier population

Parameters:params (dict) – dictionary containing key/value pairs for simulation
Returns:params – dictionary containing key/value pairs for simulation
Return type:dict

Notes

defaults.check_inf_params(params={})[source]

Checks inference parameter dictionary for various keywords and sets to default values if not present

Parameters:params (dict, optional) – dictionary containing initial key/value pairs for inference
Returns:params – dictionary containing final key/value pairs for inference
Return type:dict
defaults.check_sampler_params(params)[source]

Sets parameter values pertaining to basic constants of inference

Parameters:params (dict) – dictionary containing key/value pairs for inference
Returns:params – dictionary containing key/value pairs for inference
Return type:dict
defaults.check_sim_params(params={})[source]

Checks simulation parameter dictionary for various keywords and sets to default values if not present

Parameters:params (dict, optional) – dictionary containing initial key/value pairs for simulation of catalog
Returns:params – dictionary containing final key/value pairs for simulation of catalog
Return type:dict
defaults.check_variable_sigmas(params)[source]

Sets parameter values pertaining to widths of Gaussian PDF components

Parameters:params (dict) – dictionary containing key/value pairs for simulation
Returns:params – dictionary containing key/value pairs for simulation
Return type:dict

Notes

rms_scatter –> variable_sigmas

General Utilities

utils.ingest(in_info)[source]

Function reading in parameter file to define functions necessary for generation of posterior probability distributions

Parameters:in_info (string or dict) – string containing path to plaintext input file or dict containing likelihood input parameters
Returns:in_dict – dict containing keys and values necessary for posterior probability distributions
Return type:dict
utils.safe_log(arr, threshold=4.450147717014403e-308)[source]

Takes the natural logarithm of an array that might contain zeros.

Parameters:
  • arr (ndarray, float) – array of values to be logged
  • threshold (float, optional) – small, positive value to replace zeros and negative numbers
Returns:

logged – logged values, with small value replacing un-loggable values

Return type:

ndarray

Simulation Utilities

sim_utils.choice(weights)[source]

Function sampling discrete distribution

Parameters:weights (numpy.ndarray) – relative probabilities for each category
Returns:index – chosen category
Return type:int

Statistics

stat_utils.acors(xtimeswalkersbins, mode='bins')[source]

Calculates autocorrelation time for MCMC chains

Parameters:
  • xtimeswalkersbins (numpy.ndarray, float) – emcee chain values of dimensions (n_iterations, n_walkers, n_parameters)
  • mode (string, optional) – ‘bins’ for one autocorrelation time per parameter, ‘walkers’ for one autocorrelation time per walker
Returns:

taus – autocorrelation times by bin or by walker depending on mode

Return type:

numpy.ndarray, float

stat_utils.calculate_kld(pe, qe, vb=True)[source]

Calculates the Kullback-Leibler Divergence between two PDFs.

Parameters:
  • pe (numpy.ndarray, float) – probability distribution evaluated on a grid whose distance from q will be calculated.
  • qe (numpy.ndarray, float) – probability distribution evaluated on a grid whose distance to p will be calculated.
  • vb (boolean) – report on progress to stdout?
Returns:

Dpq – the value of the Kullback-Leibler Divergence from q to p

Return type:

float

stat_utils.calculate_rms(pe, qe, vb=True)[source]

Calculates the Root Mean Square Error between two PDFs.

Parameters:
  • pe (numpy.ndarray, float) – probability distribution evaluated on a grid whose distance _from_ q will be calculated.
  • qe (numpy.ndarray, float) – probability distribution evaluated on a grid whose distance _to_ p will be calculated.
  • vb (boolean) – report on progress to stdout?
Returns:

rms – the value of the RMS error between q and p

Return type:

float

stat_utils.cf(xtimes)[source]

Helper function to calculate autocorrelation time for chain of MCMC samples

Parameters:xtimes (numpy.ndarray, float) – single parameter values for a single walker over all iterations
Returns:cf – autocorrelation time over all time lags for one parameter of one walker
Return type:numpy.ndarray, float
stat_utils.cfs(x, mode)[source]

Helper function for calculating autocorrelation time for MCMC chains

Parameters:
  • x (numpy.ndarray, float) – input parameter values of length number of iterations by number of walkers if mode=’walkers’ or dimension of parameters if mode=’bins’
  • mode (string) – ‘bins’ for one autocorrelation time per parameter, ‘walkers’ for one autocorrelation time per walker
Returns:

cfs – autocorrelation times for all walkers if mode=’walkers’ or all parameters if mode=’bins’

Return type:

numpy.ndarray, float

stat_utils.cft(xtimes, lag)[source]

Helper function to calculate autocorrelation time for chain of MCMC samples

Parameters:
  • xtimes (numpy.ndarray, float) – single parameter values for a single walker over all iterations
  • lag (int) – maximum lag time in number of iterations
Returns:

ans – autocorrelation time for one time lag for one parameter of one walker

Return type:

numpy.ndarray, float

stat_utils.gr_test(sample, threshold=1.2)[source]

Performs the Gelman-Rubin test of convergence of an MCMC chain

Parameters:
  • sample (numpy.ndarray, float) – chain output
  • threshold (float, optional) – Gelman-Rubin test statistic criterion (usually around 1)
Returns:

test_result – True if burning in, False if post-burn in

Return type:

boolean

stat_utils.mean(population)[source]

Calculates the mean of a population

Parameters:population (np.array, float) – population over which to calculate the mean
Returns:mean – mean value over population
Return type:np.array, float
stat_utils.multi_parameter_gr_stat(sample)[source]

Calculates the Gelman-Rubin test statistic of convergence of an MCMC chain over multiple parameters

Parameters:sample (numpy.ndarray, float) – multi-parameter chain output
Returns:Rs – vector of the potential scale reduction factors
Return type:numpy.ndarray, float
stat_utils.norm_fit(population)[source]

Calculates the mean and standard deviation of a population

Parameters:population (np.array, float) – population over which to calculate the mean
Returns:norm_stats – mean and standard deviation over population
Return type:tuple, list, float
stat_utils.single_parameter_gr_stat(chain)[source]

Calculates the Gelman-Rubin test statistic of convergence of an MCMC chain over one parameter

Parameters:chain (numpy.ndarray, float) – single-parameter chain
Returns:R_hat – potential scale reduction factor
Return type:float

Plotting Utilities

plot_utils.plot_h(sub_plot, bin_ends, to_plot, s='--', c='k', a=1, w=1, d=[(0, (1, 0.0001))], l=None, r=False)[source]

Helper function to plot horizontal lines of a step function

Parameters:
  • sub_plot (matplotlib.pyplot subplot object) – subplot into which step function is drawn
  • bin_ends (list or ndarray) – list or array of endpoints of bins
  • to_plot (list or ndarray) – list or array of values within each bin
  • s (string, optional) – matplotlib.pyplot linestyle
  • c (string, optional) – matplotlib.pyplot color
  • a (int or float, [0., 1.], optional) – matplotlib.pyplot alpha (transparency)
  • w (int or float, optional) – matplotlib.pyplot linewidth
  • d (list of tuple, optional) – matplotlib.pyplot dash style, of form [(start_point, (points_on, points_off, …))]
  • l (string, optional) – label for function
  • r (boolean, optional) – True for rasterized, False for vectorized
plot_utils.plot_step(sub_plot, bin_ends, to_plot, s='--', c='k', a=1, w=1, d=[(0, (1, 0.0001))], l=None, r=False)[source]

Plots a step function

Parameters:
  • sub_plot (matplotlib.pyplot subplot object) – subplot into which step function is drawn
  • bin_ends (list or ndarray) – list or array of endpoints of bins
  • to_plot (list or ndarray) – list or array of values within each bin
  • s (string, optional) – matplotlib.pyplot linestyle
  • c (string, optional) – matplotlib.pyplot color
  • a (int or float, [0., 1.], optional) – matplotlib.pyplot alpha (transparency)
  • w (int or float, optional) – matplotlib.pyplot linewidth
  • d (list of tuple, optional) – matplotlib.pyplot dash style, of form [(start_point, (points_on, points_off, …))]
  • l (string, optional) – label for function
  • r (boolean, optional) – True for rasterized, False for vectorized

Notes

Make this not need a subplot

plot_utils.plot_v(sub_plot, bin_ends, to_plot, s='--', c='k', a=1, w=1, d=[(0, (1, 0.0001))], r=False)[source]

Helper function to plot vertical lines of a step function

Parameters:
  • sub_plot (matplotlib.pyplot subplot object) – subplot into which step function is drawn
  • bin_ends (list or ndarray) – list or array of endpoints of bins
  • to_plot (list or ndarray) – list or array of values within each bin
  • s (string, optional) – matplotlib.pyplot linestyle
  • c (string, optional) – matplotlib.pyplot color
  • a (int or float, [0., 1.], optional) – matplotlib.pyplot alpha (transparency)
  • w (int or float, optional) – matplotlib.pyplot linewidth
  • d (list of tuple, optional) – matplotlib.pyplot dash style, of form [(start_point, (points_on, points_off, …))]
  • r (boolean, optional) – True for rasterized, False for vectorized
plot_utils.set_up_plot()[source]

Sets up plots to look decent