多维联合正态分布代码matlab(以二维为例)
作者:互联网
clc;
close all; clear;
% 多维联合高斯概率分布
%%
% 二维联合概率分布
%方法一:调包
mu = zeros(1,2); % 均值
sigma = [1 , 0.9;
0.9 , 1]; % 协方差
rng('default') % For reproducibility
X = mvnrnd(mu,sigma,1000);
y = mvnpdf(X,mu,sigma);
figure
scatter3(X(:,1),X(:,2),y, '.')
xlabel('X1')
ylabel('X2')
zlabel('Probability Density')
%%
%方法二:直接上公式
sigma2 = cov(X);
mu2 = mean(X);
y2 = sqrt(1/((2*pi)^length(mu2)*det(sigma2))) * exp(-1/2*(X-mu2)*inv(sigma2)*(X-mu2)');
y3 = zeros(1000, 1);
for i = 1: 1000
y3(i) = y2(i, i);
end
figure
scatter3(X(:,1),X(:,2),y3, '.')
xlabel('X1')
ylabel('X2')
zlabel('Probability Density')
MATLAB不配拥有代码段格式吗。。。。。
标签:mu2,为例,mu,matlab,多维,y3,1000,sigma,sigma2 来源: https://blog.csdn.net/hsuuuu1/article/details/120694802