Hi
I was searching for stuff to do a new tutorial about and I suddenly found this on an unfinished mod, I cant remember the use I was going to give to it.
The function creates a linear regression from 2 arrays, one of the x values of some points and another one of the y values of those points, once its created it evaluates it in one point.
Example of use:
Thanks for reading
I was searching for stuff to do a new tutorial about and I suddenly found this on an unfinished mod, I cant remember the use I was going to give to it.
The function creates a linear regression from 2 arrays, one of the x values of some points and another one of the y values of those points, once its created it evaluates it in one point.
Code:
RegLine( x, y, point )
{
x2 = 0;
xt = 0;
yt = 0;
xy = 0;
for( i = 0; i < x.size; i ++ )
{
x2 += ( x[i] * x[i] );
yt += y[i];
xt += x[i];
xy += ( x[i] * y[i] );
}
b = ( x.size * xy - xt * yt ) / ( x.size * x2 - xt * xt );
a = ( yt - b * xt ) / x.size;
result = b * point + a;
return result;
}
Example of use:
Spoiler (Click to View)