1.2.3-SNAPSHOT Arrow_down_16x16

sample-multinomial

incanter.stats

  • (sample-multinomial size & options)
Returns a sequence representing a sample from a multinomial distribution.

Arguments: size -- number of values to return

Options:
:categories (default [0 1]) -- the values returned
:probs (default [0.5 0.5]) -- the probabilities associated with each category


References:
http://en.wikipedia.org/wiki/Multinomial_distribution#Sampling_from_a_multinomial_distribution


Examples:
(use '(incanter core stats charts))

(sample-multinomial 10)
(sample-multinomial 10 :probs [0.25 0.5 0.25])

;; estimate sample proportions
(def sample-size 1000.0)
(def categories [:red :yellow :blue :green])
(def data (to-dataset (sample-multinomial sample-size
:categories categories
:probs [0.5 0.25 0.2 0.05])))

;; check the sample proportions
(view (pie-chart categories
(map #(div (count ($ :col-0 ($where {:col-0 %} data)))
sample-size)
categories)))


0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source incanter/stats.clj:1165 top

(defn sample-multinomial
" Returns a sequence representing a sample from a multinomial distribution.

  Arguments: size -- number of values to return

  Options:
    :categories (default [0 1]) -- the values returned
    :probs (default [0.5 0.5]) -- the probabilities associated with each category


  References:
    http://en.wikipedia.org/wiki/Multinomial_distribution#Sampling_from_a_multinomial_distribution


  Examples:
    (use '(incanter core stats charts))

    (sample-multinomial 10)
    (sample-multinomial 10 :probs [0.25 0.5 0.25])

    ;; estimate sample proportions
    (def sample-size 1000.0)
    (def categories [:red :yellow :blue :green])
    (def data (to-dataset (sample-multinomial sample-size 
			   	              :categories categories
				              :probs [0.5 0.25 0.2 0.05])))

    ;; check the sample proportions
    (view (pie-chart categories
                     (map #(div (count ($ :col-0 ($where {:col-0 %} data))) 
                                sample-size)
                          categories)))
                      

"
  ([size & options]
     (let [opts (when options (apply assoc {} options))
	   probs (or (:probs opts) [0.5 0.5])
	   categories (or (:categories opts) (range (count probs)))
	   cumulative-probs (cumulative-sum probs)]
       (for [x (sample-uniform size)] 
	 (loop [i 0]
	   (if (>= (nth cumulative-probs i) x)
	     (nth categories i)
	     (recur (inc i))))))))
Vars in incanter.stats/sample-multinomial: >= defn for inc let loop nth
Used in 0 other vars

Comments top

No comments for sample-multinomial. Log in to add a comment.