[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/8WJpBVc9    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Sep 26:
; statistics

(define (sum xs) (apply + xs))

(define (square x) (* x x))

(define (mean xs) (/ (sum xs) (length xs)))

(define (std-dev xs)
  (let* ((n (length xs)) (x-bar (/ (sum xs) n)))
    (define (diff x) (- x x-bar))
    (sqrt (/ (sum (map square (map diff xs))) (- n 1)))))

(define (linear-regression xs ys)
  (let* ((n (length xs))
         (x (sum xs)) (y (sum ys))
         (xx (sum (map square xs)))
         (xy (sum (map * xs ys)))
         (yy (sum (map square ys)))
         (slope (/ (- (* n xy) (* x y)) (- (* n xx) (* x x))))
         (intercept (- (/ y n) (* slope (/ n) x))))
    (values slope intercept)))

(define (correlation xs ys)
  (let* ((n (length xs)) (x-bar (mean xs)) (y-bar (mean ys)))
    (define (x-diff x) (- x x-bar)) (define (y-diff y) (- y y-bar))
    (/ (sum (map * (map x-diff xs) (map y-diff ys)))
       (- n 1) (std-dev xs) (std-dev ys))))

; average height (xs) and weight (ys) of american women age 30-39
; slope=61.272 intercept=-39.062 correlation=0.99

(define xs '(1.47 1.50 1.52 1.55 1.57 1.60 1.63 1.65 1.68 1.70
             1.73 1.75 1.78 1.80 1.83))

(define ys '(52.21 53.12 54.48 55.84 57.20 58.57 59.93
       61.29 63.11 64.47 66.28 68.10 69.92 72.19 74.46))

(display (mean xs)) (newline)
(display (mean ys)) (newline)
(display (std-dev xs)) (newline)
(display (std-dev ys)) (newline)
(call-with-values
  (lambda () (linear-regression xs ys))
  (lambda (slope intercept)
    (display slope) (newline)
    (display intercept) (newline)))
(display (correlation xs ys)) (newline)

; anscombe's quartet
; mean=7.5 stdev=4.12 slope=0.5 intercept=3 correlation=0.816
; graphs at http://en.wikipedia.org/wiki/File:Anscombe%27s_quartet_3.svg

(define xs-1 '(10 8 13 9 11 14 6 4 12 7 5))
(define ys-1 '(8.04 6.95 7.58 8.81 8.33 9.96 7.24 4.26 10.84 4.82 5.68))

(define xs-2 '(10 8 13 9 11 14 6 4 12 7 5))
(define ys-2 '(9.14 8.14 8.74 8.77 9.26 8.10 6.13 3.10 9.13 7.26 4.74))

(define xs-3 '(10 8 13 9 11 14 6 4 12 7 5))
(define ys-3 '(7.46 6.77 12.74 7.11 7.81 8.84 6.08 5.39 8.15 6.42 5.73))

(define xs-4 '(8 8 8 8 8 8 8 19 8 8 8))
(define ys-4 '(6.58 5.76 7.71 8.84 8.47 7.04 5.25 12.50 5.56 7.91 6.89))

(display (mean xs-1)) (newline)
(display (mean xs-2)) (newline)
(display (mean xs-3)) (newline)
(display (mean xs-4)) (newline)
(display (std-dev xs-1)) (newline)
(display (std-dev xs-2)) (newline)
(display (std-dev xs-3)) (newline)
(display (std-dev xs-4)) (newline)

(display (mean ys-1)) (newline)
(display (mean ys-2)) (newline)
(display (mean ys-3)) (newline)
(display (mean ys-4)) (newline)
(display (std-dev ys-1)) (newline)
(display (std-dev ys-2)) (newline)
(display (std-dev ys-3)) (newline)
(display (std-dev ys-4)) (newline)

(call-with-values
  (lambda () (linear-regression xs-1 ys-1))
  (lambda (slope intercept)
    (display slope) (newline)
    (display intercept) (newline)))
(call-with-values
  (lambda () (linear-regression xs-2 ys-2))
  (lambda (slope intercept)
    (display slope) (newline)
    (display intercept) (newline)))
(call-with-values
  (lambda () (linear-regression xs-3 ys-3))
  (lambda (slope intercept)
    (display slope) (newline)
    (display intercept) (newline)))
(call-with-values
  (lambda () (linear-regression xs-4 ys-4))
  (lambda (slope intercept)
    (display slope) (newline)
    (display intercept) (newline)))

(display (correlation xs-1 ys-1)) (newline)
(display (correlation xs-2 ys-2)) (newline)
(display (correlation xs-2 ys-2)) (newline)
(display (correlation xs-2 ys-2)) (newline)


Output:
1.6506666666666665
62.078
0.11423451233985206
7.037514983490772
61.272186542107434
-39.06195591883866
0.9945837935768894
9
9
9
9
3.3166247903554
3.3166247903554
3.3166247903554
3.3166247903554
7.500909090909093
7.500909090909091
7.500000000000001
7.50090909090909
2.031568135925815
2.0316567355016177
2.030423601123667
2.0305785113876023
0.500090909090908
3.000090909090921
0.4999999999999992
3.000909090909098
0.4997272727272698
3.0024545454545724
0.4999090909090927
3.0017272727272557
0.8164205163448399
0.8162365060002428
0.8162365060002428
0.8162365060002428


Create a new paste based on this one


Comments: