# Untitled

a guest
Jun 30th, 2021
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. ## code for treatment and illness and overlap
2.
3. import random
4. import datetime
5.
6. #generate random days for illness
7.
8. def illness(start_date, end_date):
9. # Duration of start-end range (R)
10. max_days = (end_date - start_date).days
11.
12. # How many days should the illness start (m)
13. illness_days = random.randint(2, min(max_days, 5))
14.
15.
16. # Number of days after start_date that the first random date is on (N)
17. illness_start = random.randint(0, max_days - illness_days)
18.
19. # Make a list of illness_days consecutive dates starting from the first illness day
20. illness_dates = [start_date + datetime.timedelta(days=illness_start + d) for d in range(illness_days)]
21.
22.
23. #print (type('list comprehension', [start_date + datetime.timedelta(days=illness_start + d) for d in range(illness_days)])
24.
25. return illness_dates
26.
27.
28. #generate random days for microdosing treatment
29. def treatment(start_date, end_date):
30. # Duration of start-end range (R)
31. max_days = (end_date - start_date).days
32.
33. # How many days should the illness start (m)
34. illness_days = random.randint(10, min(max_days, 20))
35.
36.
37. # Number of days after start_date that the first random date is on (N)
38. illness_start = random.randint(0, max_days - illness_days)
39.
40. # Make a list of illness_days consecutive dates starting from the first illness day
41. treatment_dates = [start_date + datetime.timedelta(days=illness_start + d) for d in range(illness_days)]
42.
43.
44. #print (type('list comprehension', [start_date + datetime.timedelta(days=illness_start + d) for d in range(illness_days)])
45.
46. return treatment_dates
47.
48.
49. #count overlapping dates
50. def overlap(final_treat_list, final_ill_list):
51. my_list = list(set(final_treat_list).intersection(final_ill_list))
52. c = len(my_list)
53. # if there is overlap in the set of random dates, c > 0
54. if c > 0:
55. return 1
56. else:
57. return 0
58.
59. #Select random dates for the illness and supplement treatment
60. ill_start_date = datetime.date(2020, 1, 1)
61. ill_end_date = datetime.date(2020, 3, 30)
62. treat_start_date = datetime.date(2020, 1, 1)
63. treat_end_date = datetime.date(2020, 2, 29)
64.
65. #check overlapping by simulating the random dates 15,000 times
66. overlap_list = []
67. for _ in range(15000):
68. overlap_list.append(overlap(illness(ill_start_date, ill_end_date), treatment(treat_start_date, treat_end_date)))
69.
70. print(overlap_list)
71.
72.
73. #This calculates how often an overlap period occures consecutively
74. check = 0
75. result = []
76. count = 0
77. for el in overlap_list:
78. if el == check:
79. count+=1
80. #print ('count under true condition', count)
81. else:
82. #check = el
83. count = 0
84. #print ('this is the count', count)
85. #if count>0:
86. # result.append(count)
87. result.append(count)
88. print ('this is result list', result)
89.
90. count = 0
91.
92. #this counts how often there is 8 or more consecutive non-overlap periods
93. for i in result :
94. if i > 7 :
95. count = count + 1
96.
97.
98. print ('this is the total results', count/15000)
RAW Paste Data