GithubHelp home page GithubHelp logo

ashenfad / cljx-sampling Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 13 KB

Consistent sampling and random numbers for Clojure/ClojureScript

License: Other

Clojure 96.41% Java 3.59%
clojure clojurescript sampling random-number-generator

cljx-sampling's Introduction

cljx-sampling

This Clojure/ClojureScript library provides consistent sampling and random number generation regardless of the environment. That means using the same intial seed we can produce the exact same results (sampled items or random numbers) whether in the JVM or in the browser.

Installation

cljx-sampling is available as a Maven artifact from Clojars.

For Leiningen:

Clojars Project

Overview

This library has two useful namespaces:

As we review each, feel free to follow along in the REPL:

user> (ns demo
        (:require [cljx-sampling.random :as random]
                  [cljx-sampling.core :refer [sample]]))

Random number generation

cljx-sampling.random provides seedable random number generation using a 32-bit Xorshift. While this random number generator is surprisingly strong for its simplicity, it is not cryptographically secure.

To use, simply create a random number generator and then call next-boolean!, next-int!, next-double!, or next-gaussian! as needed.

demo> (def rng (random/create))
demo> (random/next-boolean! rng)
true
demo> (random/next-int! rng)
3321045053
demo> (random/next-int! rng 8)
4
demo> (random/next-double! rng)
0.5436311555095017
demo> (random/next-double! rng 10)
8.794576390646398
demo> (random/next-gaussian! rng)
0.09573863197719758

When we create a generator, we can provide a seed to make the numbers deterministic.

demo> (def rng1 (random/create "foobar"))
demo> (def rng2 (random/create "foobar"))
demo> (repeatedly 10 #(random/next-int! rng1 100))
(51 14 22 53 21 32 9 97 63 65)
demo> (repeatedly 10 #(random/next-int! rng2 100))
(51 14 22 53 21 32 9 97 63 65)

Sampling

cljx-sampling.core provides in-memory random sampling over collections. While the original population is kept in memory, the resulting sample is a lazy sequence.

By default, sampling is done without replacement. This is equivalent to a lazy Fisher-Yates shuffle.

demo> (sample (range 5))
(2 3 1 0 4)

Setting :replace as true will sample with replacement. Since there is no limit to the number of items that may be sampled with replacement from a population, the result will be an infinite length list. So make sure to take however many samples are needed.

demo> (take 10 (sample (range 5) :replace true))
(2 3 3 2 4 1 1 1 3 0)

Each call to sample will return a new sample order.

demo> (sample (range 5))
(0 2 3 1 4)
demo> (sample (range 5))
(3 1 4 2 0)

Setting the :seed parameter allows the sample order to be deterministic.

demo> (sample (range 5) :seed 7)
(1 4 2 0 3)
demo> (sample (range 5) :seed 7)
(1 4 2 0 3)
demo> (sample (range 5) :seed "foobar")
(2 1 0 4 3)
demo> (sample (range 5) :seed "foobar")
(2 1 0 4 3)

Weighted Sampling

A sample may be weighted using the :weigh parameter. If the parameter is supplied with a function that takes an item and produces a non-negative weight, then the resulting sample will be weighted accordingly.

demo> (take 5 (sample [:heads :tails]
                      :weigh {:heads 0.5 :tails 0.5}
                      :replace true))
(:tails :heads :heads :heads :tails)

The weights need not sum to 1.

demo> (->> (sample [:heads :tails]
                   :weigh {:heads 2 :tails 1}
                   :replace true)
           (take 100)
           (frequencies))
{:heads 66, :tails 34}

License

Copyright (C) 2014-2017 - Adam Ashenfelter

Distributed under the Apache License, Version 2.0.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.