-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.py
More file actions
83 lines (73 loc) · 2.12 KB
/
bootstrap.py
File metadata and controls
83 lines (73 loc) · 2.12 KB
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
import random
#units will represent my pooled sample
units = []
#controls is the controls in that bootstrap run
controls = []
#treatments is the treatments in that run
treatments = []
#This represents the sampling distribution, by holding the difference in num successes found during each bootstrap
differences = []
def findIndex(array, value):
for x in range(0, len(array)):
if array[x] >= value:
return x
class Unit:
def __init__(self, value):
self.value = value
units.append(self)
#X is number of successes, n is total count
controlX = 10
controlN = 20
treatmentX = 16
treatmentN = 20
direction = True #true for upper tail
twotailed = False
#Generate X success and N-X fails for control
for x in range(0, controlN):
if x < controlX:
temp = Unit(True)
else:
temp = Unit(False)
#Same for treatment
for x in range(0, treatmentN):
if x < treatmentX:
temp = Unit(True)
else:
temp = Unit(False)
bootstrapAmount = 1000000
for x in range(0, bootstrapAmount):
#Order numbers from 1-40 in random order
samples = random.sample(range(0, 40), 40)
#Put into groups
for x in range(0, 20):
controls.append(units[samples[x]])
for x in range(20, 40):
treatments.append(units[samples[x]])
#Get bootstrap Xes
sampControlX = 0
for x in controls:
if x.value == True:
sampControlX += 1
sampTreatX = 0
for x in treatments:
if x.value == True:
sampTreatX += 1
#Calc diff and add to distr
differences.append(sampTreatX - sampControlX)
controls.clear()
treatments.clear()
differences.sort()
foundDifference = treatmentX - controlX
location = findIndex(differences, foundDifference)
p = 0
if twotailed:
if (location > bootstrapAmount/2):
p = ((bootstrapAmount - location) / bootstrapAmount) * 2
else:
p = (location / boostrapAmount) * 2
else:
if direction:
p = (bootstrapAmount - location) / bootstrapAmount
else:
p = location / bootstrapAmount
print(p)