math.body.scm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ;; Jason K. MacDuffie
  2. ;; I dedicate this to the public domain, for the benefit of all
  3. ;; due to the triviality of the library.
  4. (define pi 3.141592653589793)
  5. (define e 2.718281828459045)
  6. (define i 0+i)
  7. (define (deg->rad d)
  8. (/ (* d pi) 180))
  9. (define (rad->deg r)
  10. (/ (* r 180) pi))
  11. (define (dsin d)
  12. (sin (deg->rad d)))
  13. (define (dcos d)
  14. (cos (deg->rad d)))
  15. (define (dtan d)
  16. (tan (deg->rad d)))
  17. (define (dasin a)
  18. (rad->deg (asin a)))
  19. (define (dacos a)
  20. (rad->deg (acos a)))
  21. (define (datan a)
  22. (rad->deg (atan a)))
  23. (define (integrate f minval maxval dx)
  24. ;; simple integration
  25. (let loop ((x minval)
  26. (sum 0.0))
  27. (if (<= x maxval)
  28. (loop (+ x dx)
  29. (+ sum (* dx (f x))))
  30. sum)))
  31. (define (factorial x)
  32. ;; Technically this is a continuation of factorial
  33. ;; But it is convenient sometimes
  34. (if (and (exact? x) (integer? x) (not (negative? x)))
  35. (let loop ((i 1)
  36. (p 1))
  37. (if (<= i x)
  38. (loop (+ i 1) (* p i))
  39. p))
  40. (gamma (+ x 1))))
  41. (define (stirling-gamma z)
  42. (* (sqrt (* 2 pi (/ z)))
  43. (expt (* (/ e) (+ z (/ (- (* 12 z) (/ 1/10 z)))))
  44. z)))
  45. (define (gamma z)
  46. (define a (- (real-part z) (floor (real-part z))))
  47. (define b (imag-part z))
  48. (cond
  49. ((and (integer? z)
  50. (positive? z))
  51. (let loop ((i 1)
  52. (p 1.0))
  53. (if (< i z)
  54. (loop (+ i 1) (* i p))
  55. p)))
  56. ((< (real-part z) 150)
  57. (let loop ((i z)
  58. (p (stirling-gamma (+ 150 a (* 0+i b)))))
  59. (if (< (real-part i) 150)
  60. (loop (+ i 1)
  61. (/ p i))
  62. p)))
  63. ((< (real-part z) 151)
  64. (stirling-gamma z))
  65. (else
  66. (let loop ((i z)
  67. (p (stirling-gamma (+ 150 a (* 0+i b)))))
  68. (if (> (real-part i) 151)
  69. (loop (- i 1)
  70. (* p (- i 1)))
  71. p)))))
  72. (define (inverse-gamma x)
  73. (if (<= x 0.8856032)
  74. (error "inverse-gamma" "x must be greater than 0.8856032"))
  75. (if (>= x 2e300)
  76. (error "inverse-gamma" "x must be less than 2.0e300"))
  77. (let ((upper-bound (let loop ((u 1.46164))
  78. (if (< (gamma u) x)
  79. (loop (+ u 1))
  80. u))))
  81. (let loop ((frac 0.5)
  82. (prev 0.0)
  83. (r (- upper-bound 0.5)))
  84. (if (not (= prev r))
  85. (loop (* frac 0.5)
  86. r
  87. (if (< (gamma r) x)
  88. (+ r frac)
  89. (- r frac)))
  90. (if (= x (gamma (round r)))
  91. (round r)
  92. r)))))
  93. (define (inverse-factorial x)
  94. (let ((y (- (inverse-gamma x) 1)))
  95. (if (and (exact? x) (integer? y))
  96. (exact y)
  97. y)))
  98. ;; Inverse of the function (* n (log n))
  99. (define (anti-nlogn x)
  100. (define (nlogn n)
  101. (* n (log n)))
  102. (let ((stopat
  103. (cond
  104. ((< x 2.75)
  105. (error "value must be at least 2.75"))
  106. ((< x 10.0)
  107. 2000)
  108. (else
  109. 100))))
  110. (let loop ((guess x)
  111. (i 0))
  112. (if (or (= i stopat)
  113. (= (nlogn guess) x))
  114. guess
  115. (loop (/ x (log guess))
  116. (+ i 1))))))
  117. ;; Some statistics functions
  118. (define (mean l)
  119. (/ (apply + l) (length l)))
  120. (define (variance l . sample-option)
  121. (define sample? (and (not (null? sample-option))
  122. (eq? (car sample-option) 'sample)))
  123. (define a (square (apply + l)))
  124. (define b (apply + (map square l)))
  125. (define n (length l))
  126. (define ss (- b (/ a n)))
  127. (/ ss (if sample? (- n 1) n)))
  128. (define (deviation l . sample-option)
  129. (define sample? (and (not (null? sample-option))
  130. (eq? (car sample-option) 'sample)))
  131. (sqrt (apply variance
  132. (list l (if sample? 'sample 'population)))))
  133. (define (z-scores l)
  134. (define sd (deviation l))
  135. (define m (mean l))
  136. (map (lambda (x)
  137. (/ (- x m) sd))
  138. l))
  139. (define (normal-pmf mu sigsq)
  140. (define C (/ (sqrt (* 2 sigsq pi))))
  141. (lambda (x)
  142. (* C (exp (- (/ (square (- x mu))
  143. (* 2 sigsq)))))))
  144. (define (normal-cdf mu sigsq)
  145. ;; For simplicity, make internal mu 0
  146. (define f (normal-pmf 0 sigsq))
  147. (define (simple-cdf x)
  148. (if (< x -10)
  149. 0.0
  150. (integrate f (* -4 sigsq) x (* 5.0e-5 sigsq))))
  151. (lambda (x)
  152. (if (< x mu)
  153. (simple-cdf (- x mu))
  154. (- 1 (simple-cdf (- mu x))))))
  155. (define (primes upto-minus-one)
  156. ;; Sieve of Eratosthenes
  157. (cond
  158. ((> upto-minus-one 10000000)
  159. (error "primes"
  160. "Cannot calculate for values greater than 10000000"
  161. upto-minus-one))
  162. ((< upto-minus-one 2)
  163. '())
  164. (else
  165. (let ((upto (+ upto-minus-one 1)))
  166. (define stop-at (exact (floor (sqrt upto))))
  167. (define sieve-vector (make-vector upto #t))
  168. (define (believed-prime? x)
  169. (vector-ref sieve-vector x))
  170. (vector-set! sieve-vector 0 #f)
  171. (vector-set! sieve-vector 1 #f)
  172. (let loop1 ((i 2))
  173. (if (<= i stop-at)
  174. (if (believed-prime? i)
  175. (let loop2 ((j (* i 2)))
  176. (if (< j upto)
  177. (begin
  178. (vector-set! sieve-vector j #f)
  179. (loop2 (+ j i)))
  180. (loop1 (+ i 1))))
  181. (loop1 (+ i 1)))
  182. ;; now return a list of primes
  183. (let make-primes ((out '())
  184. (j (- upto 1)))
  185. (if (>= j 0)
  186. (if (believed-prime? j)
  187. (make-primes (cons j out) (- j 1))
  188. (make-primes out (- j 1)))
  189. out))))))))
  190. (define (prime? n)
  191. (define upto
  192. (floor (exact (sqrt n))))
  193. (if (< n 2)
  194. #f
  195. (let loop ((next (primes upto)))
  196. (cond
  197. ((null? next)
  198. #t)
  199. ((= 0 (modulo n (car next)))
  200. #f)
  201. (else
  202. (loop (cdr next)))))))
  203. (define (approx-equal a b eps)
  204. (< (abs (- (/ a b) 1)) eps))
  205. (define (~= x y . rest)
  206. (and (approx-equal x y 1e-13)
  207. (or (null? rest)
  208. (apply ~= x rest))))
  209. (define (percent-scale l)
  210. (let ((total (apply + l)))
  211. (map (lambda (i) (* 100 (/ i total))) l)))