arviz_stats.mode#
- arviz_stats.mode(data, dim=None, group='posterior', var_names=None, filter_vars=None, coords=None, **kwargs)[source]#
Compute the mode.
The mode is the value that appears most frequently in a data set. If the data is of type float, we assume it is continuous and use the half-sample method [1]. If the data is of type int, we assume it is discrete and use
numpy.unique
to find the most frequent value.- 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 asxarray.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"]
.- group
hashable
, default “posterior” Group on which to compute the mode
- var_names
str
orlist
ofstr
, optional Names of the variables for which the mode should be computed.
- filter_vars{
None
, “like”, “regex”}, defaultNone
- coords
dict
, optional Dictionary of dimension/index names to coordinate values defining a subset of the data for which to perform the computation.
- **kwargs
any
, optional Forwarded to the array or dataarray interface for mode.
- dataarray_like,
- Returns:
ndarray
,xarray.DataArray
,xarray.Dataset
,xarray.DataTree
Requested mode of the provided input.
See also
xarray.Dataset.mean
,xarray.Dataset.median
References
[1]Bickel DR, Fruehwirth R. On a Fast, Robust Estimator of the Mode: Comparisons to Other Robust Estimators with Applications. Computational Statistics & Data Analysis. 2006. https://doi.org/10.1016/j.csda.2005.07.011 arXiv preprint https://doi.org/10.48550/arXiv.math/0505419
Examples
Calculate the mode 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.mode(data) ...: Out[1]: array(0.38766724)
Calculate the modes for specific variables:
In [2]: import arviz_base as azb ...: dt = azb.load_arviz_data("centered_eight") ...: azs.mode(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 6.265 theta (school) float64 64B 3.258 5.921 3.061 6.42 2.393 3.359 3.554 3.351
Calculate the modes excluding the school dimension:
In [3]: azs.mode(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 6.265 theta (school) float64 64B 3.258 5.921 3.061 6.42 2.393 3.359 3.554 3.351 tau float64 8B 1.176