API for bayes
-
by David Edgar Liebke
Full namespace name:
incanter.bayes
Overview
This is library provides functions for performing
basic Bayesian modeling and inference.
Public Variables and Functions
sample-model-params
function
Usage: (sample-model-params size {:keys [x y coefs residuals]})
Returns a sample of the given size of the parameters (coefficients and
error variance) of the given linear-model. The sample is generated using
Gibbs sampling.
See also:
incanter.stats/linear-model
Examples:
(use '(incanter core datasets stats charts bayes))
(def ols-data (to-matrix (get-dataset :survey)))
(def x (sel ols-data (range 0 2313) (range 1 10)))
(def y (sel ols-data (range 0 2313) 10))
(def lm (linear-model y x :intercept false))
(def param-samp (sample-model-params 5000 lm))
;; view trace plots
(view (trace-plot (:var param-samp )))
(view (trace-plot (sel (:coefs param-samp) :cols 0)))
;; view histograms
(view (histogram (:var param-samp)))
(view (histogram (sel (:coefs param-samp) :cols 0)))
;; calculate statistics
(map mean (trans (:coefs param-samp)))
(map median (trans (:coefs param-samp)))
(map sd (trans (:coefs param-samp)))
;; show the 95% bayesian confidence interval for the first coefficient
(quantile (sel (:coefs param-samp) :cols 0) :probs [0.025 0.975])
Source
sample-multinomial-params
function
Usage: (sample-multinomial-params size counts)
Returns a sample of multinomial proportion parameters.
The counts are assumed to have a multinomial distribution.
A uniform prior distribution is assigned to the multinomial vector
theta, then the posterior distribution of theta is
proportional to a dirichlet distribution with parameters
(plus counts 1).
Examples:
(use '(incanter core stats bayes charts))
(def samp-props (sample-multinomial-params 1000 [727 583 137]))
;; view means, 95% CI, and histograms of the proportion parameters
(mean (sel samp-props :cols 0))
(quantile (sel samp-props :cols 0) :probs [0.0275 0.975])
(view (histogram (sel samp-props :cols 0)))
(mean (sel samp-props :cols 1))
(quantile (sel samp-props :cols 1) :probs [0.0275 0.975])
(view (histogram (sel samp-props :cols 1)))
(mean (sel samp-props :cols 2))
(quantile (sel samp-props :cols 2) :probs [0.0275 0.975])
(view (histogram (sel samp-props :cols 2)))
;; view a histogram of the difference in proportions between the first
;; two candidates
(view (histogram (minus (sel samp-props :cols 0) (sel samp-props :cols 1))))
Source
sample-mvn-params
function
Usage: (sample-mvn-params size y & options)
Returns samples of means (sampled from an mvn distribution) and vectorized covariance
matrices (sampled from an inverse-wishart distribution) for the given mvn data.
Arguments:
size -- the number of samples to return
y -- the data used to estimate the parameters
Returns map with following fields:
:means
:sigmas
Examples:
(use '(incanter core stats bayes charts))
(def y (sample-mvn 500 :mean [0 0] :sigma (identity-matrix 2)))
(def samp (sample-mvn-params 1000 y))
(map mean (trans (:means samp)))
(symmetric-matrix (map mean (trans (:sigmas samp))) :lower false)
(view (histogram (sel (:means samp) :cols 0) :x-label "mean 1"))
(view (histogram (sel (:means samp) :cols 1) :x-label "mean 2"))
(view (histogram (sel (:sigmas samp) :cols 1) :x-label "covariance"))
(view (histogram (sel (:sigmas samp) :cols 0) :x-label "variance 1"))
(view (histogram (sel (:sigmas samp) :cols 2) :x-label "variance 2"))
(map #(quantile % :probs [0.025 0.0975]) (trans (:means samp)))
(map #(quantile % :probs [0.025 0.0975]) (trans (:sigmas samp)))
(use '(incanter core stats bayes charts))
(def y (sample-mvn 500 :sigma (symmetric-matrix [10 5 10]) :mean [5 2]))
(def samp (sample-mvn-params 1000 y))
(symmetric-matrix (map mean (trans (:sigmas samp))) :lower false)
(map mean (trans (:means samp)))
Source
sample-proportions
function
Usage: (sample-proportions size counts)
sample-proportions has been renamed sample-multinomial-params
Source