autocorrelation.lisp 1.76 KB
Newer Older
1 2
;; Autocorrelation
;; Liam Healy, Sun Dec 31 2006 - 13:19
3
;; Time-stamp: <2012-01-13 12:01:14EST autocorrelation.lisp>
4
;;
5
;; Copyright 2006, 2007, 2008, 2009, 2011 Liam M. Healy
6 7 8 9 10 11 12 13 14 15 16 17 18 19
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 21 22 23 24 25

(in-package :gsl)

;;; To do: stride other than 1 when that information is availble from
;;; the vector.

26 27 28
(defmfun autocorrelation ((data vector) &optional mean)
  (("gsl_stats" :type "_lag1_autocorrelation")
   ("gsl_stats" :type "_lag1_autocorrelation_m"))
29 30
  ((((grid:foreign-pointer data) :pointer) (1 :int) ((dim0 data) :sizet))
   (((grid:foreign-pointer data) :pointer) (1 :int) ((dim0 data) :sizet)
31 32 33
    (mean :double)))
  :definition :generic
  :element-types :no-complex
34
  :c-return :double
35
  :inputs (data)
36
  :documentation			; FDL
liam's avatar
liam committed
37
  "The lag-1 autocorrelation of the dataset data.
38 39 40 41
  a_1 = {\sum_{i = 1}^{n} (x_{i} - \Hat\mu) (x_{i-1} - \Hat\mu)
  \over
  \sum_{i = 1}^{n} (x_{i} - \Hat\mu) (x_{i} - \Hat\mu)}.")

liam's avatar
liam committed
42 43
;;; Examples and unit test

Liam Healy's avatar
Liam Healy committed
44
(save-test autocorrelation
45
  (let ((vec #m(-3.21d0 1.0d0 12.8d0)))
46
      (let ((mean (mean vec)))
47 48
	(list
	 (autocorrelation vec)
liam's avatar
liam committed
49
	 (autocorrelation vec mean)))))