Practice 24 25
Practice 24 25
Practice 24 25
%Practice_24_25
x=places;
[a,b]=size(x);
%1)
xstd=zeros(a,b);
m=mean(x);
d=std(x);
mm=zeros(a,b);
for i=1:b
mm(1:a,i)=m(1,i);
xstd(1:a,i)=(x(1:a,i)-mm(1:a,i))./d(1,i);
end
xstd=xstd;% standardized matrix
Co=cov(xstd);
[Var,M]=var(xstd)
%2)
[U,S,V] = svd(xstd);
%3)
[coeff,score,latent,tsquared,explained,mu] = pca(x);
scatter3(score(:,1),score(:,2),score(:,3))
axis equal
xlabel('1st Principal Component')
ylabel('2nd Principal Component')
zlabel('3rd Principal Component')
%4
biplot(coeff(:,1:2),'scores',score(:,1:2),'varlabels',{'v_1','v_2','v_3','v_4','v_
5','v_6','v_7','v_8','v_9'});
%5
Xcentered = score*coeff'
%The new data in Xcentered is the original ingredient data centered by subtracting
the column means from the corresponding columns.
coeff: returns any output arguments from previous syntaxes using additional options
for computing and handling special data types, specified by one or more pairs of
Name,Value arguments. For example, you can specify the number of principal components
that pca returns or the use of an algorithm other than SVD. returns the coefficients
of principal components, also known as loads, for the data matrix n by p, X. The
rows of X correspond to observations and the columns correspond to variables. The
matrix of coefficients is p times p. Each coeff column contains coefficients for a
major component, and the columns are in a descending order of component variance. By
default, pca centers the data and uses the singular value decomposition (SVD)
algorithm.
score,latent: Returns the scores of the principal components in Score and the
variances of the Principal Components in Latent. You can use any of the input
arguments from the preceding syntax. Principal component scores are representations
of X in the space of principal components. The score rows correspond to observations
and the columns correspond to the components. The variances of the principal
components are eigenvalues of the covariance matrix of X.
tsquared: returns the Hotelling T-squared statistic for each observation in X.
explained,mu: returns explained, the percentage of the total variance explained by
each principal component, and mu, the estimated mean of each variable in X.
explained =
75.2903
13.5940
5.0516
3.3194
1.4752
0.7428
0.2862
0.2066
0.0338
The first major component is a unique axis in space. When you project each observation
onto that axis, the resulting values form a new variable. The variance of this
variable is the maximum among all possible options of the first axis.
The second main component is another axis in space, perpendicular to the first.
Projecting observations onto this axis generates another new variable. The variance
of this variable is the maximum among all possible options of this second axis.
The entire set of principal components is the same size as the original set of
variables. However, it is common for the sum of the variances of the first principal
components to exceed 80% of the total variance of the original data. In this case
it happens with the first two components, so the first three components explain 93.8%
of all variability. The representation of data in the space of the first three main
components is then visualized.
The data show the greatest variability along the first major component axis. This is
the greatest possible variance among all possible options of the first axis. The
variability along the second major component axis is the greatest among all possible
options remaining on the second axis. The third major component axis has the third
largest variability, which is significantly less than the variability along the
second major component axis. The other axes of major components fourth to ninth
explain a low percentage of all data variability.
Mean:mu=m
mu =
1.0e+03 *
m =
1.0e+03 *
coeff =
1.0e+04 *
1.0e+07 *
2.4414
0.4408
0.1638
0.1076
0.0478
0.0241
0.0093
0.0067
0.0011
Each score column corresponds to a major component. The latent vector stores the
variances of the nine principal components.
tsquared =
8.4050
5.1931
3.2688
9.4194
6.1923
3.3970
5.9722
3.2165
4.6678
4.7782
15.9169
22.7917
6.0554
5.0675
9.3016
6.4867
4.6551
7.5485
5.8749
21.3506
15.4678
7.5842
7.2807
9.8120
5.4753
13.8910
3.9544
10.3084
4.6434
4.3817
7.7022
…
…
…
S =
33.4353 0 0 0 0 0 0 0 0
0 19.9546 0 0 0 0 0 0 0
0 0 19.3496 0 0 0 0 0 0
0 0 0 17.3799 0 0 0 0 0
0 0 0 0 15.7187 0 0 0 0
0 0 0 0 0 14.3814 0 0 0
0 0 0 0 0 0 12.7169 0 0
0 0 0 0 0 0 0 10.2136 0
0 0 0 0 0 0 0 0 6.2843
%4) The nine variables are represented in this biplot by a vector, and the direction and
length of the vector indicate how each variable contributes to the two major components on
the graph.
Practice 25
We assume the matrix defining the problem is of full rank. Given A ꞓ ₵𝑚𝑥𝑛 of full rank, m ≥
n, b ꞓ ₵𝑚 , find x ꞓ ₵𝑛 such that ||b- Ax || is minimized.The solution x and the corresponding
point y = Ax that is closest to b in range(A) are given by x = A'b, y = Pb, where A’ꞓ ₵𝑛𝑥𝑚 is
the pseudoinverse of A and P = AA’ꞓ ₵𝑚𝑥𝑚 ,is the orthogonal projector onto range(A).
The map from vectors of coefficients of polynomials p of degree < n=15 to vectors
(p(x1),p(x2),…,p(xm)) of sampled polynomial values is linear. Any linear map can be expressed
by an mxn Vandermond matrix: A=[x^0 x^1 … x^14], where x=t
So:
%1
n=15<14, m=100 and x=t : p(x0)=a0*t^0=a0*a0’,because P = AA’ so t^0=a0’ and t^0’=a0. The
structure of the coefficient matrix A is A=[ t’^0 t’^1 t’^2 … t’^14] or A= fliplr(vander(t'))
and the right hand side b of the LSP problem Ax = b obtained after imposing that the 100
nodes of the form (ti, f(ti)) are in the graph of the polynomial p(x), is b= exp^(sin(4*t)),
because f(x)=y=Ax=b. The size of the matrix A is mxn (100x15) and the vector b is mx1 (100x1).
%2
m=100;% number of nodes
n=15;% polynomial degree =14<15
t = (0:1:99)/99;% Since we must discretize in the interval [0,1], the values range
from 0-99 and divide /99
A = fliplr(vander(t));% Vandermonde matrix mxn:100x15
A = A(1:100,1:15);% We are left with the part of the matrix that concerns us,
since this function returns an mxm matrix and ours is only mxn
b=exp(sin(4*t'));
bnorm=b/ 2006.787453080206;
%3
x_inv=pinv(A)*bnorm;
x15_inv=x_inv(15,1)% 1.0000
%4 5
[Q,R] = qr(A);
x_qr= R\(Q\bnorm);
x15_qr=x_qr(15,1)% 1.0000
%6
x_md=A\bnorm;
x15_md=x_md(15,1)% 1.0000
%7
x=A\b;
exact_x15=x(15,1)/ x(15,1);%
%8
The errors are very small, of the order of E-08, which means that the polynomial
approximation is good and the polynomial of grade 14 of this type minimizes vertical
error of least squares.
%9
x=A\bnorm;
y= A*x; % least squares problem
K_2= cond(A) % 2.2718e+10
theta = asin(norm(bnorm-y)/norm(bnorm)) % 3.7461e-06
eta = norm(A)*norm(x)/norm(y) % 2.1036e+05
%10
forward error ≲ condition number × backward error (where a≲b means that a ≤ b and
a⋍b).
𝑓𝑜𝑟𝑤𝑎𝑟𝑑𝑒𝑟𝑟𝑜𝑟
≲ conditionnumber
𝑏𝑎𝑐𝑘𝑤𝑎𝑟𝑑𝑒𝑟𝑟𝑜𝑟
𝑓𝑒xinv
= 1.6158e − 08 ≲ C ≪ 1
be
𝑓𝑒xqr
= 1.6151e − 08 ≲ C ≪ 1
be
𝑓𝑒xcd
= 9.1820e − 08 ≲ C ≪ 1
be
be=norm(bnorm(15,1)-b(15,1))/norm(b(15,1)) % 0.9995
Cinv=1.6150e-08/be % 1.6158e-08 << 1
Cqr=1.6143e-08/be % 1.6151e-08 << 1
Cmd=9.1774e-08/be % 9.1820e-08 << 1
We can interpret C as an amplification factor of the change in the input data and
in this case it takes values << 1, therefore, the errors are in accordance with the
limit of the condition number to solve the LSP provided in the course notes, the
problem is well conditioned and the solution of the problem is stable and there is
a low sensitivity to errors in the input data.
%11
These results have the same order of accuracy.