1// Copyright ©2016 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
5package stat_test
6
7import (
8	"fmt"
9	"math"
10
11	"gonum.org/v1/gonum/floats"
12	"gonum.org/v1/gonum/integrate"
13	"gonum.org/v1/gonum/stat"
14)
15
16func ExampleROC_weighted() {
17	y := []float64{0, 3, 5, 6, 7.5, 8}
18	classes := []bool{false, true, false, true, true, true}
19	weights := []float64{4, 1, 6, 3, 2, 2}
20
21	tpr, fpr, _ := stat.ROC(nil, y, classes, weights)
22	fmt.Printf("true  positive rate: %v\n", tpr)
23	fmt.Printf("false positive rate: %v\n", fpr)
24
25	// Output:
26	// true  positive rate: [0 0.25 0.5 0.875 0.875 1 1]
27	// false positive rate: [0 0 0 0 0.6 0.6 1]
28}
29
30func ExampleROC_unweighted() {
31	y := []float64{0, 3, 5, 6, 7.5, 8}
32	classes := []bool{false, true, false, true, true, true}
33
34	tpr, fpr, _ := stat.ROC(nil, y, classes, nil)
35	fmt.Printf("true  positive rate: %v\n", tpr)
36	fmt.Printf("false positive rate: %v\n", fpr)
37
38	// Output:
39	// true  positive rate: [0 0.25 0.5 0.75 0.75 1 1]
40	// false positive rate: [0 0 0 0 0.5 0.5 1]
41}
42
43func ExampleROC_threshold() {
44	y := []float64{0.1, 0.4, 0.35, 0.8}
45	classes := []bool{false, false, true, true}
46	stat.SortWeightedLabeled(y, classes, nil)
47
48	tpr, fpr, thresh := stat.ROC(nil, y, classes, nil)
49	fmt.Printf("true  positive rate: %v\n", tpr)
50	fmt.Printf("false positive rate: %v\n", fpr)
51	fmt.Printf("cutoff thresholds: %v\n", thresh)
52
53	// Output:
54	// true  positive rate: [0 0.5 0.5 1 1]
55	// false positive rate: [0 0 0.5 0.5 1]
56	// cutoff thresholds: [+Inf 0.8 0.4 0.35 0.1]
57}
58
59func ExampleROC_unsorted() {
60	y := []float64{8, 7.5, 6, 5, 3, 0}
61	classes := []bool{true, true, true, false, true, false}
62	weights := []float64{2, 2, 3, 6, 1, 4}
63
64	stat.SortWeightedLabeled(y, classes, weights)
65
66	tpr, fpr, _ := stat.ROC(nil, y, classes, weights)
67	fmt.Printf("true  positive rate: %v\n", tpr)
68	fmt.Printf("false positive rate: %v\n", fpr)
69
70	// Output:
71	// true  positive rate: [0 0.25 0.5 0.875 0.875 1 1]
72	// false positive rate: [0 0 0 0 0.6 0.6 1]
73}
74
75func ExampleROC_knownCutoffs() {
76	y := []float64{8, 7.5, 6, 5, 3, 0}
77	classes := []bool{true, true, true, false, true, false}
78	weights := []float64{2, 2, 3, 6, 1, 4}
79	cutoffs := []float64{-1, 3, 4}
80
81	stat.SortWeightedLabeled(y, classes, weights)
82
83	tpr, fpr, _ := stat.ROC(cutoffs, y, classes, weights)
84	fmt.Printf("true  positive rate: %v\n", tpr)
85	fmt.Printf("false positive rate: %v\n", fpr)
86
87	// Output:
88	// true  positive rate: [0.875 1 1]
89	// false positive rate: [0.6 0.6 1]
90}
91
92func ExampleROC_equallySpacedCutoffs() {
93	y := []float64{8, 7.5, 6, 5, 3, 0}
94	classes := []bool{true, true, true, false, true, true}
95	weights := []float64{2, 2, 3, 6, 1, 4}
96	n := 9
97
98	stat.SortWeightedLabeled(y, classes, weights)
99	cutoffs := make([]float64, n)
100	floats.Span(cutoffs, math.Nextafter(y[0], y[0]-1), y[len(y)-1])
101
102	tpr, fpr, _ := stat.ROC(cutoffs, y, classes, weights)
103	fmt.Printf("true  positive rate: %.3v\n", tpr)
104	fmt.Printf("false positive rate: %.3v\n", fpr)
105
106	// Output:
107	// true  positive rate: [0.167 0.333 0.583 0.583 0.583 0.667 0.667 0.667 1]
108	// false positive rate: [0 0 0 1 1 1 1 1 1]
109}
110
111func ExampleROC_aUC() {
112	y := []float64{0.1, 0.35, 0.4, 0.8}
113	classes := []bool{true, false, true, false}
114
115	tpr, fpr, _ := stat.ROC(nil, y, classes, nil)
116
117	// Compute Area Under Curve.
118	auc := integrate.Trapezoidal(fpr, tpr)
119	fmt.Printf("true  positive rate: %v\n", tpr)
120	fmt.Printf("false positive rate: %v\n", fpr)
121	fmt.Printf("auc: %v\n", auc)
122
123	// Output:
124	// true  positive rate: [0 0 0.5 0.5 1]
125	// false positive rate: [0 0.5 0.5 1 1]
126	// auc: 0.25
127}
128