-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW3 Python.py
117 lines (86 loc) · 2.95 KB
/
HW3 Python.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
# (a) Simulate a sample path of non-homogeneous Poisson process whose mean
# value function is given by m(t) = 4 log(1 + t) , 0 <= t <= 10 .
# (b) Repeat (a) 10000 times to estimate E[T1] and Var(T1) , where T1 is the first event time.
# (c) Simulate a sample path of non-homogeneous Poisson process whose mean
# value function is given by m(t) = t2 + 2t , 0 <= t <= 10 .
# (d) Repeat (c) sufficiently many times and obtain a good estimate of the probability
# that exactly 5 events occur between time t = 4 and time t = 5 . (cf. (6))
import numpy
### Part A ###
def mFunction(t):
return(4*numpy.log(1+t))
n = numpy.random.poisson(10)
process1 = []
for i in range(0, n):
process1.append(numpy.random.uniform(0, 10))
def N_1(t):
value = 0
for i in process1:
if(i < t):
value +=1
return value
def Nt(t):
return N_1(mFunction(t))
print("N(t) represents the number of events before time t in a non-homogeneous Poisson process with mean function m(t) = 4 log(1 + t).")
print()
print("N(0) = ", Nt(0))
print("N(1) = ", Nt(1))
print("N(2) = ", Nt(2))
print("N(3) = ", Nt(3))
print("N(4) = ", Nt(4))
print("N(5) = ", Nt(5))
print("N(6) = ", Nt(6))
print("N(7) = ", Nt(7))
print("N(7) = ", Nt(7))
print("N(8) = ", Nt(8))
print("N(9) = ", Nt(9))
print("N(10) = ", Nt(10))
print()
### Part B ###
firstEventTimes = []
for i in range(0,10000):
n = numpy.random.poisson(10)
process1 = []
for i in range(0, n):
process1.append(numpy.random.uniform(0, 10))
for j in range(0, 10000):
if(Nt(j/10) >= 1 and Nt(j/10 -.1) == 0):
firstEventTimes.append(j/10)
break
print("In 10,000 trials, the average time of the first event was ", numpy.mean(firstEventTimes), ".")
print("The variance seems to be ", numpy.var(firstEventTimes), ".")
print()
### Part C ###
def mFunction_2(t):
return(t**2 + 2*t)
n = numpy.random.poisson(120)
process1 = []
for i in range(0, n):
process1.append(numpy.random.uniform(0, 120))
def Nt_2(t):
return N_1(mFunction_2(t))
print("N(t) represents the number of events before time t in a non-homogeneous Poisson process with mean function m(t) = t^2 + 2t.")
print()
print("N(0) = ", Nt_2(0))
print("N(1) = ", Nt_2(1))
print("N(2) = ", Nt_2(2))
print("N(3) = ", Nt_2(3))
print("N(4) = ", Nt_2(4))
print("N(5) = ", Nt_2(5))
print("N(6) = ", Nt_2(6))
print("N(7) = ", Nt_2(7))
print("N(7) = ", Nt_2(7))
print("N(8) = ", Nt_2(8))
print("N(9) = ", Nt_2(9))
print("N(10) = ", Nt_2(10))
print()
### Part D ###
fourAndFive = 0
for i in range(0,10000):
n = numpy.random.poisson(120)
process1 = []
for i in range(0, n):
process1.append(numpy.random.uniform(0, 120))
if(Nt_2(5) - Nt_2(4) == 5):
fourAndFive += 1
print("In 10,000 trials, ", fourAndFive, " processes had exactly 5 events between time 4 and time 5. That's ", fourAndFive/10000, " of the trials.")