-
Notifications
You must be signed in to change notification settings - Fork 2
/
stat.go
85 lines (72 loc) · 1.58 KB
/
stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package BayesianNetwork
import (
// "math"
// "testing"
"fmt"
)
type NetworkStat struct {
bn *BayesianNetwork
count []int
total int
}
type StatMap map[string][]float64
func NewNetworkStat(bn *BayesianNetwork) *NetworkStat {
return &NetworkStat{
bn: bn,
total: 0,
count: make([]int, len(bn.nodeIndex)),
}
}
// run through the entire network and increment
// if the value is "T", else ignore
func (stat *NetworkStat) Update() {
for i, node := range stat.bn.nodeIndex {
assignment := node.GetAssignment()
if assignment == "F" {
continue
}
stat.count[i] += 1
}
stat.total += 1
}
// return a mapping of the normalized probablilities
func (stat *NetworkStat) GetStats() StatMap {
stats := make(map[string][]float64, len(stat.count))
for i, node := range stat.bn.GetNodes() {
stats[node.Name()] = []float64{
float64(stat.count[i]) / float64(stat.total),
float64(stat.total-stat.count[i]) / float64(stat.total),
}
}
return stats
}
type NodeStat struct {
node *Node
count, total int
}
func NewNodeStat(node *Node) *NodeStat {
return &NodeStat{
node: node,
count: 0,
total: 0,
}
}
func (ns *NodeStat) Update(assignment string) {
ns.total += 1
if assignment == "F" {
return
}
ns.count += 1
}
func (ns *NodeStat) GetStats() []float64 {
return []float64{
float64(ns.count) / float64(ns.total),
float64(ns.total-ns.count) / float64(ns.total),
}
}
func (ns *NodeStat) String() string {
return fmt.Sprintf("%s: T: %.3f F: %.3f",
ns.node.Name(),
float64(ns.count)/float64(ns.total),
float64(ns.total-ns.count)/float64(ns.total))
}