From: Giorgio on
"John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <htpbp9$s5a$1(a)fred.mathworks.com>...
> "Giorgio " <christianjp(a)inwind.it> wrote in message <htpag1$6pv$1(a)fred.mathworks.com>...
> > "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <htp9le$epg$1(a)fred.mathworks.com>...
> > > "Giorgio " <christianjp(a)inwind.it> wrote in message <htp8ls$d5s$1(a)fred.mathworks.com>...
> > >
> > > > i would like to find out the OLS slope that passes through the smallest x, and highest y.. i.e. passing through min(x), max(y).
> > > =====
> > >
> > > I must be missing something. In the data you've given min(x) and max(y) occur at 2 separate points. So, if this is to be a linear fit passing through these 2 points, then those 2 points constrain the line completely, irrespective of the rest of data. Just connect them with a line and you're done.
> >
> > Apologies. In this example, the OLS regression should pass through point(-0.651047629509525,-0.806235701025338), second row for each vector (ignore first data point and in my post above x should be y and vice versa).
>
> Assume that the line must pass through the point
> (x0,y0).
>
> Then for column vectors X and Y, the slope of that
> line is just
>
> S = (X-x0)\(Y-x0);
>
> The equation of the line is given by the point/slope
> formula.
>
> y = S*(x - x0) + y0
>
> HTH,
> John

i am new to matlab, but my attempt appears to work

function [intercept,slope]=linfix(x,y,xcord,ycord)

% estimates OLS regression for bidemsional data set

xi=x-xcord;
yi=y-ycord;

slope=sum((xi).*(yi))/sum((xi).^2);
intercept=ycord-slope*xcord;

t=linspace(min(x),max(x),100);
plot(x,y,'x',t,t.*slope+intercept,'-');

of course, comments welcome
From: TideMan on
On May 29, 9:49 am, "Giorgio " <christia...(a)inwind.it> wrote:
> "John D'Errico" <woodch...(a)rochester.rr.com> wrote in message <htpbp9$s5....(a)fred.mathworks.com>...
> > "Giorgio " <christia...(a)inwind.it> wrote in message <htpag1$6p...(a)fred.mathworks.com>...
> > > "Matt J " <mattjacREM...(a)THISieee.spam> wrote in message <htp9le$ep....(a)fred.mathworks.com>...
> > > > "Giorgio " <christia...(a)inwind.it> wrote in message <htp8ls$d5...(a)fred.mathworks.com>...
>
> > > > > i would like to find out the OLS slope that passes through the smallest x, and highest y..  i.e. passing through min(x), max(y).
> > > > =====
>
> > > > I must be missing something. In the data you've given min(x) and max(y) occur at 2 separate points. So, if this is to be a linear fit passing through these 2 points, then those 2 points constrain the line completely, irrespective of the rest of data. Just connect them with a line and you're done.
>
> > > Apologies.  In this example, the OLS regression should pass through point(-0.651047629509525,-0.806235701025338), second row for each vector (ignore first data point and in my post above x should be y and vice versa).
>
> > Assume that the line must pass through the point
> > (x0,y0).
>
> > Then for column vectors X and Y, the slope of that
> > line is just
>
> > S = (X-x0)\(Y-x0);
>
> > The equation of the line is given by the point/slope
> > formula.
>
> >   y = S*(x - x0) + y0
>
> > HTH,
> > John
>
> i am new to matlab, but my attempt appears to work
>
> function [intercept,slope]=linfix(x,y,xcord,ycord)
>
> % estimates OLS regression for bidemsional data set
>
> xi=x-xcord;
> yi=y-ycord;
>
> slope=sum((xi).*(yi))/sum((xi).^2);
> intercept=ycord-slope*xcord;
>
> t=linspace(min(x),max(x),100);
> plot(x,y,'x',t,t.*slope+intercept,'-');
>
> of course, comments welcome

You could replace this:
slope=sum((xi).*(yi))/sum((xi).^2);
with this:
slope=xi\yi;