Sling Academy
Home/Golang/Implementing Statistical Functions: Mean, Median, Mode in Go

Implementing Statistical Functions: Mean, Median, Mode in Go

Last updated: November 24, 2024

Understanding Statistical Functions in Go

Statistical functions are essential tools in data processing and analysis. They help summarize data and derive meaningful insights. This article will guide you through implementing three fundamental statistical functions: Mean, Median, and Mode in Go.

Mean

The mean, often referred to as the average, is calculated by summing all the elements in a dataset and dividing by the number of elements.

// Basic implementation of Mean in Go
package main

import "fmt"

func mean(numbers []float64) float64 {
    sum := 0.0
    for _, number := range numbers {
        sum += number
    }
    return sum / float64(len(numbers))
}

func main() {
    data := []float64{1.0, 2.0, 3.0, 4.0, 5.0}
    fmt.Printf("Mean: %.2f\n", mean(data))
}

Median

The median is the middle value of a dataset when sorted in ascending order. If the dataset has an even number of observations, the median is the average of the two middle numbers.

// Intermediate implementation of Median in Go
package main

import (
    "fmt"
    "sort"
)

func median(numbers []float64) float64 {
    sort.Float64s(numbers)
    len := len(numbers)
    if len%2 == 0 {
        return (numbers[len/2-1] + numbers[len/2]) / 2
    }
    return numbers[len/2]
}

func main() {
    data := []float64{3.0, 5.0, 1.0, 4.0, 2.0}
    fmt.Printf("Median: %.2f\n", median(data))
}

Mode

The mode is the number that appears most frequently in a dataset. A dataset may have one mode, more than one mode, or no mode at all.

// Advanced implementation of Mode in Go
package main

import "fmt"

func mode(numbers []int) []int {
    frequencyMap := make(map[int]int)
    maxFrequency := 0
    var modes []int

    for _, number := range numbers {
        frequencyMap[number]++
        if frequencyMap[number] > maxFrequency {
            maxFrequency = frequencyMap[number]
        }
    }

    for number, frequency := range frequencyMap {
        if frequency == maxFrequency {
            modes = append(modes, number)
        }
    }

    return modes
}

func main() {
    data := []int{1, 2, 2, 3, 3, 3, 4}
    fmt.Printf("Mode: %v\n", mode(data))
}

Conclusion

Implementing these statistical functions in Go provides a deeper understanding of data manipulation and statistical analysis. They can serve as the foundation for more complex operations in data analysis.

Next Article: Calculating GCD and LCM with Go

Previous Article: Solving Quadratic Equations Using Go’s Math Functions

Series: Numbers and Math in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant