github.com/jgbaldwinbrown/perf@v0.1.1/benchmath/anormal.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package benchmath
     6  
     7  import "github.com/aclements/go-moremath/stats"
     8  
     9  // AssumeNormal is an assumption that a sample is normally distributed.
    10  // The summary statistic is the sample mean and comparisons are done
    11  // using the two-sample t-test.
    12  var AssumeNormal = assumeNormal{}
    13  
    14  type assumeNormal struct{}
    15  
    16  var _ Assumption = assumeNormal{}
    17  
    18  func (assumeNormal) SummaryLabel() string {
    19  	return "mean"
    20  }
    21  
    22  func (assumeNormal) Summary(s *Sample, confidence float64) Summary {
    23  	// TODO: Perform a normality test.
    24  
    25  	sample := s.sample()
    26  	mean, lo, hi := sample.MeanCI(confidence)
    27  
    28  	return Summary{
    29  		Center:     mean,
    30  		Lo:         lo,
    31  		Hi:         hi,
    32  		Confidence: confidence,
    33  	}
    34  }
    35  
    36  func (assumeNormal) Compare(s1, s2 *Sample) Comparison {
    37  	t, err := stats.TwoSampleWelchTTest(s1.sample(), s2.sample(), stats.LocationDiffers)
    38  	if err != nil {
    39  		// The t-test failed. Report as if there's no
    40  		// significant difference, along with the error.
    41  		return Comparison{P: 1, N1: len(s1.Values), N2: len(s2.Values), Warnings: []error{err}}
    42  	}
    43  	return Comparison{P: t.P, N1: len(s1.Values), N2: len(s2.Values)}
    44  }