File: dataprotos.lsp

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (94 lines) | stat: -rw-r--r-- 3,062 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
;;;; A Data Set Prototype
(defproto data-set-proto '(data title))

(defmeth data-set-proto :isnew (data &key title)
  (send self :data data)
  (if title (send self :title title)))

(defmeth data-set-proto :title (&optional (title nil set))
"Method args: (&optional title)
Sets or retrieves the object's title."
  (if set (setf (slot-value 'title) title))
  (slot-value 'title))

(defmeth data-set-proto :data (&optional (data nil set))
"Method args: (&optional data)
Sets or retrieves the object's data."
  (if set (setf (slot-value 'data) data))
  (slot-value 'data))

(defmeth data-set-proto :describe (&optional (stream t))
"Method args: (&optional (stream t))
Prints a simple description of the object to STREAM."
  (let ((title (send self :title))
	       (data (send self :data)))
    (format stream "This is ~a~%" title)
    (format stream "The sample mean is ~g~%" (mean data))
    (format stream "The sample standard deviation is ~g~%"
            (standard-deviation data))))

(defmeth data-set-proto :plot () (histogram (send self :data)))

(send data-set-proto :title "a data set")

(defun make-data-set (x &key (title "a data set") (print t))
  (let ((object (send data-set-proto :new x :title title)))
    (if print (send object :describe))
    object))

;;;; A Time Series Prototype
(defproto time-series-proto () () data-set-proto)

(defmeth time-series-proto :plot ()
  (let ((data (send self :data)))
    (plot-points (iseq 0 (- (length data) 1)) data)))

(defmeth time-series-proto :describe (&optional (stream t))
  (call-next-method stream)
  (format stream 
          "The autocorrelation is ~g~%" 
          (autocorrelation (send self :data))))

(defun autocorrelation (x)
  (let* ((n (length x)))
    (/ (sum (* (select x (iseq 0 (- n 2))) 
               (select x (iseq 1 (- n 1)))))
       (sum (* x x)))))

(send time-series-proto :title "a time series")

(defun make-time-series (x &key (title "a data set") (print t))
  (let ((object (send time-series-proto :new x :title title)))
    (if print (send object :describe))
    object))

;;;; A Rectangular Data Set Prototype
(defproto rect-data-proto '(labels) () data-set-proto)

(defmeth rect-data-proto :isnew (data &key title labels)
  (let ((n (length data)))
    (send self :data data)
    (if title (send self :title title))
    (send self :labels
          (if labels
              labels
              (mapcar #'(lambda (x) (format nil "X~a" x)) 
                      (iseq 0 (- n 1)))))))

(defmeth rect-data-proto :labels (&optional (labels nil set))
  (if set (setf (slot-value 'labels) labels))
  (slot-value 'labels))

(send rect-data-proto :title "a rectangular data set")

;;;; A Data Set Instance
(setf x (send data-set-proto :new (chisq-rand 20 5)))

;;;; A Time Series Instance
(setf y (send time-series-proto :new
                (let ((e (normal-rand 21)))
                  (+ (select e (iseq 1 20)) 
                     (* .6 (select e (iseq 0 19)))))))

;;;; A Rectangular Data Set Instance
(setf z (send rect-data-proto :new (uniform-rand '(20 20 20 20))))