## Matlab

Linear Regression: fitting a parabola
```									```
%% Least squares example 11 Oct 2007
clear all; close all;
%% Ground truth latent variable theta
theta=[5; -15; 1];

%% Data: noisy parabola
N=20; % number of points
sigmanoise=5;  % standard deviation of noise
x=10*rand(N,1); % coordinate x
y=theta(1)+theta(2)*x+theta(3)*x.^2+sigmanoise*randn(N,1); % coordinate y

%% plot of the points
figure(1);plot(x,y,'b.')
%% Least Squares:
%solving the system A theta = y is replaced by finding
% theta that minimises ||A theta-y||

A=[ones(N,1) x x.^2];
ThetaEstimate=inv(A'*A)*A'*y
'to compare with Ground Truth'
theta

%% plot
t=[min(x)-1:.01:max(x)+1];
yt=ThetaEstimate(1)+ThetaEstimate(2)*t+ThetaEstimate(3)*t.^2;
figure(1);hold on, plot(t,yt,'r'), title('data in blue, fitted curve in red')

```
```
Principal Component Analysis, PCA in 2D
```									```
%%
clear all; close all;

%% Data
N=100;
x1=10*randn(N,1);
x2=4+x1+10*randn(N,1);

%% plot data
figure(1);plot(x1,x2,'x')

%% pca
X=[(x1-mean(x1))';(x2-mean(x2))'];
C=(X*X'/N); % Covariance
[U,S,V] = svd(C);

figure(1); hold on, plot(mean(x1),mean(x2),'ko'), hold on, quiver(mean(x1),mean(x2),V(1,1),V(2,1),sqrt(S(1,1)),'r'), hold on, ...
quiver(mean(x1),mean(x2),V(1,2),V(2,2),sqrt(S(2,2)),'g'),hold off, axis([min(x1)-1 max(x1)+1 min(x2)-1 max(x2)+1]), axis equal, title('Principal axis in green and red with their magnitudes scaled by their eigenvalues')

```
```