在 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 规则(此处(,我正在寻求有关如何将其推广到二元正态的帮助(特别是下面的步骤 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 变得不相关(然后协方差矩阵将对角线。你不需要它成为标识,因为 anywya 你稍后会规范化(。然后,您可以将您称为"第二个选项"的转换单独应用于新的 x 和 y。很快,您需要对角化协方差矩阵。

作为旁注,您的代码加/减 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]样本。

最新更新