white.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;; -*-Scheme-*-
  21. ;;;
  22. ;;; $Id: white.scm,v 1.1 1994/07/20 19:42:43 cph Exp $
  23. ;;;
  24. ;;; Copyright (c) 1993-94 Massachusetts Institute of Technology
  25. ;;;
  26. ;;; This material was developed by the Scheme project at the
  27. ;;; Massachusetts Institute of Technology, Department of Electrical
  28. ;;; Engineering and Computer Science. Permission to copy this
  29. ;;; software, to redistribute it, and to use it for any purpose is
  30. ;;; granted, subject to the following restrictions and understandings.
  31. ;;;
  32. ;;; 1. Any copy made of this software must include this copyright
  33. ;;; notice in full.
  34. ;;;
  35. ;;; 2. Users of this software agree to make their best efforts (a) to
  36. ;;; return to the MIT Scheme project any improvements or extensions
  37. ;;; that they make, so that these may be included in future releases;
  38. ;;; and (b) to inform MIT of noteworthy uses of this software.
  39. ;;;
  40. ;;; 3. All materials developed as a consequence of the use of this
  41. ;;; software shall duly acknowledge such use, in accordance with the
  42. ;;; usual standards of acknowledging credit in academic research.
  43. ;;;
  44. ;;; 4. MIT has made no warrantee or representation that the operation
  45. ;;; of this software will be error-free, and MIT is under no
  46. ;;; obligation to provide any services, by way of maintenance, update,
  47. ;;; or otherwise.
  48. ;;;
  49. ;;; 5. In conjunction with products arising from the use of this
  50. ;;; material, there shall be no use of the name of the Massachusetts
  51. ;;; Institute of Technology nor of any adaptation thereof in any
  52. ;;; advertising, promotional, or sales literature without prior
  53. ;;; written consent from MIT in each case.
  54. ;;;; White Noise Generation
  55. (declare (usual-integrations))
  56. (define (make-noise-vector method state length)
  57. (let ((v (flo:vector-cons length)))
  58. (do ((i 0 (fix:+ i 1)))
  59. ((fix:= i length))
  60. (flo:vector-set! v i (method state)))
  61. v))
  62. (define (make-uniform-noise-vector state length)
  63. (let ((v (flo:vector-cons length)))
  64. (do ((i 0 (fix:+ i 1)))
  65. ((fix:= i length))
  66. (flo:vector-set! v i (flo:- (flo:random-unit state) .5)))
  67. v))
  68. (define (make-gaussian-noise-vector:polar-method state length)
  69. (let ((length (if (odd? length) (+ length 1) length)))
  70. (let ((v (flo:vector-cons length))
  71. (t (flo:vector-cons 3)))
  72. (let ((x1 (lambda () (flo:vector-ref t 0)))
  73. (set-x1! (lambda (x) (flo:vector-set! t 0 x)))
  74. (x2 (lambda () (flo:vector-ref t 1)))
  75. (set-x2! (lambda (x) (flo:vector-set! t 1 x)))
  76. (s (lambda () (flo:vector-ref t 2)))
  77. (set-s! (lambda (x) (flo:vector-set! t 2 x))))
  78. (declare (integrate-operator x1 set-x1! x2 set-x2! s set-s!))
  79. (do ((i 0 (fix:+ i 2)))
  80. ((fix:= i length))
  81. (let loop ()
  82. (set-x1! (flo:random-unit state))
  83. (set-x1! (flo:- (flo:+ (x1) (x1)) 1.))
  84. (set-x2! (flo:random-unit state))
  85. (set-x2! (flo:- (flo:+ (x2) (x2)) 1.))
  86. (set-s! (flo:+ (flo:* (x1) (x1)) (flo:* (x2) (x2))))
  87. (if (flo:< (s) 1.)
  88. (begin
  89. (set-s! (flo:sqrt (flo:/ (flo:* -2. (flo:log (s))) (s))))
  90. (flo:vector-set! v i (flo:* (x1) (s)))
  91. (flo:vector-set! v (fix:+ i 1) (flo:* (x2) (s))))
  92. (loop)))))
  93. v)))
  94. (define (marsaglia-maclaren-method f f-limits h v)
  95. ;; F is the (1-sided) normalized probability density function.
  96. ;; F-LIMITS is special; see Knuth text and code below.
  97. ;; H is the number of divisions per horizontal unit.
  98. ;; V is the number of divisions per vertical unit.
  99. ;; Assumptions:
  100. ;; (1) F is defined for non-negative reals.
  101. ;; (2) F is positive and monotonically non-increasing.
  102. ;; (3) The total area under F is 1.
  103. ;; (4) Nearly all of F's area occurs between 0 and 3.
  104. ;; (5) H and V are exact powers of two.
  105. ;; **** The procedure MM-WORST-CASE is specifically for the Gaussian PDF.
  106. (call-with-values (lambda () (mm-tables f f-limits h v))
  107. (lambda (aj sj pj qj dj bj fj+6h)
  108. (let ((h*3 (* h 3))
  109. (hv (exact->inexact (* h v)))
  110. (h (exact->inexact h)))
  111. (lambda (state)
  112. (let ((body
  113. (lambda (U)
  114. (let ((a
  115. (flo:vector-ref aj
  116. (flo:truncate->exact (flo:* U hv)))))
  117. (if (flo:< a 0.)
  118. (let loop ((j 0))
  119. (cond ((fix:= j h*3)
  120. (mm-worst-case state))
  121. ((flo:< U (flo:vector-ref pj j))
  122. (if (flo:< U (flo:vector-ref qj j))
  123. (flo:+ (flo:vector-ref sj j)
  124. (flo:/ (flo:random-unit state) h))
  125. (mm-algorithm-L state
  126. h
  127. (flo:vector-ref sj j)
  128. (flo:vector-ref dj j)
  129. (flo:vector-ref bj j)
  130. (vector-ref fj+6h j))))
  131. (else
  132. (loop (fix:+ j 1)))))
  133. (flo:+ a (flo:/ (flo:random-unit state) h)))))))
  134. (let ((U (flo:random-unit state)))
  135. (if (flo:< U .5)
  136. (body (flo:* U 2.))
  137. (flo:negate (body (flo:* (flo:- U .5) 2.)))))))))))
  138. (define (mm-algorithm-L state h s a/b b f)
  139. (let loop ()
  140. (call-with-values
  141. (lambda ()
  142. (let ((U (flo:random-unit state))
  143. (V (flo:random-unit state)))
  144. (if (flo:< V U)
  145. (values V U)
  146. (values U V))))
  147. (lambda (U V)
  148. (let ((X (flo:+ s (flo:/ U h))))
  149. (if (and (flo:> V a/b)
  150. (flo:> V (flo:+ U (flo:/ (f X) b))))
  151. (loop)
  152. X))))))
  153. (define (mm-worst-case state)
  154. (let loop ()
  155. (let ((U (flo:random-unit state))
  156. (V (flo:random-unit state)))
  157. (let ((W (flo:+ (flo:* U U) (flo:* V V))))
  158. (if (flo:< W 1.)
  159. (let ((T (flo:sqrt (flo:/ (flo:- 9. (flo:* 2. (flo:log W))) W))))
  160. (let ((X (flo:* U T)))
  161. (if (flo:< X 3.)
  162. (let ((X (flo:* V T)))
  163. (if (flo:< X 3.)
  164. (loop)
  165. X))
  166. X)))
  167. (loop))))))
  168. (define (mm-tables f f-limits h v)
  169. (let ((pj (mm-pj f h v)))
  170. (let ((pj+6h (mm-pj+6h f h)))
  171. (call-with-values (lambda () (mm-abj f f-limits h pj+6h))
  172. (lambda (aj bj)
  173. (let ((*pj (mm-*pj h pj (mm-pj+3h f h pj) pj+6h)))
  174. (values (vector->flonum-vector (mm-large-table h v pj))
  175. (vector->flonum-vector (mm-sj h))
  176. (vector->flonum-vector *pj)
  177. (vector->flonum-vector (mm-qj h *pj pj+6h))
  178. (vector->flonum-vector (mm-dj aj bj))
  179. (vector->flonum-vector bj)
  180. (mm-fj+6h f h pj+6h))))))))
  181. (define (mm-large-table h v pj)
  182. (let ((h*3 (* h 3))
  183. (hv (* h v)))
  184. (let ((table (make-vector hv -1)))
  185. (let loop ((j 0) (i 0))
  186. (if (not (= j h*3))
  187. (let ((i* (+ i (* (vector-ref pj j) hv))))
  188. (subvector-fill! table i i* (/ j h))
  189. (loop (+ j 1) i*))))
  190. table)))
  191. (define (mm-sj h)
  192. (make-initialized-vector (+ (* h 3) 1)
  193. (lambda (j)
  194. (/ j h))))
  195. (define (mm-pj f h v)
  196. (make-initialized-vector (* h 3)
  197. (let ((hv (* h v)))
  198. (lambda (j)
  199. (/ (truncate->exact (* v (f (/ (+ j 1) h)))) hv)))))
  200. (define (mm-pj+3h f h pj)
  201. (make-initialized-vector (* h 3)
  202. (lambda (j)
  203. (- (/ (f (/ (+ j 1) h)) h)
  204. (vector-ref pj j)))))
  205. (define (mm-pj+6h f h)
  206. (make-initialized-vector (* h 3)
  207. (lambda (j)
  208. (let ((xu (/ (+ j 1) h)))
  209. (- (simpsons-rule f (/ j h) xu 128)
  210. (/ (f xu) h))))))
  211. (define (mm-abj f f-limits h pj+6h)
  212. (let ((h*3 (* h 3)))
  213. (let ((aj (make-vector h*3))
  214. (bj (make-vector h*3)))
  215. (do ((j 0 (+ j 1)))
  216. ((= j h*3))
  217. (let ((xu (/ (+ j 1) h)))
  218. (call-with-values (lambda () (f-limits (/ j h) xu))
  219. (lambda (a b)
  220. (let ((fxu (f xu))
  221. (p (vector-ref pj+6h j)))
  222. (vector-set! aj j (/ (- a fxu) p))
  223. (vector-set! bj j (/ (- b fxu) p)))))))
  224. (values aj bj))))
  225. (define (mm-dj aj bj)
  226. (make-initialized-vector (vector-length aj)
  227. (lambda (j)
  228. (/ (vector-ref aj j) (vector-ref bj j)))))
  229. (define (mm-*pj h pj pj+3h pj+6h)
  230. (let ((h*3 (* h 3)))
  231. (let ((*pj (make-vector (+ h*3 1))))
  232. (do ((j 0 (+ j 1)))
  233. ((= j h*3))
  234. (vector-set! *pj j
  235. (+ (if (= j 0)
  236. (do ((j 0 (+ j 1))
  237. (p 0 (+ p (vector-ref pj j))))
  238. ((= j h*3) p))
  239. (vector-ref *pj (- j 1)))
  240. (vector-ref pj+3h j)
  241. (vector-ref pj+6h j))))
  242. (vector-set! *pj h*3 1)
  243. *pj)))
  244. (define (mm-qj h *pj pj+6h)
  245. (make-initialized-vector (* h 3)
  246. (lambda (j)
  247. (- (vector-ref *pj j) (vector-ref pj+6h j)))))
  248. (define (mm-fj+6h f h pj+6h)
  249. (make-initialized-vector (* h 3)
  250. (lambda (j)
  251. (let ((base (exact->inexact (f (/ (+ j 1) h))))
  252. (scale (exact->inexact (vector-ref pj+6h j))))
  253. (lambda (x)
  254. (flo:/ (flo:- (f x) base) scale))))))
  255. (define (simpsons-rule f a b n/2)
  256. (let ((b-a (- b a))
  257. (n (* 2 n/2)))
  258. (let ((x
  259. (lambda (i)
  260. (+ a (* (/ i n) b-a)))))
  261. (let loop ((i 1) (sum (f a)))
  262. (let ((i (+ i 1))
  263. (sum (+ sum (* 4 (f (x i))))))
  264. (if (= i n)
  265. (/ (* (/ b-a n) (+ sum (f b))) 3)
  266. (loop (+ i 1)
  267. (+ sum (* 2 (f (x i)))))))))))
  268. (define one-sided-unit-gaussian-pdf
  269. (let ((sqrt-pi/2 (sqrt (* 2 (atan 1 1)))))
  270. (lambda (x)
  271. (/ (exp (/ (* x x) -2))
  272. sqrt-pi/2))))
  273. (define (one-sided-unit-gaussian-pdf-limits xl xu)
  274. (let ((xd (- xu xl))
  275. (fxl (one-sided-unit-gaussian-pdf xl))
  276. (fxu (one-sided-unit-gaussian-pdf xu)))
  277. (if (<= xu 1)
  278. (values fxl (* (+ 1 (* xd xu)) fxu))
  279. (values
  280. (let ((y (/ (- fxu fxl) xd)))
  281. (let ((x
  282. (let ((f
  283. (lambda (x)
  284. (* (- x) (one-sided-unit-gaussian-pdf x)))))
  285. (let ((limit (* (abs y) 1e-6))
  286. (d0 (/ xd 2)))
  287. (let loop ((x (+ xl d0)) (d (/ d0 2)))
  288. (let ((fx (f x)))
  289. (cond ((< (abs (- fx y)) limit)
  290. x)
  291. ((< fx y)
  292. (if (>= x xu) (error "Can't find root."))
  293. (loop (+ x d) (/ d 2)))
  294. (else
  295. (if (<= x xl) (error "Can't find root."))
  296. (loop (- x d) (/ d 2))))))))))
  297. (- (one-sided-unit-gaussian-pdf x)
  298. (* y (- x xl)))))
  299. fxl))))