我有 5 个变量A、V、h、l和b,它们都来自不同的分布。我想通过拉丁超立方体采样的方法从每个分布中制作 1,000 个均匀分布的样本。这是一个现实的要求,即它真的比简单的随机抽样更好吗?你有任何关于我如何在 matlab 中做到这一点的参考吗?此页面表明我需要以某种方式转换样本...
更新 #2:使用 Statistics Toolbox 的内置函数的解决方案
基本问题是您是否希望您的样本在规则网格上。如果没有,您可以使用内置函数lhsdesign:
p = 1000 % Number of points
N = 5 % Number of dimensions
lb = [1 1 1 1 1]; % lower bounds for A,V,h,l and b
ub = [10 10 10 10 10]; % upper bounds for A,V,h,l and b
X = lhsdesign(p,N,'criterion','correlation');
D = bsxfun(@plus,lb,bsxfun(@times,X,(ub-lb)));
Run Code Online (Sandbox Code Playgroud)
'criterion','correlation' 会给你想要的“平等分配”。
D 然后包含参数的不规则坐标分布。
首先,我以为您是在常规网格上寻找样本,这似乎确实是一项艰巨的任务。我试图修改上面的方法D = round(bsxfun...),但它不会给你满意的结果。所以对于这种情况,我仍然在这里提供我的初步想法:
以下解决方案远非快速和优雅,但至少是一个解决方案。
% For at least 1000 samples M=6 divisions are necessary
M = 6;
N = 5;
% the perfect LHC distribution would have 1296 samples for M=6 divisions
% and 5 dimensions
counter_max = M^(N-1); %=1296
% pre-allocation
D = zeros(6,6,6,6,6);
counter = 0;
while counter < 1000
c = randi(6,1,5);
if ( sum( D( c(1) , c(2) , c(3) , c(4) , : )) < 1 && ...
sum( D( c(1) , c(2) , c(3) , : , c(5) )) < 1 && ...
sum( D( c(1) , c(2) , : , c(4) , c(5) )) < 1 && ...
sum( D( c(1) , : , c(3) , c(4) , c(5) )) < 1 && ...
sum( D( : , c(2) , c(3) , c(4) , c(5) )) < 1 )
D(c(1),c(2),c(3),c(4),c(5)) = 1;
X(counter,:) = c;
counter = counter+1;
end
end
Run Code Online (Sandbox Code Playgroud)
X 最终包含所有样本的坐标。
如您所见,我使用了带有基础 if 条件的 while 循环。您希望有 1000 个样本,这是一个现实的数字,可以在合理的时间内完成。我实际上会建议您使用尽可能接近最大值 1296 的样本数。这可能会花费您很长时间。但是当您只创建一次结果矩阵并一次又一次地使用它时,请毫不犹豫地运行它 24 小时。您还可以实现如下所述的中断代码:在 MatLab 中,是否可以终止脚本,但将其所有内部变量保存到工作区?看看在那之前你得到了多少样本。(我测试的时候20分钟拿到了900个样本)
更新:显示方法限制的示例:
下面的例子将说明提问者可能愿意做什么以及结果实际上应该是什么样子。因为我对一个好的解决方案也很感兴趣,我的解决方案有限,无法提供“100%的结果”。
想象一个N=3有多个M=10分部的立方体 ( ) 。
M = 10;
N = 3;
counter_max = M^(N-1); %=100 maximum number of placeable samples
% pre-allocations
D = zeros(10,10,10);
counter = 0;
while counter < counter_max
c = randi(10,1,3);
% if condition checks if there is already a sample in the same row,
% coloumn or z-coordinate,
if ( sum( D( c(1) , c(2) , : )) < 1 && ...
sum( D( c(1) , : , c(3) )) < 1 && ...
sum( D( : , c(2) , c(3) )) < 1 )
%if not a new sample is generated
D(c(1),c(2),c(3)) = 1;
counter = counter+1;
X(counter,:) = c;
end
end
Run Code Online (Sandbox Code Playgroud)
经过大约 10000 次迭代后,100 个可能放置的样本中的 85 个得到以下分布:
其中颜色表示到最近邻居的归一化距离。对于大多数点来说很好(1),但是由于有 15 个缺失的样本,一些点与其他点的距离更远。
问题是:我怀疑是否有可能在合理的时间内获得所有 100 个样本。当一个人在迭代次数上绘制生成的样本时,您会得到:

......所以想要的结果似乎很难获得。
请将此答案视为鼓励而不是解决方案。