Commit 574f2fd4 authored by Dr.李's avatar Dr.李

update examples

parent 2578387a
...@@ -3,9 +3,7 @@ ...@@ -3,9 +3,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"%matplotlib inline\n", "%matplotlib inline\n",
...@@ -21,10 +19,8 @@ ...@@ -21,10 +19,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 10,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"\"\"\"\n", "\"\"\"\n",
...@@ -35,6 +31,8 @@ ...@@ -35,6 +31,8 @@
"end_date = '2018-02-14'\n", "end_date = '2018-02-14'\n",
"\n", "\n",
"frequency = '10b'\n", "frequency = '10b'\n",
"industry_lower = 1.0\n",
"industry_upper = 1.0\n",
"method = 'risk_neutral'\n", "method = 'risk_neutral'\n",
"neutralize_risk = industry_styles\n", "neutralize_risk = industry_styles\n",
"industry_name = 'sw_adj'\n", "industry_name = 'sw_adj'\n",
...@@ -43,22 +41,20 @@ ...@@ -43,22 +41,20 @@
"benchmark_total_upper = 1.0\n", "benchmark_total_upper = 1.0\n",
"horizon = map_freq(frequency)\n", "horizon = map_freq(frequency)\n",
"weight_gap = 0.01\n", "weight_gap = 0.01\n",
"benchmark_code = 905\n", "benchmark_code = 300\n",
"universe_name = ['zz800']\n", "universe_name = ['zz800']\n",
"universe = Universe('custom', universe_name)\n", "universe = Universe('custom', universe_name)\n",
"ref_dates = makeSchedule(start_date, end_date, frequency, 'china.sse')\n", "ref_dates = makeSchedule(start_date, end_date, frequency, 'china.sse')\n",
"\n", "\n",
"executor = NaiveExecutor()\n", "executor = NaiveExecutor()\n",
"data_source = 'postgres+psycopg2://postgres:we083826@192.168.0.102/alpha'\n", "data_source = 'postgres+psycopg2://postgres:A12345678!@10.63.6.220/alpha'\n",
"engine = SqlEngine(data_source)" "engine = SqlEngine(data_source)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 11,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"\"\"\"\n", "\"\"\"\n",
...@@ -78,20 +74,22 @@ ...@@ -78,20 +74,22 @@
" b_type.append(BoundaryType.RELATIVE)\n", " b_type.append(BoundaryType.RELATIVE)\n",
" l_val.append(benchmark_total_lower)\n", " l_val.append(benchmark_total_lower)\n",
" u_val.append(benchmark_total_upper)\n", " u_val.append(benchmark_total_upper)\n",
" else:\n", " elif name in {'SIZE', 'SIZENL', 'BETA'}:\n",
" b_type.append(BoundaryType.ABSOLUTE)\n", " b_type.append(BoundaryType.ABSOLUTE)\n",
" l_val.append(0.0)\n", " l_val.append(0.0)\n",
" u_val.append(0.0)\n", " u_val.append(0.0)\n",
" else:\n",
" b_type.append(BoundaryType.RELATIVE)\n",
" l_val.append(industry_lower)\n",
" u_val.append(industry_upper)\n",
"\n", "\n",
"bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)" "bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 12,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"\"\"\"\n", "\"\"\"\n",
...@@ -106,17 +104,13 @@ ...@@ -106,17 +104,13 @@
" horizon=horizon,\n", " horizon=horizon,\n",
" offset=1)\n", " offset=1)\n",
"\n", "\n",
"return_groups = codes_return.groupby('trade_date')\n", "return_groups = codes_return.groupby('trade_date')"
"industry_total = engine.fetch_industry_matrix_range(universe, dates=ref_dates, category=industry_name, level=industry_level)\n",
"industry_groups = industry_total.groupby('trade_date')"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 13,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def factor_analysis(engine, factor_name, universe, benchmark_code, positive):\n", "def factor_analysis(engine, factor_name, universe, benchmark_code, positive):\n",
...@@ -124,6 +118,9 @@ ...@@ -124,6 +118,9 @@
" \"\"\"\n", " \"\"\"\n",
" Model phase: we need 1 constant linear model and one linear regression model\n", " Model phase: we need 1 constant linear model and one linear regression model\n",
" \"\"\"\n", " \"\"\"\n",
" industry_total = engine.fetch_industry_matrix_range(universe, dates=ref_dates, category=industry_name, level=industry_level)\n",
" industry_groups = industry_total.groupby('trade_date')\n",
" \n",
" alpha_name = [str(factor_name) + '_' + ('pos' if positive else 'neg')]\n", " alpha_name = [str(factor_name) + '_' + ('pos' if positive else 'neg')]\n",
" simple_expression = LAST(factor_name) if positive else -LAST(factor_name)\n", " simple_expression = LAST(factor_name) if positive else -LAST(factor_name)\n",
"\n", "\n",
...@@ -233,10 +230,8 @@ ...@@ -233,10 +230,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 14,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"df = engine.fetch_factor_coverage(start_date='2011-01-01',\n", "df = engine.fetch_factor_coverage(start_date='2011-01-01',\n",
...@@ -244,22 +239,28 @@ ...@@ -244,22 +239,28 @@
" universe=universe_name[0])\n", " universe=universe_name[0])\n",
"df = df[df.source != 'risk_exposure']\n", "df = df[df.source != 'risk_exposure']\n",
"df = df.groupby('factor').mean()\n", "df = df.groupby('factor').mean()\n",
"df = df[df.coverage >= 0.98]" "df = df[df.coverage >= 0.95]"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 15,
"metadata": { "metadata": {},
"collapsed": true "outputs": [
}, {
"outputs": [], "name": "stdout",
"output_type": "stream",
"text": [
"Wall time: 39min 15s\n"
]
}
],
"source": [ "source": [
"%%time\n", "%%time\n",
"\n", "\n",
"from dask.distributed import Client\n", "from dask.distributed import Client\n",
"\n", "\n",
"client = Client('192.168.0.102:8786')\n", "client = Client('10.63.6.13:8786')\n",
"\n", "\n",
"tasks = client.map(worker_func_positive, df.index.tolist(), pure=False)\n", "tasks = client.map(worker_func_positive, df.index.tolist(), pure=False)\n",
"res1 = client.gather(tasks)\n", "res1 = client.gather(tasks)\n",
...@@ -281,13 +282,11 @@ ...@@ -281,13 +282,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 17,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"writer = pd.ExcelWriter(f'{universe_name[0]}.xlsx', engine='xlsxwriter')\n", "writer = pd.ExcelWriter(f'{universe_name[0]}_{benchmark_code}.xlsx', engine='xlsxwriter')\n",
"factor_df.to_excel(writer, sheet_name='returns')\n", "factor_df.to_excel(writer, sheet_name='returns')\n",
"ic_df.to_excel(writer, sheet_name='ics')\n", "ic_df.to_excel(writer, sheet_name='ics')\n",
"writer.close()" "writer.close()"
...@@ -296,18 +295,7 @@ ...@@ -296,18 +295,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [] "source": []
} }
...@@ -328,7 +316,7 @@ ...@@ -328,7 +316,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"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment