statistics

analysis.statistics

Statistics over stored, labelled arrays: the implementations behind the software-based curated functions.

Each function here is named by a record in tvbo/database/functions/ (GroupedCorrelation, FalseDiscoveryControl, Histogram) and returns a mapping of labelled arrays, which a study-level analysis stores one variable per key. Each runs along one named dimension and keeps every other one, so a statistic per question, region or pair is one call on the whole array rather than a loop in the study.

Attributes

Name Description
GROUPINGS How units are cut into groups: bins of equal width over the grouping variable’s range, or runs of equal size along its order.
METHODS The false-discovery procedures: Benjamini-Hochberg, and Benjamini-Yekutieli for tests of arbitrary dependence.
MIN_POINTS The fewest points a correlation is stated for; two points always correlate perfectly, so their r and p say nothing.

Functions

Name Description
false_discovery_control The p-values p adjusted to control the false discovery rate, and the tests kept at rate q.
group_index The group, 0 .. n_groups - 1, of each entry of the finite 1-D values.
grouped_correlation The correlation of x with y over group means, the units of dim cut into n_groups groups, beside the same correlation over single units.
histogram How many of values fall in each of n_bins equal-width bins from lower to upper.

false_discovery_control

analysis.statistics.false_discovery_control(p, q=None, method='bh', dim=None)

The p-values p adjusted to control the false discovery rate, and the tests kept at rate q.

The entries of a family that are not NaN are its tests; a NaN is no test, stays NaN, is never selected and is not counted, which is how a test that could not be made is passed in. Any other entry is a probability, and one outside 0 to 1, an infinite one included, is refused. The rate q is one number above 0 and below 1 (:func:_rate): at 1 nothing would be controlled and at 0 nothing could be kept, so both ends are refused with every value beyond them. A family is all of p, or, with dim, each run along that dimension, one family per element of the others. bh is the Benjamini-Hochberg step-up procedure, valid for independent or positively dependent tests, and by the Benjamini-Yekutieli one, valid under any dependence.

Returns

Name Type Description
dict p_adjusted, shaped and labelled as p: the smallest rate at which each test is selected, as :func:scipy.stats.false_discovery_control gives it. n_tests, the size of each family. With q, selected, true for every test up to the largest p-value that lies at or below q times its rank over the number of tests, and n_selected, their count per family.

group_index

analysis.statistics.group_index(values, n_groups, grouping='equal_width')

The group, 0 .. n_groups - 1, of each entry of the finite 1-D values.

equal_width cuts the range from the smallest to the largest value into n_groups bins of equal width, each closed below and open above and the last closed on both sides; a bin no value falls in stays empty. equal_count orders the values, ties in order of appearance, and cuts that order into n_groups consecutive runs whose sizes differ by at most one, the longer runs first, as :func:numpy.array_split does; equal values can then land in neighbouring groups.

grouped_correlation

analysis.statistics.grouped_correlation(
    x,
    y,
    n_groups,
    dim,
    by=None,
    grouping='equal_width',
)

The correlation of x with y over group means, the units of dim cut into n_groups groups, beside the same correlation over single units.

The units are grouped by by, or by x when no by is given (:func:group_index), and each group contributes the mean of its members’ x and of their y. A unit enters where x, y and by are all finite; labelled inputs are paired on the labels of dim, each of which names one unit, and only units every input has are kept; bare 1-D inputs are paired by position. Any other dimension an input carries is kept: the statistic is computed once per element of those dimensions, each from its own finite units, so y on (subject, question) gives one result per question, and a by that carries question groups each question by its own slice.

A correlation, its p-value and the line are NaN where they are undefined: fewer than :data:MIN_POINTS groups with members (or units, for the unit-level pair), or a constant x or y. More groups than units is no error: equal_count then gives each unit a group of its own and leaves the last groups empty.

Returns

Name Type Description
dict On group, one entry per group in the order of the grouping variable, NaN for a group without members: x_mean, y_mean, y_sd (sample standard deviation of the members’ y, NaN for a single member), y_se (y_sd over the square root of the group’s size) and size. Over the group means: slope and intercept of the least-squares line of y_mean on x_mean, its Pearson r and two-sided p, and the line at the smallest and the largest x_mean as fit_x and fit_y on end. Over single units: r_unit and p_unit. Counts: n_nonempty_groups, n_units (units entering) and n_units_total (units entering at least one element of the other dimensions, a scalar).

histogram

analysis.statistics.histogram(values, n_bins, lower, upper, where=None)

How many of values fall in each of n_bins equal-width bins from lower to upper.

Every finite entry of values is counted, whatever its dimensions; with where, only the entries whose where is true or non-zero. Two labelled arrays are paired by label: along a dimension both label they carry the same labels, in any order, and a label only one of them has is refused; where may lack a dimension of values, and then marks every entry along it. Two bare arrays, or a labelled and a bare one, are paired by position. A bin is closed below and open above and the last is closed on both sides, as :func:numpy.histogram cuts them; a value outside the range is not counted.

Returns

Name Type Description
dict count and center on bin, the bins’ edge on bin_edge, their common width, and n, the number of values counted.