| 1 | ;; Skewness and kurtosis. |
|---|
| 2 | ;; Liam Healy, Sun Dec 31 2006 - 14:20 |
|---|
| 3 | ;; Time-stamp: <2008-03-09 19:21:48EDT higher-moments.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | ;;; To do: stride other than 1 when that information is availble from |
|---|
| 8 | ;;; the vector. |
|---|
| 9 | |
|---|
| 10 | (defmfun skewness-nomsd (data) |
|---|
| 11 | "gsl_stats_skew" |
|---|
| 12 | (((gsl-array data) :pointer) (1 :int) ((dim0 data) size)) |
|---|
| 13 | :c-return :double |
|---|
| 14 | :index skewness |
|---|
| 15 | :export nil |
|---|
| 16 | :documentation ; FDL |
|---|
| 17 | "The skewness of data, defined as |
|---|
| 18 | skew = (1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^3 |
|---|
| 19 | where x_i are the elements of the dataset data. |
|---|
| 20 | The skewness measures the asymmetry of the tails of a distribution.") |
|---|
| 21 | |
|---|
| 22 | (defmfun skewness-msd (data mean standard-deviation) |
|---|
| 23 | "gsl_stats_skew_m_sd" |
|---|
| 24 | (((gsl-array data) :pointer) (1 :int) |
|---|
| 25 | ((dim0 data) size) (mean :double) (standard-deviation :double)) |
|---|
| 26 | :c-return :double |
|---|
| 27 | :index skewness |
|---|
| 28 | :export nil |
|---|
| 29 | :documentation ; FDL |
|---|
| 30 | "The skewness of the dataset data using the |
|---|
| 31 | given values of the mean and standard deviation, |
|---|
| 32 | skew = (1/N) \sum ((x_i - mean)/sd)^3 |
|---|
| 33 | These functions are useful if you have already computed the mean and |
|---|
| 34 | standard deviation of data and want to avoid recomputing them.") |
|---|
| 35 | |
|---|
| 36 | (export 'skewness) |
|---|
| 37 | (defun-optionals skewness |
|---|
| 38 | (data &optional mean standard-deviation) |
|---|
| 39 | -nomsd -msd |
|---|
| 40 | ;; FDL |
|---|
| 41 | "The skewness of data defined as |
|---|
| 42 | skew = (1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^3 |
|---|
| 43 | where x_i are the elements of the dataset data. |
|---|
| 44 | The skewness measures the asymmetry of the tails of a distribution.") |
|---|
| 45 | |
|---|
| 46 | (defmfun kurtosis-nomsd (data) |
|---|
| 47 | "gsl_stats_kurtosis" |
|---|
| 48 | (((gsl-array data) :pointer) (1 :int) ((dim0 data) size)) |
|---|
| 49 | :c-return :double |
|---|
| 50 | :index kurtosis |
|---|
| 51 | :export nil) |
|---|
| 52 | |
|---|
| 53 | (defmfun kurtosis-msd (data mean standard-deviation) |
|---|
| 54 | "gsl_stats_kurtosis_m_sd" |
|---|
| 55 | (((gsl-array data) :pointer) (1 :int) |
|---|
| 56 | ((dim0 data) size) (mean :double) (standard-deviation :double)) |
|---|
| 57 | :c-return :double |
|---|
| 58 | :index kurtosis |
|---|
| 59 | :export nil) |
|---|
| 60 | |
|---|
| 61 | (export 'kurtosis) |
|---|
| 62 | (defun-optionals kurtosis |
|---|
| 63 | (data &optional mean standard-deviation) |
|---|
| 64 | -nomsd -msd |
|---|
| 65 | ;; FDL |
|---|
| 66 | "The kurtosis of data defined as |
|---|
| 67 | kurtosis = ((1/N) \sum ((x_i - \Hat\mu)/\Hat\sigma)^4) - 3 |
|---|
| 68 | The kurtosis measures how sharply peaked a distribution is, |
|---|
| 69 | relative to its width. The kurtosis is normalized to zero |
|---|
| 70 | for a gaussian distribution.") |
|---|
| 71 | |
|---|
| 72 | (defmfun weighted-skewness-nomsd (data weights) |
|---|
| 73 | "gsl_stats_wskew" |
|---|
| 74 | (((gsl-array weights) :pointer) (1 :int) |
|---|
| 75 | ((gsl-array data) :pointer) (1 :int) ((dim0 data) size)) |
|---|
| 76 | :c-return :double |
|---|
| 77 | :index weighted-skewness |
|---|
| 78 | :export nil) |
|---|
| 79 | |
|---|
| 80 | (defmfun weighted-skewness-msd (data weights mean standard-deviation) |
|---|
| 81 | "gsl_stats_wskew_m_sd" |
|---|
| 82 | (((gsl-array data) :pointer) (1 :int) |
|---|
| 83 | ((dim0 data) size) (mean :double) (standard-deviation :double)) |
|---|
| 84 | :c-return :double |
|---|
| 85 | :index weighted-skewness |
|---|
| 86 | :export nil) |
|---|
| 87 | |
|---|
| 88 | (export 'weighted-skewness) |
|---|
| 89 | (defun-optionals weighted-skewness |
|---|
| 90 | (data weights &optional mean standard-deviation) |
|---|
| 91 | -nomsd -msd |
|---|
| 92 | ;; FDL |
|---|
| 93 | "The weighted skewness of the dataset. |
|---|
| 94 | skew = (\sum w_i ((x_i - xbar)/\sigma)^3) / (\sum w_i).") |
|---|
| 95 | |
|---|
| 96 | (defmfun weighted-kurtosis-nomsd (data weights) |
|---|
| 97 | "gsl_stats_wkurtosis" |
|---|
| 98 | (((gsl-array data) :pointer) (1 :int) ((dim0 data) size)) |
|---|
| 99 | :c-return :double |
|---|
| 100 | :index weighted-kurtosis |
|---|
| 101 | :export nil) |
|---|
| 102 | |
|---|
| 103 | (defmfun weighted-kurtosis-msd (data weights mean standard-deviation) |
|---|
| 104 | "gsl_stats_wkurtosis_m_sd" |
|---|
| 105 | (((gsl-array data) :pointer) (1 :int) |
|---|
| 106 | ((dim0 data) size) (mean :double) (standard-deviation :double)) |
|---|
| 107 | :c-return :double |
|---|
| 108 | :index weighted-kurtosis |
|---|
| 109 | :export nil) |
|---|
| 110 | |
|---|
| 111 | (export 'weighted-kurtosis) |
|---|
| 112 | (defun-optionals weighted-kurtosis |
|---|
| 113 | (data weights &optional mean standard-deviation) |
|---|
| 114 | -nomsd -msd |
|---|
| 115 | ;; FDL |
|---|
| 116 | "The weighted kurtosis of the dataset. |
|---|
| 117 | kurtosis = ((\sum w_i ((x_i - xbar)/sigma)^4) / (\sum w_i)) - 3.") |
|---|
| 118 | |
|---|
| 119 | ;;; Examples and unit test |
|---|
| 120 | |
|---|
| 121 | #| |
|---|
| 122 | (make-tests higher-moments |
|---|
| 123 | (letm ((vec (vector-double-float #(-3.21d0 1.0d0 12.8d0)))) |
|---|
| 124 | (let* ((mean (mean vec)) |
|---|
| 125 | (sd (standard-deviation vec mean))) |
|---|
| 126 | (list |
|---|
| 127 | (skewness vec) |
|---|
| 128 | (skewness vec mean sd) |
|---|
| 129 | (kurtosis vec) |
|---|
| 130 | (kurtosis vec mean sd))))) |
|---|
| 131 | |# |
|---|
| 132 | |
|---|
| 133 | (LISP-UNIT:DEFINE-TEST HIGHER-MOMENTS |
|---|
| 134 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 135 | (LIST |
|---|
| 136 | (LIST 0.2765118983985497d0 0.2765118983985497d0 |
|---|
| 137 | -2.333333333333333d0 -2.333333333333333d0)) |
|---|
| 138 | (MULTIPLE-VALUE-LIST |
|---|
| 139 | (LETM ((VEC (VECTOR-DOUBLE-FLOAT #(-3.21d0 1.0d0 12.8d0)))) |
|---|
| 140 | (LET* ((MEAN (MEAN VEC)) |
|---|
| 141 | (SD (STANDARD-DEVIATION VEC MEAN))) |
|---|
| 142 | (LIST (SKEWNESS VEC) (SKEWNESS VEC MEAN SD) |
|---|
| 143 | (KURTOSIS VEC) |
|---|
| 144 | (KURTOSIS VEC MEAN SD))))))) |
|---|