Debian Codegraph @

dstream/describe.go

raw symbols SCIP
1 package dstream
2
3 import (
4 "math"
5 )
6
7 // Stats contains summary statistics for a float64 Dstream variable.
8 type Stats struct {
9
10 // The mean value
11 Mean float64
12
13 // The minimum value
14 Min float64
15
16 // The maximum value
17 Max float64
18
19 // The standard deviation of the values
20 SD float64
21
22 // The number of non inf/nan values
23 N int
24
25 // The number of Nan values
26 NaN int
27
28 // The number of Inf values
29 Inf int
30 }
31
32 // Describe computes summary statistics for the float64 columns of a dstream.
33 func Describe(data Dstream) map[string]Stats {
34
35 data.Reset()
36
37 p := data.NumVar()
38 stats := make([]Stats, p)
39 first := true
40
41 // Get the min, max and sum.
42 for data.Next() {
43 for j := 0; j < p; j++ {
44 u := data.GetPos(j)
45 x, ok := u.([]float64)
46 if !ok {
47 continue
48 }
49
50 for i, y := range x {
51
52 if math.IsNaN(y) {
53 stats[j].NaN++
54 continue
55 }
56
57 if math.IsInf(y, 0) {
58 stats[j].Inf++
59 continue
60 }
61
62 stats[j].N++
63
64 stats[j].Mean += y
65
66 if (first && i == 0) || y < stats[j].Min {
67 stats[j].Min = y
68 }
69
70 if (first && i == 0) || y > stats[j].Max {
71 stats[j].Max = y
72 }
73 }
74 }
75
76 first = false
77 }
78
79 // Convert sum to mean.
80 for j := range stats {
81 stats[j].Mean /= float64(stats[j].N)
82 }
83
84 // Get the standard deviation.
85 data.Reset()
86 for data.Next() {
87 for j := 0; j < p; j++ {
88 u := data.GetPos(j)
89 x, ok := u.([]float64)
90 if !ok {
91 continue
92 }
93
94 for _, y := range x {
95 u := y - stats[j].Mean
96 stats[j].SD += u * u
97 }
98 }
99 }
100
101 // Convert sum of squares to SD.
102 for j := range stats {
103 stats[j].SD = math.Sqrt(stats[j].SD / float64(stats[j].N))
104 }
105
106 // Put the statistics into a map indexed by variable names.
107 stm := make(map[string]Stats)
108 names := data.Names()
109 for j := 0; j < p; j++ {
110 stm[names[j]] = stats[j]
111 }
112
113 return stm
114 }