R:生成N个权重的所有排列(P的倍数) [英] R: Generating all permutations of N weights in multiples of P
问题描述
我需要创建一个函数(在R中):
-给定 N 个可能的变量以将权重归因于
-创建所有可能的权重偏移(总计为100%);
-受权重必须以 P 的倍数(通常为1%)
I need to create a function (in R) which:
- given N possible variables to attribute weights to;
- creates all possible permuations of weights (summing to 100%);
- subject to the constraint that weights must occur in multiples of P (usually 1%)
很显然,由于N和P成反比,所以我不能指定 N = 7和 P = 0.4.但是,我只想指定整数解,即 P = 0.01.
Obviously, as N and P are inversely related - i.e. I can't specify N=7, and P=0.4. However, I'd like to be able to specify integer solutions only, i.e. P=0.01.
很抱歉,如果这是一个众所周知的问题-我不是数学家,我使用的是我知道的术语进行搜索,但是找不到足够接近的词.
Sorry if this is a well-known problem - I'm not a math person, and I've searched using terms I know, but didn't find anything close enough.
我会发布我编写的代码,但是..并不令人印象深刻或有见地.
I'd post the code I've written, but.. it's not impressive or insightful.
感谢您的帮助!
推荐答案
假设权重的顺序很重要,这些就是组成;如果没有,则为分区.无论哪种情况,它们都受零件数量(即您所说的N)的限制,尽管后面的代码使用了numparts
.还有一个问题是是否允许权重为0.
Assuming the order of the weights matters, these are compositions; if they don't then these are partitions. In either case, they are restricted by the number of parts, what you have called N, though the code which follows uses numparts
. There is also the question of whether weights of 0 are allowed.
由于您希望权重加1,因此需要1/p为整数,在下面的代码中为sumparts
;它不取决于重量的数量.有了合成后,您可以将它们乘以p,即除以n
,以获得权重.
Since you want weights to add up to 1, you need 1/p to be an integer, which in the following code is sumparts
; it does not depend on the number of weights. Once you have the compositions, you can multiply them by p, i.e. divide by n
, to get your weights.
R具有partitions
程序包以生成此类组合物或受限分区.下面的代码应该很容易说明:矩阵中的每一列都是一组权重.我取了七个权重,p = 0.1或10%,禁止权重为0:这提供了84种可能性;允许权重为0意味着8008种可能性.如果p = 0.01或1%,那么权重为0时将有1,120,529,256个可能性,而权重为0时将有1,705,904,746个可能性.如果顺序无关紧要,请使用restrictedparts
代替compositions
.
R has a partitions
package to generate such compositions or restricted partitions. The following code should be self explanatory: each column in the matrix is a set of weights. I have taken seven weights and p=0.1 or 10%, and prohibited weights of 0: this gives 84 possibilities; allowing weights of 0 would mean 8008 possibilities. With p=0.01 or 1% there would be 1,120,529,256 possibilities without weights of 0, and 1,705,904,746 with. If order does not matter, use restrictedparts
instead of compositions
.
> library(partitions)
> numparts <- 7 # number of weights
> sumparts <- 10 # reciprocal of p
> weights <- compositions(n=sumparts, m=numparts, include.zero=FALSE)/sumparts
> weights
[1,] 0.4 0.3 0.2 0.1 0.3 0.2 0.1 0.2 0.1 0.1 0.3 0.2 0.1 0.2 0.1 0.1 0.2 0.1 0.1
[2,] 0.1 0.2 0.3 0.4 0.1 0.2 0.3 0.1 0.2 0.1 0.1 0.2 0.3 0.1 0.2 0.1 0.1 0.2 0.1
[3,] 0.1 0.1 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.1 0.1 0.1 0.2 0.2 0.3 0.1 0.1 0.2
[4,] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.2 0.2 0.2 0.2 0.2 0.2 0.3 0.3 0.3
[5,] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
[6,] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
[7,] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
[1,] 0.1 0.3 0.2 0.1 0.2 0.1 0.1 0.2 0.1 0.1 0.1 0.2 0.1 0.1 0.1 0.1 0.3 0.2 0.1
[2,] 0.1 0.1 0.2 0.3 0.1 0.2 0.1 0.1 0.2 0.1 0.1 0.1 0.2 0.1 0.1 0.1 0.1 0.2 0.3
[3,] 0.1 0.1 0.1 0.1 0.2 0.2 0.3 0.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