From 9c4dc22df967daddc91e01740353a4a8638488cf Mon Sep 17 00:00:00 2001
From: AJ Rossini
Date: Sun, 7 Nov 2010 12:16:56 +0100
Subject: [PATCH] example of a class-based probability system for computations,
eventually likelihoods and bayesian computations
---
src/probability/gaussian.lisp | 61 ++++++++++++++++++++++
src/probability/probability.lisp | 106 +++++++++++++++++++++++++++++++++++++++
2 files changed, 167 insertions(+)
create mode 100644 src/probability/gaussian.lisp
create mode 100644 src/probability/probability.lisp
diff --git a/src/probability/gaussian.lisp b/src/probability/gaussian.lisp
new file mode 100644
index 0000000..0574e8c
--- /dev/null
+++ b/src/probability/gaussian.lisp
@@ -0,0 +1,61 @@
+;;; -*- mode: lisp -*-
+
+;;; Time-stamp: <2010-11-07 11:38:53 tony>
+;;; Creation: <2009-03-12 17:14:56 tony>
+;;; File: gaussian.lisp
+;;; Author: AJ Rossini
+;;; Copyright: (c)2009--, AJ Rossini. Currently licensed under MIT
+;;; license. See file LICENSE.mit in top-level directory
+;;; for information.
+;;; Purpose: Gaussian probability law structure and functions
+
+;;; What is this talk of 'release'? Klingons do not make software
+;;; 'releases'. Our software 'escapes', leaving a bloody trail of
+;;; designers and quality assurance people in its wake.
+
+;;; This organization and structure is new to the 21st Century
+;;; version.. Think, "21st Century Schizoid Man".
+
+
+(in-package :cls-probability)
+
+(defun gaussian-probability-density-univariate (x params))
+
+(defclass gaussian-probability-parameters (probability-parameters)
+ ((mean :documentation "vector (or value) which denotes the mean structure.")
+ (variance :documentation "matrix (or value) which denotes covariance structure."))
+ :documentation "standard mean-variance parameterization for gaussian probability law.")
+
+(defclass gaussian-probability-univariate-parameters (gaussian-probability-parameters)
+ ((mean :documentation "vector (or value) which denotes the mean structure."
+ :type univariate-value)
+ (variance :documentation "matrix (or value) which denotes covariance structure."
+ :type univariate-value))
+ :documentation "standard mean-variance parameterization for gaussian probability law.")
+
+(defclass gaussian-probability-multivariate-parameters (gaussian-probability-parameters)
+ ((mean :documentation "vector (or value) which denotes the mean structure."
+ :type array-like)
+ (variance :documentation "matrix (or value) which denotes covariance structure."
+ :type matrix-like))
+ :documentation "standard mean-variance parameterization for gaussian probability law.")
+
+(defgeneric gaussian-probability-density (x params)
+ (:documentation "general gaussian density method.")
+ (:method ((x xarray-vector)
+ (params gaussian-probability-multivariate-parameters))
+ ;; compute multivariate likelihood, and if X is something else, convert it an proceed to this method.
+ )
+ (:method ((x number)
+ (params gaussian-probability-univariate-parameters))
+ (\ (exp (* -1.0 (/ (- x (mean params))
+ (standard-deviation params ))))
+ (sqrt (* 2.0 pi (variance params))))))
+
+
+
+(defclass gaussian-probability-law (probability-law)
+ (()))
+
+
+
diff --git a/src/probability/probability.lisp b/src/probability/probability.lisp
new file mode 100644
index 0000000..71b2a96
--- /dev/null
+++ b/src/probability/probability.lisp
@@ -0,0 +1,106 @@
+;;; -*- mode: lisp -*-
+
+;;; Time-stamp: <2010-11-07 11:29:30 tony>
+;;; Creation: <2010-11-06 01:51:20 tony>
+;;; File: probability.lisp
+;;; Author: AJ Rossini
+;;; Copyright: (c)2010--, AJ Rossini. Currently licensed under MIT
+;;; license. See file LICENSE.mit in top-level directory
+;;; for information.
+;;; Purpose: Probability functions
+
+;;; What is this talk of 'release'? Klingons do not make software
+;;; 'releases'. Our software 'escapes', leaving a bloody trail of
+;;; designers and quality assurance people in its wake.
+
+;;; This organization and structure is new to the 21st Century
+;;; version.. Think, "21st Century Schizoid Man".
+
+
+;;; Current computations are handled by leveraging the cl-variates and
+;;; gsll packages, as they have flexibility and the capability to be
+;;; reproducible. This is just a stub for the interface/API that we
+;;; would like to be able to use.
+
+(in-package :cls-probability)
+
+(defgeneric density (probability-law-instance value))
+(defmethod density ((pli probability-law) value))
+
+(defgeneric distribution (probability-law-instance value))
+
+(defgeneric quantile (probability-law-instance value))
+
+(defgeneric interquartile-range (probability-law))
+
+(defgeneric draw-variates (probability-law-instance n))
+
+(defclass probability-parameters ()
+ ()
+ :documentation "Virtual class to denote prob parameters")
+
+(defclass probability-law ()
+ ((density-function
+ :type prob-function
+ :documention "density function, if exists")
+ (mass-function
+ :type prob-function
+ :documention "function")
+ (support-function
+ :documentation "List of values for discrete mass functions, list of pairs denoting for ranges")
+ (support-class
+ :type symbol
+ :documentation "'REAL, 'DISCRETE'")
+ (parameters
+ :type list)
+ (prng-stream
+ :type unif-stream
+ :documentation "current underlying prng stream object, typically Uniform[0,1]"))
+ (:documentation "sufficient data to compute probabilistic
+ quantities. Given the support of the probability law, and a
+ function mapping the law to the prob result, we can compute, in an
+ expensive manner, most quantities. When feasible, we can accelerate
+ this quite a bit.")
+ ())
+
+
+#|
+
+;; We basically want to support the following style of construct:
+
+ (let ((my-abstract-law (make 'probability-law
+ :density/mass (gaussian-law
+ :parameters '(:mean 5 :variance 3))
+ :seed 1324
+ :name "Gaussian(5,3)"
+ :documentation "model-distribution, used
+ for likelihoods, probabilities of
+ asympts, and other somethings."))
+ (my-empirical-law (make 'probability-law
+ :density/mass (empirical-law data-vector-or-data-list)
+ :seed 415
+ :name "Empirical Law from observations"
+ :documentation "based on observations,
+ bootstrap/resampling style
+ probability.")))
+ (mean my-law)
+ (variance my-law)
+ (standard-deviation my-law)
+ (draw-variates my-law 10)
+ ;; one of the following would return a number, the other would return a 'nil
+ (probability-density-function my-law x)
+ (probability-mass-function my-law x)
+ (cumulative-distribution-function my-law x)
+ (survivorship-function my-law x)
+ (hazard-function my-law x)
+ (cumulative-hazard-function my-law x)
+
+
+ (mean my-empirical-law) ; empirical mean
+ (draw-variates my-empirical-law 10) ; bootstrap (unweighted)
+ ;; the rest would consist of empirical
+
+ ())
+
+|#
+
--
2.10.5.GIT