Skip to content

Commit e99a33e

Browse files
committed
final practice
1 parent 457dbca commit e99a33e

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

new Season/bank_account_sol.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,30 @@ def sdf_Tree(r0,dr,n,p):
4444
print(Rstr)
4545
plt.plot(i,R,'s-')
4646

47+
b = np.array([100,50]).reshape((2,1))
48+
a1 = [np.exp(0.1+0.11), np.exp(0.1+0.09)]
49+
a1 = np.array(a1).reshape((2,1))
50+
51+
a1 = np.ones((2,1))*np.exp(0.1)*np.exp(
52+
np.array([0.11,0.09])).reshape(2,1)
53+
a2 = np.ones((2,1))*np.exp(R[1]*2)
54+
A = np.hstack([a1,a2])
55+
56+
x = np.linalg.inv(A).dot(b)
57+
price = x.sum()
58+
59+
price2 = (100*np.exp(
60+
-0.11)*0.6 + 50*np.exp(-0.09)*0.4)*np.exp(-0.1)
61+
62+
print(price)
63+
print(price2)
64+
65+
66+
67+
68+
69+
70+
71+
4772

4873

new Season/exam_practice.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
d = dict()
2+
for i in range(2,13):
3+
d[i] = []
4+
5+
for i in range(1,7):
6+
for j in range(1,7):
7+
d[i+j].append((i,j))
8+
9+
for x in d:
10+
print(x,d[x])
11+
12+
#%%
13+
import numpy as np
14+
n = 10000
15+
x = np.random.random((n,10))
16+
y = np.random.random((n,10))
17+
18+
#length = np.zeros((n,1))
19+
length = []
20+
for i in range(n):
21+
s = 0
22+
for j in range(10):
23+
if j==9:
24+
s += np.sqrt((x[i,j]-x[i,0])**2+(y[i,j]-y[i,0])**2)
25+
else:
26+
s += np.sqrt((x[i,j]-x[i,j+1])**2+(y[i,j]-y[i,j+1])**2)
27+
length.append(s)
28+
length = np.array(length)
29+
30+
31+
'''
32+
dx = np.diff(x, axis=1)
33+
dy = np.diff(y, axis=1)
34+
length = np.sqrt(dx**2 + dy**2).sum(axis=1)
35+
'''
36+
import matplotlib.pyplot as plt
37+
plt.hist(length,bins=30)
38+
print(length.mean())
39+
print(length.std())
40+
41+
42+
43+
44+
45+

new Season/practice5.docx

67.8 KB
Binary file not shown.

new Season/vix_data.xlsx

72.9 KB
Binary file not shown.

new Season/vix_sol.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
import pandas as pd
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
data = pd.read_excel("vix_data.xlsx")
7+
data.index = data.pop("DATE")
8+
data.dropna(inplace=True)
9+
data["SP500"] = np.log(data["SP500"])
10+
r = data.diff()
11+
12+
fig, ax = plt.subplots(1,1,figsize=(6,4))
13+
ax.scatter(x=r["SP500"], y=r["VIXCLS"],marker='+')
14+
model = pd.ols(x=r["SP500"], y=r["VIXCLS"])
15+
alpha = model.beta.intercept
16+
beta = model.beta.x
17+
x = np.array([-0.15,0.15])
18+
y = alpha + beta*x
19+
ax.plot(x,y,'r-')
20+
ax.text(0,20,r"$\Delta VIX_t ={0:0.3f}+{1:0.3f}\Delta logS_t$".format(alpha, beta), fontsize=15)
21+
ax.set_xlabel(r"$\Delta log S_t$")
22+
ax.set_ylabel(r"$\Delta VIX_t$")
23+
fig.show()
24+
25+
26+
rv = pd.rolling_std(r["SP500"], window=20) * np.sqrt(252)
27+
model1 = pd.ols(x=data["VIXCLS"]/100.0, y=rv)
28+
alpha1 = model1.beta.intercept
29+
beta1 = model1.beta.x
30+
x1 = np.array([0,0.9])
31+
y1 = alpha1 + beta1*x1
32+
33+
fig1, ax1 = plt.subplots(1,2,figsize=(10,4))
34+
ax1[0].scatter(x=data["VIXCLS"]/100.0,y=rv,marker=".")
35+
ax1[0].plot(x1,y1,'r-')
36+
ax1[0].set_xlabel("VIX")
37+
ax1[0].set_ylabel("Historical Vol")
38+
ax1[0].text(-0.15,1.0,r"$Y ={0:0.3f}+{1:0.3f}X+\epsilon$".format(alpha1, beta1), fontsize=15)
39+
40+
ax1[1].plot(data["VIXCLS"]/100.0)
41+
ax1[1].plot(rv)
42+

0 commit comments

Comments
 (0)