Skip to content

Commit 457dbca

Browse files
committed
2 parents f03e45b + aa9daf1 commit 457dbca

File tree

7 files changed

+129
-3
lines changed

7 files changed

+129
-3
lines changed

new Season/bank_account_sol.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
def sdf_MC(r0,dr,n,p):
5+
m = 100000
6+
x = np.random.random((m,n-1))
7+
ud = np.where(x<p,dr,-dr)
8+
R = np.c_[np.zeros((m,1)),ud]
9+
R = r0 + R.cumsum(axis=1)
10+
ba = np.exp(-R.sum(axis=1)).mean()
11+
return ba
12+
13+
def sdf_Tree(r0,dr,n,p):
14+
x = np.ones(n)
15+
for i in range(n-1,-1,-1):
16+
R = np.arange(r0+i*dr,r0-(i+1)*dr,-2*dr)
17+
df = x * np.exp(-R)
18+
x = p*df[:-1] + (1-p)*df[1:]
19+
#p*x[:-1]*exp(-R[:-1]) + (1-p)*x[1:]*exp(-R[1:])
20+
ba = df[0]
21+
return ba
22+
23+
24+
r0 = 0.1
25+
dr = 0.01
26+
p = 0.6
27+
n = 10
28+
29+
mc = sdf_MC(r0, dr, n, p)
30+
tree = sdf_Tree(r0, dr, n, p)
31+
32+
print("MC: E[1/X] = %.6f" % mc)
33+
print("Tree: E[1/X] = %.6f" % tree)
34+
'''
35+
for i in range(1,11):
36+
df = sdf_Tree(r0,dr,i,p)
37+
print(-np.log(df)/i)
38+
'''
39+
sdf_Tree_vec = np.vectorize(sdf_Tree)
40+
i = np.arange(1,11)
41+
dfs = sdf_Tree_vec(r0, dr, i, p)
42+
R = -np.log(dfs)/i
43+
Rstr = ["{0:.5%}".format(r) for r in R]
44+
print(Rstr)
45+
plt.plot(i,R,'s-')
46+
47+
48+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pandas as pd
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import time
5+
6+
p = pd.read_excel("stockprice.xlsx")
7+
p.index = p.pop('Date')
8+
r = np.log(p).diff()
9+
r.dropna(inplace=True)
10+
print(r.describe())
11+
print(r.corr())
12+
13+
pp = p / p.ix[0,:]
14+
pp.plot(figsize=(6,3))
15+
16+
#weight: 투자비중
17+
#ret: 종목별 일간로그수익률 데이터 (pandas.DataFrame)
18+
#n: 시뮬레이션 회수
19+
#res: 함수 실행 결과 시뮬레이션 시나리오별 포트폴리오 수익률 (n개)
20+
def histSimulation(weight, ret, n):
21+
res = np.zeros(n)
22+
s = np.random.randint(0,len(ret),(n,20))
23+
for i in range(n):
24+
sampled = np.exp(ret.ix[s[i,:],:].sum())-1
25+
pr = np.dot(weight, sampled)
26+
res[i] = pr
27+
return res
28+
29+
def mnSimulation(weight, ret, n):
30+
res = np.zeros(n)
31+
for i in range(n):
32+
temp = np.random.multivariate_normal(ret.mean(), ret.cov(), 20)
33+
sampled = np.exp(temp.sum(axis=0))-1
34+
pr = np.dot(weight, sampled)
35+
res[i] = pr
36+
return res
37+
38+
def performSimulation(weight, ret, n, fun):
39+
res = fun(weight, ret, n)
40+
fig, ax = plt.subplots(1,1,figsize=(6,4))
41+
ax.hist(res, bins=50)
42+
temp = [res.mean(),res.std(),np.percentile(res,1),np.percentile(res,99)]
43+
xmax = res.max()
44+
t = "mean={0:.3%}\nstd={1:.3%}\n1% quantile={2:.3%}\n99% quantile={3:.3%}"
45+
ax.text(xmax+0.01, n/50, t.format(temp[0],temp[1],temp[2],temp[3]), fontsize=12)
46+
ax.set_xlim([res.min(), res.max()])
47+
ax.set_title(fun.__name__)
48+
fig.show()
49+
50+
numOfStocks = 5
51+
w = np.ones(numOfStocks) / numOfStocks
52+
n = 10000
53+
54+
t0 = time.time()
55+
performSimulation(w,r,n,histSimulation)
56+
performSimulation(w,r,n,mnSimulation)
57+
print("computation time = %0.3f" % (time.time()-t0))
58+

new Season/practice.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python "c:\FEProgramming1\new Season\practice1_sol.py"

new Season/practice1_sol.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,31 @@
1212
Prob = s/N
1313
pi = Prob * 4
1414
print(pi)
15-
15+
'''
1616
#%%
1717
import random
18+
import numpy as np
1819

1920
N = 100000
2021
count = 0
22+
r = np.random.random((N,2))
23+
#x: min value
24+
#y: max value
25+
x1 = r.min(axis = 1)
26+
x2 = 1 - r.max(axis=1)
27+
x3 = r.max(axis=1) - r.min(axis=1)
28+
x = np.vstack((x1,x2,x3))
29+
mx = x.max(axis=0)
30+
i = mx < 0.5
31+
print("{0:.4%}".format(i.sum()/N))
32+
33+
p = r[i,0:2]
34+
import matplotlib.pyplot as plt
35+
plt.scatter(p[:,0],p[:,1],marker='.')
36+
plt.show()
37+
38+
39+
'''
2140
for i in range(N):
2241
x = random.random()
2342
y = random.random()
@@ -32,7 +51,8 @@
3251
3352
prob = count/N
3453
print(prob)
35-
'''
54+
55+
3656
#%%
3757
import math
3858
number = "02-769-3937"
@@ -55,7 +75,6 @@ def distance(s,e):
5575
5676
calcLength(number)
5777
58-
'''
5978
for i in range(len(n)-1):
6079
s = n[i]
6180
e = n[i+1]

new Season/practice3.docx

104 KB
Binary file not shown.

new Season/practice4.docx

16 KB
Binary file not shown.

new Season/stockprice.xlsx

93.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)