Given the following list of numbers:
456
386
363
402
357
323
329
354
279
279
305
340
351
409
478
543
For example, for this list, the first calculation would be
$$(456-386)-(386-363) = \color{blue}{47} \in [-59, 59]$$
This is in tolerance so no changes are needed but as we go down the list lets say to
$$(363-402)-(402-357) = \color{red}{-84} \notin [-59, 59]$$
I need help finding a way to know which of these three numbers needs to be changed so that this number is at least in tolerance. It should use minimal changes. Also needs to find a way to not affect previous calculations.
You can solve the problem via quadratic programming as follows. For $i\in\{1,\dots,n\}$, let $x_i$ be the given values, and let $y_i$ be the adjusted values. The problem is to minimize $\sum_{i=1}^n (y_i-x_i)^2$ subject to linear constraints \begin{align} -59 \le y_i - 2 y_{i+1} + y_{i+2} &\le 59 && \text{for $i\in\{1,\dots,n-2\}$} \\ y_i &\le x_i \end{align}
The resulting optimal solution has $y_i=x_i$ for all $i$ except the following: \begin{matrix} i & x_i & y_i \\ \hline 4 & 402 & 389.5 \\ 8 & 354 & 333.5 \end{matrix}
By request, here is the SAS code I used:
By request, here is Python code: