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

update examples

parent 2578387a
......@@ -3,9 +3,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
......@@ -21,10 +19,8 @@
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
......@@ -35,6 +31,8 @@
"end_date = '2018-02-14'\n",
"\n",
"frequency = '10b'\n",
"industry_lower = 1.0\n",
"industry_upper = 1.0\n",
"method = 'risk_neutral'\n",
"neutralize_risk = industry_styles\n",
"industry_name = 'sw_adj'\n",
......@@ -43,22 +41,20 @@
"benchmark_total_upper = 1.0\n",
"horizon = map_freq(frequency)\n",
"weight_gap = 0.01\n",
"benchmark_code = 905\n",
"benchmark_code = 300\n",
"universe_name = ['zz800']\n",
"universe = Universe('custom', universe_name)\n",
"ref_dates = makeSchedule(start_date, end_date, frequency, 'china.sse')\n",
"\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)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
......@@ -78,20 +74,22 @@
" b_type.append(BoundaryType.RELATIVE)\n",
" l_val.append(benchmark_total_lower)\n",
" u_val.append(benchmark_total_upper)\n",
" else:\n",
" elif name in {'SIZE', 'SIZENL', 'BETA'}:\n",
" b_type.append(BoundaryType.ABSOLUTE)\n",
" l_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",
"bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
......@@ -106,17 +104,13 @@
" horizon=horizon,\n",
" offset=1)\n",
"\n",
"return_groups = codes_return.groupby('trade_date')\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')"
"return_groups = codes_return.groupby('trade_date')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def factor_analysis(engine, factor_name, universe, benchmark_code, positive):\n",
......@@ -124,6 +118,9 @@
" \"\"\"\n",
" Model phase: we need 1 constant linear model and one linear regression model\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",
" simple_expression = LAST(factor_name) if positive else -LAST(factor_name)\n",
"\n",
......@@ -233,10 +230,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"df = engine.fetch_factor_coverage(start_date='2011-01-01',\n",
......@@ -244,22 +239,28 @@
" universe=universe_name[0])\n",
"df = df[df.source != 'risk_exposure']\n",
"df = df.groupby('factor').mean()\n",
"df = df[df.coverage >= 0.98]"
"df = df[df.coverage >= 0.95]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wall time: 39min 15s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"from dask.distributed import Client\n",
"\n",
"client = Client('192.168.0.102:8786')\n",
"client = Client('10.63.6.13:8786')\n",
"\n",
"tasks = client.map(worker_func_positive, df.index.tolist(), pure=False)\n",
"res1 = client.gather(tasks)\n",
......@@ -281,13 +282,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 17,
"metadata": {},
"outputs": [],
"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",
"ic_df.to_excel(writer, sheet_name='ics')\n",
"writer.close()"
......@@ -296,18 +295,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": []
}
......@@ -328,7 +316,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.6.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