-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKS.go
232 lines (182 loc) · 4.71 KB
/
KS.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"regexp"
"sort"
"strconv"
"time"
// "reflect"
)
func main() {
tGlob0 := time.Now()
var distro1file, distro2file string
var distro1, distro2 Distro
var d, prob float64
if len(os.Args) < 3 {
log.Fatal("Please, you need to provide two files to be compared...")
}
distro1file = os.Args[1]
distro2file = os.Args[2]
distro1.Populate(distro1file)
distro2.Populate(distro2file)
fmt.Println(distro2)
d, prob = KSTest(&distro1, &distro2)
fmt.Println(d, prob)
tGlob1 := time.Now()
fmt.Println()
log.Println("Wall time for all ", tGlob1.Sub(tGlob0))
}
type Distro struct {
Values []float64
}
func (distro *Distro) Populate (inFile string) () {
var fileObj *os.File
var nReader *bufio.Reader
var readLine string
var err error
var nLines, voids int
// var reg = regexp.MustCompile(`(\d\.{0,1}\d*)e{0,1}(\D{0,1}\d*)`)
var reg = regexp.MustCompile(`([+-]*\d*\.*\d*e*[+-]\d*)`) // should match any number
var res []string
var num float64
var exp int64
var temp float64
log.Println("Opening data file...")
if fileObj, err = os.Open(inFile); err != nil {
log.Fatal("Can't open %s: error: %s\n", inFile, err)
}
defer fileObj.Close()
nLines, voids = LinesCount(fileObj)
distro.Values = make([]float64, nLines-voids)
// log.Println("Made slice with ", nLines, " elements")
nReader = bufio.NewReader(fileObj)
line := 0
for {
if readLine, err = Readln(nReader); err != nil {
log.Println("Done reading ", line, " lines from file " , inFile, " with ", nLines, " lines with err ", err)
break
}
if len(readLine) == 0 {
break
}
if res = reg.FindStringSubmatch(readLine); len(res) == 0 {
log.Fatal("Regexp found nothing, problem reading file.")
} else if len(res) == 3 {
num, _ = strconv.ParseFloat(res[1], 64)
exp, _ = strconv.ParseInt(res[2], 10, 64)
temp = float64(num * math.Pow10(int(exp)))
} else if len(res) == 2 {
temp, _ = strconv.ParseFloat(res[1], 64)
} else {
log.Fatal("Wrong res ", res)
}
// num, _ = strconv.ParseFloat(res[1], 64)
// exp, _ = strconv.ParseInt(res[1], 10, 64)
// temp = int64(num * math.Pow10(int(exp)))
distro.Values[line] = temp
fmt.Println("temp ", distro.Values[line])
line++
}
sort.Sort(float64arr(distro.Values))
}
func KSTest(distro1, distro2 *Distro) (d, prob float64) {
var en1, en2, en, dt, fn1,fn2 float64
var j1, j2 int
d = 0
prob = 0
en1 = float64(len(distro1.Values))
en2 = float64(len(distro2.Values))
j1 = 1
j2 = 1
fn1 = 0
fn2 = 0
log.Println("KS test")
log.Println("Dimensions ", len(distro1.Values), len(distro2.Values))
for {
if (j1 > len(distro1.Values)-1 || j2 > len(distro2.Values)-1) {
break
}
fmt.Println(j1, distro1.Values[j1], j2, distro2.Values[j2])
time.Sleep(1 * time.Millisecond)
if distro1.Values[j1] <= distro2.Values[j2] {
fmt.Println("Case 1")
j1++
fn1 = float64(j1)/en1
} else if distro1.Values[j1] >= distro2.Values[j2] {
fmt.Println("Case 2")
j2++
fn2 = float64(j2)/en2
}
dt = math.Abs(fn2-fn1)
if dt > d {
fmt.Println("Case 3")
d = dt
}
// os.Exit(0)
}
log.Println("Done KS start prob")
en = math.Sqrt(en1*en2 / (en1+en2))
prob = ProbKs((en+0.12+0.11/en)*d)
return d, prob
}
func ProbKs (alam float64) (float64) {
const EPS1 = 0.001
const EPS2 = 1.0e-8
var a2, fac, sum, term, termbf float64
log.Println("Calculating probability")
fac = 2.0
sum = 0.0
termbf = 0.0
a2 = -2.0*alam*alam
for j:=1; j<=100;j++ {
term=fac*math.Exp(a2*float64(j*j))
sum += term
if ((math.Abs(term) <= EPS1*termbf) || (math.Abs(term) <= EPS2*sum)) {
return sum
}
fac = -fac
termbf = math.Abs(term)
}
return 1.0
}
func LinesCount(fileObj *os.File) (totCount int, voidCount int) {
var (
nReader *bufio.Reader
readLine string
err error
)
log.Println("Counting lines")
nReader = bufio.NewReader(fileObj)
totCount, voidCount = 0, 0
for {
if readLine, err = Readln(nReader); err != nil {break}
if len(readLine) == 0 {voidCount++}
totCount++
}
// log.Println("Rewind file")
if _, err := fileObj.Seek(0,0); err != nil {
log.Fatal("Failed seek of file with error ", err)
}
// log.Println("Done")
return totCount, voidCount
}
func Readln(r *bufio.Reader) (string, error) {
/*from http://stackoverflow.com/questions/6141604/go-readline-string*/
var (isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln),err
}
type float64arr []float64
func (a float64arr) Len() int { return len(a) }
func (a float64arr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a float64arr) Less(i, j int) bool { return a[i] < a[j] }