0

我将具有从混合正态分布生成向量的功能。首先,我已经完成了从以下生成向量的功能N(mu, Sigma)

function [X] = BSXgen(N,d,mu,sigma)
    A = chol(sigma);
    X = bsxfun(@plus, mu, A'*randn(d,N));
end

我可以使用它 从二维空间BSXgen(10,2,[0;0],diag([1 1]))生成向量。10

接下来,我将从混合正态分布中生成数据。我在做什么:

N=5000; %number of vectors
d=2; %dimension of space
K=5; %number of mixtures
p=0.2*ones(1,K); %probability of every mixture - in this case p1=...=p5=0.2

mu = [ [2;2] [4;1] [2;8] [4;6] [8;2]] %matrix with mu parameters for every mixture

SIGMA = diag([0.5 0.5]); %in this case - for every mixture we will have the same SIGMA matrix

U = unifrnd(0,1,1,N); %Random variable from U(0,1)

%Generating vectors from mixture
X = [];
for i=1:K
    m = size(U(U<sum(p([1:i])) & U>= sum(p([1:i-1]))));
    X = [X BSXgen(m(2),d,mu(:,i),SIGMA)];
end

%Plotting vectors
X=X';
scatter(X(:,1),X(:,2),3,'fill')

我对吗?我真的从混合物中生成数据吗?我不确定,所以我会非常感谢任何意见。

4

0 回答 0