Skip to content

Commit

Permalink
Fix for sample-multinomial with size 1 resulting in ISeq from Double
Browse files Browse the repository at this point in the history
- Evaluating (stats/sample-multinomial 1) causes the exception
  java.lang.IllegalArgumentException: Don't know how to create ISeq from:
  java.lang.Double, because sample-uniform returns a sequence if called with a
  size > 1 and a double otherwise.
- This happens because sample-uniform returns a double if called with size=1
  and a seq otherwise. The fix is to make sample-uniform always return a seq,
  even if it just has one element.
- There is only one place where sample-uniform is called with size=1 and this
  commit updates the call.
- There are two places where sample-uniform is called with size passed in as a
  parameter. One call is in a place in sample where it is always true that
  size>1. The other call is in sample-multinomial and this commit fixes the
  problem there.
  • Loading branch information
Nimalan Mahendran committed Mar 18, 2012
1 parent 7c6878a commit 7214006
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions modules/incanter-core/src/incanter/stats.clj
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,9 @@
(let [min-val (double min)
max-val (double max)
dist (DoubleUniform. min-val max-val (DoubleMersenneTwister.))]
(if (= size 1)
(if integers
(DoubleUniform/staticNextIntFromTo min-val max-val)
(DoubleUniform/staticNextDoubleFromTo min-val max-val))
(if integers
(for [_ (range size)] (DoubleUniform/staticNextIntFromTo min-val max-val))
(for [_ (range size)] (DoubleUniform/staticNextDoubleFromTo min-val max-val)))))))
(if integers
(for [_ (range size)] (DoubleUniform/staticNextIntFromTo min-val max-val))
(for [_ (range size)] (DoubleUniform/staticNextDoubleFromTo min-val max-val))))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -1699,7 +1695,7 @@
(loop [samp-indices [] indices-set #{}]
(if (= (count samp-indices) size)
samp-indices
(let [i (sample-uniform 1 :min 0 :max max-idx :integers true)]
(let [i (first (sample-uniform 1 :min 0 :max max-idx :integers true))]
(if (contains? indices-set i)
(recur samp-indices indices-set)
(recur (conj samp-indices i) (conj indices-set i))))))))))))
Expand Down

0 comments on commit 7214006

Please sign in to comment.