2
$\begingroup$

I have a set of data X containing members xi (these are pixel values). I need to calculate the median and MAD of f(X), where f is a color space transform from linear RGB to sRGB (i.e. f(x) = x^(1/2.2)).

For reasons of computing speed I would strongly prefer not to have to calculate f(xi) for all xi as the images can be quite large and this calculation is done as part of a live preview, so calculation time is important. In most cases I already have the median and MAD of X computed.

Are there simple functions I can apply to m(X) and MAD(X) to get m(f(X)) and MAD(f(X))?

@Henry has confirmed in the comments that m(f(x)) = f(m(x)), so the focus of the question is now on MAD(f(x)). If there isn't a way of calculating it exactly, a computationally cheap way of approximating it or estimating it would also be useful.

$\endgroup$
2
  • 4
    $\begingroup$ A monotonic transformation will preserve the median (but not the deviation about the median). So $\text{median}(f(x)) = f(\text{median}(x))$ $\endgroup$
    – Henry
    Commented May 23, 2023 at 15:57
  • $\begingroup$ Yes, I felt that ought to be true of the median but I'm grateful for the confirmation (and that it isn't true for the MAD as well). I'll update the question to highlight this piece of the answer and focus it on the MAD (a means of estimating it would also be helpful if there's no exact way to calculate it). $\endgroup$ Commented May 23, 2023 at 21:05

1 Answer 1

1
$\begingroup$

The median absolute deviation (MAD) will not commute with the monotonic function of the data, in most cases. Intuitively, this is because the absolute deviation operation "folds" the data around the median, so the monotonicity of the function is "lost" (cannot be utilized).

Allow me to propose a slightly different, but practically similar solution:

The MAD is a robust measure of dispersion (spread) of the data values. Therefore, practically speaking, perhaps your goal can be achieved using a different measure of dispersion (spread) of the data values, which can accommodate the monotonicity of the function.

Specifically, I propose the Inter-Quartile Range (IQR), defined as: $\text{IQR}(X) = \text{Q}_3(X) - \text{Q}_1(X)$, where $\text{Q}_3(X)$ is the value below which 75% of the values exist, and $\text{Q}_1(X)$ is the value below which 25% of the values exist. Note: $\text{median}(X) = \text{Q}_2(X)$ is the value below which 50% of the values exist.

The advantage of this proposal is that for all 3 measurements $\text{Q}_k(X)$, $k = \{1,2,3\}$, for a monotonic function $f(\cdot)$, we have that: $$\text{Q}_k(f(X)) = f(\text{Q}_k(X))$$ This fact is a generalization of what user @Henry already mentioned in a comment to the question: $\text{median}(f(X)) = f(\text{median}(X))$. In fact, every quantile (not just the 3 quartiles mentioned above) commutes with a monotonic function of the data, because a monotonic function will not change the sorted order of the data.

Finally, note that for a symmetric distribution of the data $X$, the median will equal the average of the first and third quartiles, and therefore the MAD will equal half the IQR. But in general I doubt your pixel data $X$ will be symmetric; Nonetheless, if the users of the system/software expect values previously reported by MAD, then if you decide to use IQR, you can simply report half the IQR.

$\endgroup$
3
  • $\begingroup$ Thank you, that sounds interesting. Generally the distribution won't be symmetric, but it will be strongly peaked around the median, and in terms of a measure of the spread of the data perhaps just making the approximation that it is symmetric might be ok. I'll try it and see how it works out. $\endgroup$ Commented May 24, 2023 at 7:43
  • 1
    $\begingroup$ A friend (not on this site) extended your answer in a way which allows for arbitrary increases in the accuracy: instead of taking just the 2 percentiles to obtain the IQR and halving that, take n evenly-spaced percentiles, transform those using f(x) and calculate the MAD of those percentiles in the usual way. It's more computation than your answer but still a lot less than the brute force way, and the higher n the closer the estimate gets to the true MAD(f(X)). With say 100 percentiles the error is very low. $\endgroup$ Commented May 24, 2023 at 8:29
  • $\begingroup$ @AdrianK-B. Your friend's suggestion of summarizing the data distribution using many percentiles (quantiles) and applying MAD(f(S)), where S is the summary, sounds a great idea! It only requires sorting the data values in X once, and simply picking the $n$ percentiles (quantiles), i.e., you can easily implement a function with a sequence of $n$ percentiles (quantiles) yourself, although you can use existing libraries such as numpy.percentile() and numpy.quantile() in the NumPy Python library. $\endgroup$
    – Number
    Commented May 24, 2023 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.