生成与指定百分比成比例的序列的算法

时间:2012-07-10 20:19:06

标签: algorithm language-agnostic sequences random-sample

给出一个物体地图和指定的比例(假设它们加起来可以很容易达到100):

val ss : Map[String,Double] = Map("A"->42, "B"->32, "C"->26)

如何生成一个序列,使得对于大小为n的子集,有~42%的“A”,~32%的“B”和~26%的“C”? (显然,小n会有更大的错误)。

(工作语言是Scala,但我只是要求算法。)

更新:我拒绝了一种随机方法,因为例如,序列以AA开头的可能性约为16%,而BB的可能性约为11%。对于n精确==(比例之和),分布将是完美的,这将是非常低的几率。所以,按照@ MvG的回答,我实现如下:

/**
Returns the key whose achieved proportions are most below desired proportions
*/
def next[T](proportions : Map[T, Double], achievedToDate : Map[T,Double]) : T = {
    val proportionsSum = proportions.values.sum
    val desiredPercentages = proportions.mapValues(v => v / proportionsSum)
    //Initially no achieved percentages, so avoid / 0 
    val toDateTotal = if(achievedToDate.values.sum == 0.0){
        1
    }else{
        achievedToDate.values.sum
    }
    val achievedPercentages = achievedToDate.mapValues(v => v / toDateTotal)
    val gaps = achievedPercentages.map{ case (k, v) =>
        val gap = desiredPercentages(k) - v
        (k -> gap)
    }
    val maxUnder = gaps.values.toList.sortWith(_ > _).head
    //println("Max gap is " + maxUnder)
    val gapsForMaxUnder = gaps.mapValues{v => Math.abs(v - maxUnder) < Double.Epsilon }
    val keysByHasMaxUnder = gapsForMaxUnder.map(_.swap)
    keysByHasMaxUnder(true)
}

/**
Stream of most-fair next element 
*/
def proportionalStream[T](proportions : Map[T, Double], toDate : Map[T, Double]) : Stream[T] = {
    val nextS = next(proportions, toDate)
    val tailToDate = toDate + (nextS -> (toDate(nextS) + 1.0))
    Stream.cons(
        nextS,
        proportionalStream(proportions, tailToDate)
    )
}

使用时,例如:

val ss : Map[String,Double] = Map("A"->42, "B"->32, "C"->26)
val none : Map[String,Double] = ss.mapValues(_ => 0.0)
val mySequence = (proportionalStream(ss, none) take 100).toList
println("Desired : " + ss)
println("Achieved : " + mySequence.groupBy(identity).mapValues(_.size))
mySequence.map(s => print(s))
println

产生:

Desired : Map(A -> 42.0, B -> 32.0, C -> 26.0)
Achieved : Map(C -> 26, A -> 42, B -> 32)
ABCABCABACBACABACBABACABCABACBACABABCABACABCABACBA
CABABCABACBACABACBABACABCABACBACABABCABACABCABACBA

5 个答案:

答案 0 :(得分:3)

对于序列的每个项目,计算在0(包括)和100(不包括)之间等分的(伪)随机数 r

  • 如果0≤ r &lt; 42,取A
  • 如果42≤ r &lt; (42 + 32),取B
  • 如果(42 + 32)≤ r &lt; (42 + 32 + 26)= 100,取C

答案 1 :(得分:3)

对于确定性方法,最明显的解决方案可能是:

  • 跟踪到目前为止序列中每个项目的出现次数。
  • 对于下一个项目,选择预期和实际计数(或者比例,如果您愿意)之间的差异最大的项目,但仅限于预期计数(分配比例)大于实际计数。
  • 如果存在并列关系,请以任意但确定的方式将其分解,例如:选择按字母顺序排列的最低项目。

这种方法将确保以这种方式生成的无限序列的每个前缀最佳地遵守规定的比率。

快速&amp;脏python概念证明(不要指望任何变量“名称”有任何意义):

import sys

p = [0.42, 0.32, 0.26]
c = [0, 0, 0]
a = ['A', 'B', 'C']
n = 0

