Gonum package: how to draw samples

Dennis H. Proksch
2 min readJan 3, 2024

--

Photo by Ana Dujmovic on Unsplash

If you need to draw samples for your coding project and want to use go, then the gonum package might be helpful. What follows is a working example to get you started.

(The code provided should run on both Linux and MacOs. I tested it on Ubuntu.)

Setting up the working directory

First, we set up a working directory for our project:

mkdir my-gonum-example
cd my-gonum-example
go mod init example.com/my-gonum-example
touch main.go

This should not have been any surprise.

main.go

Next, open the main.go file. In it, paste this code:

package main

import (
"fmt"

"gonum.org/v1/gonum/stat/distuv"
"gonum.org/v1/gonum/stat/sampleuv"
)

func main() {
// initiliaze a uniform distribution
dist := distuv.Uniform{Min: 0, Max: 1}

// getting one realization
fmt.Println(dist.Rand()) // => e.g. 0.8768263755248197

// getting 3 realizations
out := make([]float64, 3)

// defining the IIDer from which we sample from
smpl := sampleuv.IIDer{Dist: dist}

// populationg out with realizations from dist
smpl.Sample(out)

// looking at the output
fmt.Println(out) // => e.g. [0.8768263755248197 0.7978395862690562 0.3284950221596318]
}

In this file, we first declare the package and the imports. We need two gonum packages: `distuv` and `sampleuv`. “uv” stands for univariate distributions. `distuv` thus refers to all univariate distributions which are baked in into gonum. `sampleuv` provides all the functions to draw samples based on univariate distributions.

Next, we declare the main function. Besides what’s written in the comments, pay attention to these pecularities:

  • You should used “keyed” fields. For example, do not write `distuv.Uniform{0, 1}`, but `distuv.Uniform{Min: 0, Max: 1}`. Otherwise go’s static checker will complain.
  • The documentation actually specifys a third element to the Uniform type: `rand.Source`. We actually provide it implicitly the value nil. Gonum takes then care of the rest. You can provide a `rand.Source` of your own. Therefore, use the math/rand package of the standard libarary.
  • The `IIDer` takes a dist-type as input. If you want a sample with `Sample()`, the function itself does not return anything. Instead, it asks you for a slice as input which is then modified. I guess they made this choice to save (lots of) memory.

Running the file

We want to run the file. If you jump right into `go run main.go`, it will fail. We have not yet downloaded the packages we need. So, let’s update the mod file, download them, and then run our code.

go mod tidy

# you might or might not need the next two lines
go get gonum.org/v1/gonum/stat/distuv
go get gonum.org/v1/gonum/stat/sampleuv

go run main.go

Liked what you read?

I would be happy if you follow me on LinkedIn or on Medium.

--

--

Dennis H. Proksch

Process consultant for innovation, resilience and excellence. My mission: help you achieve twice as much at half the effort. Writing about tech and business.