xarray_einstats.stats.median_abs_deviation#

xarray_einstats.stats.median_abs_deviation(da, dims=None, *, center=None, scale=1, nan_policy=None, **kwargs)[source]#

Wrap and extend scipy.stats.median_abs_deviation.

Usage examples available at Intro to the stats module.

All parameters take the same values and types as the scipy counterpart with the exception of scale. Here scale can also take DataArray values in which case, broadcasting is handled by xarray, as shown in the example.

Examples

Use a DataArray as scale.

import xarray as xr
from xarray_einstats import tutorial, stats
ds = tutorial.generate_mcmc_like_dataset(3)
s_da = xr.DataArray([1, 2, 1, 1], coords={"chain": ds.chain})
stats.median_abs_deviation(ds["mu"], dims="draw", scale=s_da)
<xarray.DataArray (chain: 4, team: 6)>
0.3468 0.4532 0.5054 0.876 0.6265 0.7342 ... 0.4382 0.5668 0.7103 0.2494 0.3485
Coordinates:
  * team     (team) <U1 'a' 'b' 'c' 'd' 'e' 'f'
  * chain    (chain) int64 0 1 2 3

Note that this doesn’t work with the scipy counterpart because s_da can’t be broadcasted with the output:

from scipy import stats
stats.median_abs_deviation(ds["mu"], axis=1, scale=s_da)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [2], in <cell line: 2>()
      1 from scipy import stats
----> 2 stats.median_abs_deviation(ds["mu"], axis=1, scale=s_da)

File ~/checkouts/readthedocs.org/user_builds/xarray-einstats/envs/latest/lib/python3.9/site-packages/scipy/stats/_stats_py.py:3378, in median_abs_deviation(x, axis, center, scale, nan_policy)
   3375         med = np.expand_dims(center(x, axis=axis), axis)
   3376         mad = np.median(np.abs(x - med), axis=axis)
-> 3378 return mad / scale

File ~/checkouts/readthedocs.org/user_builds/xarray-einstats/envs/latest/lib/python3.9/site-packages/xarray/core/arithmetic.py:81, in SupportsArithmetic.__array_ufunc__(self, ufunc, method, *inputs, **kwargs)
     72     raise NotImplementedError(
     73         "xarray objects are not yet supported in the `out` argument "
     74         "for ufuncs. As an alternative, consider explicitly "
     75         "converting xarray objects to NumPy arrays (e.g., with "
     76         "`.values`)."
     77     )
     79 join = dataset_join = OPTIONS["arithmetic_join"]
---> 81 return apply_ufunc(
     82     ufunc,
     83     *inputs,
     84     input_core_dims=((),) * ufunc.nin,
     85     output_core_dims=((),) * ufunc.nout,
     86     join=join,
     87     dataset_join=dataset_join,
     88     dataset_fill_value=np.nan,
     89     kwargs=kwargs,
     90     dask="allowed",
     91     keep_attrs=_get_keep_attrs(default=True),
     92 )

File ~/checkouts/readthedocs.org/user_builds/xarray-einstats/envs/latest/lib/python3.9/site-packages/xarray/core/computation.py:1204, in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, meta, dask_gufunc_kwargs, *args)
   1202 # feed DataArray apply_variable_ufunc through apply_dataarray_vfunc
   1203 elif any(isinstance(a, DataArray) for a in args):
-> 1204     return apply_dataarray_vfunc(
   1205         variables_vfunc,
   1206         *args,
   1207         signature=signature,
   1208         join=join,
   1209         exclude_dims=exclude_dims,
   1210         keep_attrs=keep_attrs,
   1211     )
   1212 # feed Variables directly through apply_variable_ufunc
   1213 elif any(isinstance(a, Variable) for a in args):

File ~/checkouts/readthedocs.org/user_builds/xarray-einstats/envs/latest/lib/python3.9/site-packages/xarray/core/computation.py:315, in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args)
    310 result_coords, result_indexes = build_output_coords_and_indexes(
    311     args, signature, exclude_dims, combine_attrs=keep_attrs
    312 )
    314 data_vars = [getattr(a, "variable", a) for a in args]
--> 315 result_var = func(*data_vars)
    317 out: tuple[DataArray, ...] | DataArray
    318 if signature.num_outputs > 1:

File ~/checkouts/readthedocs.org/user_builds/xarray-einstats/envs/latest/lib/python3.9/site-packages/xarray/core/computation.py:771, in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, vectorize, keep_attrs, dask_gufunc_kwargs, *args)
    766     if vectorize:
    767         func = _vectorize(
    768             func, signature, output_dtypes=output_dtypes, exclude_dims=exclude_dims
    769         )
--> 771 result_data = func(*input_data)
    773 if signature.num_outputs == 1:
    774     result_data = (result_data,)

ValueError: operands could not be broadcast together with shapes (4,6) (4,)