[vox-tech] OT - Finding slope of a line, with lots of points
Peter Jay Salzman
vox-tech@lists.lugod.org
Fri, 21 Mar 2003 14:12:26 -0800
hi jay,
that's called the best fit line, least squares, or linear regression.
i'm sure you can find it on google.
the main idea is:
1. draw an "abstract line" that represents the best fit with no spcified
slope and y intercept.
2. compute the sum of the distances of each data point to the best fit
line.
3. realize that the sum of all the distances is zero. after all, this
is what we mean by a "best fit line".
4. realize that the dilema is answered by summing the absolute value of
all the distances between your data points and the best fit line.
in terms of numbers, form a sum of the square of all deviations.
suppose you have N data points. the sum of deviations is:
N N
D^2 = sum (di)^2 = sum (yi - mxi - b)^2
i=0 i=0
where (yi, xi) are your data points and (m, b) characterize the best fit
line.
minimize with respect to m (the slope of the best fit line) and b (the y
intercept of the best fit line). these two equations are minimized when
their derivates are equal to zero. technically, you need to take a 2nd
derivative to make sure you didn't find maximums or inflection points,
but take my word for it right now. i've done the calculation many times
over).
so now you have two equations, in m and b, which are set equal to zero.
solve one for m, then plug m into the 2nd equation.
solve the 2nd equation for b. plug b back into m.
now you have an equation for m and b of the best fit line that only
depends on your data points, xi and yi.
i don't actually have the equation in front of me -- i derive it every
time i need it (or if a computer is around, i look it up). :)
pete
BEGIN jAY Strauss <me@heyjay.com>
> Sorry, I already googled, but can't find what I'm looking for.
>
> I remember back in science class (must have been Chem), we'd do experiments
> and plot a bunch of points on graph paper. Then we'd fudge a line that went
> thru the points (trying to get equal numbers of points on one side of the
> line as the other), and that was our "expected" slope.
>
> Is there some formula or algorithm for doing this with a computer? (I'll do
> it in Perl) Or where, under what topic would I go a googling?
>
> Thank
> Jay
>
> _______________________________________________
> vox-tech mailing list
> vox-tech@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech
--
Fingerprint: B9F1 6CF3 47C4 7CD8 D33E 70A9 A3B9 1945 67EA 951D