I am stuck to this code and particularly the point F=[ones(size(x)) x x.^2]
. What does it do mathematically? I cannot understand what the heck the matrix has to do with this least-squares regression.
I think it should do something like:
$\begin{align} \min \sum_{i}&r_{i}\\ s.t. \\ r_{i}=(o_{i}- &h_{i})^2 \end{align} $
where $r_{i}$ is the distance between the observed value $o_{i}$ and the plotting value $h_{i}$.
I do not have access to MATLAB so please refer to some free software such as R or Python if you are going to demo.
x=[1 3 5 7 9 11]' y=[0.0100 2.3984 11.0256 4.0272 0.2408 0.0200]' plot(x,y,'r*') hold on % Model a_1+a_2*x+a_3*x^2 F=[ones(size(x)) x x.^2] a=F\y % solves F*a=y with Least-Squares l=max(x)-min(x); % greatest distance between x -points t=[floor(min(x)):l/100:ceil(max(x))]'; % plot distance a bit greater plot(t,[ones(size(t)) t t.^2]*a,'b') % [ones(size(t)) t t.^2]*a calcs fits in t legend('Data points','Fits') axis([0,12,-2,15]) SSE=sum((y-F*a).^2) % a sum SST=sum((y-mean(y)).^2) % total sum rSquared=1-SSE/SST % R-squared fun = @(x)(-a(1)-a(2)*x-a(3)*x^2) % -1 * fit, cos fminsearch looks for min x_max=fminsearch(fun,6) plot(x_max,a(1)+a(2)*x_max+a(3)*x_max^2,'o') hold off
...this code is supposed to do the same thing:
x = [1 3 5 7 9 11]’; y = [0.0100 2.3984 11.0256 4.0272 0.2408 0.0200]’; X = [x.^0 x x.^2]; % Model: y = b1 + b2x + b3x^2 -> y = Xb x_grid = 0:0.01:11; % grid for drawing b1_hat = inv(X’*X)*X’*y; % Least squares y1_hat = b1_hat(1)*x_grid.^0 + b1_hat(2)*x_grid.^1 +... b1_hat(3)*x_grid.^2; figure(1) % Drawing the fitted polynomial plot(x,y,’*’,x_grid,y_hat) optimal1 = max(y_hat)
...I am now confused by this X = [x.^0 x x.^2];
, the logic seems the same as earlier but I cannot understand yet it.