-
Notifications
You must be signed in to change notification settings - Fork 0
/
power_final.py
191 lines (143 loc) · 6.78 KB
/
power_final.py
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
# Test performance program
# Om testen toe te voegen
# 1. schrijf uw functie testnaam(sample) onder 'Test definitions'. De functie moet een p-waarde
# teruggeven. Als de functie een teststatistiek teruggeeft, bereken dan de
# p-waarde met de monte carlo functie get_pval(sample, statistic), zie pycke test als voorbeeld
# 2. voeg de functie toe aan de list 'tests'
# 3. voeg de naam (string) van de functie toe aan de list 'test_names'
# Om verdelingen toe te voegen
# 1. schrijf een functie verdelingnaam(n) die een sample van grootte n geeft
# uit de verdeling
# 2. voeg in de functie 'samples()' de sample toe aan de list
# 3. voeg de naam (string) van de verdeling toe aan de list 'distribution_names'
# peace Nicolas
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
import time
from tests import *
from sampling import *
N = 500 # number of populations for each sample size
max_size = 200
steps = 10
def samples():
data = list()
for i in range(0, steps):
sample_size = (i+1)*round(max_size/steps)
row = list()
for j in range(0, N):
# row.append([2*np.pi*np.random.rand(sample_size),
# np.random.vonmises(0, 1/2, sample_size),
# np.random.vonmises(0, 1, sample_size),
# np.random.vonmises(0, 2, sample_size),
# wrapped_cauchy(sample_size, 0, 1/2),
# wrapped_cauchy(sample_size, 0, 1),
# wrapped_cauchy(sample_size, 0, 2),
# wrapped_normal(sample_size, 0, 1/2),
# wrapped_normal(sample_size, 0, 1),
# wrapped_normal(sample_size, 0, 2),
# cardioid(sample_size, 0, 1/10),
# ,
# cardioid(sample_size, 0, 1/2),
# double_vonmises(sample_size, 3), semicircle(sample_size, 0.6), semicircle(sample_size, 0.8)]
# )
row.append([final(sample_size, 2), final(sample_size, 5), final(sample_size, 20)])
## row.append([ vonmises_mix(sample_size, 0, .25, np.pi, .25, .5),
## vonmises_mix(sample_size, 0, .25, np.pi/2, .25, .5),
## vonmises_mix(sample_size, 0, .25, np.pi/4, .25, .5),
## vonmises_mix(sample_size, 0, .5, np.pi, .5, .5),
## vonmises_mix(sample_size, 0, .5, np.pi, .25, .5),
## vonmises_mix(sample_size, 0, .5, np.pi, .1, .5),
## semicircle(sample_size, 0.6),
## semicircle(sample_size, 0.8),
## semicircle(sample_size, 0.95)])
## row.append([
## np.random.vonmises(0, 1, sample_size),
## np.random.vonmises(0, 1/2, sample_size),
## np.random.vonmises(0, 1/4, sample_size),
## wrapped_cauchy(sample_size, 0, 1/2),
## wrapped_cauchy(sample_size, 0, 1),
## wrapped_cauchy(sample_size, 0, 2),
## wrapped_normal(sample_size, 0, 1),
## wrapped_normal(sample_size, 0, 1.5),
## wrapped_normal(sample_size, 0, 2),
## cardioid(sample_size, 0, 1/4),
## cardioid(sample_size, 0, 1/8),
## cardioid(sample_size, 0, 1/16),
## ])
##
data.append(row)
return data
## Test performance ##
tests = [rayleigh, rayleigh2]
test_names = ["rayleigh", "rayleigh2"]
distribution_names = ["final", ""]
labels = ["b=2", "b=5", "b=20"]
#distribution_names = ["uniform", "vonmises (0, 1/2)", "vonmises (0, 1)", "vonmises (0, 2)",
# "cauchy (0, 1/2)", "cauchy (0, 1)", "cauchy (0, 2)",
# "normal (0, 1/2)", "normal (0, 1)", "normal (0, 2)",
# "cardioid (0, 1/10)", "cardioid (0, 1/4)", "cardioid (0, 1/2)", "double vonmises", "semicircle 0.6", "semicircle 0.8"]
def performance(data, alpha):
rejections = [[[0 for y in range(0, len(data[0][0]))] for x in range(0, len(data))] for test in tests]
a = [0 for b in range(0, len(tests))]
for i in range(0, len(data)):
print(i, " / ", len(data))
for j in range(0, len(data[i])):
for t in range(0, len(tests)):
test = tests[t]
start = time.time()
for k in range(0, len(data[i][j])):
p = test(data[i][j][k])
if p<alpha:
rejections[t][i][k] += 1
end = time.time()
a[t] += end-start
print([round(x, 3) for x in a])
print([round(x, 3) for x in a])
return (1/(N))*np.array(rejections)
def plot_performance(table):
print(table)
fig, axs = plt.subplots(len(tests), 1, sharex = True, sharey = True)
plt.suptitle("Power")
X = np.linspace(20, max_size, steps)
X_ticks = [20, 100, 200]
if len(table[0][0])>1:
for t in range(0, len(tests)):
for q in range(0, 3):
Y_i = []
for j in range(0, steps):
Y_i.append(table[t][j][q])
if t == 1:
axs[t].plot(X, Y_i, label=labels[q])
axs[t].legend()
else:
axs[t].plot(X, Y_i)
axs[t].set_ylim(ymin = -0.05, ymax = 1.05)
axs[t].label_outer()
axs[t].set_xticks(X, True)
axs[t].set_xticks(X_ticks, False)
#if i == 0:
# axs[t][i].hlines(0.05,0, max_size, colors="r")
for i in range(0, len(tests)):
axs[i].set(ylabel = test_names[i])
axs[-1].set(xlabel = distribution_names[0])
axs[-1].xaxis.label.set_size(12)
plt.legend()
elif len(table[0][0])==1:
for t in range(0, len(tests)):
Y_i = []
for j in range(0, steps):
Y_i.append(table[t][j][0])
axs[t].plot(X, Y_i)
axs[t].set_ylim(ymin=0, ymax = 0.2)
axs[t].hlines(0.05, 0, max_size, colors="r")
axs[t].label_outer()
axs[t].set(ylabel = test_names[t])
axs[-1].set(xlabel = distribution_names[0])
plt.show()
## Run ##
print("generating samples")
S = samples()
print("testing performance")
perf = performance(S, 0.001)
plot_performance(perf)