[Algorithm] Reservoir Sampling
Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability.
To solve the problem which n size is unknown, Reservior Sampling is a perfect algorithm to use:
Reservoir sampling algorithm can be used for randomly choosing a sample from a stream of n items, where n is unknow.
Here we still need to prove that
Consider the (i)th item, with its compatibility probability of 1/i. The probability I will be choose the i at the time n > i can be demonstrated by a simple formula
i/i: Probability the ith item will be selected;
(1 - i/i+1): Probability the i+1th item will NOT be selected;
(1 - i/i+2): Probability the i+2th item will NOT be selected;
(1 - 1 / n): Probability the nth item will NOT be selected;
In the end, the probability of ith item will be selected at given n, which n > i is 1/n.
Let’s attempt to solve using loop invariants. On the ith iteration of our loop to pick a random element, let’s assume we already picked an element uniformly from [0, i - 1]. In order to maintain the loop invariant, we would need to pick the ith element as the new random element at 1 / (i + 1) chance. For the base case where i = 0, let’s say the random element is the first one.
function Reservoir_Sampling (ary) {
let selected;
const size = ary.length; for (let i = 0; i < size; i++) {
if (Math.floor(Math.random() * size) === 1) {
selected = ary[i];
break;
}
} return selected;
}
[Algorithm] Reservoir Sampling的更多相关文章
- 【算法34】蓄水池抽样算法 (Reservoir Sampling Algorithm)
蓄水池抽样算法简介 蓄水池抽样算法随机算法的一种,用来从 N 个样本中随机选择 K 个样本,其中 N 非常大(以至于 N 个样本不能同时放入内存)或者 N 是一个未知数.其时间复杂度为 O(N),包含 ...
- 算法系列:Reservoir Sampling
copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- 蓄水池采样算法(Reservoir Sampling)
蓄水池采样算法 问题描述分析 采样问题经常会被遇到,比如: 从 100000 份调查报告中抽取 1000 份进行统计. 从一本很厚的电话簿中抽取 1000 人进行姓氏统计. 从 Google 搜索 & ...
- Reservoir Sampling - 蓄水池抽样
问题起源于编程珠玑Column 12中的题目10,其描述如下: How could you select one of n objects at random, where you see the o ...
- 水塘抽样(Reservoir Sampling)问题
水塘抽样是一系列的随机算法,其目的在于从包含n个项目的集合S中选取k个样本,其中n为一很大或未知的数量,尤其适用于不能把所有n个项目都存放到主内存的情况. 在高德纳的计算机程序设计艺术中,有如下问题: ...
- Spark MLlib之水塘抽样算法(Reservoir Sampling)
1.理解 问题定义可以简化如下:在不知道文件总行数的情况下,如何从文件中随机的抽取一行? 首先想到的是我们做过类似的题目吗?当然,在知道文件行数的情况下,我们可以很容易的用C运行库的rand函数随机的 ...
- Reservoir Sampling - 蓄水池抽样问题
问题起源于编程珠玑Column 12中的题目10,其描述如下: How could you select one of n objects at random, where you see the o ...
- 蓄水池抽样算法 Reservoir Sampling
2018-03-05 14:06:40 问题描述:给出一个数据流,这个数据流的长度很大或者未知.并且对该数据流中数据只能访问一次.请写出一个随机选择算法,使得数据流中所有数据被选中的概率相等. 问题求 ...
- 随机抽样问题(蓄水池问题Reservoir Sampling)
转自:孤影醉残阳 http://hi.baidu.com/siyupy/item/e4bb218fedf4a0864414cfad 随机抽样问题(蓄水池问题Reservoir Sampling) 随即 ...
随机推荐
- HDU 4737 A Bit Fun (2013成都网络赛)
A Bit Fun Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- ubuntu单独安装字体包
查看当前字体的命令: fc-list 文泉驿字体下载地址: http://wenq.org/wqy2/index.cgi?ZenHei#v0_8 下载到的是一个deb安装包 在下载完字体以后将字体包拷 ...
- TFS分布式文件系统
简介 TFS(Taobao !FileSystem)是一个高可扩展.高可用.高性能.面向互联网服务的分布式文件系统,主要针对海量的非结构化数据,它构筑在普通的Linux机器集群上,可为外部提供高可靠和 ...
- Revit API遍历全部风管,找到与风管相关的墙开洞
涉及向量计算,求相交等相关技术. ) { foreach (Face face in solid.Faces) ...
- ASP.NET MVC异步验证是如何工作的03,jquery.validate.unobtrusive.js是如何工作的
在上一篇"ASP.NET MVC异步验证是如何工作的02,异步验证表单元素的创建"中了解了ASP.NET异步验证是如何创建表单元素的,本篇体验jquery.validate.uno ...
- SUSE Linux – Zypper 命令示例
來源:http://www.linuxidc.com/Linux/2014-11/109214.htm Zypper是SUSE Linux中用于安装,升级,卸载,管理仓库.进行各种包查询的命令行接口. ...
- UIScrollView的判断位置的属性如下:
contentSize:CGSize类型,scrollview可以滑动的区域,例如,一个view的frame为(0,0,320,480),而scrollview的contentSize为(320,10 ...
- WordPress基础:去除顶部管理员工具条
想去掉这条东西有多种方式,个人比较喜欢这个,灵活~ 打开用户,在用户选项里,把这个勾走.
- iOS 去掉UITableView风格为group时候的最顶部的空白距离
CGRect frame=CGRectMake(0, 0, 0, CGFLOAT_MIN); self.tableView.tableHeaderView=[[UIView alloc]initW ...
- 删除数据库字段一样的row, 并增加唯一索引
DELETE FROM `groups` WHERE name IN (select a.name from ( SELECT name FROM `groups` GROUP BY name HAV ...