R:生成 P 的倍数中 N 权重的所有排列
我需要创建一个函数(在 R 中):
- 给定N个可能的变量来赋予权重;
- 创建所有可能的权重排列(总和为 100%);
- 受权重必须以 P 的倍数出现的约束(通常为 1%)
显然,因为 N 和 P 成反比 - 即我无法指定 N=7,并且P=0.4。但是,我希望能够仅指定整数解,即 P=0.01。
抱歉,如果这是一个众所周知的问题 - 我不是数学家,我已经使用我知道的术语进行了搜索,但没有找到足够接近的内容。
我会发布我编写的代码,但是..它并不令人印象深刻或富有洞察力。
感谢您的帮助!
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%)
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.
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设权重的顺序很重要,这些是组合;如果不这样做,那么这些就是分区。无论哪种情况,它们都受到部件数量的限制,即您所说的 N,尽管后面的代码使用
numparts
。还有一个问题是权重是否允许为0。由于您希望权重之和为 1,因此需要 1/p 为整数,在以下代码中为
sumparts
;它不取决于权重的数量。获得成分后,您可以将它们乘以 p,即除以n
,以获得权重。R 有一个
partitions
包来生成此类组合或受限分区。以下代码应该是不言自明的:矩阵中的每一列都是一组权重。我取了 7 个权重,p=0.1 或 10%,禁止权重为 0:这给出了 84 种可能性;允许权重为 0 意味着有 8008 种可能性。当 p=0.01 或 1% 时,如果权重不为 0,则有 1,120,529,256 种可能性,如果权重为 0,则有 1,705,904,746 种可能性。如果顺序不重要,请使用restrictedparts
而不是compositions
。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.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 byn
, to get your weights.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, userestrictedparts
instead ofcompositions
.编辑:函数已更新,因为它两次给出了一些结果。
您可以尝试这个基于递归计算的函数。无论顺序如何,它都会为您提供所有可能的组合。我已经这样做了,否则您将获得具有所有可能排列的多个行。
计算基于整数。最小权重P设为1,Pint成为可分的重量单位数。 max.W 将是可以赋予一个变量的最大单位数。
该算法如下:
如果 N=2,则对给定的最小和最大权重进行所有可能的组合。
如果N> 2、应用该算法,N=1到ceiling(max.weight / N),最大权重指定为当前最大权重+1减去N,最小权重为N。
这将给出所有可能的整数组合。与 P 相乘得出原始权重。
或者在函数中:
这给出了矩阵列中的组合:
请注意!使用您提供的测试用例(7 个变量,跳跃 0.01),您将为大量可能性计算很长的时间。当 N=7 且 P=0.04 时,您已经有 3555 种可能的组合。当 N=0.2 时,就有 336,443 种可能性。如果这就是你所追求的,你必须考虑这些组合的每一种可能的排列。
EDIT : function is updated, as it gave some results twice.
You can try this function, based on recursive calculation. It will give you all possible combinations, regardless of the order. I've done it this way, as you otherwise get a multiple of the rows with all possible permutations.
The calculation is based on integers. The minimum weight P is set as 1, and Pint becomes the number of weight units that can be divided. max.W will be the maximum amount of units that can be given to one variable.
The algorithm goes as follows :
if N=2, then make all possible combinations for the given minimum and maximum weight.
if N > 2, apply this algorithm for N = 1 to ceiling(max.weight / N), with the maximum weight specified as the current maximum weight +1 minus N, and the minimum weight as N.
This gives you all possible combinations of integers. Multiplication with P gives the original weights.
Or in function :
This gives the combinations in columns of a matrix :
Be warned! with the testcase you gave (7 variables, jumps of 0.01), you'll be calculating a very long time for the huge amounts of possibilities. With N=7 and P=0.04, you have already 3555 possible combinations. With N=0.2, that becomes 336,443 possibilities. And you have to take into account every possible permutation of these combinations if that is what you're after.