| 1 |
; Take REPS samples from unit sphere, verify random distribution. |
| 2 |
; |
| 3 |
; This test checks that: |
| 4 |
; * Every sample has unit length, within numerical tolerance. |
| 5 |
; * The REPS samples are uniformly distributed. |
| 6 |
; * Rotations of the REPS samples are uniformly distributed. |
| 7 |
(define (test-sphere sphereg dim-sizes REPS rotate?) |
| 8 |
(define random-int (random-source-make-integers (current-random-source))) |
| 9 |
(define random-real (random-source-make-reals (current-random-source))) |
| 10 |
(define N (- (vector-length dim-sizes) 1)) |
| 11 |
|
| 12 |
; Fix list of samples |
| 13 |
(define samples |
| 14 |
(generator->list (gtake sphereg REPS))) |
| 15 |
|
| 16 |
(define (l2-norm VEC) |
| 17 |
(sqrt (vector-fold |
| 18 |
(lambda (sum x l) (+ sum (/ (* x x) |
| 19 |
(* l l)))) |
| 20 |
0 |
| 21 |
VEC |
| 22 |
dim-sizes))) |
| 23 |
|
| 24 |
; Rotate the j'th amnd k'th coordinates of a vector VEC |
| 25 |
; by cosine co and sine si |
| 26 |
(define (pair-rot VEC j k co si) |
| 27 |
(define oj (vector-ref VEC j)) |
| 28 |
(define ok (vector-ref VEC k)) |
| 29 |
(define nj (+ (* co oj) (* si ok))) |
| 30 |
(define nk (+ (* (- si) oj) (* co ok))) |
| 31 |
(list->vector |
| 32 |
(map (lambda (index) |
| 33 |
(cond |
| 34 |
((= index j) nj) |
| 35 |
((= index k) nk) |
| 36 |
(else (vector-ref VEC index)))) |
| 37 |
(iota (vector-length VEC))))) |
| 38 |
|
| 39 |
; Apply a random rotation to a collection of vectors |
| 40 |
(define how-many-rots |
| 41 |
(if (< 10 N) 10 N)) |
| 42 |
|
| 43 |
(define (arb-rot VEC-LIST) |
| 44 |
(define j (random-int N)) |
| 45 |
(define k (+ j 1 (random-int (- N j)))) |
| 46 |
(define theta (* 3.14 (random-real))) |
| 47 |
(define co (cos theta)) |
| 48 |
(define si (sin theta)) |
| 49 |
(define rvl |
| 50 |
(map (lambda (vec) |
| 51 |
(pair-rot vec j k co si)) |
| 52 |
VEC-LIST)) |
| 53 |
(if (not (= 0 (random-int how-many-rots))) |
| 54 |
(arb-rot rvl) |
| 55 |
rvl)) |
| 56 |
|
| 57 |
; Expect a vector approaching zero. That is, each individual |
| 58 |
; coordinate should be uniformly randomly distributed in the |
| 59 |
; interval [-1,1]. The sum of REPS samples of these should |
| 60 |
; converge to zero, within pi/2 sqrt(REPS). |
| 61 |
(define (converge-to-zero samples) |
| 62 |
(fold (lambda (acc sample) (vector-map + sample acc)) |
| 63 |
(make-vector REPS 0.0) |
| 64 |
samples)) |
| 65 |
|
| 66 |
(define (should-be-zero samples) |
| 67 |
(l2-norm (converge-to-zero samples))) |
| 68 |
|
| 69 |
(define (norm-should-be-zero samples) |
| 70 |
(/ (should-be-zero samples) (* 1.57 (sqrt REPS)))) |
| 71 |
|
| 72 |
(define (check-zero samples) |
| 73 |
(define zz (norm-should-be-zero samples)) |
| 74 |
(test-assert (< zz 1))) |
| 75 |
|
| 76 |
; maximum allowed tolerance for radius deviation |
| 77 |
(define EPS (* 2e-15 (sqrt N))) |
| 78 |
|
| 79 |
; Each individual sphere radius should be 1.0 to within float |
| 80 |
; tolerance. |
| 81 |
(for-each |
| 82 |
(lambda (SAMP) |
| 83 |
(test-approximate 1.0 (l2-norm SAMP) EPS)) |
| 84 |
samples) |
| 85 |
|
| 86 |
; The distribution should be zero |
| 87 |
(check-zero samples) |
| 88 |
|
| 89 |
; Rotate wildly. Should still be uniform. |
| 90 |
(when rotate? |
| 91 |
(for-each |
| 92 |
(lambda (junk) (check-zero (arb-rot samples))) |
| 93 |
(make-list 12)))) |
| 94 |
|
| 95 |
(define (test-ball ballg dim-sizes) |
| 96 |
(define (l2-norm VEC) |
| 97 |
(sqrt (vector-fold |
| 98 |
(lambda (sum x l) (+ sum (/ (* x x) |
| 99 |
(* l l)))) |
| 100 |
0 |
| 101 |
VEC |
| 102 |
dim-sizes))) |
| 103 |
|
| 104 |
(define (test-ball-generates-on-radius radius err) |
| 105 |
(test-assert |
| 106 |
(generator-any |
| 107 |
(lambda (vec) |
| 108 |
(define n (l2-norm vec)) |
| 109 |
(and (> n (- radius err)) |
| 110 |
(< n (+ radius err)))) |
| 111 |
(gtake ballg 10000)))) |
| 112 |
|
| 113 |
(define (test-ball-avg-zero N) |
| 114 |
(define vec-sum |
| 115 |
(generator-fold |
| 116 |
(lambda (vec acc) |
| 117 |
(vector-map + vec acc)) |
| 118 |
(make-vector (vector-length dim-sizes) 0.0) |
| 119 |
(gtake ballg N))) |
| 120 |
(define avg-vec |
| 121 |
(vector-map |
| 122 |
(lambda (e) |
| 123 |
(/ e N)) |
| 124 |
vec-sum)) |
| 125 |
(define n (l2-norm avg-vec)) |
| 126 |
(test-assert (< n 1))) |
| 127 |
|
| 128 |
(test-ball-generates-on-radius 0.0 0.1) |
| 129 |
(test-ball-generates-on-radius 0.5 0.1) |
| 130 |
(test-ball-generates-on-radius 1.0 0.1) |
| 131 |
|
| 132 |
(test-ball-avg-zero 5000)) |