PasteRack.org
Paste # 48488
2022-01-14 19:56:19

Fork as a new paste.

Paste viewed 123 times.


Embed:

violin plots

  1. #lang racket
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;; require
  4.  
  5. (require math/statistics
  6.          plot/pict
  7.          plot/utils)
  8.  
  9. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10. ;; functions
  11.  
  12. (define (violin vals
  13.                 #:bandwidth [bandwidth (silverman (second vals))]
  14.                 #:x-min [x-min #f]
  15.                 #:x-max [x-max #f]
  16.                 #:y-min [y-min #f]
  17.                 #:y-max [y-max #f]
  18.                 #:color [color (interval-color)]
  19.                 #:style [style (interval-style)]
  20.                 #:line1-color [line1-color (interval-line1-color)]
  21.                 #:line1-width [line1-width (interval-line1-width)]
  22.                 #:line1-style [line1-style (interval-line1-style)]
  23.                 #:line2-color [line2-color (interval-line2-color)]
  24.                 #:line2-width [line2-width (interval-line2-width)]
  25.                 #:line2-style [line2-style (interval-line2-style)]
  26.                 #:alpha [alpha (interval-alpha)]
  27.                 #:label [label #f])
  28.   (define y-shift (first vals))
  29.   (define-values (f low high)
  30.     (kde (second vals) bandwidth))
  31.   (define x-axis (const 0))
  32.   (define x-min* (or x-min low))
  33.   (define x-max* (or x-max high))
  34.   (define settings
  35.     `([#:y-min . ,y-min]
  36.       [#:y-max . ,y-max]
  37.       [#:color . ,color]
  38.       [#:style . ,style]
  39.       [#:line1-color . ,line1-color]
  40.       [#:line1-width . ,line1-width]
  41.       [#:line1-style . ,line1-style]
  42.       [#:line2-color . ,line2-color]
  43.       [#:line2-width . ,line2-width]
  44.       [#:line2-style . ,line2-style]
  45.       [#:alpha . ,alpha]
  46.       [#:label . ,label]))
  47.   (list (keyword-apply/dict function-interval settings
  48.                             (shift-up (invert f) y-shift)
  49.                             (shift-up f y-shift)
  50.                             x-min* x-max* null)))
  51.  
  52. (define (shift-up f shift)
  53.   (λ (x)
  54.     (+ (f x) shift)))
  55.  
  56. (define ((invert f) x)
  57.   (- (f x)))
  58.  
  59. (define (silverman vals)
  60.   (define iqr (interquartile-range vals))
  61.   (define n (length vals))
  62.   (* 0.9
  63.      (min (stddev vals) (/ iqr 1.34))
  64.      (expt n -0.2)))
  65.  
  66. (define (interquartile-range vals)
  67.   (- (quantile 3/4 < vals)
  68.      (quantile 1/4 < vals)))
  69.  
  70. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  71. ;; example
  72. ;;
  73. ;; `(violin (list y-center x-data))`
  74. ;;
  75.  
  76. (parameterize ([plot-y-ticks no-ticks]
  77.                [plot-y-label #f]
  78.                [plot-x-far-ticks no-ticks]
  79.                [plot-x-label "Time (sec)"])
  80.   (plot (list (violin `[0.00 (0 1 1 2 3 4 4 4 5 6 7 9 10 10 10 11 13)])
  81.               (violin `[0.30 (15 16 17 18 19 20 20 21 23 30)]))))

=>

image