func ExampleHistogram() { x := make([]float64, 101) for i := range x { x[i] = 1.1 * float64(i) // x data ranges from 0 to 110 } dividers := []float64{7, 20, 100, 1000} fmt.Println(`Histogram counts the amount of data in the bins specified by the dividers. In this data set, there are 7 data points less than 7 (dividers[0]), 12 data points between 7 and 20 (dividers[2] and dividers[1]), and 0 data points above 1000. Since dividers has length 4, there will be 5 bins.`) hist := Histogram(nil, dividers, x, nil) fmt.Printf("Hist = %v\n", hist) fmt.Println() fmt.Println("For ease, the floats Span function can be used to set the dividers") nBins := 10 // Create one fewer divider than bins, but add two to work with Span (see // note below) dividers = make([]float64, nBins+1) min, _ := floats.Min(x) max, _ := floats.Max(x) floats.Span(dividers, min, max) // Span includes the min and the max. Trim the dividers to create 10 buckets dividers = dividers[1 : len(dividers)-1] fmt.Println("len dividers = ", len(dividers)) hist = Histogram(nil, dividers, x, nil) fmt.Printf("Hist = %v\n", hist) fmt.Println() fmt.Println(`Histogram also works with weighted data, and allows reusing of the count field in order to avoid extra garbage`) weights := make([]float64, len(x)) for i := range weights { weights[i] = float64(i + 1) } Histogram(hist, dividers, x, weights) fmt.Printf("Weighted Hist = %v\n", hist) // Output: // Histogram counts the amount of data in the bins specified by // the dividers. In this data set, there are 7 data points less than 7 (dividers[0]), // 12 data points between 7 and 20 (dividers[2] and dividers[1]), and 0 data points // above 1000. Since dividers has length 4, there will be 5 bins. // Hist = [7 12 72 10 0] // // For ease, the floats Span function can be used to set the dividers // len dividers = 9 // Hist = [11 10 10 10 9 11 10 10 9 11] // // Histogram also works with weighted data, and allows reusing of // the count field in order to avoid extra garbage // Weighted Hist = [77 175 275 375 423 627 675 775 783 1067] }
func ExampleHistogram() { x := make([]float64, 101) for i := range x { x[i] = 1.1 * float64(i) // x data ranges from 0 to 110 } dividers := []float64{0, 7, 20, 100, 1000} fmt.Println(`Histogram counts the amount of data in the bins specified by the dividers. In this data set, there are 7 data points less than 7 (between dividers[0] and dividers[1]), 12 data points between 7 and 20 (dividers[1] and dividers[2]), and 0 data points above 1000. Since dividers has length 5, there will be 4 bins.`) hist := Histogram(nil, dividers, x, nil) fmt.Printf("Hist = %v\n", hist) fmt.Println() fmt.Println("For ease, the floats Span function can be used to set the dividers") nBins := 10 dividers = make([]float64, nBins+1) min := floats.Min(x) max := floats.Max(x) // Increase the maximum divider so that the maximum value of x is contained // within the last bucket. max += 1 floats.Span(dividers, min, max) // Span includes the min and the max. Trim the dividers to create 10 buckets hist = Histogram(nil, dividers, x, nil) fmt.Printf("Hist = %v\n", hist) fmt.Println() fmt.Println(`Histogram also works with weighted data, and allows reusing of the count field in order to avoid extra garbage`) weights := make([]float64, len(x)) for i := range weights { weights[i] = float64(i + 1) } Histogram(hist, dividers, x, weights) fmt.Printf("Weighted Hist = %v\n", hist) // Output: // Histogram counts the amount of data in the bins specified by // the dividers. In this data set, there are 7 data points less than 7 (between dividers[0] // and dividers[1]), 12 data points between 7 and 20 (dividers[1] and dividers[2]), // and 0 data points above 1000. Since dividers has length 5, there will be 4 bins. // Hist = [7 12 72 10] // // For ease, the floats Span function can be used to set the dividers // Hist = [11 10 10 10 10 10 10 10 10 10] // // Histogram also works with weighted data, and allows reusing of // the count field in order to avoid extra garbage // Weighted Hist = [66 165 265 365 465 565 665 765 865 965] }
// checkProbQuantContinuous checks that the Prob, Rand, and Quantile are all consistent. // checkProbContinuous only checks that Prob is a valid distribution (integrates // to 1 and greater than 0). However, this is also true if the PDF of a different // distribution is used. This checks that PDF is also consistent with the // CDF implementation and the random samples. func checkProbQuantContinuous(t *testing.T, i int, xs []float64, c cumulantProber, tol float64) { ps := make([]float64, 101) floats.Span(ps, 0, 1) var xp, x float64 for i, p := range ps { x = c.Quantile(p) if p == 0 { xp = x if floats.Min(xs) < x { t.Errorf("Sample of x less than Quantile(0). Case %v.", i) break } continue } if p == 1 { if floats.Max(xs) > x { t.Errorf("Sample of x greater than Quantile(1). Case %v.", i) break } } // The integral of the PDF between xp and x should be the difference in // the quantiles. q := quad.Fixed(c.Prob, xp, x, 1000, nil, 0) if math.Abs(q-(p-ps[i-1])) > 1e-10 { t.Errorf("Integral of PDF doesn't match quantile. Case %v. Want %v, got %v.", i, p-ps[i-1], q) break } pEst := stat.CDF(x, stat.Empirical, xs, nil) if math.Abs(pEst-p) > tol { t.Errorf("Empirical CDF doesn't match quantile. Case %v.", i) } xp = x } }
func main() { fmt.Println(floats.Max([]float64{3, 1, 4, 1})) }