# What value minimizes the error from a set of values under modular arithmetic?

I'm working with real numbers using modular arithmetic, say in the range $[0,12)$. I would like to calculate some kind of 'modular mean' over a set of values $X$ that minimizes the total error. In other words, the value $\bar{x} \in [0,12)$ such that

$\Sigma_{x \in X} \min(\bar{x}-x \mod N, x-\bar{x} \mod N)$

is minimized.

Using the ordinary mean doesn't quite work. If you consider the clock-face. Then for the values $\{1,3\}$ we get $2$ which is good. But for the values $\{10,1\}$ we get $\frac{11}{2}\mod N = 5.5$. However the value I'm looking for is $11.5$ because this gives an error of

$(11.5-10 \mod N) + (1-11.5 \mod N) = 3$

rather than $5.5$ which gives an error of $11$.

Is there a function/algorithm to find the value of $\bar{x}$? (Assuming the above makes sense!)

If you're minimizing the sum of the absolute values of the errors, then you can easily get degenerate solutions. For example, for the values $\{1,3\}$, yes, $2$ minimizes the total error, but so does $2.9973546$. And for the values $\{1,3.99,4.01\}$, the unique minimum is at $3.99$, when you probably want something closer to $3$.