| 1 | ;; Absolute deviation |
|---|
| 2 | ;; Liam Healy, Sun Dec 31 2006 - 13:19 |
|---|
| 3 | ;; Time-stamp: <2008-03-09 19:21:48EDT absolute-deviation.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | ;;; To do: stride other than 1 when that information is availble from |
|---|
| 9 | ;;; the vector. |
|---|
| 10 | |
|---|
| 11 | (defmfun absolute-deviation-nom (data) |
|---|
| 12 | "gsl_stats_absdev" |
|---|
| 13 | (((gsl-array data) :pointer) (1 :int) ((dim0 data) size)) |
|---|
| 14 | :c-return :double |
|---|
| 15 | :index absolute-deviation |
|---|
| 16 | :export nil |
|---|
| 17 | :documentation ; FDL |
|---|
| 18 | "The absolute deviation from the mean of data. The |
|---|
| 19 | absolute deviation from the mean is defined as |
|---|
| 20 | absdev = (1/N) \sum |x_i - \Hat\mu| |
|---|
| 21 | where x_i are the elements of the dataset data. The |
|---|
| 22 | absolute deviation from the mean provides a more robust measure of the |
|---|
| 23 | width of a distribution than the variance. This function computes the |
|---|
| 24 | mean of data via a call to #'mean.") |
|---|
| 25 | |
|---|
| 26 | (defmfun absolute-deviation-m (data mean) |
|---|
| 27 | "gsl_stats_absdev_m" |
|---|
| 28 | (((gsl-array data) :pointer) (1 :int) |
|---|
| 29 | ((dim0 data) size) (mean :double)) |
|---|
| 30 | :c-return :double |
|---|
| 31 | :index absolute-deviation |
|---|
| 32 | :export nil |
|---|
| 33 | :documentation ; FDL |
|---|
| 34 | "The absolute deviation of the dataset data |
|---|
| 35 | relative to the given value of mean, |
|---|
| 36 | absdev = (1/N) \sum |x_i - mean|. |
|---|
| 37 | This function is useful if you have already computed the mean of |
|---|
| 38 | data (and want to avoid recomputing it), or wish to calculate the |
|---|
| 39 | absolute deviation relative to another value (such as zero, or the |
|---|
| 40 | median).") |
|---|
| 41 | |
|---|
| 42 | (export 'absolute-deviation) |
|---|
| 43 | (defun-optionals absolute-deviation (data &optional mean) |
|---|
| 44 | -nom -m |
|---|
| 45 | ;; FDL |
|---|
| 46 | "The absolute deviation from the mean of data. The |
|---|
| 47 | absolute deviation from the mean is defined as |
|---|
| 48 | absdev = (1/N) \sum |x_i - \Hat\mu| |
|---|
| 49 | where x_i are the elements of the dataset data. The |
|---|
| 50 | absolute deviation from the mean provides a more robust measure of the |
|---|
| 51 | width of a distribution than the variance. This function computes the |
|---|
| 52 | mean of data via a call to #'mean.") |
|---|
| 53 | |
|---|
| 54 | (defmfun weighted-absolute-deviation-nom (data weights) |
|---|
| 55 | "gsl_stats_wabsdev" |
|---|
| 56 | (((gsl-array weights) :pointer) (1 :int) |
|---|
| 57 | ((gsl-array data) :pointer) (1 :int) ((dim0 data) size)) |
|---|
| 58 | :c-return :double |
|---|
| 59 | :index weighted-absolute-deviation |
|---|
| 60 | :export nil) |
|---|
| 61 | |
|---|
| 62 | (defmfun weighted-absolute-deviation-m (data weights mean) |
|---|
| 63 | "gsl_stats_wabsdev_m" |
|---|
| 64 | (((gsl-array weights) :pointer) (1 :int) |
|---|
| 65 | ((gsl-array data) :pointer) (1 :int) |
|---|
| 66 | ((dim0 data) size) (mean :double)) |
|---|
| 67 | :c-return :double |
|---|
| 68 | :index weighted-absolute-deviation |
|---|
| 69 | :export nil) |
|---|
| 70 | |
|---|
| 71 | (export 'weighted-absolute-deviation) |
|---|
| 72 | (defun-optionals weighted-absolute-deviation (data weights &optional mean) |
|---|
| 73 | -nom -m |
|---|
| 74 | ;; FDL |
|---|
| 75 | "The weighted absolute deviation from the weighted |
|---|
| 76 | mean, defined as |
|---|
| 77 | absdev = (\sum w_i |x_i - \Hat\mu|) / (\sum w_i).") |
|---|
| 78 | |
|---|
| 79 | ;;; Examples and unit test |
|---|
| 80 | |
|---|
| 81 | #| |
|---|
| 82 | (make-tests absolute-deviation |
|---|
| 83 | (letm ((vec (vector-double-float #(-3.21d0 1.0d0 12.8d0))) |
|---|
| 84 | (weights (vector-double-float #(3.0d0 1.0d0 2.0d0)))) |
|---|
| 85 | (let ((mean (mean vec))) |
|---|
| 86 | (list |
|---|
| 87 | (absolute-deviation vec) |
|---|
| 88 | (weighted-absolute-deviation vec weights) |
|---|
| 89 | (absolute-deviation vec mean))))) |
|---|
| 90 | |# |
|---|
| 91 | |
|---|
| 92 | (LISP-UNIT:DEFINE-TEST ABSOLUTE-DEVIATION |
|---|
| 93 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 94 | (LIST (LIST 6.18d0 6.647777777777779d0 6.18d0)) |
|---|
| 95 | (MULTIPLE-VALUE-LIST |
|---|
| 96 | (LETM ((VEC (VECTOR-DOUBLE-FLOAT #(-3.21d0 1.0d0 12.8d0))) |
|---|
| 97 | (WEIGHTS (VECTOR-DOUBLE-FLOAT #(3.0d0 1.0d0 2.0d0)))) |
|---|
| 98 | (LET ((MEAN (MEAN VEC))) |
|---|
| 99 | (LIST (ABSOLUTE-DEVIATION VEC) |
|---|
| 100 | (WEIGHTED-ABSOLUTE-DEVIATION VEC WEIGHTS) |
|---|
| 101 | (ABSOLUTE-DEVIATION VEC MEAN))))))) |
|---|
| 102 | |
|---|