(define-values (struct:statistics
statistics-constructor
statistics?
statistics-field-ref
set-statistics-field!)
(make-struct-type 'statistics #f 7 0))
(define statistics-time-dependant?
(make-struct-field-accessor statistics-field-ref 0 'time-dependant?))
(define set-statistics-time-dependant?!
(make-struct-field-mutator set-statistics-field! 0 'time-dependant?))
(define statistics-time-last-synchronized
(make-struct-field-accessor statistics-field-ref 1 'time-last-synchronized))
(define set-statistics-time-last-synchronized!
(make-struct-field-mutator set-statistics-field! 1 'time-last-synchronized))
(define statistics-minimum
(make-struct-field-accessor statistics-field-ref 2 'minimum))
(define set-statistics-minimum!
(make-struct-field-mutator set-statistics-field! 2 'minimum))
(define statistics-maximum
(make-struct-field-accessor statistics-field-ref 3 'maximum))
(define set-statistics-maximum!
(make-struct-field-mutator set-statistics-field! 3 'maximum))
(define statistics-n
(make-struct-field-accessor statistics-field-ref 4 'n))
(define set-statistics-n!
(make-struct-field-mutator set-statistics-field! 4 'n))
(define statistics-sum
(make-struct-field-accessor statistics-field-ref 5 'sum))
(define set-statistics-sum!
(make-struct-field-mutator set-statistics-field! 5 'sum))
(define statistics-sum-of-squares
(make-struct-field-accessor statistics-field-ref 6 'sum-of-squares))
(define set-statistics-sum-of-squares!
(make-struct-field-mutator set-statistics-field! 6 'sum-of-squares))
(define (make-statistics time-dependant? time)
(statistics-constructor time-dependant? time +inf.0 -inf.0 0 0.0 0.0))
(define (statistics-accumulate! statistics value time)
(if (< value (statistics-minimum statistics))
(set-statistics-minimum! statistics value))
(if (> value (statistics-maximum statistics))
(set-statistics-maximum! statistics value))
(let ((weighted-value (* value time))
(weighted-sum-squared (* value value time)))
(set-statistics-n!
statistics (+ (statistics-n statistics) time))
(set-statistics-sum!
statistics (+ (statistics-sum statistics) weighted-value))
(set-statistics-sum-of-squares!
statistics (+ (statistics-sum-of-squares statistics)
weighted-sum-squared))))
(define (statistics-tally! statistics value)
(statistics-accumulate! statistics value 1))
(define (statistics-mean statistics)
(/ (statistics-sum statistics)
(statistics-n statistics)))
(define (statistics-mean-square statistics)
(/ (statistics-sum-of-squares statistics)
(statistics-n statistics)))
(define (statistics-variance statistics)
(- (statistics-mean-square statistics)
(* (statistics-mean statistics)
(statistics-mean statistics))))
(define (statistics-standard-deviation statistics)
(sqrt (statistics-variance statistics)))