Reservoir sampling improvements

未关闭
#647 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
活跃
技术栈
java
领域
data

调研方向

未指定文件或测试。首先定位 reservoir-sampling 的实现及其序列化和合并路径,然后将提议的改进拆分为一个范围明确的变更;在就一种方案及其兼容性测试和概率测试达成一致之前,不能视为完成。

由索引模型根据 Issue 内容生成。

描述

We can make a number of improvements to reservoir sampling:

  • We can go from 2 random draws per accepted sample to 1 by picking a random long from 0 to n and accepting if it's less than k -- which also provides the location to use. This would get around the current max size bit limit (which is unlikely to be an issue, but still) by removing the use of a double for a value in [0, 1). But that'd also require a new serialization version since n is currently stored with fewer than 64 bits.
  • After discussing with a stats person about a year ago, we believe we could even switch to a random draw from the distribution for the next item, dropping the random draws to O(accepted items) rather than O(n). As long as merges happen independent of that random draw (so no adversary able to inspect it) then we should be able to merge and re-draw for the merged sample.
  • Merge can be improved to give uniform second-order probabilities. This is the exciting one for me. But there's a caveat.

Proposed merge, with python pseudocode:

# Merge two random samples. Samples are lists s1 and s2. The numbers n1 and n2
# are the sizes of the sets from which s1 and s2 were sampled.  Value s is the specified
# size for the merged sample. Requires s <= len(s1) and s <= len(s2). 
def merge(s1, s2, n1, n2, s): 
  # Find number to draw from s1. (The others will be from s2.)
  t = 0 # Number of observations to draw from s1.

  for i in range(s):
    j = random.randint(1, n1 + n2)
    
    if j <= n1:
      t += 1
      n1 -= 1
    else: n2 -= 1

  # Draw the samples.
  a = random.sample(s1, t) # Sample without replacement.
  b = random.sample(s2, s - t) # Sample without replacement.

  return a + b # Make one list with the elements of a and b.

The random sample without replacement is important here, which is the source of the caveat: You can almost use the first t and s-t samples from s1 and s2, respectively, except that there's a position bias from the initial reservoir fill. For items 0 to k-1, they can only ever appear at that specific index. We don't start placing randomly until after the reservoir is filled.

The options I can think of for this:

  1. Randomly permute items when the reservoir fills. If it hasn't filled at merge, you're just inserting as new items of weight 1 anyway. Doesn't help with already-serialized samples.
  2. Randomly permute the input sample at merge (a modified Fisher-Yates shuffle since you can stop after the first t or s-t items). But this is an undesirable side-effect for merging, even if it doesn't impact the probabilities.
  3. Copy the array and permute that. Uses more space, and potentially a lot more for a large reservoir, but avoids side-effects. Probably permute an array of integer indices to avoid creating an array of arbitrary objects.

Maybe we can mix 1 and 3? For new samples, randomly permute upon fill and track that with a flag (which would need to be serialized going forward, of course). If a sample needs to be merged and doesn't have the flag, then copy and permute the array.

This should probably have been 3 separate issues. But wanted to document the ideas before I forget yet again.

主要语言
Java
星标
958
派生
226
平均合并
3 天 40 分钟
30 天内合并 PR
13

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/datasketches-java 的其他 Issue

查看 apache/datasketches-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。