Calculates weighted median using interpolation and optionally estimates standard deviation and distribution of median. Input is data and standard deviations of data points. If standard deviations not given normal mean is calculated.
The point indexes of the weights are placed symmetrically and interpolation for the 50% point, is used to give consistent results. If standard deviation of median is required, a weighted likelihood bootstrap calculates the mean and if required an empirical distribution of the median is returned.
x=[1 2 3 4];
sd=[2 1 1 3];
Can also use:
[wm,wsd]=wtmedian(x) % no (or unit) sds: usual median
wtmedian(x,sd) % gives median only, no bootstrap
cnt=10000;[wm,wed,dist]=wtmedian(x,sd,cnt) % additional bootstrap samples
For extra information:
Interpolation is used to avoid the existence of upper and lower weighted medians and instead give a consistent result (see https://en.wikipedia.org/wiki/Weighted_median). The use of interpolation may give slightly different results from methods not using interpolation.
The point indices of the weights are placed so the the distribution of weights runs from 0.5 to n+0.5, similar to creating a bar graph of the weight distribution with the ith bar extending from i-0.5 to i+0.5, and then the cumulative distribution is the integral of this, and discrete values are read off at integer points. This distribution is the same if read from lower to higher values or higher to lower values.
The weighted likelihood bootstrap replaces integer repeat numbers for data points in the usual resampling bootstrap, with continuous values. see
Bill Whiten (2023). Weighted median (from data & sds) & its standard deviation (https://www.mathworks.com/matlabcentral/fileexchange/56712-weighted-median-from-data-sds-its-standard-deviation), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Inspired by: weighted median
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!
Changed wtmean.m to use parametric bootstrap to give closer agreement with standard deviation