Skip to content

Commit

Permalink
Add scale function
Browse files Browse the repository at this point in the history
  • Loading branch information
Symbolics committed Feb 18, 2024
1 parent 211db0d commit 289b27e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions ls-statistics.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
(:import-from #:nu.statistics
#:quantiles
#:ensure-sorted-reals
#:sorted-reals-elements)
(:import-from #:alexandria #:random-elt #:shuffle)
#:sorted-reals-elements
#:sd)
(:import-from #:alexandria #:random-elt #:shuffle #:if-let)
(:export #:interquartile-range
#:fivenum
#:mean
#:scale
#:variance))
;; #:quantile))

Expand Down Expand Up @@ -64,3 +66,26 @@ Note that alexandria's default for variance will return biased variance. We cha
(t (alexandria:variance object :biased biased?)))) ;no bias to be consistent with nu.statistics

;; Should quantile be empirical-quantile or quantile? Probably nu.statistics should be updated to use distributions. If so, what about mean and variance?


;; TODO: test for scale = T and then use root-mean-square
;; Note: the dispatch on distributions has not been tested
(defun scale (x &key (center (mean x)) (scale (sd x)))
"Return (x - x̄) / s where X̄ is the mean and S is the standard deviation
Modeled on the R scale function.
See https://stat.ethz.ch/R-manual/R-devel/library/base/html/scale.html
"
(typecase x
(distributions::r-rayleigh (distributions:scale x))
(distributions::r-t (distributions:scale x))
(t (when (and (not center) (not scale))
(return-from scale x))
(if (not center)
(setf center 0))
(if (not scale)
(setf scale 1))
(values (nu:e/ (nu:e- x center)
scale)
center scale))))

2 changes: 1 addition & 1 deletion statistics.asd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

(defsystem "statistics"
:name "Statistics functions"
:version "1.2.0"
:version "1.3.0"
:license :MS-PL
:author "Steve Nunez <[email protected]>"
:long-name "Consolidated Common Lisp statistical functions"
Expand Down

0 comments on commit 289b27e

Please sign in to comment.