MyNixOS website logo
Description

Uniform draws of partitions and cycle-partitions, with thinning.

A Haskell library for efficient uniform random sampling of cycle partition graphs on sets of vertices, and partitions of lists or vectors. Selection can be subject to conditions.

builds.sr.ht status

Summary

A Haskell library for efficient uniform random sampling of cycle partition graphs on sets of vertices, and partitions of lists or vectors. Selection can be subject to conditions.

Cycle partitions

A cycle partition graph on a set of vertices V is a directed graph G = (E, V) such that for each i in V there exists a unique j in V such that (i, j) in E. In other words, it is a partition of V into a graph with disjoint cycle graphs.

Define C(V) to be the set of cycle partitions graphs of V. uniformCyclePartition samples from the uniform distribution on C(V), in O(|V|) time.

To do so, it relies on the fact that

σ -> (i, σ(i)) , for i = 1..|V|

defines a bijective map between the permutations σ on |V| distinct elements and the edge sets of C(V). Therefore, sampling a uniform cycle partition without conditions is as simple as sampling a uniform permutation.

Note self-loops are allowed in the possible configurations.

uniformCyclePartitionThin samples uniformly from the set of cycle partition graphs satisfying a predicate, in O(n/p) time on average, where p is the proportion of cycle partition graphs satisfying the predicate. It works by rejection sampling, so the user is asked to provide a maximum number of sampling attempts to guarantee termination.

List or vector partitions

This package provides functions to draw uniform samples from all 2^(n-1) possible partitions of an ordered list (or vector). uniformPartition selects a single element uniformly across all possible partitions in O(n) time, and uniformPartitionThin samples uniformly conditional on a predicate in O(n/p) time on average, where p is the proportion of elements for which the predicate is True.

Only the partitioning is randomized: Input list order is preserved.

The samplers randomize the placement of each breakpoint in the partition. In other words the sample space is viewed as a perfect binary tree, and random selection is a random walk from root to leaf. The implementation samples a bit array to determine the walk path instead of creating an actual tree structure, for efficiency.

At the moment, the uniformPartitionThin is implemented only for lists.

Short-circuiting

The predicate provided to uniformPartitionThin checks each partition element, a chunk of elements in the original list, in turn. Since partitions are built lazily, the sampler will short-circuit and start sampling a new partition after the first partition element for which the predicate is False. This is just a consequence of the short-circuiting in base package function all.

Similarly, if the predicate itself is short-circuiting, the sampler will short-circuit.

Contributing

Tickets

Send by email, without need for an account, to ~brendanrbrown/[email protected]

Man pages for tickets on SourceHut, particularly the "Email access" section.

Patches

Man pages for sending patches upstream.

Metadata

Version

0.1.2.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows