将随机抽取从双变量法线转换为 Matlab 中的单位正方形

Transform random draws from a bivariate normal into the unit square in Matlab

我在 Matlab 报告中有一个 nx2 矩阵 r n 来自二元正态分布

n=1000;
m1=0.3;
m2=-m1;
v1=0.2;
n=10000;
v2=2;
rho=0.5;

mu = [m1, m2];
sigma = [v1,rho*sqrt(v1)*sqrt(v2);rho*sqrt(v1)*sqrt(v2),v2];
r = mvnrnd(mu,sigma,n);

我想将这些抽奖归一化到单位正方形[0,1]^2


第一个选项

rmax1=max(r(:,1));
rmin1=min(r(:,1));
rmax2=max(r(:,2));
rmin2=min(r(:,2));
rnew=zeros(n,2);
for i=1:n
    rnew(i,1)=(r(i,1)-rmin1)/(rmax1-rmin1);
    rnew(i,2)=(r(i,2)-rmin2)/(rmax2-rmin2); 
end

第二个选项

rmin1rmax1rmin2rmax2 可能因采样过程而变化很大。另一种方法是应用 68–95–99.7 规则 (here),我正在寻求有关如何将其概括为双变量正态分布的帮助(特别是下面的第 1 步 ).这是我的想法

%Step 1: transform the draws in r into draws from a bivariate normal 
%with variance-covariance matrix equal to the 2x2 identity matrix 
%and mean equal to mu
%How?
%Let t be the transformed vector

%Step 2: apply the 68–95–99.7 rule to each column of t
tmax1=mu(1)+3*1;
tmin1=mu(1)-3*1;
tmax2=mu(2)+3*1;
tmin2=mu(2)-3*1;
tnew=zeros(n,2);
for i=1:n
    tnew(i,1)=(t(i,1)-tmin1)/(tmax1-tmin1);
    tnew(i,2)=(t(i,1)-tmin2)/(tmax2-tmin2);
 end
%Step 3: discard potential values (very few) outside [0,1]

在你的例子中,随机向量的 x 和 y 坐标是相关的,所以它不仅仅是 x 和 y 的独立变换。你首先需要旋转你的样本,使 x 和 y 变得不相关(然后协方差矩阵将是对角线的。你不需要它是身份,因为无论如何你稍后会归一化)。然后,您可以将称为“第二个选项”的转换独立应用于新的 x 和 y。很快,您需要对协方差矩阵进行对角化。

作为旁注,您的代码 adds/subtracts 3 乘以 1,而不是标准差的 3 倍。此外,您可以避免 for 循环,使用(例如)Matlab 的 bsxfun 应用矩阵和向量之间的操作:

t = bsxfun(@minus,r,mean(r,1)); % center the data
[v, d] = eig(sigma);            % find the directions for projection
t = t * v;                      % the projected data is uncorrelated  
sigma_new = sqrt(diag(d));      % that's the std in the new coordinates
% now transform each coordinate independently
tmax1 = 3*sigma_new(1);
tmin1 = -3*sigma_new(1);
tmax2 = 3*sigma_new(2);
tmin2 = -3*sigma_new(2);
tnew = bsxfun(@minus, t, [tmin1, tmin2]);
tnew = bsxfun(@rdivide, tnew, [tmax1-tmin1, tmax2-tmin2]);

如您所写,您仍然需要丢弃 [0,1] 之外的少数样本。