...

Source file src/gonum.org/v1/plot/plotutil/errorpoints.go

Documentation: gonum.org/v1/plot/plotutil

     1  // Copyright ©2015 The Gonum 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 plotutil
     6  
     7  import (
     8  	"math"
     9  	"sort"
    10  
    11  	"gonum.org/v1/plot/plotter"
    12  )
    13  
    14  // ErrorPoints holds a set of x, y pairs along
    15  // with their X and Y errors.
    16  type ErrorPoints struct {
    17  	plotter.XYs
    18  	plotter.XErrors
    19  	plotter.YErrors
    20  }
    21  
    22  // NewErrorPoints returns a new ErrorPoints where each
    23  // point in the ErrorPoints is given by evaluating the
    24  // center function on the Xs and Ys for the corresponding
    25  // set of XY values in the pts parameter.  The XError
    26  // and YError are computed likewise, using the err
    27  // function.
    28  //
    29  // This function can be useful for summarizing sets of
    30  // scatter points using a single point and error bars for
    31  // each element of the scatter.
    32  func NewErrorPoints(f func([]float64) (c, l, h float64), pts ...plotter.XYer) (*ErrorPoints, error) {
    33  
    34  	c := &ErrorPoints{
    35  		XYs:     make(plotter.XYs, len(pts)),
    36  		XErrors: make(plotter.XErrors, len(pts)),
    37  		YErrors: make(plotter.YErrors, len(pts)),
    38  	}
    39  
    40  	for i, xy := range pts {
    41  		xs := make([]float64, xy.Len())
    42  		ys := make([]float64, xy.Len())
    43  		for j := 0; j < xy.Len(); j++ {
    44  			xs[j], ys[j] = xy.XY(j)
    45  			if err := plotter.CheckFloats(xs[j], ys[j]); err != nil {
    46  				return nil, err
    47  			}
    48  		}
    49  		c.XYs[i].X, c.XErrors[i].Low, c.XErrors[i].High = f(xs)
    50  		if err := plotter.CheckFloats(c.XYs[i].X, c.XErrors[i].Low, c.XErrors[i].High); err != nil {
    51  			return nil, err
    52  		}
    53  		c.XYs[i].Y, c.YErrors[i].Low, c.YErrors[i].High = f(ys)
    54  		if err := plotter.CheckFloats(c.XYs[i].Y, c.YErrors[i].Low, c.YErrors[i].High); err != nil {
    55  			return nil, err
    56  		}
    57  	}
    58  
    59  	return c, nil
    60  }
    61  
    62  // MeanAndConf95 returns the mean
    63  // and the magnitude of the 95% confidence
    64  // interval on the mean as low and high
    65  // error values.
    66  //
    67  // MeanAndConf95 may be used as
    68  // the f argument to NewErrorPoints.
    69  func MeanAndConf95(vls []float64) (mean, lowerr, higherr float64) {
    70  	n := float64(len(vls))
    71  
    72  	sum := 0.0
    73  	for _, v := range vls {
    74  		sum += v
    75  	}
    76  	mean = sum / n
    77  
    78  	sum = 0.0
    79  	for _, v := range vls {
    80  		diff := v - mean
    81  		sum += diff * diff
    82  	}
    83  	stdev := math.Sqrt(sum / n)
    84  
    85  	conf := 1.96 * stdev / math.Sqrt(n)
    86  	return mean, conf, conf
    87  }
    88  
    89  // MedianAndMinMax returns the median
    90  // value and error on the median given
    91  // by the minimum and maximum data
    92  // values.
    93  //
    94  // MedianAndMinMax may be used as
    95  // the f argument to NewErrorPoints.
    96  func MedianAndMinMax(vls []float64) (med, lowerr, higherr float64) {
    97  	n := len(vls)
    98  	if n == 0 {
    99  		panic("plotutil: MedianAndMinMax: No values")
   100  	}
   101  	if n == 1 {
   102  		return vls[0], 0, 0
   103  	}
   104  	sort.Float64s(vls)
   105  	if n%2 == 0 {
   106  		med = (vls[n/2]-vls[n/2-1])/2 + vls[n/2-1]
   107  	} else {
   108  		med = vls[n/2]
   109  	}
   110  
   111  	min := vls[0]
   112  	max := vls[0]
   113  	for _, v := range vls {
   114  		min = math.Min(min, v)
   115  		max = math.Max(max, v)
   116  	}
   117  
   118  	return med, med - min, max - med
   119  }
   120  

View as plain text