Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
alpha-mind
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dr.李
alpha-mind
Commits
ad3cf547
Commit
ad3cf547
authored
Mar 22, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update example 7
parent
19e12944
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
390 additions
and
42 deletions
+390
-42
Example 7 - Portfolio Optimizer Performance.ipynb
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
+390
-42
No files found.
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
View file @
ad3cf547
...
@@ -17,10 +17,8 @@
...
@@ -17,10 +17,8 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"metadata": {},
"collapsed": true
},
"outputs": [],
"outputs": [],
"source": [
"source": [
"import timeit\n",
"import timeit\n",
...
@@ -46,24 +44,23 @@
...
@@ -46,24 +44,23 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"metadata": {},
"collapsed": true
},
"outputs": [],
"outputs": [],
"source": [
"source": [
"ref_date = '2018-02-08'\n",
"ref_date = '2018-02-08'\n",
"u_names = ['sh50', 'hs300', 'zz500', 'zz800', 'ashare_ex']\n",
"u_names = ['sh50', 'hs300', 'zz500', 'zz800', 'ashare_ex']\n",
"b_codes = [16, 300, 905, 906, None]\n",
"b_codes = [16, 300, 905, 906, None]\n",
"risk_model = 'short'\n",
"risk_model = 'short'\n",
"factor = 'EPS'\n",
"lb = 0.0\n",
"lb = 0.0\n",
"ub = 0.1\n",
"ub = 0.1\n",
"data_source = 'postgres+psycopg2://postgres:
we083826@localhost
/alpha'\n",
"data_source = 'postgres+psycopg2://postgres:
A12345678!@10.63.6.220
/alpha'\n",
"engine = SqlEngine(data_source)\n",
"engine = SqlEngine(data_source)\n",
"\n",
"\n",
"universes = [Universe('custom', [u_name]) for u_name in u_names]\n",
"universes = [Universe('custom', [u_name]) for u_name in u_names]\n",
"codes_set = [engine.fetch_codes(ref_date, universe=universe) for universe in universes]\n",
"codes_set = [engine.fetch_codes(ref_date, universe=universe) for universe in universes]\n",
"data_set = [engine.fetch_data(ref_date,
'ep_q'
, codes, benchmark=b_code, risk_model=risk_model) for codes, b_code in zip(codes_set, b_codes)]"
"data_set = [engine.fetch_data(ref_date,
factor
, codes, benchmark=b_code, risk_model=risk_model) for codes, b_code in zip(codes_set, b_codes)]"
]
]
},
},
{
{
...
@@ -76,9 +73,21 @@
...
@@ -76,9 +73,21 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
3
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2018-03-22 17:53:03,797 - ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-22 17:53:04,072 - ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-22 17:53:04,821 - ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-22 17:53:07,024 - ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-22 17:53:07,034 - ALPHA_MIND - INFO - ashare_ex is finished\n"
]
}
],
"source": [
"source": [
"df = pd.DataFrame(columns=u_names, index=['scipy', 'alphamind'])\n",
"df = pd.DataFrame(columns=u_names, index=['scipy', 'alphamind'])\n",
"\n",
"\n",
...
@@ -86,7 +95,7 @@
...
@@ -86,7 +95,7 @@
"\n",
"\n",
"for u_name, sample_data in zip(u_names, data_set):\n",
"for u_name, sample_data in zip(u_names, data_set):\n",
" factor_data = sample_data['factor']\n",
" factor_data = sample_data['factor']\n",
" er = factor_data[
'ep_q'
].values\n",
" er = factor_data[
factor
].values\n",
" lbound = np.ones(len(er)) * lb\n",
" lbound = np.ones(len(er)) * lb\n",
" ubound = np.ones(len(er)) * ub\n",
" ubound = np.ones(len(er)) * ub\n",
"\n",
"\n",
...
@@ -116,9 +125,69 @@
...
@@ -116,9 +125,69 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
4
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sh50</th>\n",
" <th>hs300</th>\n",
" <th>zz500</th>\n",
" <th>zz800</th>\n",
" <th>ashare_ex</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>scipy</th>\n",
" <td>32.43</td>\n",
" <td>132.91</td>\n",
" <td>297.11</td>\n",
" <td>829.00</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <td>0.31</td>\n",
" <td>0.47</td>\n",
" <td>0.64</td>\n",
" <td>0.83</td>\n",
" <td>2.74</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sh50 hs300 zz500 zz800 ashare_ex\n",
"scipy 32.43 132.91 297.11 829.00 NaN\n",
"alphamind 0.31 0.47 0.64 0.83 2.74"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"df"
"df"
]
]
...
@@ -133,17 +202,29 @@
...
@@ -133,17 +202,29 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
5
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2018-03-22 17:53:09,185 - ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-22 17:53:09,228 - ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-22 17:53:09,366 - ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-22 17:53:09,694 - ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-22 17:53:15,241 - ALPHA_MIND - INFO - ashare_ex is finished\n"
]
}
],
"source": [
"source": [
"df = pd.DataFrame(columns=u_names, index=['alphamind'])\n",
"df = pd.DataFrame(columns=u_names, index=['alphamind'])\n",
"turn_over_target = 0.5\n",
"turn_over_target = 0.5\n",
"number = 1\n",
"number = 1\n",
"\n",
"\n",
"for u_name, sample_data in zip(u_names
[:2]
, data_set):\n",
"for u_name, sample_data in zip(u_names, data_set):\n",
" factor_data = sample_data['factor']\n",
" factor_data = sample_data['factor']\n",
" er = factor_data[
'ep_q'
].values\n",
" er = factor_data[
factor
].values\n",
" lbound = np.ones(len(er)) * lb\n",
" lbound = np.ones(len(er)) * lb\n",
" ubound = np.ones(len(er)) * ub\n",
" ubound = np.ones(len(er)) * ub\n",
" \n",
" \n",
...
@@ -166,9 +247,60 @@
...
@@ -166,9 +247,60 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
6
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sh50</th>\n",
" <th>hs300</th>\n",
" <th>zz500</th>\n",
" <th>zz800</th>\n",
" <th>ashare_ex</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <td>0.99</td>\n",
" <td>15.35</td>\n",
" <td>65.26</td>\n",
" <td>156.84</td>\n",
" <td>2,641.52</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sh50 hs300 zz500 zz800 ashare_ex\n",
"alphamind 0.99 15.35 65.26 156.84 2,641.52"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"df"
"df"
]
]
...
@@ -185,12 +317,24 @@
...
@@ -185,12 +317,24 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
7
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2018-03-22 17:53:15,699 - ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-22 17:53:16,466 - ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-22 17:53:17,160 - ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-22 17:53:19,262 - ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-22 17:54:24,333 - ALPHA_MIND - INFO - ashare_ex is finished\n"
]
}
],
"source": [
"source": [
"df = pd.DataFrame(columns=u_names, index=['alphamind', 'direct'])\n",
"df = pd.DataFrame(columns=u_names, index=['alphamind', 'direct'])\n",
"number = 1
0
\n",
"number = 1\n",
"\n",
"\n",
"for u_name, sample_data in zip(u_names, data_set):\n",
"for u_name, sample_data in zip(u_names, data_set):\n",
" all_styles = risk_styles + industry_styles + ['COUNTRY']\n",
" all_styles = risk_styles + industry_styles + ['COUNTRY']\n",
...
@@ -199,7 +343,7 @@
...
@@ -199,7 +343,7 @@
" risk_exposure = factor_data[all_styles].values\n",
" risk_exposure = factor_data[all_styles].values\n",
" special_risk = factor_data['s_srisk'].values\n",
" special_risk = factor_data['s_srisk'].values\n",
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
" er = factor_data[
'ep_q'
].values\n",
" er = factor_data[
factor
].values\n",
"\n",
"\n",
" bm = np.zeros(len(er))\n",
" bm = np.zeros(len(er))\n",
" lbound = -np.ones(len(er)) * np.inf\n",
" lbound = -np.ones(len(er)) * np.inf\n",
...
@@ -221,9 +365,69 @@
...
@@ -221,9 +365,69 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
8
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sh50</th>\n",
" <th>hs300</th>\n",
" <th>zz500</th>\n",
" <th>zz800</th>\n",
" <th>ashare_ex</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <td>104.94</td>\n",
" <td>191.47</td>\n",
" <td>281.23</td>\n",
" <td>827.31</td>\n",
" <td>29,734.30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>direct</th>\n",
" <td>0.13</td>\n",
" <td>111.10</td>\n",
" <td>37.12</td>\n",
" <td>183.78</td>\n",
" <td>2,194.78</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sh50 hs300 zz500 zz800 ashare_ex\n",
"alphamind 104.94 191.47 281.23 827.31 29,734.30\n",
"direct 0.13 111.10 37.12 183.78 2,194.78"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"df"
"df"
]
]
...
@@ -237,9 +441,21 @@
...
@@ -237,9 +441,21 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
9
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2018-03-22 17:54:24,724 - ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-22 17:54:49,448 - ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-22 17:56:47,931 - ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-22 18:14:21,352 - ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-22 18:14:23,308 - ALPHA_MIND - INFO - ashare_ex is finished\n"
]
}
],
"source": [
"source": [
"def func(x):\n",
"def func(x):\n",
" return 0.5 * x @ sec_cov @ x - er @ x\n",
" return 0.5 * x @ sec_cov @ x - er @ x\n",
...
@@ -257,7 +473,7 @@
...
@@ -257,7 +473,7 @@
" risk_exposure = factor_data[all_styles].values\n",
" risk_exposure = factor_data[all_styles].values\n",
" special_risk = factor_data['s_srisk'].values\n",
" special_risk = factor_data['s_srisk'].values\n",
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
" er = factor_data[
'ep_q'
].values\n",
" er = factor_data[
factor
].values\n",
" \n",
" \n",
" bm = np.zeros(len(er))\n",
" bm = np.zeros(len(er))\n",
" lbound = np.zeros(len(er))\n",
" lbound = np.zeros(len(er))\n",
...
@@ -289,9 +505,69 @@
...
@@ -289,9 +505,69 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
10
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sh50</th>\n",
" <th>hs300</th>\n",
" <th>zz500</th>\n",
" <th>zz800</th>\n",
" <th>ashare_ex</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <td>22.85</td>\n",
" <td>43.37</td>\n",
" <td>56.38</td>\n",
" <td>96.70</td>\n",
" <td>844.01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>scipy</th>\n",
" <td>11.82</td>\n",
" <td>12,556.23</td>\n",
" <td>58,165.61</td>\n",
" <td>493,142.76</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sh50 hs300 zz500 zz800 ashare_ex\n",
"alphamind 22.85 43.37 56.38 96.70 844.01\n",
"scipy 11.82 12,556.23 58,165.61 493,142.76 NaN"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"df"
"df"
]
]
...
@@ -306,9 +582,21 @@
...
@@ -306,9 +582,21 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
11
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2018-03-22 18:14:23,659 - ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-22 18:14:28,900 - ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-22 18:16:07,065 - ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-22 18:23:41,655 - ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-22 18:23:45,044 - ALPHA_MIND - INFO - ashare_ex is finished\n"
]
}
],
"source": [
"source": [
"df = pd.DataFrame(columns=u_names, index=['alphamind', 'scipy'])\n",
"df = pd.DataFrame(columns=u_names, index=['alphamind', 'scipy'])\n",
"number = 1\n",
"number = 1\n",
...
@@ -330,7 +618,7 @@
...
@@ -330,7 +618,7 @@
" risk_exposure = factor_data[all_styles].values\n",
" risk_exposure = factor_data[all_styles].values\n",
" special_risk = factor_data['s_srisk'].values\n",
" special_risk = factor_data['s_srisk'].values\n",
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
" er = factor_data[
'ep_q'
].values\n",
" er = factor_data[
factor
].values\n",
" \n",
" \n",
" if 'weight' in factor_data:\n",
" if 'weight' in factor_data:\n",
" bm = factor_data.weight.values\n",
" bm = factor_data.weight.values\n",
...
@@ -365,12 +653,72 @@
...
@@ -365,12 +653,72 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {
"metadata": {},
"collapsed": true
"outputs": [
},
{
"outputs": [],
"data": {
"source": []
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sh50</th>\n",
" <th>hs300</th>\n",
" <th>zz500</th>\n",
" <th>zz800</th>\n",
" <th>ashare_ex</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <td>22.60</td>\n",
" <td>32.65</td>\n",
" <td>61.21</td>\n",
" <td>65.76</td>\n",
" <td>744.30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>scipy</th>\n",
" <td>14.00</td>\n",
" <td>3,379.72</td>\n",
" <td>48,615.18</td>\n",
" <td>213,248.36</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sh50 hs300 zz500 zz800 ashare_ex\n",
"alphamind 22.60 32.65 61.21 65.76 744.30\n",
"scipy 14.00 3,379.72 48,615.18 213,248.36 NaN"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
...
@@ -398,7 +746,7 @@
...
@@ -398,7 +746,7 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.6.
3
"
"version": "3.6.
4
"
},
},
"varInspector": {
"varInspector": {
"cols": {
"cols": {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment