sample

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2018 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GoDoc Build Status codecov.io

Sample

Sample is a Go package to calculate useful values from samples of statistical populations, including:

  • the sample mean

  • the sample-based unbiased estimation of the standard deviation of the population

  • the standard error of the mean

  • the confidence intervals of the mean, assuming the samples comes from a Normal distribution of unknown variance

The standard Go float64 type is used in all computations.

This package does not take advantage of multicore architectures.

Import

import "github.com/alcortesm/sample"

Examples

package main

import (
	"fmt"
	"log"

	"github.com/alcortesm/sample"
)

func main() {
	data := []float64{1.1, 0.9, 1.1, 1.3, 1.0}

	// ignoring errors for demonstration purposes
	mean, _ := sample.Mean(data)
	fmt.Println(mean) // 1.08

	sd, _ := sample.StandardDeviation(data)
	fmt.Println(sd) // 0.15

	se, _ := sample.StandardError(data)
	fmt.Println(se) // 0.07

	confidence := 0.95
	ci, _ := sample.MeanConfidenceIntervals(data, confidence)
	fmt.Printf("[%1.2f, %1.2f]\n", ci[0], ci[1]) // [0.90, 1.26]
}

Author

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package sample implements some useful functions to process samples from statistical populations. The standard Go `float64` type is used in all computations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSampleTooSmall    = errors.New("too few sample points")
	ErrInvalidConfidence = errors.New("invalid confidence level, 0 < confidence < 1)")
)

ErrSampleTooSmall is returned when the provided data sample set is too small for a computation.

ErrInvalidConfidenceLevel is returned when the confidence level passed to MeanConfidenceIntervals is not in the valid range.

Functions

func Mean

func Mean(data []float64) (float64, error)

Mean computes the sample mean of a population sample.

If the sample size is less than 1, it returns ErrSampleTooSmall

func MeanConfidenceIntervals

func MeanConfidenceIntervals(data []float64, confidence float64) ([2]float64, error)

MeanConfidenceIntervals assumes the sample points are from a Normal distribution of unknown variance and calculates the confidence intervals of the mean of the distribution for the given confidence level.

If the sample size is less than 2, it returns ErrSampleTooSmall.

If the confidence value is not in the ]0, 1[ it returns ErrInvalidConfidence.

func StandardDeviation

func StandardDeviation(data []float64) (float64, error)

StandardDeviation computes the sample-based unbiased estimation of the standard deviation of a population.

This implementation uses the Bessel's correction and is downward biassed as per Jensen's inequality.

It is calculated as sqrt(1/(N-1) sum_i_N(x_i-mean(x))).

If the sample size is less than 2, it returns ErrSampleTooSmall

func StandardError

func StandardError(data []float64) (float64, error)

StandardError returns the standard deviation of the sampling distribution of the mean, also known as the "Standard Error of the Mean".

If the sample size is less than 2, it returns ErrSampleTooSmall

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL