#lang racket
(define-values (struct:discrete-histogram
discrete-histogram-constructor
discrete-histogram?
discrete-histogram-field-ref
set-discrete-histogram-field!)
(make-struct-type 'discrete-histogram #f 4 0))
(define make-discrete-histogram
(case-lambda
((n1 n2 dynamic?)
(unless (<= n1 (+ n2 1))
(error 'make-discrete-histogram
"illegal range ~a .. ~a" n1 n2))
(discrete-histogram-constructor
n1 n2 dynamic? (make-vector (+ (- n2 n1) 1))))
((n1 n2)
(make-discrete-histogram n1 n2 #f))
(()
(make-discrete-histogram 0 -1 #t))))
(define discrete-histogram-n1
(make-struct-field-accessor discrete-histogram-field-ref 0 'n1))
(define set-discrete-histogram-n1!
(make-struct-field-mutator set-discrete-histogram-field! 0 'n1))
(define discrete-histogram-n2
(make-struct-field-accessor discrete-histogram-field-ref 1 'n2))
(define set-discrete-histogram-n2!
(make-struct-field-mutator set-discrete-histogram-field! 1 'n2))
(define discrete-histogram-dynamic?
(make-struct-field-accessor discrete-histogram-field-ref 2 'dynamic?))
(define discrete-histogram-bins
(make-struct-field-accessor discrete-histogram-field-ref 3 'bins))
(define set-discrete-histogram-bins!
(make-struct-field-mutator set-discrete-histogram-field! 3 'bins))
(define (discrete-histogram-increment! h i)
(discrete-histogram-accumulate! h i 1))
(define (discrete-histogram-accumulate! h i weight)
(let* ((n1 (discrete-histogram-n1 h))
(n2 (discrete-histogram-n2 h))
(n (+ (- n2 n1) 1))
(dynamic? (discrete-histogram-dynamic? h))
(bins (discrete-histogram-bins h)))
(let/ec exit
(unless (<= n1 i n2)
(if (not dynamic?)
(exit (void))
(let* ((new-n1 (if (> n 0) (min i n1) i))
(new-n2 (if (> n 0) (max i n2) i))
(new-n (+ (- new-n2 new-n1) 1))
(new-bins (make-vector new-n)))
(do ((i 0 (+ i 1)))
((= i n) (void))
(vector-set! new-bins (+ (- n1 new-n1) i)
(vector-ref bins i)))
(set! n1 new-n1)
(set! n2 new-n2)
(set! n new-n)
(set! bins new-bins)
(set-discrete-histogram-n1! h n1)
(set-discrete-histogram-n2! h n2)
(set-discrete-histogram-bins! h bins))))
(let ((bin (- i n1)))
(vector-set! bins bin (+ (vector-ref bins bin) weight))))))
(define (discrete-histogram-get h i)
(let ((n1 (discrete-histogram-n1 h))
(n2 (discrete-histogram-n2 h))
(bins (discrete-histogram-bins h)))
(if (<= n1 i n2)
(vector-ref bins (- i n1))
(raise-mismatch-error 'discrete-histogram-bins
"index out of range" i))))
(define (discrete-histogram-max h)
(let* ((n1 (discrete-histogram-n1 h))
(n2 (discrete-histogram-n2 h))
(n (+ (- n2 n1) 1))
(bins (discrete-histogram-bins h))
(max -inf.0))
(do ((i 0 (+ i 1)))
((>= i n) max)
(let ((x (vector-ref bins i)))
(when (> x max)
(set! max x))))))
(define (discrete-histogram-min h)
(let* ((n1 (discrete-histogram-n1 h))
(n2 (discrete-histogram-n2 h))
(n (+ (- n2 n1) 1))
(bins (discrete-histogram-bins h))
(min +inf.0))
(do ((i 0 (+ i 1)))
((>= i n) min)
(let ((x (vector-ref bins i)))
(when (< x min)
(set! min x))))))
(define (discrete-histogram-mean h)
(let* ((n1 (discrete-histogram-n1 h))
(n2 (discrete-histogram-n2 h))
(n (+ (- n2 n1) 1))
(bins (discrete-histogram-bins h))
(wmean 0.0)
(W 0.0))
(do ((i 0 (+ i 1)))
((= i n) wmean)
(let ((xi (+ n1 i))
(wi (vector-ref bins i)))
(when (> wi 0.0)
(set! W (+ W wi))
(set! wmean (+ wmean (* (- xi wmean) (/ wi W)))))))))
(define (discrete-histogram-sigma h)
(let* ((n1 (discrete-histogram-n1 h))
(n2 (discrete-histogram-n2 h))
(n (+ (- n2 n1) 1))
(bins (discrete-histogram-bins h))
(wvariance 0.0)
(wmean 0.0)
(W 0))
(do ((i 0 (+ i 1)))
((= i n) wmean)
(let ((xi (+ n1 i))
(wi (vector-ref bins i)))
(when (> wi 0.0)
(set! W (+ W wi))
(set! wmean (+ wmean (* (- xi wmean) (/ wi W)))))))
(set! W 0.0)
(do ((i 0 (+ i 1)))
((= i n) (sqrt wvariance))
(let ((xi (+ n1 i))
(wi (vector-ref bins i)))
(when (> wi 0.0)
(let ((delta (- xi wmean)))
(set! W (+ W wi))
(set! wvariance (+ wvariance
(* (- (* delta delta) wvariance)
(/ wi W))))))))))
(define (discrete-histogram-sum h)
(let* ((n1 (discrete-histogram-n1 h))
(n2 (discrete-histogram-n2 h))
(n (+ (- n2 n1) 1))
(bins (discrete-histogram-bins h))
(sum 0.0))
(do ((i 0 (+ i 1)))
((>= i n) sum)
(set! sum (+ sum (vector-ref bins i))))))
(provide
(rename-out (discrete-histogram-increment! unchecked-discrete-histogram-increment!)
(discrete-histogram-accumulate! unchecked-discrete-histogram-accumumate!)))
(provide/contract
(discrete-histogram?
(-> any/c boolean?))
(make-discrete-histogram
(case-> (-> exact-integer? exact-integer? boolean? discrete-histogram?)
(-> exact-integer? exact-integer? discrete-histogram?)
(-> discrete-histogram?)))
(discrete-histogram-n1
(-> discrete-histogram? exact-integer?))
(discrete-histogram-n2
(-> discrete-histogram? exact-integer?))
(discrete-histogram-dynamic?
(-> discrete-histogram? boolean?))
(discrete-histogram-bins
(-> discrete-histogram? (vectorof real?)))
(discrete-histogram-increment!
(-> discrete-histogram? exact-integer? void?))
(discrete-histogram-accumulate!
(-> discrete-histogram? exact-integer? real? void?))
(discrete-histogram-get
(-> discrete-histogram? exact-integer? real?))
(discrete-histogram-max
(-> discrete-histogram? real?))
(discrete-histogram-min
(-> discrete-histogram? real?))
(discrete-histogram-mean
(-> discrete-histogram? real?))
(discrete-histogram-sigma
(-> discrete-histogram? (>=/c 0.0)))
(discrete-histogram-sum
(-> discrete-histogram? real?)))