forked from 09superm/python_project_2019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.py
More file actions
39 lines (30 loc) · 1.7 KB
/
2.py
File metadata and controls
39 lines (30 loc) · 1.7 KB
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
import numpy as np
from scipy.optimize import minimize
import math
# GMVP 최소분산포트폴리오
def MinVol_objective(x): # 목적함수
variance = x.T @ covmat @ x # 포트폴리오의 위험 = 비중 * 공분산 * 비중
standard_deviation = variance ** 0.5 # std는 variance의 루트값
return (standard_deviation)
def weight_sum_constraint(x): # 제약함수 x값의 합이 1
return (x.sum() - 1.0)
def MinVol(covmat, lb, ub): # 최적화 구하는 함수 인풋 값은 covmat(공분산 매트릭스),lb(최소비중), ub(최대비중)
x0 = np.repeat(1 / covmat.shape[1], covmat.shape[1]) # 동일 비중만큼 줌
lbound = np.repeat(lb, covmat.shape[1]) # 최소비중
ubound = np.repeat(ub, covmat.shape[1]) # 최대비중
bnds = tuple(zip(lbound, ubound)) # 최소비중과 최대비중의 짝을 만들어줌
constraints = ({"type": "eq", "fun": weight_sum_constraint}) # 제약조건
result = minimize(fun=MinVol_objective,
x0=x0,
method="SLSQP",
constraints=constraints,
bounds=bnds)
return (result.x)
gmvpwgt = MinVol(covmat,0,1)
gmvpret = "%0.6f" % rt_ret.dot(gmvpwgt)
gmvpvar = gmvpwgt.dot(gmvpwgt.dot(covmat))
gmvpstd = "%0.6f" % math.sqrt(gmvpvar)
print("\n===== 결과 =====\n")
print(str(your_input) + "로 포트폴리오를 구성했을 때,\n",
your_input[0], gmvpwgt[0:1], your_input[1], gmvpwgt[1:2], your_input[2], gmvpwgt[2:3], your_input[3], gmvpwgt[3:4],your_input[4],str(gmvpwgt[4:5])
+ "만큼 투자하면 위험을 최소화할 수 있습니다.\n", "이 때 발생되는 위험은",gmvpstd, "수익률의 평균은", str(gmvpret), "입니다.")