# Using absolute error as the cost function

This is related to my previous post Minimize distance between curves.

I have a dataset with values of multiple curves. An example plot is shown below. I want to scale the curves (move up/down) so that all curves overlap.

The following is a sample dataset which includes that data points corresponding to 5 curves and coordinate inputs below

scale =  1.5;
x1 = [0,4,6,10,15,20]*scale;
y1 =  [18,17.5,13,12,8,10];
x2 = [0,10.5,28]*scale;
y2= [18.2,10.6,10.3];
x3 = [0,4,6,10,15,20]*scale;
y3 = [18,13,15,12,11,9.6];
x4 = [9,17,28]*scale;
y4 = [5,5.5,7];
x5 = [1,10,20]*scale;
y5 = [3,0.8,2];

plot(x1,y1, '*-', x2, y2,  '*-', x3, y3,  '*-', x4, y4,  '*-', x5, y5,  '*-')


To scale the curves, I need to find the scale factor for each curve.

In the answer posted in the previous post (https://scicomp.stackexchange.com/a/41555/29087) the following has been suggested.

$$\min_{a} \left(\sum_i ( a \cdot f_i -g_i)^2\right)$$ which leads to a single equation $$0 = \partial_a \left(\sum_i ( a \cdot f_i -g_i)^2\right) = 2 \sum_i f_i (a \cdot f_i -g_i)$$ and thus $$a = \frac{\sum f_i g_i}{\sum f_i^2}$$

In the above answer, the cost function is the sum of absolute error, and differentiating this gives a.

Excuse me for the naive question,

I would like to understand how the cost function has to be defined if we want to minimize absolute error instead of squared error and how to estimate the scale factor.

Suggestions will be really appreciated.

EDIT: $$\sum_i abs(a*f_i - g_i)$$

min: $$\sum_i u_i$$ such that,

$$f_i * a - u_i <= g_i$$,

$$-f_i * a - u_i <= -g_i$$

I started setting up the problem like the below

% Loss Function (absolute error loss)
% input curves
scale =  1.5;
x1 = [0,4,6,10,15,20]*scale;
y1 =  [18,17.5,13,12,8,10];
x2 = [0,10.5,28]*scale;
y2= [18.2,10.6,10.3];

% y2 is the target function and y1 is the function to be shifted
f = y1;
g = interp1(x2,y2,x1);

% linprog inputs
% x = linprog(f,A,b)
% i = 1:length(x1)
A = [fi - 1; -fi -1];
x = [a ui];
b = [gi -gi];

% obj = sum_i u_i (function to minimize)
% solve system


But I am not sure how to proceed after this and solve this system in MATLAB's/scipy's linprog since the function has unknown parameters.

It will be helpful if suggestions are given for solvig the above using the linprog function in scipy library.

• Maybe it's better to stay in tje other thread. In principle, you would again have to define what you mean by "absolute error". Say we use somerhing simple as $\sum_j a_j^2$. You'd then end up with a form of ridge regression. I'll write more in the other thread. Jul 6, 2022 at 16:28
• @davidhigh I actually mean error defined in the form |afi−gi|. Thank you, I will check the other thread. Jul 7, 2022 at 8:16
• I understand, but you need yomes constraints ... otherwise you can scale any curve using $a_i = 0$. This will give you zero deviation -- I'm quite sure this isn't what you are looking for. As said, one first needs to get a correct formalization of the idea. As promised, I'll post something as soon as I find the time. Jul 7, 2022 at 8:35
• @davidhigh Please have a look at my edit when you have time, I tried to set the constraints. Thank you Jul 7, 2022 at 8:44
• Yes, Natasha, the simplest illustration would be a case where you have just a collection of "single" data values $x_1,...,x_n$ and we want $\overline x$ that best approximates all of them. Minimizing the sum of squares $\sum_i (x_i - \overline x)^2$ is achieved by taking $\overline x$ to be the average (arithmetic mean) of the data values, and this is sensitive to the extreme values, far from the average. On the other hand minimizing $\sum_i |x_i - \overline x|$ is achieved by taking $\overline x$ to be the median of the data values, and is not affected by the extreme values.
– hardmath
Jul 8, 2022 at 15:06