sampling

package
v0.0.0-...-a8cedd3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package sampling implements random sampling algorithms.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ints

func Ints(samplesize, n int, r rand.Source64, buf []int) []int

Ints appends to buf a simple random sample of the integers [0,n) and returns the resulting slice. The sample is not sorted.

Random numbers are taken from r, or from an internal generator bootstrapped from math.rand's global generator if r is nil.

If samplesize > n, the sample will be of size n instead.

The time complexity of this function is O(s(1+log(n/s))).

func Ints32

func Ints32(samplesize int, n int32, r rand.Source, buf []int32) []int32

Ints32 appends to buf a simple random sample of the integers [0,n) and returns the resulting slice. The sample is not sorted.

Random numbers are taken from r, or from an internal generator bootstrapped from math.rand's global generator if r is nil.

If samplesize > n, the sample will be of size n instead.

The time complexity of this function is O(s(1+log(n/s))).

func Ints64

func Ints64(samplesize int, n int64, r rand.Source64, buf []int64) []int64

Ints64 appends to buf a simple random sample of the integers [0,n) and returns the resulting slice. The sample is not sorted.

Random numbers are taken from r, or from an internal generator bootstrapped from math.rand's global generator if r is nil.

If samplesize > n, the sample will be of size n instead.

The time complexity of this function is O(s(1+log(n/s))).

Example
package main

import (
	"fmt"
	"math/rand"

	"github.com/greatroar/randstat/sampling"
)

func main() {
	// Ints64 can sample efficiently from very large ranges.
	// The final argument may be nil.
	r := rand.NewSource(42).(rand.Source64)
	sample := sampling.Ints64(10, 1e18, r, nil)

	for _, x := range sample {
		fmt.Println(x)
	}

}
Output:

209976569432343488
512273708663700480
204888252163057856
311815901727164224
943870718497463296
188287930289750272
629183818546644096
208260206501070304
937471898537404672
639599965878877696

Types

type Varopt

type Varopt struct {
	// contains filtered or unexported fields
}

A Varopt is a weighted reservoir sampler. Items are included in its sample with probability proportional to their weight.

Varopt implements the algorithm of https://arxiv.org/pdf/0803.0473.pdf.

func NewVaropt

func NewVaropt(samplesize int, r rand.Source64) *Varopt

NewVaropt constructs a Varopt sampler.

Random numbers are taken from r, or from an internal generator bootstrapped from math.rand's global generator if r is nil.

func (*Varopt) Item

func (v *Varopt) Item(i int) interface{}

Item returns the item at index i in the current sample.

The index i must be at least zero and less than s.Len(). Items occur in the sample in random order.

func (*Varopt) Len

func (v *Varopt) Len() int

Len returns the number of items currently in the sample.

The number of items is the minimum of the desired sample size and the number of items Shown with positive weight.

func (*Varopt) Show

func (v *Varopt) Show(x interface{}, w float64) (reject interface{})

Show presents x to v as a candidate for inclusion in its random sample.

An item with zero weight is always rejected. A negative weight causes Show to panic.

If x is accepted into the sample, reject is set to the item evicted to make space for it, if any. For the first samplesize items, reject will be nil.

Example (ReuseMemory)
package main

import (
	"math/rand"

	"github.com/greatroar/randstat/sampling"
)

func main() {
	// The return value from Show can be used to reuse allocated memory.

	type item struct {
		letter rune
		index  int
		weight float64
	}

	sample := sampling.NewVaropt(6, nil)
	var x *item

	for i, l := range []rune("abcdefghijklmnopqrstuvwxyz") {
		if x == nil {
			x = new(item)
		}
		*x = item{letter: l, index: i, weight: rand.Float64()}

		reject := sample.Show(x, x.weight)

		// A rejected item is no longer in the sample and may be recycled.
		x, _ = reject.(*item)
	}
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL