gphist.analysis module

Analysis and plotting functions.

gphist.analysis.calculate_confidence_limits(histograms, confidence_levels, bin_range)

Calculates confidence limits from distributions represented as histograms.

The band corresponding to each confidence level CL is given by a histogram’s quantile levels (1-CL)/2 and 1-(1-CL)/2.

Parameters:
  • histograms (ndarray) – Array of shape (nhist,nbins+2) containing nhist histograms with identical binning and including under- and overflow bins.
  • confidence_levels (ndarray) – Array of confidence levels to calculate.
  • bin_range (ndarray) – Array of length 2 containing the [min,max) range corresponding to the histogram contents in bins [1:-1].
Returns:

Array of shape (2*ncl+1,nhist) where elements [i,j] and [-i,j] give the

limits of the confidence band for confidence_levels[i] of histograms[j], and element [ncl,j] gives the median for histograms[j]. Values that lie outside of bin_range will be clipped.

Return type:

ndarray

gphist.analysis.calculate_histograms(DH, DH0, DA, DA0, f, f0, phi, phi0, de_evol, de0_evol, q, q0, nlp, num_bins, min_value, max_value)

Build histograms for all permutations of posterior weightings.

The undefined ratio DA(z=0)/DA0(z=0) is never evaluated. The binning args are only used for histogramming DH/DH0 and DA/DA0. The dark-energy binning is hard coded for now.

Parameters:
  • DH (ndarray) – Array of shape (nsamples,nz) of DH(z) values to use.
  • DH0 (ndarray) – Array of shape (nz,) used to normalize each DH(z).
  • DA (ndarray) – Array of shape (nsamples,nz) of DA(z) values to use.
  • DA0 (ndarray) – Array of shape (nz,) used to normalize each DA(z).
  • f – (ndarray): array of shaoe(nasmple,nz) of f = 1 + d ln(phi) / d ln(a) replaces phi
  • de_evol (ndarray) – Array of shape (nde,nsamples,nz) of nde dark-energy evolution histories, or None if they have not been calculated.
  • de0_evol (ndarray) – Array of shape (nde,nz) of nde fiducial dark-energy evolution histories, used for normalization.
  • nlp (ndarray) – Array of shape (npost,nsamples) containing the nlp posterior weights to use.
  • num_bins (int) – Number of equally spaced bins to use for each histogram.
  • min_value (float) – Values less than this are accumulated in an underflow bin.
  • max_value (float) – Values greater than equal to this are accumulated in an overflow bin.
Returns:

Arrays of histograms for DH/DH0, DA/DA0, de_evol/de0_evol with shapes

(nperm,nz,num_bins+2), (nperm,nz-1,num_bins+2), (nde,nperm,nz,num_bins+2), respectively, where nperm = 2**npost. There are no DA/DA0 histograms for z=0, hence the nz vs nz-1 dimensions. The mapping between permutations and the permutation index is given by the binary representation of the index. For example, iperm = 5 = 2^0 + 2^2 combines posteriors 0 and 2.

Return type:

tuple

Raises:

AssertionError – Unexpected sizes of DH0,DA0,DA,de_evol,de0_evol,nlp.

gphist.analysis.calculate_posteriors_nlp(zprior, DH, DA, mu, apar, aperp, posteriors)

Calculate -log(prob) for each combination of posterior and prior sample.

Parameters:
  • zprior (ndarray) – Redshifts where prior is sampled, in increasing order.
  • DH (ndarray) – Array of shape (nsamples,nz) of DH(z) values to use.
  • DA (ndarray) – Array of shape (nsamples,nz) of DA(z) values to use.
  • posteriors (list) – List of posteriors to use. Each posterior should inherit from the posterior.Posterior base class.
Returns:

An array of shape (npost,nsamples) containing the nlp values

calculated for each posterior independently.

Return type:

ndarray

gphist.analysis.get_bin_indices(data, num_bins, min_value, max_value)

Create array of integer bin indices in preparation for histogramming.

Results are in a format suitable for using with numpy.bincount. This function only handles uniform binning, but is generally faster than numpy.histogram in this case, especially when this method is broadcast over data that is destined for multiple histograms.

Parameters:
  • data (ndarray) – Array of data to histogram.
  • num_bins (int) – Number of equally spaced bins to use for each histogram.
  • min_value (float) – Values less than this are accumulated in an underflow bin.
  • max_value (float) – Values greater than equal to this are accumulated in an overflow bin.
Returns:

Array of integer bin indices with the same shape as data. Each

input data value is replaced with its corresponding bin index in the range [1:num_bins+1] or 0 if value < min_value or num_bins+1 if value >= max_value.

Return type:

ndarray

Raises:

ValueError – num_bins <= 0 or min_value >= max_value.

gphist.analysis.get_delta_chisq(confidence_levels=(0.6827, 0.9543, 0.9973), num_dof=2)

Calculate delta(chisq) values.

The default confidence_level values correspond to the enclosed probabilities of 1,2,3-sigmas for a 1D Gaussian pdf.

Parameters:
  • confidence_levels – Iterable of confidence levels that should be converted to delta(chisq) values, which do not need to be sorted. In case this argument is a numpy array, the calculation will be broadcast over it.
  • num_dof (int) – Number of degrees of freedom to assume for the calculation.
Returns:

Array of delta(chisq) values with the same shape as the input

confidence_levels.

Return type:

ndarray

gphist.analysis.get_permutations(n)

Builds an array of permutations.

Parameters:n (int) – Length of the array to permute.
Returns:
Array of booleans with shape (2*n,n). Element (i,j) is True if
element j belongs to the i-th permutation.
Return type:ndarray
gphist.analysis.quantiles(histogram, quantile_levels, bin_range)

Calculate quantiles of a histogram.

The quantiles are estimated by inverse linear interpolation of the cummulative histogram.

Parameters:
  • histogram (ndarray) – Array of nbins+2 histogram contents, with bins [0] and [-1] containing under- and overflow contents, respectively.
  • quantile_levels (ndarray) – Array of nq quantile levels to calculate. Must be in the range (0,1) but do not need to be sorted.
  • bin_range (ndarray) – Array of length 2 containing the [min,max) range corresponding to the histogram contents in bins [1:-1].
Returns:

Array of nq estimated abscissa values for each input level. Each value will

be in the range specified by bin_range. Any values that should be outside of this range will be clipped.

Return type:

ndarray

Raises:

ValueError – bin_range does not have 2 elements.

gphist.analysis.select_random_realizations(DH, DA, nlp, num_realizations, random_state=None, print_warnings=True)

Select random realizations of generated expansion histories.

Parameters:
  • DH (ndarray) – Array of shape (nsamples,nz) of DH(z) values to use.
  • DA (ndarray) – Array of shape (nsamples,nz) of DA(z) values to use.
  • nlp (ndarray) – Array of shape (npost,nsamples) containing the nlp posterior weights to use.
  • num_realizations (int) – Number of random rows to return.
  • random_state (numpy.RandomState) – Random state to use, or use default state if None.
  • print_warnings (bool) – Print a warning for any posterior permutation whose selected realizations include repeats.
Returns:

Arrays of random realizations of DH and DA, with shape

(nperm,num_realizations,nz) where nperm = 2**npost is the total number of posterior permutations. Note that a realization might be selected more than once. Use the print_warnings argument to flag this.

Return type:

tuple

Raises:

AssertionError – Unexpected sizes of DH,DA, or nlp.