while n < 70*5:
    n += 1
    x = 0
    s = n*p[0] - c[0]
    for i in [1, 2]:
        si = n*p[i] - c[i]
        if si > s:
            x = i
            s = si
    sys.stdout.write(a[x])
    if n % 70 == 0:
        sys.stdout.write('\n')
    c[x] += 1

生成

ABCABCABACABACBABCAABCABACBACABACBABCABACABACBACBAABCABCABACABACBABCAB
ACABACBACABACBABCABACABACBACBAABCABCABACABACBABCAABCABACBACABACBABCABA
CABACBACBAABCABCABACABACBABCABACABACBACBAACBABCABACABACBACBAABCABCABAC
ABACBABCABACABACBACBAACBABCABACABACBACBAABCABCABACABACBABCABACABACBACB
AACBABCABACABACBACBAABCABCABACABACBABCAABCABACBACBAACBABCABACABACBACBA

答案 2 :(得分:1)

子集中每个条目的数量将与地图中的相同,但应用了比例因子。

缩放系数为n/100

因此,如果n为50,那么您将拥有{ Ax21, Bx16, Cx13 }

根据自己的喜好随机输入订单。

答案 3 :(得分:0)

最简单的“确定性”[就每个类别的#elements而言]解决方案[IMO]将是:按预定义顺序添加元素,然后随机播放结果列表

首先,从每个元素添加map(x)/100 * n个元素x选择如何处理整数算术以避免一个元素关闭],然后随机抽取结果列表。

使用fisher-yates shuffle对列表进行混洗是很简单的,它在大多数语言中实现:例如,java有Collections.shuffle(),而C ++有random_shuffle()

在java中,它将如下:

int N = 107;
List<String> res = new ArrayList<String>();
for (Entry<String,Integer> e : map.entrySet()) { //map is predefined Map<String,Integer> for frequencies
    for (int i = 0; i < Math.round(e.getValue()/100.0 * N); i++) {
        res.add(e.getKey());
    }
}
Collections.shuffle(res);

答案 4 :(得分:0)

这是不确定的,但是给出了接近MvG的值的分布。它遇到的问题是它可以在开始时给予AAA权利。鉴于它是如何证明我对MvG的异议是错误的(并且我不期望任何赞成票),我在这里发布它是为了完整性。

现在,如果有人知道expand函数是确定性的并且不会仅仅复制MvG的方法(使calc函数无效),我全都听见了!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>ErikE's answer</title>
</head>
<body>
<div id="output"></div>
<script type="text/javascript">
if (!Array.each) {
   Array.prototype.each = function(callback) {
      var i, l = this.length;
      for (i = 0; i < l; i += 1) {
         callback(i, this[i]);
      }
   };
}

if (!Array.prototype.sum) {
   Array.prototype.sum = function() {
      var sum = 0;
      this.each(function(i, val) {
         sum += val;
      });
      return sum;
   };
}

function expand(counts) {
   var
      result = "",
      charlist = [],
      l,
      index;
   counts.each(function(i, val) {
      char = String.fromCharCode(i + 65);
      for ( ; val > 0; val -= 1) {
         charlist.push(char);
      }
   });
   l = charlist.length;
   for ( ; l > 0; l -= 1) {
      index = Math.floor(Math.random() * l);
      result += charlist[index];
      charlist.splice(index, 1);
   }
   return result;
}

function calc(n, proportions) {
   var percents = [],
      counts = [],
      errors = [],
      fnmap = [],
      errorSum,
      worstIndex;

   fnmap[1] = "min";
   fnmap[-1] = "max";

   proportions.each(function(i, val) {
      percents[i] = val / proportions.sum() * n;
      counts[i] = Math.round(percents[i]);
      errors[i] = counts[i] - percents[i];
   });

   errorSum = counts.sum() - n;
   while (errorSum != 0) {
      adjust = errorSum < 0 ? 1 : -1;
      worstIndex = errors.indexOf(Math[fnmap[adjust]].apply(0, errors));
      counts[worstIndex] += adjust;
      errors[worstIndex] = counts[worstIndex] - percents[worstIndex];
      errorSum += adjust;
   }
   return expand(counts);
}

document.body.onload = function() {
   document.getElementById('output').innerHTML = calc(99, [25.1, 24.9, 25.9, 24.1]);
};
</script>
</body>
</html>