blob: 3ccb67f34aa3691a965c574a6563f06f6da7c8d2 (
plain) (
blame)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
;;; SPDX-FileCopyrightText: 2020 Arvydas Silanskas
;;; SPDX-FileCopyrightText: 2024 Bradley J Lucier
;;; SPDX-License-Identifier: MIT
;;;
;;; Take REPS samples from unit sphere, verify random distribution.
;;;
;;; This test checks that:
;;; * Every sample has unit length, within numerical tolerance.
;;; * The REPS samples are uniformly distributed.
;;; * Rotations of the REPS samples are uniformly distributed.
(define (test-sphere sphereg dim-sizes REPS rotate?)
(define random-int (random-source-make-integers (current-random-source)))
(define random-real (random-source-make-reals (current-random-source)))
(define N (- (vector-length dim-sizes) 1))
;; Fix list of samples
(define samples
(generator->list (gtake sphereg REPS)))
(define (l2-norm VEC)
(sqrt (vector-fold
(lambda (sum x l) (+ sum (/ (* x x)
(* l l))))
0
VEC
dim-sizes)))
;; Rotate the j'th amnd k'th coordinates of a vector VEC
;; by cosine co and sine si
(define (pair-rot VEC j k co si)
(define oj (vector-ref VEC j))
(define ok (vector-ref VEC k))
(define nj (+ (* co oj) (* si ok)))
(define nk (+ (* (- si) oj) (* co ok)))
(list->vector
(map (lambda (index)
(cond
((= index j) nj)
((= index k) nk)
(else (vector-ref VEC index))))
(iota (vector-length VEC)))))
;; Apply a random rotation to a collection of vectors
(define how-many-rots
(if (< 10 N) 10 N))
(define (arb-rot VEC-LIST)
(define j (random-int N))
(define k (+ j 1 (random-int (- N j))))
(define theta (* 3.14 (random-real)))
(define co (cos theta))
(define si (sin theta))
(define rvl
(map (lambda (vec)
(pair-rot vec j k co si))
VEC-LIST))
(if (not (= 0 (random-int how-many-rots)))
(arb-rot rvl)
rvl))
;; Expect a vector approaching zero. That is, each individual
;; coordinate should be uniformly randomly distributed in the
;; interval [-1,1]. The sum of REPS samples of these should
;; converge to zero. The standard deviation of a uniform
;; distribution is sqrt(REPS/12).
;; https://en.wikipedia.org/wiki/Continuous_uniform_distribution
;; So setting max bound of 9 stddev should allow it to usually
;; pass.
(define (converge-to-zero samples)
(fold (lambda (acc sample) (vector-map + sample acc))
(make-vector REPS 0.0)
samples))
(define (should-be-zero samples)
(l2-norm (converge-to-zero samples)))
(define (norm-should-be-zero samples)
(/ (should-be-zero samples) (sqrt (/ REPS 12.0))))
(define (check-zero samples)
(define num-stddev 9.0)
(define zz (norm-should-be-zero samples))
(test-assert (< zz num-stddev)))
;; maximum allowed tolerance for radius deviation
(define EPS (* 2e-15 (sqrt N)))
;; Each individual sphere radius should be 1.0 to within float
;; tolerance.
(for-each
(lambda (SAMP)
(test-approximate 1.0 (l2-norm SAMP) EPS))
samples)
;; The distribution should be zero
(check-zero samples)
;; Rotate wildly. Should still be uniform.
(when rotate?
(for-each
(lambda (junk) (check-zero (arb-rot samples)))
(make-list 12))))
(define (test-ball ballg dim-sizes)
(define (l2-norm VEC)
(sqrt (vector-fold
(lambda (sum x l) (+ sum (/ (* x x)
(* l l))))
0
VEC
dim-sizes)))
(define (test-ball-generates-on-radius radius err)
(test-assert
(generator-any
(lambda (vec)
(define n (l2-norm vec))
(and (> n (- radius err))
(< n (+ radius err))))
(gtake ballg 10000))))
(define (test-ball-avg-zero N)
(define vec-sum
(generator-fold
(lambda (vec acc)
(vector-map + vec acc))
(make-vector (vector-length dim-sizes) 0.0)
(gtake ballg N)))
(define avg-vec
(vector-map
(lambda (e)
(/ e N))
vec-sum))
(define n (l2-norm avg-vec))
(test-assert (< n 1)))
(test-ball-generates-on-radius 0.0 0.1)
(test-ball-generates-on-radius 0.5 0.1)
(test-ball-generates-on-radius 1.0 0.1)
(test-ball-avg-zero 5000))
;; Number of standard deviation difference from the expected value
;; before we say a test failed. If set to 2, then one out of
;; 20 tests will fail even if the code is correct. Setting it to
;; 3 means that only one out of a 1000 tests will fail even if the
;; code is correct.
(define STDs 3)
(define (test-ellipsoid a b N)
(define epsilon 1e-10)
(define pi (* 4 (atan 1)))
(define g (make-ellipsoid-generator (vector a b)))
(define points (generator->list (gtake g N)))
;; The points on the "top" of the ellipse, with
;; x between -a/2 and a/2
(define top
(filter (lambda (v)
(and (< (- (/ a 2)) (vector-ref v 0) (/ a 2))
(< 0 (vector-ref v 1))))
points))
;; The points on the "right" of the ellipse, with
;; y between -b/2 and b/2
(define right
(filter (lambda (v)
(and (< (- (/ b 2)) (vector-ref v 1) (/ b 2))
(< 0 (vector-ref v 0))))
points))
(define (arc-length a b)
;; parametrization: (a\cos t, b\sin t)
;; this is the norm of the derivative
(lambda (t)
(sqrt (+ (square (* a (sin t)))
(square (* b (cos t)))))))
(define (simpsons-rule f t0 t1 N)
;; O(Delta^4) numerical integration
;; integrate f between t0 and t1 with N intervals
(let* ((Delta (/ (- t1 t0) N))
(sum1 (do ((i 0 (+ i 1))
(sum 0. (+ sum
(f (+ t0 (* i Delta)))
(f (+ t0 (* (+ i 1) Delta))))))
((= i N) sum)))
(sum2 (do ((i 0 (+ i 1))
(sum 0. (+ sum (f (+ t0 (* (+ i 0.5) Delta))))))
((= i N) sum))))
(* Delta (/ (+ (* 4 sum2) sum1) 6))))
(define p-right
(/ (simpsons-rule (arc-length a b) (- (* pi 1/6)) (* pi 1/6) 100)
(simpsons-rule (arc-length a b) 0 (* pi 2) 100)))
(define p-top
(/ (simpsons-rule (arc-length a b) (* pi 1/3) (* pi 2/3) 100)
(simpsons-rule (arc-length a b) 0 (* pi 2) 100)))
;; test that they're all more-or-less on the ellipse
(test-assert (every (lambda (p)
(< (abs (- (sqrt (+ (square (/ (vector-ref p 0) a))
(square (/ (vector-ref p 1) b))))
1))
epsilon))
points))
(for-each (lambda (p m)
;; p = probability of landing in arc, m = measured number
(test-assert (< (abs (- (* p N) m))
(* STDs (sqrt (* N p (- 1 p)))))))
(list p-right p-top)
(map length (list right top)))
)
(define (test-ellipse a b N)
;; This test with two-dimensional ellipses stands in for all
;; dimensions, as the code to generate points is independent
;; of dimension
(define pi (* 4 (atan 1)))
(define interior-points
(generator->list (gtake (make-ball-generator (vector a b)) N)))
(define (in-ellipse? point)
(< (+ (square (/ (vector-ref point 0) a))
(square (/ (vector-ref point 1) b)))
1))
;; Find points inside rectangles inside various parts
;; of the ellipse
(define center
(filter (lambda (v)
(and (< (- (/ a 4))
(vector-ref v 0)
(/ a 4))
(< (- (/ b 4))
(vector-ref v 1)
(/ b 4))))
interior-points))
(define right-x
;; (right-x b/4) is on the ellipse
(* a (sqrt 15/16)))
(define right
(filter (lambda (v)
(and (< (- right-x (/ a 2))
(vector-ref v 0)
right-x)
(< (- (/ b 4))
(vector-ref v 1)
(/ b 4))))
interior-points))
(define top-y
;; (a/4 top-y) is on the ellipse
(* b (sqrt 15/16)))
(define top
(filter (lambda (v)
(and (< (- (/ a 4))
(vector-ref v 0)
(/ a 4))
(< (- top-y (/ b 2))
(vector-ref v 1)
top-y)))
interior-points))
;; p is he fraction of the area in the ellipse
;; contained in the rectangles (it's all the same).
#;(define p
(/ (* (/ a 2) (/ b 2))
(* pi a b)))
(define p (/ (* 4 pi)))
;; check that all the points are truly inside the ellipse.
(test-assert (every in-ellipse? interior-points))
(for-each (lambda (p m)
;; p = probability of landing in rectangle,
;; m = measured number of events
(test-assert (< (abs (- (* p N) m))
(* STDs (sqrt (* N p (- 1 p)))))))
(list p p p)
(map length (list center right top))))
|