arviz_stats.median

Contents

arviz_stats.median#

arviz_stats.median(data, dim=None, group='posterior', var_names=None, filter_vars=None, coords=None, round_to=None, skipna=False, **kwargs)[source]#

Compute the median.

The median is the middle value of a set of numbers, half of the numbers are above the median and half are below.

Parameters:
dataarray_like, xarray.DataArray, xarray.Dataset, xarray.DataTree, DataArrayGroupBy, DatasetGroupBy, or idata-like

Input data. It will have different pre-processing applied to it depending on its type:

  • array-like: call array layer within arviz-stats.

  • xarray object: apply dimension aware function to all relevant subsets

  • others: passed to arviz_base.convert_to_dataset then treated as xarray.Dataset. This option is discouraged due to needing this conversion which is completely automated and will be needed again in future executions or similar functions.

    It is recommended to first perform the conversion manually and then call arviz_stats.mode. This allows controlling the conversion step and inspecting its results.

dimsequence of hashable, optional

Dimensions over which to compute the mode. Defaults to rcParams["data.sample_dims"].

grouphashable, default “posterior”

Group on which to compute the mode

var_namesstr or list of str, optional

Names of the variables for which the mode should be computed.

filter_vars{None, “like”, “regex”}, default None
coordsdict, optional

Dictionary of dimension/index names to coordinate values defining a subset of the data for which to perform the computation.

round_to: int or str or None, optional
If integer, number of decimal places to round the result. Integers can be negative.

If string of the form ‘2g’ number of significant digits to round the result. Defaults to rcParams[“stats.round_to”] if None. Use the string “None” or “none” to return raw numbers.

skipna: bool, default False

If True, ignore NaN values.

**kwargsany, optional

Forwarded to the array or dataarray interface for mode.

Returns:
ndarray, xarray.DataArray, xarray.Dataset, xarray.DataTree

Requested mode of the provided input.

Examples

Calculate the median of a Normal random variable:

In [1]: import arviz_stats as azs
   ...: import numpy as np
   ...: data = np.random.default_rng().normal(size=2000)
   ...: azs.median(data)
   ...: 
Out[1]: array(-0.013)

Calculate the medians for specific variables:

In [2]: import arviz_base as azb
   ...: dt = azb.load_arviz_data("centered_eight")
   ...: azs.median(dt, var_names=["mu", "theta"])
   ...: 
Out[2]: 
<xarray.DataTree 'posterior'>
Group: /posterior
    Dimensions:  (school: 8)
    Coordinates:
      * school   (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon'
    Data variables:
        mu       float64 8B 4.1
        theta    (school) float64 64B 5.8 5.0 3.7 4.7 3.6 3.9 6.1 4.6

Calculate the medians excluding the school dimension:

In [3]: azs.median(dt, dim=["chain", "draw"])
Out[3]: 
<xarray.DataTree 'posterior'>
Group: /posterior
    Dimensions:  (school: 8)
    Coordinates:
      * school   (school) <U16 512B 'Choate' 'Deerfield' ... 'Mt. Hermon'
    Data variables:
        mu       float64 8B 4.1
        theta    (school) float64 64B 5.8 5.0 3.7 4.7 3.6 3.9 6.1 4.6
        tau      float64 8B 3.5