-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSTAT160BHW5.py
167 lines (139 loc) · 4.14 KB
/
PSTAT160BHW5.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
"""
This is the Python HW 5
for PSTAT 160B
Prof Ichiba
TA: Mousavi
Section: W 1:00 - 1:50pm
"""
# Import libraries
from __future__ import division
import random
import math
import matplotlib.pyplot as plt
import numpy as np
# In the class we see the simple symmetric random walk approximates the standard Brownian motion
def Part_A():
# Simulate a Brownian motion-like sample path
# Graphing (ti, U(ti)) from i = 0, 1, ... N where 2 ^ N
# Where ti := i / N
# And U(ti) = sum of iid normal random variables with mean 0 and variance 1 / N
# Define constants
N = (2 ** 10)
mean = 0
var = (1 / N)
sd = math.sqrt(var)
Ti = [0] # Initialize first time as zero
values = [0] # Initialize the first point as zero
# Loop through 0 to N
# Get time values and generate iid normal random variables
for i in range(1, N):
Ti.append(i / N)
cospi = np.random.normal(mean, sd)
values.append(cospi)
# Get the cumulative sum and save as a list
# Thank you numpy
# These U(ti) values represent B(ti) values
UTi = np.cumsum(values)
# Plot the sample path
plt.plot(Ti, UTi)
plt.show()
def Part_B():
# Repeat this simulation 10000 times to estimate the joint probability
# P((max B(s) from (0 <= s <= 1) > b), B(1) < a) for b >= a, b >= 0
# Can choose appropriate range of (b, a)
# Here a = 0, b = 2
a = 0
b = 1
count = 0
num_sim = 10000
current_sim = 0
# Define constants (taken from part A)
N = (2 ** 10)
mean = 0
var = (1 / N)
sd = math.sqrt(var)
# Run simulations
while current_sim < num_sim:
# Taken from part A
Ti = [0] # Initialize first time as zero
values = [0] # Initialize the first point as zero
# Loop through 0 to N
# Get time values and generate iid normal random variables
for i in range(1, N):
Ti.append(i / N)
cospi = np.random.normal(mean, sd)
values.append(cospi)
# Get the cumulative sum and save as a list
# Thank you numpy
# These U(ti) values represent B(ti) values
UTi = np.cumsum(values)
# Check conditions: if max value is greater than b
# And if the last value is less than a
# If so increment the count
maximum = max(UTi)
last_point = UTi[-1]
if maximum > b and last_point < a:
count += 1
# Increment count
current_sim += 1
# Print the probability
prob = count / num_sim
print("Part B:")
print("The probability that P((max B(s) from (0 <= s <= 1) > b), B(1) < a) for b >= a, b >= 0 is %f" %prob)
def Part_C():
# Let us denote T by the uniform random variable on the unit interval [0, 1]
# independent of Brownian motion.
# As in part b, estimate the tail probability P(|B(T)| >= x) ; x > 0
# First we'll repeat Part A
# Simulate a Brownian motion-like sample path
# Graphing (ti, U(ti)) from i = 0, 1, ... N where 2 ^ N
# Where ti := i / N
# And U(ti) = sum of iid normal random variables with mean 0 and variance 1 / N
# Define constants
N = (2 ** 10)
mean = 0
var = (1 / N)
sd = math.sqrt(var)
Ti = [0] # Initialize first time as zero
values = [0] # Initialize the first point as zero
# Pick a value for x, x > 0
x = 0.5
num_sim = 1000
current_sim = 0
count = 0
# Run simulations
while current_sim < num_sim:
# Taken from part A
# Loop through 0 to N
# Get time values and generate iid normal random variables
for i in range(1, N):
Ti.append(i / N)
cospi = np.random.normal(mean, sd)
values.append(cospi)
# Get the cumulative sum and save as a list
# Thank you numpy
# These U(ti) values represent B(ti) values
UTi = np.cumsum(values)
# Generate uniform random variable
T = np.random.uniform(0, 1)
# Since time is linear and represents a proportion from 0 to 1
# And t(0) corresponds to B(0), t(N) corresponds to B(N)
# t(T) corresponds to B(T * N)
TN = math.floor(T * N)
# Now we index B(s) (or UTi) for the appropropriate value with T plugged in
# We want the absolute value as well
BT = abs(UTi[TN])
# Check if the the absolute value is greater than x
if BT > x:
count += 1
# Increment count
current_sim += 1
if current_sim % 100 == 0:
print(current_sim)
# Print results
prob = count / num_sim
print("Part C:")
print("Our estimate of the tail probability P(|B(T)| >= x) ; x > 0 is %f" %prob)
Part_A()
Part_B()
Part_C()