问题描述
我正在尝试生成 100 个范围为 [0.005, 0.008] 且总和为 1 的均匀随机数.我正在寻找几个与我的担忧相关的问题,但我没有找到答案.谁能给我一个建议?
I am trying to generate 100 uniform random numbers in range [0.005, 0.008] with sum of one. I was looking to several questions which were relevant to my concerns but I did not find my answer.Could anyone give me a suggestion?
推荐答案
首先,我将稍微修改您的示例,假设 100 个变量受 [0.008, 0.012] 的限制并且它们的总和为 1(这确保在您采样的集合中有可行的点).
To start, I'm going to slightly modify your example, assuming the 100 variables are bounded by [0.008, 0.012] and that they sum to 1 (this ensures there are feasible points in the set you're sampling).
命中并运行"算法统一采样n 维空间的有界子集.对于您的情况,我们有 n=100 个维度;让我们定义相应的变量x_1, x_2, ..., x_100
.然后我们有三种类型的约束来限制我们想要从中采样的空间区域.
The "hit and run" algorithm uniformly samples over a bounded subset of an n-dimensional space. For your case, we have n=100 dimensions; let's define corresponding variables x_1, x_2, ..., x_100
. Then we have three types of constraints to bound our region of the space we want to sample from.
变量的下限为 0.008 -- 这可以通过以下线性不等式来捕获:
Variables are lower bounded by 0.008 -- this can be captured by the following linear inequalities:
x_1 >= 0.008
x_2 >= 0.008
...
x_100 >= 0.008
变量的上限为 0.012 -- 这可以通过以下线性不等式来捕获:
Variables are upper bounded by 0.012 -- this can be captured by the following linear inequalities:
x_1 <= 0.012
x_2 <= 0.012
...
x_100 <= 0.012
变量总和为 1 -- 这可以通过以下方式捕获:
Variables sum to 1 -- this can be captured by:
x_1 + x_2 + ... + x_100 = 1
假设我们想要获得 10 组均匀分布在我们空间内的变量.然后我们可以通过以下方式在R中使用hitandrun
包:
Let's say we wanted to get 10 sets of variables that are uniformly distributed within our space. Then we can use the hitandrun
package in R in the following way:
library(hitandrun)
n <- 100
lower <- 0.008
upper <- 0.012
s <- 1
constr <- list(constr = rbind(-diag(n), diag(n), rep(1, n), rep(-1, n)),
dir = rep("<=", 2*n+2),
rhs = c(rep(-lower, n), rep(upper, n), s, -s))
samples <- hitandrun(constr, n.samples=10)
dim(samples)
# [1] 10 100
请注意,这需要很长时间才能运行(在我的情况下略少于 2 小时),因为我们在高维空间(维度 n=100)中进行采样,并确保命中和运行算法的均匀采样实际上为它绘制的每个样本执行 O(n^3) 次迭代.您可以通过调整函数的 thin
参数来减少运行时间,尽管这可能会影响绘制的独立性.
Note that this takes quite a long while to run (slightly less than 2 hours in my case) because we are sampling in a high-dimensional space (dimension n=100), and to ensure uniform samples the hit and run algorithm actually performs O(n^3) iterations for each sample it draws. You may be able to decrease the runtime my adjusting the thin
parameter to the function, though this could affect the independence of your draws.
这篇关于生成 N 个和为 1 的均匀随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!