We are answering questions in <cite data-cite="bibtex_lane2019online">(Lane, 2019)</cite>

The first question we answer is on how to find the smallest absolute difference for the set of numbers $S=\left\{2,3,4,9,16\right\}$


```python
s=[2,3,4,9,16]
result=[]
for i in range(10,1,-1):
    sum=0
    for j in s:
        sum += abs(j-i)
    result.append((i,sum))
result.sort(key=lambda x: x[1])
print("The number that gives the smallest absolute difference is %d.  The sum of absolute differences is %d." 
      % (result[0][0],result[0][1]))
```

    The number that gives the smallest absolute difference is 4.  The sum of absolute differences is 20.


We can generalize the logic in the cell above into a that operates on a list of numbers.  Let us make the convention that we enumerate the elements $s_j \in S$ starting with $0$, so if $S$ has three elements then $S=\left\{s_0,s_1,s_2\right\}.$  We know the number we should need to subtract from the elements of $s_j \in S$ should be 
\begin{equation}
\underset{i} {\mathrm{argmin}} \sum_{j=0}^{\left|S\right|-1} \left|s_j-i\right|.
\label{equ:argmin-1}
\end{equation}

We do not claim this range to search is optimal, but we show that we can choose a range that is guaranteed to find the value of $i$ that minimizes the expression above.  Let $s_{\text{max}}=\text{max}\left(S\right)$ be the largest element in $S$, and let $s_{\text{min}}=\text{min}\left(S\right)$ be the smallest element in $S$.  Then the value of $i$ that satisfies equation \ref{equ:argmin-1} is in the closed interval  $\left[ s_{\text{min}}, s_{\text{max}} \right].$ 

To see why this is so, if we use any number $k$ less than the smallest element $s_{\text{min}}$ or greater than the largest element $s_{\text{max}}$ then we are adding at least $\left|s_{\text{min}}-k\right|$ or $\left|s_{\text{max}}-k\right|$ unnecessarily to every term in the sum.


```python
def find_min_abs_diff_val(s):
    """
    returns the integer that when subtracted from every element of s
    gives the smallest sum of the absolute values of all the differences.
    for clarification see the section titled, "Smallest Absolute Deviation,"
    in http://onlinestatbook.com/2/summarizing_distributions/what_is_ct.html
    """
    s.sort()
    s_min=s[0]
    s_max=s[len(s)-1]
    result=[]
    for i in range(s_min, s_max, 1):
        sum=0
        for j in s:
            sum += abs(j-i)
        result.append((i,sum))
    result.sort(key=lambda x: x[1])
    return result[0][0]

print(find_min_abs_diff(s))
```

    4


078fd1a35bbc73b87465f579d1fd9f2d3a1d32e2
ipynb
Jupyter Notebook
ch-3/smallest-absolute-difference.ipynb
jhancock1975/online-status-book-exercises
70059beffc7f8b2ce84a4bb5c6bcbaf8eda339fa
Apache-2.0
ch-3/smallest-absolute-difference.ipynb
jhancock1975/online-status-book-exercises
70059beffc7f8b2ce84a4bb5c6bcbaf8eda339fa
Apache-2.0
ch-3/smallest-absolute-difference.ipynb
jhancock1975/online-status-book-exercises
70059beffc7f8b2ce84a4bb5c6bcbaf8eda339fa
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES