gauss.scm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. (declare (usual-integrations))
  21. ;;; UNIFORM-RANDOM produces an inexact number x, 0 <= x < 1
  22. #|
  23. (define uniform-random
  24. (let* ((random-max (expt 2 23))
  25. (frandom-max (exact->inexact random-max)))
  26. (lambda ()
  27. (/ (random random-max)
  28. frandom-max))))
  29. |#
  30. (define (uniform-random) (random 1.))
  31. (define (nonzero-uniform-random)
  32. (let ((x (uniform-random)))
  33. (if (= x 0.)
  34. (nonzero-uniform-random)
  35. x)))
  36. ;;; Given uniform random numbers, we can produce pairs of
  37. ;;; gaussian-distributed numbers, with zero mean and unit
  38. ;;; standard deviation, by the following trick:
  39. (define* (gaussian-random-pair #:optional continue)
  40. ;; continue = (lambda (y1 y2) ...)
  41. (let ((continue (if (default-object? continue) cons continue))
  42. (x1 (uniform-random))
  43. (x2 (uniform-random)))
  44. (let ((r (sqrt (* -2.0 (log x1)))))
  45. (continue (* r (cos (* 2pi x2)))
  46. (* r (sin (* 2pi x2)))))))
  47. (define (gaussian-random)
  48. (gaussian-random-pair (lambda (x y) x)))
  49. (define (gaussian-random-list d)
  50. (let lp ((j d) (t '()))
  51. (if (fix:= j 0)
  52. t
  53. (gaussian-random-pair
  54. (lambda (x1 x2)
  55. (if (fix:= j 1)
  56. (cons x1 t)
  57. (lp (fix:- j 2) (cons x1 (cons x2 t)))))))))
  58. ;;; Makes a list of n 2-vectors of gaussian-distributed random numbers
  59. (define (gaussian-random-pairs n)
  60. (if (fix:= n 0)
  61. '()
  62. (cons (gaussian-random-pair vector)
  63. (gaussian-random-pairs (fix:- n 1)))))
  64. ;;; Makes a list of n d-vectors of gaussian-distributed random numbers
  65. (define (gaussian-random-tuples d n)
  66. (if (fix:= n 0)
  67. '()
  68. (cons (list->vector (gaussian-random-list d))
  69. (gaussian-random-tuples d (fix:- n 1)))))
  70. ;;; For adding zero-mean noise with a given standard deviation to a vector.
  71. (define* ((add-noise sigma) v)
  72. (list->vector (map (lambda (signal noise)
  73. (+ signal (* sigma noise)))
  74. (vector->list v)
  75. (gaussian-random-list (vector-length v)))))