% ********** 协方差矩阵的matlab理解 ********** %
clc;clear all;close all;
% ******一维数据的分析方法——方差 *******
sigma = 1;
x = sigma .* (randn(100000,1)-1);
figure(1)
ksdensity(x);
grid on;
title('概率分布 \sigma = 1');
% ****** 一维数据的分布特性 *****
%————————————————————————————————————
% ******二维到N维数据的分析方法——方差 *******
% 构造一组二维数据:
x = 5.*randn(1,1000); % 标准差为5
y = 1.*randn(1,1000);
D = [x;y];
figure
plot(D(1,:),D(2,:),'.');
hold on
ksdensity(x);
hold on
ksdensity(y');
grid on;
title('二维分布');
axis equal;
% —————————————————————— %
% ***** 多元正态分布与线性变换1 *****
N = 1000;
x = 4.*randn(1,N);
y = 1.*randn(1,N);
D = [x;y];
new_D = rotate_2D(45)*D; %旋转角度45度
figure
plot(new_D(1,:),new_D(2,:),'.');
grid on;
axis equal;
% ***** 多元正态分布与线性变换2 *****
N = 1000;
x = 1.*randn(1,N);
y = 1.*randn(1,N);
D = [x;y];
T = rotate_2D(45)*[4 0;0 1]; %T=R*S
new_D = T*D;
figure
plot(new_D(1,:),new_D(2,:),'.');
grid on;
axis equal
% —————————————————————— %
N = 1000;
x = 1.*randn(1,N);
y = 1.*randn(1,N);
D = [x;y];
T = rotate_2D(45)*[4 0;0 1];
new_D = T*D;
Rxx = new_D*new_D'./(N-1);
[V,D] = eig(Rxx); %矩阵特征值分解
figure
plot(new_D(1,:),new_D(2,:),'.');
grid on;
axis equal;
hold on;
quiver(0,0,V(1,1).*sqrt(D(1,1)),V(2,1).*sqrt(D(1,1)),'LineWidth',3)
quiver(0,0,V(1,2)*sqrt(D(2,2)),V(2,2)*sqrt(D(2,2)),'LineWidth',3)
legend('数据分布','特征向量1','特征向量2')