example of a class-based probability system for computations, eventually likelihoods...
[CommonLispStat.git] / src / probability / probability.lisp
blob71b2a9686f121a2c832abeb303d335150617d055
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2010-11-07 11:29:30 tony>
4 ;;; Creation: <2010-11-06 01:51:20 tony>
5 ;;; File: probability.lisp
6 ;;; Author: AJ Rossini <blindglobe@gmail.com>
7 ;;; Copyright: (c)2010--, AJ Rossini. Currently licensed under MIT
8 ;;; license. See file LICENSE.mit in top-level directory
9 ;;; for information.
10 ;;; Purpose: Probability functions
12 ;;; What is this talk of 'release'? Klingons do not make software
13 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
14 ;;; designers and quality assurance people in its wake.
16 ;;; This organization and structure is new to the 21st Century
17 ;;; version.. Think, "21st Century Schizoid Man".
20 ;;; Current computations are handled by leveraging the cl-variates and
21 ;;; gsll packages, as they have flexibility and the capability to be
22 ;;; reproducible. This is just a stub for the interface/API that we
23 ;;; would like to be able to use.
25 (in-package :cls-probability)
27 (defgeneric density (probability-law-instance value))
28 (defmethod density ((pli probability-law) value))
30 (defgeneric distribution (probability-law-instance value))
32 (defgeneric quantile (probability-law-instance value))
34 (defgeneric interquartile-range (probability-law))
36 (defgeneric draw-variates (probability-law-instance n))
38 (defclass probability-parameters ()
40 :documentation "Virtual class to denote prob parameters")
42 (defclass probability-law ()
43 ((density-function
44 :type prob-function
45 :documention "density function, if exists")
46 (mass-function
47 :type prob-function
48 :documention "function")
49 (support-function
50 :documentation "List of values for discrete mass functions, list of pairs denoting for ranges")
51 (support-class
52 :type symbol
53 :documentation "'REAL, 'DISCRETE'")
54 (parameters
55 :type list)
56 (prng-stream
57 :type unif-stream
58 :documentation "current underlying prng stream object, typically Uniform[0,1]"))
59 (:documentation "sufficient data to compute probabilistic
60 quantities. Given the support of the probability law, and a
61 function mapping the law to the prob result, we can compute, in an
62 expensive manner, most quantities. When feasible, we can accelerate
63 this quite a bit.")
64 ())
69 ;; We basically want to support the following style of construct:
71 (let ((my-abstract-law (make 'probability-law
72 :density/mass (gaussian-law
73 :parameters '(:mean 5 :variance 3))
74 :seed 1324
75 :name "Gaussian(5,3)"
76 :documentation "model-distribution, used
77 for likelihoods, probabilities of
78 asympts, and other somethings."))
79 (my-empirical-law (make 'probability-law
80 :density/mass (empirical-law data-vector-or-data-list)
81 :seed 415
82 :name "Empirical Law from observations"
83 :documentation "based on observations,
84 bootstrap/resampling style
85 probability.")))
86 (mean my-law)
87 (variance my-law)
88 (standard-deviation my-law)
89 (draw-variates my-law 10)
90 ;; one of the following would return a number, the other would return a 'nil
91 (probability-density-function my-law x)
92 (probability-mass-function my-law x)
93 (cumulative-distribution-function my-law x)
94 (survivorship-function my-law x)
95 (hazard-function my-law x)
96 (cumulative-hazard-function my-law x)
99 (mean my-empirical-law) ; empirical mean
100 (draw-variates my-empirical-law 10) ; bootstrap (unweighted)
101 ;; the rest would consist of empirical