Sampling without Replacement using Scala and Breeze

1.1k Views Asked by At

Is there support for sampling from a multinomial distribution without replacement? I'm imagining some sort of code like:

import breeze.linalg._
import breeze.stats.distributions._


val params = DenseVector(0.1, 0.3, 0.2, 0.4)
val mult = new Multinomial(params)

val indices = (0 until 4).toArray
val sampled_indices = mult.sample(n = 2, replacement = false)
val other_indices = (indices.toSet.diff(sampled_indices.toSet)).toArray

, where the most relevant bit is the "replacement = false" argument passed to mult.sample(). I'd like to ensure that I sample unique indices, and I'd like to do so without defining a new multinomial distribution for each draw.

Or if there's a better way to accomplish the same result, I'd be happy to hear about that as well.

1

There are 1 best solutions below

2
On

There's nothing built in, sorry. You could do something like

(0 until 2).foldLeft(Seq.empty[Int])( (seq, _) => mult.filter(!seq.contains(_)).draw :+ seq)

Really you want an unfold (or, I guess, a method that does what you listed. If you file an issue on GH, hopefully we'll get to it sometime soon...