CaitlinG
CaitlinG

Reputation: 2015

Declaring types in Go for a sample gonum application

Being an ardent fan of numpy, I was pleased to discover that a library for golang was in progress. I wrote a small test program, based heavily on the documentation, that looks like the following:

package main

import (

    "fmt"
    "math"
    "gonum.org/v1/gonum/stat"
)

func main() {

    xs := []float64 {

        23.32, 44.32, 100.12, 191.90,
        23.22, 90.21, 12.22, 191.21,
        1.21, 12.21, 34.23, 91.02,
    }

    variance := stat.Variance(xs)
    fmt.Printf("Data: %v\n", xs)

    stddev := math.Sqrt(variance)

    fmt.Printf("Standard deviation: %d\n\n", stddev)
}

When I attempted to build the program, I noticed the following compiler error:

C:\>go build hello.go
# command-line-arguments
.hello.go:19:30: not enough arguments in call to stat.Variance
        have ([]float64)
        want ([]float64, []float64)

Any advice would be most appreciated.

Thank you.

Upvotes: 0

Views: 98

Answers (1)

Shmulik Klein
Shmulik Klein

Reputation: 3914

stat.Variance expects two parameters of type []float64 of the same length:

func Variance(x, weights []float64) float64

You are missing the weights parameter. You can pass nil as the second parameter of stat.Variance function if you wants to set all the weights of the random variables to 1.

stat Package Documentation

Upvotes: 4

Related Questions