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
. Herescale
can also takeDataArray
values in which case, broadcasting is handled by xarray, as shown in the example.Examples
Use a
DataArray
asscale
.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)> Size: 192B 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 24B 'a' 'b' 'c' 'd' 'e' 'f' * chain (chain) int64 32B 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) Cell In[2], 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/stable/lib/python3.10/site-packages/scipy/stats/_stats_py.py:3617, in median_abs_deviation(x, axis, center, scale, nan_policy) 3614 med = np.expand_dims(center(x, axis=axis), axis) 3615 mad = np.median(np.abs(x - med), axis=axis) -> 3617 return mad / scale File ~/checkouts/readthedocs.org/user_builds/xarray-einstats/envs/stable/lib/python3.10/site-packages/xarray/core/arithmetic.py:83, in SupportsArithmetic.__array_ufunc__(self, ufunc, method, *inputs, **kwargs) 74 raise NotImplementedError( 75 "xarray objects are not yet supported in the `out` argument " 76 "for ufuncs. As an alternative, consider explicitly " 77 "converting xarray objects to NumPy arrays (e.g., with " 78 "`.values`)." 79 ) 81 join = dataset_join = OPTIONS["arithmetic_join"] ---> 83 return apply_ufunc( 84 ufunc, 85 *inputs, 86 input_core_dims=((),) * ufunc.nin, 87 output_core_dims=((),) * ufunc.nout, 88 join=join, 89 dataset_join=dataset_join, 90 dataset_fill_value=np.nan, 91 kwargs=kwargs, 92 dask="allowed", 93 keep_attrs=_get_keep_attrs(default=True), 94 ) File ~/checkouts/readthedocs.org/user_builds/xarray-einstats/envs/stable/lib/python3.10/site-packages/xarray/core/computation.py:1278, 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, on_missing_core_dim, *args) 1276 # feed DataArray apply_variable_ufunc through apply_dataarray_vfunc 1277 elif any(isinstance(a, DataArray) for a in args): -> 1278 return apply_dataarray_vfunc( 1279 variables_vfunc, 1280 *args, 1281 signature=signature, 1282 join=join, 1283 exclude_dims=exclude_dims, 1284 keep_attrs=keep_attrs, 1285 ) 1286 # feed Variables directly through apply_variable_ufunc 1287 elif any(isinstance(a, Variable) for a in args): File ~/checkouts/readthedocs.org/user_builds/xarray-einstats/envs/stable/lib/python3.10/site-packages/xarray/core/computation.py:320, in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args) 315 result_coords, result_indexes = build_output_coords_and_indexes( 316 args, signature, exclude_dims, combine_attrs=keep_attrs 317 ) 319 data_vars = [getattr(a, "variable", a) for a in args] --> 320 result_var = func(*data_vars) 322 out: tuple[DataArray, ...] | DataArray 323 if signature.num_outputs > 1: File ~/checkouts/readthedocs.org/user_builds/xarray-einstats/envs/stable/lib/python3.10/site-packages/xarray/core/computation.py:831, in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, vectorize, keep_attrs, dask_gufunc_kwargs, *args) 826 if vectorize: 827 func = _vectorize( 828 func, signature, output_dtypes=output_dtypes, exclude_dims=exclude_dims 829 ) --> 831 result_data = func(*input_data) 833 if signature.num_outputs == 1: 834 result_data = (result_data,) ValueError: operands could not be broadcast together with shapes (4,6) (4,)