-
Notifications
You must be signed in to change notification settings - Fork 7
/
random.clj
239 lines (194 loc) · 7.16 KB
/
random.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
(ns com.yetanalytics.datasim.math.random
"Random number generation and probabilistic operations."
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as sgen]
[com.yetanalytics.datasim.util.maths :as maths])
(:refer-clojure :exclude
[rand rand-int rand-nth random-sample random-uuid shuffle])
(:import [java.util UUID Random]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::seed int?)
(s/def ::rng
(s/with-gen #(instance? Random %)
(fn [] (sgen/fmap (fn [s] (Random. s)) (s/gen ::seed)))))
(s/def ::sd
(s/double-in :min 0.0 :infinite? false :NaN? false))
(s/def ::prob
(s/double-in :min 0.0 :max 1.0))
(s/def ::weight
(s/double-in :min -1.0 :max 1.0))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; RNG Creation ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/fdef rng
:args (s/cat)
:ret ::rng)
(defn rng
"Create a pseudorandom RNG using an arbitrary seed value."
^Random []
(Random.))
(s/fdef seed-rng
:args (s/cat :seed ::seed)
:ret ::rng)
(defn seed-rng
"Create a seeded deterministic, pseudorandom RNG, in which two RNGs created
using the same value of `seed` will output the same results."
^Random [^Long seed]
(Random. seed))
;; Random Number Generation ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/fdef rand
:args (s/cat :rng ::rng
:n (s/? number?))
:ret double?)
(defn rand
"Generate a pseudorando, uniformly distributed double value between 0 and
`n` (both inclusive).
See also: `clojure.core/rand`"
(^Double [^Random rng]
(.nextDouble rng))
(^Double [^Random rng n]
(* n (rand rng))))
(s/fdef rand-int
:args (s/cat :rng ::rng
:n (s/int-in Integer/MIN_VALUE Integer/MAX_VALUE))
:ret int?)
(defn rand-int
"Generate a pseudorandom, uniformly distributed integer value between 0
(inclusive) and `n` (exclusive).
See also: `clojure.core/rand-int`"
[rng n]
(long (rand rng n)))
(s/fdef rand-unbound-int
:args (s/cat :rng ::rng)
:ret int?)
(defn rand-unbound-int
"Generate a pseudorandom, uniformly distributed integer value in the
entire range of possible Java long/Clojure integer values."
[^Random rng]
(.nextLong rng))
(s/fdef rand-gaussian
:args (s/cat :rng ::rng
:mean double?
:sd ::sd)
:ret double?)
(defn rand-gaussian
"Generate a pseudorandom, normally distributed double value with mean
`mean` and standard deviation `sd`."
[^Random rng mean sd]
(+ mean (* sd (.nextGaussian rng))))
;; A boolean is just a Bernoulli-distribued random number (1 = true, 0 = false)
(s/fdef rand-boolean
:args (s/cat :rng ::rng
:prob ::prob)
:ret boolean?)
(defn rand-boolean
"Generate a pseudorandom boolean value where `true` is returned with
probability `prob` and `false` with probablility `1 - prob`. This
function can be used to realize `prob`, and is equivalent to generating
a Bernoulli-distributed value."
[rng prob]
(< (rand rng) prob))
;; Technically a UUID is also a number, right?
(s/fdef rand-uuid
:args (s/cat :rng ::rng)
:ret string?)
(defn rand-uuid
"Generate a pseudorandom UUID (as a string)."
[rng]
;; Derived from `clojure.test.check.generators/uuid`
;; We use decimal representations of bitmasks to avoid having to
;; coerce to unchecked longs
(let [x1 (-> (rand-unbound-int rng)
(bit-and -45057) ; 0xffffffffffff4fff
(bit-or 0x4000)) ; 0x0000000000004000
x2 (-> (rand-unbound-int rng)
(bit-or -9223372036854775808) ; 0x8000000000000000
(bit-and -4611686018427387905))] ; 0xbfffffffffffffff
(.toString (UUID. x1 x2))))
;; Collection Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- validate-not-empty
[coll]
(when (empty? coll)
(throw (ex-info "Attempted to select elements from an empty collection!"
{:type ::empty-coll}))))
(s/fdef rand-nth
:args (s/cat :rng ::rng
:coll (s/every any? :min-count 1))
:ret any?)
(defn rand-nth
"Randomly select an element from `coll`. Each element has an equal
probability of being selected.
Will throw an `::empty-coll` exception on an empty `coll`.
See also: `clojure.core/rand-nth`"
[rng coll]
(validate-not-empty coll)
(nth coll (rand-int rng (count coll))))
(s/fdef shuffle
:args (s/cat :rng ::rng
:coll (s/every any?))
:ret coll?
:fn (fn [{{:keys [coll]} :args ret :ret}]
(= (set coll) (set ret))))
(defn shuffle
"Randomly shuffle `coll` and return a lazy sequence as the result.
See also: `clojure.core/shuffle`"
([rng coll]
(shuffle rng coll (count coll)))
([rng coll cnt]
(lazy-seq
(when (< 0 cnt)
(let [[head [x & tail]] (split-at (rand-int rng cnt) coll)]
(cons x
(shuffle rng (concat head tail) (dec cnt))))))))
(s/fdef random-sample
:args (s/cat :rng ::rng
:prob ::prob
:coll (s/every any? :min-count 1)
:weights (s/? (s/map-of any? ::weight)))
:ret coll?)
(defn random-sample
"Probabilistically sample elements from `coll`, where each element has
`prob` probability of being selected. If `weights` are provided, then
the element associated with a weight has `(+ prob weight)` probability
(up to 1.0) of being selected. Returns a transducer when `coll` is not
provided.
See also: `clojure.core/random-sample`"
([rng prob]
(filter (fn [_] (rand-boolean rng prob))))
([rng prob coll]
(filter (fn [_] (rand-boolean rng prob)) coll))
([rng prob coll weights]
(filter (fn [x]
(let [prob* (maths/bound-probability (+ prob (get weights x 0.0)))]
(rand-boolean rng prob*)))
coll)))
(s/fdef choose
:args (s/cat :rng ::rng
:weights (s/map-of any? (s/keys :req-un [::weight]))
:coll (s/every any? :min-count 1)
:options (s/keys* :opt-un [::sd])))
(defn choose
"Probabilistically select one element from `coll`. The `weights` map
should be a map of `coll` elements to map with a `:weight` value."
[rng weights coll & {:keys [sd] :or {sd 0.25}}]
(validate-not-empty coll)
(let [rand-gauss
(fn [x]
(let [weight (get-in weights [x :weight] 0.0)]
(if (<= weight -1.0)
-1.0
(rand-gaussian rng (* sd weight) sd))))]
(apply max-key rand-gauss coll)))
(s/fdef choose-map
:args (s/cat :rng ::rng
:weights (s/map-of any? (s/keys :req-un [::weight]))
:coll (s/map-of any? any? :min-count 1)
:options (s/keys* :opt-un [::sd])))
(defn choose-map
"Probabilistically select one value from the map `m`. The `weights` map
should be a map from the keys of `m` to their `:weight` maps."
[rng weights m & {:keys [sd] :or {sd 0.25}}]
(get m (choose rng weights (keys m) :sd sd)))