1.均值
2.方差
3.标准差\均方差
4.协方差
5.相关性
6.加权均值
7.加权方差
8.加权标准差
样本定义
- %%
- x=[1 2 3 3 2 1]'
- y = [1 2 3 3 1 2]'
- z= 3+2*y
1.均值
- %% 均值
- xmean=mean(x)
- sum(x)/size(x,1)
xmean =
2
ans =
2
2.方差
- %% 方差(n,可修改)
- xvar=var(x,1)
- sum((x-xmean).^2)/size(x,1)%定义
- %% 方差(n-1,可修改)
- xvar_1=var(x)
- sum((x-xmean).^2)/[size(x,1)-1]%定义
- %% 根据公式推导:E(X²)-E²(X)
- mean(x.^2)-[mean(x)]^2
xvar =
0.6667
ans =
0.6667
xvar_1 =
0.8000
ans =
0.8000
ans =
0.6667
3.标准差\均方差
- %% 标准差(n,可修改)
- xstd=std(x,1)
- sqrt( sum((x-xmean).^2)/size(x,1) )%定义
- sqrt( var(x,1) )
- %% 标准差(n-1,可修改)
- xstd_1=std(x)
- sqrt(sum((x-xmean).^2)/(size(x,1)-1))%定义
- sqrt( var(x) )
xstd =
0.8165
ans =
0.8165
ans =
0.8165
xstd_1 =
0.8944
ans =
0.8944
ans =
0.8944
4.协方差
- %% 协方差(n,可修改)
- xcov = cov(x,1)%单个向量的协方差=方差
- xycov = cov(x,y,1)
- [(x-xmean)']*[y-mean(y)]/size(x,1)%定义
- %% 协方差(n-1,可修改)
- xcov_1 = cov(x)%单个向量的协方差=方差
- xycov_1 = cov(x,y)
- [(x-xmean)']*[y-mean(y)]/(size(x,1)-1)%定义
xcov =
0.6667
xycov =
0.6667 0.5000
0.5000 0.6667
ans =
0.5000
xcov_1 =
0.8000
xycov_1 =
0.8000 0.6000
0.6000 0.8000
ans =
0.6000
5.相关性
- %% 相关系数
- xycorrcoef = corrcoef(x,y)
- xzcorrcoef = corrcoef(x,z)
- yzcorrcoef = corrcoef(y,z)
- corrcoef([x,y,z])
xycorrcoef =
1.0000 0.7500
0.7500 1.0000
xzcorrcoef =
1.0000 0.7500
0.7500 1.0000
yzcorrcoef =
1 1
1 1
ans =
1.0000 0.7500 0.7500
0.7500 1.0000 1.0000
0.7500 1.0000 1.0000
6.加权均值
- %% 加权均值
- miu_w=x'*y/sum(y)
miu_w =
2.2500
7.加权方差
- %% 加权方差
- xy_var=var(x,y)
- xy_var2=[(x-miu_w).^2]'*y/sum(y)
xy_var =
0.6875
xy_var2 =
0.6875
8.加权标准差
- %% 加权标准差
- xy_std=std(x,y)
- xy_std2=sqrt(xy_var2)
xy_std =
0.8292
xy_std2 =
0.8292
3.完整代码
- %%
- disp("样本")
- x=[1 2 3 3 2 1]'
- y = [1 2 3 3 1 2]'
- z= 3+2*y
-
- %% 均值
- disp("均值")
- xmean=mean(x)
- sum(x)/size(x,1)
-
-
- %% 方差(n,可修改)
- disp("方差(n,可修改)")
- xvar=var(x,1)
- sum((x-xmean).^2)/size(x,1)%定义
- %% 方差(n-1,可修改)
- disp("方差(n-1,可修改)")
- xvar_1=var(x)
- sum((x-xmean).^2)/[size(x,1)-1]%定义
- %% 根据公式推导:E(X²)-E²(X)
- disp("公式推导:E(X²)-E²(X)")
- mean(x.^2)-[mean(x)]^2
-
-
- %% 标准差(n,可修改)
- disp("标准差(n,可修改)")
- xstd=std(x,1)
- sqrt( sum((x-xmean).^2)/size(x,1) )%定义
- sqrt( var(x,1) )
- %% 标准差(n-1,可修改)
- disp("标准差(n-1,可修改)")
- xstd_1=std(x)
- sqrt(sum((x-xmean).^2)/(size(x,1)-1))%定义
- sqrt( var(x) )
-
-
- %% 协方差(n,可修改)
- disp("协方差(n,可修改)")
- xcov = cov(x,1)%单个向量的协方差=方差
- xycov = cov(x,y,1)
- [(x-xmean)']*[y-mean(y)]/size(x,1)%定义
- %% 协方差(n-1,可修改)
- disp("协方差(n-1,可修改)")
- xcov_1 = cov(x)%单个向量的协方差=方差
- xycov_1 = cov(x,y)
- [(x-xmean)']*[y-mean(y)]/(size(x,1)-1)%定义
-
-
- %% 相关系数
- disp("相关系数")
- xycorrcoef = corrcoef(x,y)
- xzcorrcoef = corrcoef(x,z)
- yzcorrcoef = corrcoef(y,z)
- corrcoef([x,y,z])
-
-
- %% 加权均值
- disp("加权均值")
- miu_w=x'*y/sum(y)
-
- %% 加权方差
- disp("加权方差")
- xy_var=var(x,y)
- xy_var2=[(x-miu_w).^2]'*y/sum(y)
-
- %% 加权标准差
- disp("加权标准差")
- xy_std=std(x,y)
- xy_std2=sqrt(xy_var2)
参考
均值https://ww2.mathworks.cn/help/matlab/ref/mean.html
方差https://ww2.mathworks.cn/help/matlab/ref/var.html
标准差https://ww2.mathworks.cn/help/matlab/ref/std.html
评论记录:
回复评论: