Statistical computing with Breeze
Generating vectors and matrices of random values from a specified distribution is a very common task in most kinds of simulation software. The statistics-related stuff in Breeze is kept in the breeze.stats.distributions
package. Before doing anything, we will need to import it:
scala> import breeze.stats.distributions._ import breeze.stats.distributions._
Now, to fill a vector with uniformly distributed values in the interval [0, 1], you can use the Uniform
distribution:
scala> val u = Uniform(0.0, 1.0) u: breeze.stats.distributions.Uniform = Uniform(0.0,1.0)
To draw a sample from a distribution, you use the sample
method. You can specify the number of samples to draw. In which case, a vector will be returned:
scala> val sample = u.sample(5) sample: IndexedSeq[Double] = Vector(0.314402432799765, 0.9148520439005925, 0.8929580216449953, 0.9447241194598741, 0.5203139296795614) scala> val rv = DenseVector(sample) rv: breeze.linalg.DenseVector[IndexedSeq...