-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
222 lines (206 loc) · 6.55 KB
/
main.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
219
220
221
222
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"github.com/codegangsta/cli"
"github.com/RobbieMcKinstry/UniformRandom/RandGenerator"
"github.com/RobbieMcKinstry/UniformRandom/StatTests"
)
func main() {
app := cli.NewApp()
var (
size int
outputFile string
gen RandGenerator.Generator
ds *RandGenerator.Dataset
)
const (
SEED = 122949823
)
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "n",
Value: 10000,
Usage: "number of elements to generate. Defaults to 10000. NOTE: Kolgorov only uses the first 100",
Destination: &size,
}, cli.StringFlag{
Name: "f",
Value: "output.txt",
Usage: "File to which the output is written, defaults to output.txt",
Destination: &outputFile,
},
}
// Specifies how to interact with the command line tool.
// See https://github.com/codegangsta/cli for explanation
app.Commands = []cli.Command{
{
Name: "kolmogorov",
Aliases: []string{"k"},
Usage: "run the Kolmogov-Smirnov Test for uniformity",
Action: func(c *cli.Context) {
println("Please specify a subcommand. See -h for a list of commands.")
cli.ShowSubcommandHelp(c)
},
Subcommands: []cli.Command{
{
Name: "stdlib",
Usage: "uses the Golang standard library generator, found in 'golang.org/pkg/math/rand'",
Action: func(c *cli.Context) {
gen = rand.New(rand.NewSource(SEED))
ds = RandGenerator.NewDataset(gen, size)
fmt.Printf("Standard Library Kolmogorov Test Statistic:\t%f\n", StatTests.KolmogorovSmirnov(ds))
},
}, {
Name: "lipschultz",
Usage: "uses the generator seeded as a = 101427, c = 321, m = 1 << 16",
Action: func(c *cli.Context) {
gen = RandGenerator.LipschultzGen()
gen.Seed(SEED)
ds = RandGenerator.NewDataset(gen, size)
fmt.Printf("Lipschultz Kolmogorov Test Statistic:\t%f\n", StatTests.KolmogorovSmirnov(ds))
},
}, {
Name: "randu",
Usage: "uses the generator seeded as a = 65539, c = 0, m = 1 << 31",
Action: func(c *cli.Context) {
gen = RandGenerator.NewRANDUGen()
gen.Seed(SEED)
ds = RandGenerator.NewDataset(gen, size)
fmt.Printf("RANDU Kolmogorov Test Statistic:\t%f\n", StatTests.KolmogorovSmirnov(ds))
},
},
},
}, {
Name: "runs",
Aliases: []string{"r"},
Usage: "runs Test for independence",
Action: func(c *cli.Context) {
cli.ShowSubcommandHelp(c)
},
Subcommands: []cli.Command{
{
Name: "stdlib",
Usage: "uses the Golang standard library generator, found in 'golang.org/pkg/math/rand'",
Action: func(c *cli.Context) {
gen = rand.New(rand.NewSource(SEED))
ds = RandGenerator.NewDataset(gen, size)
fmt.Printf("Standard Library Run Test Statistic:\t%f\n", StatTests.Run(ds, StatTests.DIFFERENCE))
},
}, {
Name: "lipschultz",
Usage: "uses the generator seeded as a = 101427, c = 321, m = 1 << 16",
Action: func(c *cli.Context) {
gen = RandGenerator.LipschultzGen()
gen.Seed(SEED)
ds = RandGenerator.NewDataset(gen, size)
fmt.Printf("Lipschultz Run Test Statistic:\t%f\n", StatTests.Run(ds, StatTests.DIFFERENCE))
},
}, {
Name: "randu",
Usage: "uses the generator seeded as a = 65539, c = 0, m = 1 << 31",
Action: func(c *cli.Context) {
gen = RandGenerator.NewRANDUGen()
gen.Seed(SEED)
ds = RandGenerator.NewDataset(gen, size)
fmt.Printf("RANDU Run Test Statistic:\t%f\n", StatTests.Run(ds, StatTests.DIFFERENCE))
},
},
},
}, {
Name: "autocorrelation",
Aliases: []string{"a"},
Usage: "autocorrelation test for independence",
Action: func(c *cli.Context) {
cli.ShowSubcommandHelp(c)
os.Exit(0)
},
Subcommands: []cli.Command{
{
Name: "stdlib",
Usage: "uses the Golang standard library generator, found in 'golang.org/pkg/math/rand'",
Action: func(c *cli.Context) {
gen = rand.New(rand.NewSource(SEED))
ds = RandGenerator.NewDataset(gen, size)
mu, variance := StatTests.Autocorrelation(ds, 1, 3)
fmt.Printf("Standard Library Autocorrelation Test Statistic:\t%f\n", mu/variance)
},
}, {
Name: "lipschultz",
Usage: "uses the generator seeded as a = 101427, c = 321, m = 1 << 16",
Action: func(c *cli.Context) {
gen = RandGenerator.LipschultzGen()
gen.Seed(SEED)
ds = RandGenerator.NewDataset(gen, size)
mu, variance := StatTests.Autocorrelation(ds, 1, 3)
fmt.Printf("Lipschultz Autocorrelation Test Statistic:\t%f\n", mu/variance)
},
}, {
Name: "randu",
Usage: "uses the generator seeded as a = 65539, c = 0, m = 1 << 31",
Action: func(c *cli.Context) {
gen = RandGenerator.NewRANDUGen()
gen.Seed(SEED)
ds = RandGenerator.NewDataset(gen, size)
mu, variance := StatTests.Autocorrelation(ds, 1, 3)
fmt.Printf("RANDU Autocorrelation Test Statistic:\t%f\n", mu/variance)
},
},
},
}, {
Name: "chi",
Aliases: []string{"c"},
Usage: "Chi-Square Frequency Test for uniformity",
Action: func(c *cli.Context) {
cli.ShowSubcommandHelp(c)
},
Subcommands: []cli.Command{
{
Name: "stdlib",
Usage: "uses the Golang standard library generator, found in 'golang.org/pkg/math/rand'",
Action: func(c *cli.Context) {
gen = rand.New(rand.NewSource(SEED))
ds = RandGenerator.NewDataset(gen, size)
fmt.Printf("Standard Library Chi Square Test Statistic:\t%f\n", StatTests.ChiSquared(ds))
},
}, {
Name: "lipschultz",
Usage: "uses the generator seeded as a = 101427, c = 321, m = 1 << 16",
Action: func(c *cli.Context) {
gen = RandGenerator.LipschultzGen()
gen.Seed(SEED)
ds = RandGenerator.NewDataset(gen, size)
fmt.Printf("Lipschultz Chi Square Test Statistic:\t%f\n", StatTests.ChiSquared(ds))
},
}, {
Name: "randu",
Usage: "uses the generator seeded as a = 65539, c = 0, m = 1 << 31",
Action: func(c *cli.Context) {
gen = RandGenerator.NewRANDUGen()
gen.Seed(SEED)
ds = RandGenerator.NewDataset(gen, size)
fmt.Printf("RANDU Chi Square Test Statistic:\t%f\n", StatTests.ChiSquared(ds))
},
},
},
},
}
app.Run(os.Args)
file, err := os.Create(outputFile)
if err != nil {
panic(err)
}
// close file on exit and check for its returned error
defer func() {
if err := file.Close(); err != nil {
panic(err)
}
}()
w := bufio.NewWriter(file)
defer w.Flush()
for i := 0; i < ds.Len(); i++ {
val := ds.Get(i)
w.WriteString(fmt.Sprintf("%f\n", val))
}
}