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.