-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulation_RandomWalk.qmd
181 lines (143 loc) · 4.37 KB
/
Simulation_RandomWalk.qmd
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
---
title: Random Walk
format: gfm
fig-width: 7
fig-height: 7
warning: false
toc: true
fig-cap-location: bottom
---
\newpage
# Define Prolbem
$$
\begin{aligned}
& X_i = \pm 1, \quad X_i = \begin{cases}+ 1: & p \\ -1: & q = (1-p) \end{cases}\to \\
& S_n = \sum_{i = 1}^n X_i, \\
& N: 10, \quad n = 100.
\end{aligned}
$$
\newpage
## section (a)
### python coding
```{python}
## load libraries
import numpy as np
import matplotlib.pyplot as plt
def RanodmWalk(N = 10, n = 100, prob = 0.5):
Person_randomWalk = np.zeros([N, n], dtype = int)
for i in range(N):
Person_randomWalk[i, :] = np.random.choice((1, -1),
size = n, replace = True, p = [prob, 1- prob])
Person_sn = np.zeros([N, n], dtype = float)
for i in range(N):
temp = Person_randomWalk[i, :]
Person_sn[i, :] = temp.cumsum()
Person_sn_scale = 1/np.sqrt(n) * Person_sn
result = {'Person_randomWalk': Person_randomWalk,
'Person_sn': Person_sn,
'Person_sn_scale': Person_sn_scale}
return result
simulation = RanodmWalk(N = 10, n = 100, prob = 0.5)
Person_RandomWalk = simulation['Person_randomWalk']
for j in range(10):
print(f'random Walk of Person {(j + 1)}:\n')
print(Person_RandomWalk[j, :])
print("\n")
```
\newpage
## section (b):
### Generate Plots
```{python}
#| fig-cap: "scale Sn for Random Walk by Person"
nn = np.arange(1, 101)
Person_sn_scale = simulation['Person_sn_scale']
fig, ax = plt.subplots(4, 3, figsize = (24, 24))
plt.subplots_adjust(left=0.1, bottom=0.5,
right = 0.4, top = 0.8, wspace = 0.5, hspace = 0.5)
count = 0
for i in range(4):
for j in range(3):
if (i == 3):
y = Person_sn_scale[9, :]
ax[i, 1].plot(nn, y)
ax[i, 1].set_title("Person: 10")
ax[i, 1].set_xlabel(r"$n$")
ax[i, 1].set_ylabel(r"$\frac{S_n}{\sqrt{n}}$")
break
y = Person_sn_scale[count, :]
count += 1
tit = "Person: " + str(count)
ax[i, j].plot(nn, y)
ax[i, j].set_title(tit)
ax[i, j].set_xlabel(r"$n$")
ax[i, j].set_ylabel(r"$\frac{S_n}{\sqrt{n}}$")
lis = ['top', 'right', 'left', 'bottom']
for i in lis:
ax[3, 0].spines[i].set_visible(False)
ax[3, 2].spines[i].set_visible(False)
ax[3, 0].set_xticks([])
ax[3, 0].set_yticks([])
ax[3, 2].set_xticks([])
ax[3, 2].set_yticks([])
plt.show()
```
\newpage
### Generate Bell Shaped Graph
```{python}
#| fig-cap: "Bell-Shaped Graph for Simulation Sn by Persons"
from scipy import stats
def nsim_simulation(nsim = int(1e+4), prob = 0.5, n = 100, N = 10):
sum_Person = np.zeros([N, nsim], dtype = int)
for i in range(nsim):
temp = RanodmWalk(N = N, n = n, prob = prob)
res_person = temp['Person_randomWalk']
for j in range(N):
temp2 = res_person[j, :]
temp3 = temp2.sum()
sum_Person[j, i] = temp3
return sum_Person
Person_sn = nsim_simulation()
fig, ax = plt.subplots(4, 3, figsize = (24, 24))
plt.subplots_adjust(left=0.1, bottom=0.5,
right = 0.4, top = 0.8, wspace = 0.5, hspace = 0.5)
count = 0
for i in range(4):
for j in range(3):
if (i == 3):
y = Person_sn[9, :]
stdd = y.std()
mm = y.mean()
minn = y.min()
maxx = y.max()
xx = np.linspace(minn, maxx, num = 500)
y2 = stats.norm.pdf(xx, loc = mm, scale = stdd)
ax[i, 1].hist(y, density = True)
ax[i, 1].plot(xx, y2, color = 'red')
ax[i, 1].set_title("Person: 10")
ax[i, 1].set_xlabel("")
ax[i, 1].set_ylabel(r"$S_n$")
break
y = Person_sn[count, :]
stdd = y.std()
mm = y.mean()
minn = y.min()
maxx = y.max()
xx = np.linspace(minn, maxx, num = 500)
y2 = stats.norm.pdf(xx, loc = mm, scale = stdd)
count += 1
tit = "Person: " + str(count)
ax[i, j].hist(y, density = True)
ax[i, j].plot(xx, y2, color = 'red')
ax[i, j].set_title(tit)
ax[i, j].set_xlabel(r"$S_n$")
ax[i, j].set_ylabel("")
lis = ['top', 'right', 'left', 'bottom']
for i in lis:
ax[3, 0].spines[i].set_visible(False)
ax[3, 2].spines[i].set_visible(False)
ax[3, 0].set_xticks([])
ax[3, 0].set_yticks([])
ax[3, 2].set_xticks([])
ax[3, 2].set_yticks([])
plt.show()
```