npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

weighted-reservoir-sampler

v1.0.0

Published

Samples random subsets from streams.

Downloads

9

Readme

weighted-reservoir-sampler


Samples random subsets from streams.

npm install weighted-reservoir-sampler

This package is an implementation of the A-ES algorithm as described in Weighted Random Sampling over Data Streams.

Basic Usage


var Sampler = require('weighted-reservoir-sampler');

// Example of a sampler which is twice as likely to select odd numbers
var sampler = new Sampler({
    sampleSize: 9,
    weightFunction: function(item) { return (item % 2) + 1; }
});

for (var i = 0; i < 150; i++) { sampler.push(i); }
var sample = sampler.end();

Class: WeightedReservoirSampler


new WeightedReservoirSampler([options])

The WeightedReservoirSampler constructor takes an optional options argument containing configuration options detailed in the Configuration section.

weightedReservoirSampler.config([options])

If options argument is present, merges the options object into the configuration of this weightedReservoirSampler instance and returns the instance.

If options is missing, returns the configuration object of this weightedReservoirSampler instance.

weightedReservoirSampler.setConfig(config)

Replaces this instance's configuration object with config (supplying defaults for missing options).

weightedReservoirSampler.push(item)

Pushes an item to this instance's sample buffer. This function should be called for every item you wish to consider for inclusion in the sample.

weightedReservoirSampler.end()

Returns the sample, and resets this instance's sample buffer for reuse. This should be called when you have pushed all items you wish to be considered in the sample.

Configuration


The following sections document the different options that can be passed to the config(), and setConfig() functions.

sampleSize

The size of the random subset to be retained when pushing items to the weightedReservoirSampler.

var weightedReservoirSampler = new WeightedReservoirSampler({
	sampleSize: 10
});

Default: 1

weightFunction

A weight function which is applied to every item pushed to the weightedReservoirSampler. The returned weight from this function determines how likely an item is to be selected in the sample. An item with a weight of 10 is ten times more likely to be selected than an item with a weight of 1.

Note: The weight function should return a number greater than 0, otherwise the corresponding item is ignored.

var weightedReservoirSampler = new WeightedReservoirSampler({
	weightFunction: function(item) {
	    return item.length * item.width;
	}
});

Default: function() { return 1; }

random

The function to use for random number generation. The output of this function should be a number in the range [0, 1).

var weightedReservoirSampler = new WeightedReservoirSampler({
	random: function() {
	    var randomNumber;
	    // Code to set randomNumber
	    return randomNumber;
	}
});

Default: Math.random