Walker's alias method is an efficient algorithm to sample from a discrete probability distribution.

walker-alias

Walker's alias method is an efficient algorithm to sample from a discrete probability distribution. This means given an arbitrary probability distribution like {A: 1, B: 2, C: 3, D: 4}, the odds of sampling A is roughly 1 in 10 and the odds of sampling D is roughly 4 in 10.

The algorithm is able to generate a random sample in O(1) time and is ideal for cases where frequent queries are done against a fixed arbitrary weight distribution.
The preprocessing step for probability table creation is O(n) time. For floating point rounding errors, a negligible error is to be expected (<0.05% for 10000000 iterations).

Install with go mod

go get github.com/geraldywy/walker-alias

Usage

package main

import (
	"fmt"
	"time"

	wa "github.com/geraldywy/walker-alias"
)

func main() {
	// key: weight mapping, keys can be any valid int, weights can be any valid float64
	pMap := map[int]float64{
		0: 3.5,
		1: 6.5,
		2: 10,
	}
	w := wa.NewWalkerAlias(pMap, time.Now().Unix()) // init with weights and a random seed

	for i := 0; i < 5; i++ {
		randomKey := w.Random() // generates a random key in O(1)
		fmt.Println(randomKey)
	}
}

For usage with a non int key, one workaround is to use a lookup table to map the key to a unique int and map back after sampling. For a simple example with string keys, refer to example.go in the example folder. Alternatively, you could implement it with generics and submit a pull request.

Benchmarking

Walker Alias is much faster compared to alternatives like sampling via naive linear search or binary search via partitions. Below shows the result of sampling from 10 million keys using all three methods, with Walker Alias clearly outperforming others. The full implementation for the benchmarking can be found in main_test.go.

$ go test -bench=. -benchtime=60s -count=3  -run=^# -timeout 20m
goos: darwin
goarch: amd64
pkg: github.com/geraldywy/walker-alias
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
BenchmarkWalkerAlias_Random/naive_search-16                             20083                  3514372 ns/op
BenchmarkWalkerAlias_Random/naive_search-16                             20355                  3504450 ns/op
BenchmarkWalkerAlias_Random/naive_search-16                             20383                  3534106 ns/op
BenchmarkWalkerAlias_Random/binary_searching_partitions-16              130537809              558.2 ns/op
BenchmarkWalkerAlias_Random/binary_searching_partitions-16              127571498              569.2 ns/op
BenchmarkWalkerAlias_Random/binary_searching_partitions-16              126131037              562.6 ns/op
BenchmarkWalkerAlias_Random/walker_alias-16                             425654750              159.7 ns/op
BenchmarkWalkerAlias_Random/walker_alias-16                             412184373              159.7 ns/op
BenchmarkWalkerAlias_Random/walker_alias-16                             424909584              158.0 ns/op
PASS
ok      github.com/geraldywy/walker-alias       947.729s

Licensing

MIT License

Owner
Similar Resources

Sample multi docker compose environment setup

Instructions This is a demonstration of a Multi Docker Compose. The purpose of this repositoy is ongoing research on "Docker compose" architecture des

Oct 21, 2022

concurrent, cache-efficient, and Dockerfile-agnostic builder toolkit

concurrent, cache-efficient, and Dockerfile-agnostic builder toolkit

BuildKit BuildKit is a toolkit for converting source code to build artifacts in an efficient, expressive and repeatable manner. Key features: Automati

Dec 31, 2022

Kubernetes APIServer Aggregation Sample

apiserver-aggregation-sample Kubernetes APIServer Aggregation Sample Create an apiserver sample Initialize the Project apiserver-boot init repo --doma

Nov 23, 2021

Sample application accessing kubernetes secrets

Kubernetes secrets API example This git repo illustrates a small application which can access kubernetes secrets. Build small application To test the

Dec 19, 2021

This is a sample application of golang's web framework gin and orm ent.

Gin Ent Sample This is a sample application of golang's web framework gin and orm ent. Components Web Framework: Gin ORM: ent SQL Migration tool: goos

Dec 5, 2021

This repo houses some Golang introductory files, sample codes and implementations

This repo houses some Golang introductory files, sample codes and implementations. I will be updating it as I keep getting a hang of the language.

Aug 27, 2022

A kubernetes operator sample generated by kubebuilder , which run cmd in pod on specified time

init kubebuilder init --domain github.com --repo github.com/tonyshanc/sample-operator-v2 kubebuilder create api --group sample --version v1 --kind At

Jan 25, 2022

Develop sample controller step by step

Develop sample controller step by step

Jul 21, 2022

A sample for okteto pipelines with terraform

Okteto Pipeline with Terraform (PubSub) This sample covers a producer/consumer a

Dec 23, 2021
Manage your ssh alias configs easily.
Manage your ssh alias configs easily.

manssh manssh is a command line tool for managing your ssh alias config easily, inspired by storm project, powered by Go. Note: This project is actual

Nov 9, 2022
Fast docker image distribution plugin for containerd, based on CRFS/stargz
Fast docker image distribution plugin for containerd, based on CRFS/stargz

[ ⬇️ Download] [ ?? Browse images] [ ☸ Quick Start (Kubernetes)] [ ?? Quick Start (nerdctl)] Stargz Snapshotter Read also introductory blog: Startup C

Dec 29, 2022
resource manifest distribution among multiple clusters.

Providing content to managed clusters Support a primitive that enables resources to be applied to a managed cluster. Community, discussion, contributi

Dec 26, 2022
A Rancher and Kubernetes optimized immutable Linux distribution based on openSUSE

RancherOS v2 WORK IN PROGRESS RancherOS v2 is an immutable Linux distribution built to run Rancher and it's corresponding Kubernetes distributions RKE

Nov 14, 2022
Truly Minimal Linux Distribution for Containers

Statesman Statesman is a minimal Linux distribution, running from memory, that has just enough functionality to run OCI-compatible containers. Rationa

Nov 12, 2021
A Kubernetes operator that allows for automatic provisioning and distribution of cert-manager certs across namespaces

cached-certificate-operator CachedCertificate Workflow When a CachedCertificate is created or updated the operator does the following: Check for a val

Sep 6, 2022
Apachedist-resource - A concourse resource to track updates of an apache distribution, e.g. tomcat

Apache Distribution Resource A concourse resource that can track information abo

Feb 2, 2022
RancherOS v2 is an immutable Linux distribution built to run Rancher and it's corresponding Kubernetes distributions RKE2 and k3s

RancherOS v2 is an immutable Linux distribution built to run Rancher and it's corresponding Kubernetes distributions RKE2 and k3s. It is built using the cOS-toolkit and based on openSUSE

Dec 27, 2022
A package manager written in Go which uses the LFS Symlink method.

pacsym A package manager powered by symlinks. How to use The package manager assumes that all software installed is installed with /usr/pkg/<packagena

Dec 11, 2021
Sample Driver that provides reference implementation for Container Object Storage Interface (COSI) API

cosi-driver-minio Sample Driver that provides reference implementation for Container Object Storage Interface (COSI) API Community, discussion, contri

Oct 10, 2022