Application Programming Interface (API)

nbi_stat Module

Module to help with various statistical choirs

This module contains many different functions and classes for statistical tasks, including

  • Robust and online mean and (co)variance calculations

  • Scientific rounding

  • Representation of results

  • Representation of data

  • Visualisation of data

  • Propagation of uncertainty

  • Sampling of arbitrary PDF

  • Histogramming

  • Fitting

  • Likelihood

  • Hypothesis testing

  • Simultanious fitting

  • Confidence intervals

Copyright © 2019 Christian Holm Christensen

2020-04-02 13:47:41.941259 UTC



A decorator indicating abstract methods.

binned_llh(f, data, *theta[, logpdf, cdf, …])

Calculate the binned logarithmic likelihood


binned_llh_n(bins, data[, density])

cdf_cl(x, cdf, p[, direction])

Evaluate the confince interval from an evaluated CDF

chi2nu(x, y, f, theta[, delta, deltax, df, …])

Calculate the chi-square over the sample (x,y)

corner_plot(*args, **kwargs)

Draw a corner plot of several variables.

cov(x, w[, ddof, frequency, component])

Calculated weighted covariance

curve_fit(f, x, y, p0[, dy, dx, df, …])

Perform a non-linear least squares fit of f to data

dkl(p, q)

Calculate the Kullback-Leibler discrepancy (or relative entropy) of a discrete random variable with assumed probability p[i] and observed probability q[i]=n[i]/N

effective_variance(x, ex, f, p, ey[, df, …])

Calculates the effective variance

eval_cdf(f, x[, dx])

Integrates the PDF f over the range x to get a table of the CDF

fc_cl(ranked, p[, fuzzy])

Estimate the confidence interval given using the Feldman-Cousine algorithm

fc_rank(pdf, measurement, hypothesis, best)

Calculates Feldman-Cousine rank of PDF with measurements, hypotheses, and best value

fill_histogram(x, bins, sumw[, sumw2, weight])

Fill a histogram

fini_histogram(bins, sumw[, sumw2, normalize])

Finalize histogram

fit(f, *args, **kwargs)

Unified interface for curve fitting

fit_plot(x, y, delta, f, p, ep[, xdelta, df])

Plot data and a fitted funtion

format_data_table(data[, columns, rows, …])

Formats data into a LaTeX table

format_result(value[, deltas, nsig, name, …])

Function to pretty-format results

hist(data, **kwargs)

Calculates and plots a histogram of data

histo_pdf(x, y[, kind, extra, logpdf])

Generate a PDF function from a histogram

histogram(a[, bins, rnge, weights, …])

Build a histogram of data in a

init_histogram(bins[, weighted])

Initialize a histogram structure

likelihood_ratio(lh_h1, lh_h0[, loglike])

Calculate the likelihood ratio of hypothesis H1 to H0

lin_fit(f, x, y[, delta])

Fit a linear model f to data

linfit(f, x, y[, delta])

Fit a linear model f to data

llh(f, x, *theta[, logpdf, extended, …])

Calculate the logarithmic likelihood

lsq_fit(f, x, y, p0[, dy, dx, df, df_step, …])

Perform a non-linear least squares fit of f to data

lsqfit(f, x, y, p0[, dy, dx, df, df_step, …])

Perform a non-linear least squares fit of f to data

maximize_llh(f, data, p0, tomax[, kw])

Maximize a logarithmic likelihood function

mle_fit(f, data, p0, *args, **kwargs)

Do an MLE estimate of parameters of the PDF given data yield.


Determine the number of significant digits

nsigma_contour(p, cov[, n, nstep])

Calculate all n-sigma contours

nsigma_contour2(a, b, ea, eb, rho[, n, nstep])

Calculate the two parameter n-sigma contour

overall_sys_pdf(down, up[, mid, kind, …])

A decorate that adds an overall systematic uncertainty to a PDF

pdf_sampler(f, x[, dx])

Creates a function to sample a PDF

plot_cdf_cl(x, theta, cdf, ps[, dirs, fig, …])

Plots Confidence intervals of a CDF

plot_fit(x, y, delta, f, p, ep[, xdelta, df])

Plot data and a fitted funtion

plot_fit_func(x, f, p, cov, **kwargs)

Plot fit function with found parameters and (optional uncertainty band)

plot_fit_table(p, ep[, nsig, chi2nu, …])

Plot a fit table in the current (or passed) axes

plot_hist(n, x, wx, en[, b, ax, as_bar])

Plot a histogram

plot_hist_with_poisson(n, x, wx, en[, ax, …])

plot_nsigma_contour(p, cov[, ns, nstep, fig_kw])

Plot nsigma contour lines

plot_residual(x, y, f, p[, cov, ey])

Plot the residuls and uncertainty on function

print_result(x, deltas[, nsign, width])

Print a single result with uncertainties, properly rounded

propagate_uncertainty(f, x, delta[, step])

Propegate uncertainties on x to y

residual_plot(x, y, f, p[, cov, ey])

Plot the residuls and uncertainty on function

residuals(x, y, f, p[, ey])

Calculate the residuals with respect to some function

round(v[, n])

Round value(s) to the precision given by

round_result(x, deltas[, nsign])

Round result and associated uncertainties

round_result_expo(x, deltas[, nsign, expo])

sample_pdf(y, x, cdf)

Sample a PDF given by the table of the CDF


Decorate to add norm to a PDF (any PDF)

scatter_hist2d(x, y, *args, **kwargs)

Draw a two-dimensional sample histogrammed as a scatter plot

sel_llh(data, kwargs)

shape_sys_pdf(down, up[, mid, ev, kind, …])

A decorate that adds a shape systematic uncertainty to a PDF

simul_mle_fit(regions, p0, *args, **kwargs)

Perform simulatinous MLE fit over several regions

welford_init([ndim, covar])

Initialize a structure for use with welford_update

welford_merge(ma, cva, na, mb, cvb, nb[, ddof])

Merge two statistics into one

welford_update(x, mean, covar, n[, ddof, z])

Calculates running average and (co)variance by Welfords algorithm

west_init([ndim, covar, frequency, component])

Initialize a data-structure for use with west_update

west_merge(ma, cva, w1a, w2a, wa, mb, cvb, …)

Merge two statistics into one

west_update(x, w, mean, cv, sumw[, sumw2, …])

Do a West online update of mean and (co)variance of the weighted sample.



Helper class that provides a standard way to create an ABC using inheritance.

Histogram(bins[, weighted])

A 1 dimensional histogram class

Stat([covar, ddof])

Base class for statistics classes

Welford(ndim[, covar, ddof])

An unweighted sample statistics

West(ndim[, covar, frequency, component, ddof])

An weighted sample statistics

Class Inheritance Diagram

digraph inheritance7d21ca67bc { rankdir=LR; size="8.0, 12.0"; "ABC" [fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5)",tooltip="Helper class that provides a standard way to create an ABC using"]; "Histogram" [URL="api/nbi_stat.Histogram.html#nbi_stat.Histogram",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5)",target="_top",tooltip="A 1 dimensional histogram class"]; "Stat" [URL="api/nbi_stat.Stat.html#nbi_stat.Stat",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5)",target="_top",tooltip="Base class for statistics classes"]; "ABC" -> "Stat" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Welford" [URL="api/nbi_stat.Welford.html#nbi_stat.Welford",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5)",target="_top",tooltip="An unweighted sample statistics"]; "Stat" -> "Welford" [arrowsize=0.5,style="setlinewidth(0.5)"]; "West" [URL="api/nbi_stat.West.html#nbi_stat.West",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5)",target="_top",tooltip="An weighted sample statistics"]; "Stat" -> "West" [arrowsize=0.5,style="setlinewidth(0.5)"]; }