|
From: | Oisín Ó Cuanacháin |
Subject: | Re: polyfit with a fixed slope ?? |
Date: | Tue, 25 May 2010 16:01:09 +0100 |
You don't need to fit this---just calculate what you need; here's how.On 05/25/2010 08:20 AM, Oisín Ó Cuanacháin wrote:
Hi,
Does anyone know if there is a function to fit a line /with a specified
slope/ to some data. I have a load of data 'x' and
polyfit(1:length(x),x,1) gives me the best-fit line, with arbitrary slope.
What I want however is the best fit line with a particular slope. This
is essentially just a minimization problem and I know I could code
something up myself, but x has 100's of millions of elements I'm
guessing my coding efforts will take forever to run, so does such a nice
function already exist? It is essentially polyfit with an added
constraint that the slope is fixed.
First, let me rename the variables: I call your data as y(i), and x is the independent variable, 1:length(y). You say that your data model is y(i)=a*x(i)+b, and you know the value of a. To get the estimate of b, you minimize the residual squares sum((y(i)-a*x(i)-b)^2) with respect to b.
At minimum, the derivative of that wrt. b must be zero, which amounts to -2*sum(y(i)-a*x(i)-b)=0. You can solve it for b:
b=mean(y)-a*mean(x)
or, in your case, where x=1:length(y):
b=mean(y)-a*(1+length(y))/2
[Prev in Thread] | Current Thread | [Next in Thread] |