{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.insert(0, \"../\")\n", "%load_ext autoreload\n", "%autoreload 2\n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "import seaborn as sns\n", "%matplotlib notebook" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import xalpha as xa\n", "xa.set_backend(backend=\"csv\", path=\"../data\", precached=\"20170103\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from lof.predict import estimate_table , analyse_all, estimate_table_with_pos\n", "from lof.holdings import holdings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 南方原油" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "nfyy = \\\n", "estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F501018\": 100}), (\"est\", holdings[\"501018\"]))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.13174461054574946 \n", "标准差偏离: 0.1932435798187427\n", "\n", "预测偏差分位: \n", "1% 分位: -0.684 \n", "5% 分位: -0.288 \n", "25% 分位: -0.089 \n", "50% 分位: 0.001 \n", "75% 分位: 0.083 \n", "95% 分位: 0.292 \n", "99% 分位: 0.389\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.34 \n", "预测涨的比实际多: 0.22 \n", "预测跌的比实际多: 0.17 \n", "预测跌的比实际少: 0.28\n" ] } ], "source": [ "analyse_all(nfyy, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sns.distplot(list(hbyq[\"diff_real_est\"]))" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/shixin/Documents/newwork/quant finance/xalpha/xalpha/universal.py:111: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df[k] = df[k].apply(_float)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "20190103 91.02316508226221\n", "20190104 91.18736806909216\n", "20190107 91.42294464302962\n", "20190108 91.31293026490012\n", "20190109 91.47956245149223\n", "20190110 91.64779927110968\n", "20190111 91.4480866568967\n", "20190114 92.88908979944209\n", "20190115 93.26315381766078\n", "20190116 90.5099246529659\n", "20190117 89.30486689245382\n", "20190118 92.15168621763824\n", "20190121 92.99372549848364\n", "20190122 92.79668932563207\n", "20190123 92.6756540910683\n", "20190124 94.42684208140221\n", "20190125 93.09450736383893\n", "20190128 92.63988656220975\n", "20190129 92.77584558263577\n", "20190130 92.97451594230357\n", "20190131 92.26914844087126\n", "20190201 91.50933803903732\n", "20190211 92.90278673414241\n", "20190212 92.65209222168077\n", "20190213 91.92977284123937\n", "20190214 92.48416332566121\n", "20190215 92.4832972441177\n", "20190218 92.48329724411768\n", "20190219 92.03639294587313\n", "20190220 92.48693261335598\n", "20190221 91.62964296550406\n", "20190222 90.81201875561115\n", "20190225 93.00750204194448\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "../lof/predict.py:108: RuntimeWarning: invalid value encountered in double_scalars\n", " ratio = realf / estf\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "20190226 93.582304457362\n", "20190227 94.33816810147552\n", "20190228 95.81896979155046\n", "20190301 95.89543406457042\n", "20190304 97.84389948036407\n", "20190305 96.4246891225797\n", "20190306 96.23253636349062\n", "20190307 96.56988329821014\n", "20190308 96.26794961515462\n", "20190311 96.20273551683181\n", "20190312 96.24006602664714\n", "20190313 95.86490873312889\n", "20190315 94.963503868003\n", "20190318 94.42954525359667\n", "20190319 94.72598580619774\n", "20190320 93.33631797621469\n", "20190321 95.64823577512094\n", "20190322 95.31409734009567\n", "20190325 93.96994933301688\n", "20190326 93.6445446898773\n", "20190327 94.16862991650117\n", "20190328 93.68952772323584\n", "20190329 95.78771639813328\n", "20190401 96.62399238326807\n", "20190402 95.09486169935235\n", "20190403 93.64960691258692\n", "20190404 95.75551529747393\n", "20190408 96.14761429337376\n", "20190409 94.96445455675742\n", "20190410 94.66289066114739\n", "20190411 95.56211009460982\n", "20190412 95.33598278730956\n", "20190415 94.76299068168835\n", "20190416 94.97399362404677\n", "20190417 94.75415679385844\n", "20190418 94.18951016144965\n", "20190419 92.60091708793765\n", "20190422 91.80290832238703\n", "20190424 93.89846067512248\n", "20190425 93.69895420976064\n", "20190426 93.1102752231422\n", "20190429 93.16510852674017\n", "20190430 93.77927891556524\n", "20190506 92.61716228014727\n", "20190507 92.59050942013906\n", "20190508 93.6284478497083\n", "20190510 94.03478210564055\n", "20190513 93.8741697213642\n", "20190514 94.12715960081663\n", "20190515 95.30706232417994\n", "20190516 94.7961713306068\n", "20190517 94.67939882680666\n", "20190520 94.86092696767811\n", "20190521 95.2243027360382\n", "20190522 94.79388587959433\n", "20190523 95.18300982669656\n", "20190524 95.85355649001738\n", "20190527 97.07105077946854\n", "20190528 98.05156097805309\n", "20190529 99.96186069736984\n", "20190530 98.73163277852221\n", "20190531 96.3351562040816\n", "20190603 96.55563905124433\n", "20190604 97.21156323787153\n", "20190605 96.76261920541205\n", "20190606 96.09681381641354\n", "20190610 95.30348067795782\n", "20190611 96.18952022011753\n", "20190612 96.00952314767103\n", "20190613 96.55155744008282\n", "20190614 97.5026956883687\n", "20190617 95.43758164174152\n", "20190618 95.32451040195562\n", "20190619 96.24810830375714\n", "20190620 96.42764165878569\n", "20190621 95.32198353962222\n", "20190624 95.72203285641746\n", "20190625 94.25217115015049\n", "20190626 93.58052011266419\n", "20190627 93.29535657056549\n", "20190628 90.670686246542\n", "20190701 90.67068624654202\n", "20190702 90.6938498745462\n", "20190703 89.75307379818743\n", "20190704 89.75307379818744\n", "20190705 92.47241937460998\n", "20190708 92.9140824234127\n", "20190709 93.36450278400405\n", "20190710 91.44715125947677\n", "20190711 91.8935664393573\n", "20190712 92.18298032640016\n", "20190715 93.10862802174513\n", "20190716 92.5644530510635\n", "20190717 92.1677146377275\n", "20190718 92.9587315354924\n", "20190719 94.13468340175312\n", "20190722 93.28303995678155\n", "20190723 93.57064827070177\n", "20190724 92.01027336219398\n", "20190725 89.92330456144151\n", "20190726 91.90979635972518\n", "20190729 91.62725105924793\n", "20190730 91.31094280764738\n", "20190731 91.31094280764735\n", "20190801 90.44781682839397\n", "20190802 90.79175144844568\n", "20190805 90.75687719869562\n", "20190806 90.54243628225605\n", "20190807 90.54243628225606\n", "20190808 90.644870325847\n", "20190809 90.07328741006361\n", "20190812 90.42807764576827\n", "20190813 90.29415667406136\n", "20190814 90.10997503904967\n", "20190815 88.35784434709498\n", "20190816 87.97238350917476\n", "20190819 88.5265140700566\n", "20190820 87.38464280010139\n", "20190821 87.62905028080772\n", "20190822 86.69570593827197\n", "20190823 86.28380688480121\n", "20190826 87.10389392497467\n", "20190827 87.96091481008789\n", "20190828 88.3739681139831\n", "20190829 88.88414776718211\n", "20190830 87.27390988681265\n", "20190903 87.82330064989802\n", "20190904 88.65867620655332\n", "20190905 88.06156607643243\n", "20190906 87.99893055655187\n", "20190909 88.6344599754891\n", "20190910 89.49534563658688\n", "20190911 90.15820664399413\n", "20190912 88.85256787723793\n", "20190916 89.28533043350087\n", "20190917 89.85111812260914\n", "20190918 90.00653526866424\n", "20190919 89.39211419574309\n", "20190920 90.25080522906319\n", "20190923 90.25080522906318\n", "20190924 90.64289432209966\n", "20190925 91.40166638662313\n", "20190926 93.48333854414464\n", "20190927 92.29956444658865\n", "20190930 93.13302654919526\n", "20191008 91.9384706848759\n", "20191009 90.91179452860285\n", "20191010 93.8013905374314\n", "20191011 93.45003149133747\n", "20191014 93.30194580274176\n", "20191015 92.13346212878619\n", "20191016 90.81055497857905\n", "20191017 89.7076695202165\n", "20191018 88.06949260012523\n", "20191021 89.42390249187768\n", "20191022 90.12019331626425\n", "20191023 89.48278390601602\n", "20191024 88.48904896770685\n", "20191025 88.20634841989595\n", "20191028 86.88285649315218\n", "20191029 85.26656862352603\n", "20191030 86.501520975376\n", "20191031 86.31432549421694\n", "20191101 86.42841027154788\n", "20191104 87.39207465122772\n", "20191106 88.52597005736399\n", "20191107 87.21678907854887\n", "20191108 87.49209507148299\n", "20191111 88.46541187464948\n", "20191112 88.66805478573718\n", "20191113 89.19371891029766\n", "20191114 87.68706294466892\n", "20191115 87.25757726862165\n", "20191118 86.72409064919658\n", "20191119 85.219484851393\n", "20191120 86.26225429701721\n", "20191121 86.34178235591116\n", "20191122 86.87310792266275\n", "20191125 86.61388515484417\n", "20191126 86.11392863367801\n", "20191127 88.14745668177518\n", "20191128 88.1474566817752\n", "20191129 85.57956468888875\n", "20191202 85.57956468888874\n", "20191203 85.98363114131203\n", "20191204 85.73050117465297\n", "20191205 85.83449991421476\n", "20191206 85.38918817546839\n", "20191209 84.4197344628766\n", "20191210 83.91416907517555\n", "20191211 84.55875264348647\n", "20191212 83.64844455405657\n", "20191213 83.20547913670035\n", "20191216 83.91654284308439\n", "20191217 84.218657811513\n", "20191218 83.60177292256124\n", "20191219 84.0340994998693\n", "20191220 83.26203895987828\n", "20191223 82.63424047366718\n", "20191224 82.63424047366719\n", "20191225 82.63424047366718\n", "20191226 82.63424047366719\n", "20191227 84.11102619830689\n", "20191230 84.11102619830687\n", "20191231 82.28894219406196\n", "20200102 84.69876541741158\n", "20200103 86.00275480078278\n", "20200106 86.0850652755\n", "20200107 87.17455079254022\n", "20200108 87.94735162126777\n", "20200110 88.82741681149516\n", "20200113 90.08332730226532\n", "20200114 90.81033472969321\n", "20200115 91.84805811530529\n", "20200116 91.68677353090831\n", "20200117 92.63912517450484\n", "20200120 92.38119406449525\n", "20200121 92.62454331254654\n", "20200122 92.97275518641464\n", "20200123 93.38775836005385\n", "20200203 92.95941095054255\n", "20200204 93.04832439489157\n", "20200205 92.87266045001708\n", "20200206 92.91611457870212\n", "20200207 92.69833225426493\n", "20200210 92.13280714394722\n", "20200211 91.89042473137775\n", "20200212 91.46973937238718\n", "20200213 90.69404425831279\n", "20200214 89.94297298839075\n", "20200217 89.59415926235278\n", "20200218 89.18072532365557\n", "20200219 89.5449390566172\n", "20200220 90.94525779199606\n", "20200221 87.70369601486927\n", "20200224 87.77498843723728\n", "20200225 87.61441762494279\n", "20200226 87.53458737228154\n", "20200227 86.48161663019597\n", "20200228 86.256249484961\n", "20200302 85.90501760398607\n", "20200303 85.57942817468518\n", "20200304 85.12757328911255\n", "20200305 84.9428088672791\n" ] } ], "source": [ "hbyq = \\\n", "estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F162411\": 100}), (\"est\", holdings[\"162411\"]),\n", " float_holdings=True, window=4, decay=0.65, smooth=0.2)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.09761915454118616 \n", "标准差偏离: 0.12635002727389924\n", "\n", "预测偏差分位: \n", "1% 分位: -0.308 \n", "5% 分位: -0.196 \n", "25% 分位: -0.073 \n", "50% 分位: 0.003 \n", "75% 分位: 0.085 \n", "95% 分位: 0.197 \n", "99% 分位: 0.332\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.27 \n", "预测涨的比实际多: 0.24 \n", "预测跌的比实际多: 0.25 \n", "预测跌的比实际少: 0.24\n" ] } ], "source": [ "analyse_all(hbyq, \"diff_real_est\") # 存在改善!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/shixin/Documents/newwork/quant finance/xalpha/xalpha/universal.py:138: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df[k] = df[k].apply(_float)\n" ] } ], "source": [ "hbyq = \\\n", "estimate_table_with_pos(\"20190101\", \"20200305\", (\"real\", {\"F162411\": 100}), (\"est\", holdings[\"162411\"]))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.0889651321145134 \n", "标准差偏离: 0.11984714572662096\n", "\n", "预测偏差分位: \n", "1% 分位: -0.321 \n", "5% 分位: -0.219 \n", "25% 分位: -0.068 \n", "50% 分位: 0.002 \n", "75% 分位: 0.078 \n", "95% 分位: 0.161 \n", "99% 分位: 0.247\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.27 \n", "预测涨的比实际多: 0.24 \n", "预测跌的比实际多: 0.24 \n", "预测跌的比实际少: 0.25\n" ] } ], "source": [ "analyse_all(hbyq, \"diff_real_est\") # 继续改善" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dax.plot(x=\"date\", y=\"estpos\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 国泰纳斯达克" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "gt = estimate_table(\"20190301\", \"20200310\", (\"real\", {\"F513100\": 100}), (\"est\", {\"20\": 99}))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.02232150761113922 \n", "标准差偏离: 0.03331837141011317\n", "\n", "预测偏差分位: \n", "1% 分位: -0.136 \n", "5% 分位: -0.046 \n", "25% 分位: -0.016 \n", "50% 分位: -0.001 \n", "75% 分位: 0.016 \n", "95% 分位: 0.04 \n", "99% 分位: 0.062\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.32 \n", "预测涨的比实际多: 0.27 \n", "预测跌的比实际多: 0.17 \n", "预测跌的比实际少: 0.24\n" ] } ], "source": [ "analyse_all(gt, \"diff_real_est\") ##sota 0.025 0.035 98" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 博时标普" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "warning: this fund has no data for rate\n" ] } ], "source": [ "sp500 = \\\n", "estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F513500\": 100}), (\"est\", holdings[\"513500\"]))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.04710265681733935 \n", "标准差偏离: 0.06961432146964162\n", "\n", "预测偏差分位: \n", "1% 分位: -0.189 \n", "5% 分位: -0.119 \n", "25% 分位: -0.019 \n", "50% 分位: 0.01 \n", "75% 分位: 0.044 \n", "95% 分位: 0.094 \n", "99% 分位: 0.197\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.52 \n", "预测涨的比实际多: 0.07 \n", "预测跌的比实际多: 0.07 \n", "预测跌的比实际少: 0.35\n" ] } ], "source": [ "analyse_all(sp500, \"diff_real_est\") # 仓位设定有问题,导致预测总是实际少" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "sp500 = \\\n", "estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F513500\": 100}), (\"est\", {\"166\": 99.5}))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.017746793204680103 \n", "标准差偏离: 0.040475675508421405\n", "\n", "预测偏差分位: \n", "1% 分位: -0.123 \n", "5% 分位: -0.026 \n", "25% 分位: -0.005 \n", "50% 分位: 0.002 \n", "75% 分位: 0.011 \n", "95% 分位: 0.033 \n", "99% 分位: 0.116\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.38 \n", "预测涨的比实际多: 0.21 \n", "预测跌的比实际多: 0.2 \n", "预测跌的比实际少: 0.21\n" ] } ], "source": [ "analyse_all(sp500, \"diff_real_est\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 纳斯达克" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "nsdq = \\\n", "estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F161130\": 100}), (\"est\", {\"20\": 94}))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.12216182410912323 \n", "标准差偏离: 0.1833932579608965\n", "\n", "预测偏差分位: \n", "1% 分位: -0.522 \n", "5% 分位: -0.301 \n", "25% 分位: -0.073 \n", "50% 分位: 0.002 \n", "75% 分位: 0.086 \n", "95% 分位: 0.243 \n", "99% 分位: 0.599\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.36 \n", "预测涨的比实际多: 0.24 \n", "预测跌的比实际多: 0.16 \n", "预测跌的比实际少: 0.25\n" ] } ], "source": [ "analyse_all(nsdq, \"diff_real_est\") # 跟踪的很一般" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20190111 94.98915406894054\n", "20190114 96.18608890311174\n", "20190123 94.94949306632245\n", "20190214 96.1975188414544\n", "20190219 97.75149945255046\n", "20190220 98.30207512761253\n", "20190221 98.69959941828886\n", "20190225 96.81241407707077\n", "20190228 95.67668864004108\n", "20190318 94.29177804471698\n", "20190528 95.43999862226586\n", "20190529 96.63121411967879\n", "20190530 97.76632699876653\n", "20190531 98.64625233615371\n", "20190619 97.12878286893026\n", "20190705 95.4811363698285\n", "20190708 94.32921056786233\n", "20190709 93.30115834909205\n", "20190710 92.3482717770836\n", "20190711 93.48083076088028\n", "20190730 94.51678844585928\n", "20190816 95.5504028193633\n", "20190830 96.63244544044227\n", "20191111 97.48645432229227\n", "20191120 95.66089703264777\n", "20191121 93.27526312650791\n", "20191122 91.87241207960061\n", "20191127 93.11839958565166\n", "20191128 94.50536520402375\n", "20191129 95.91147884041469\n", "20191227 94.75852521267205\n", "20191230 93.90114316430976\n", "20191231 92.80383861833349\n", "20200102 91.99205840992055\n", "20200113 93.52936836733129\n", "20200121 94.94180228458376\n", "20200122 95.93597723699204\n", "20200123 96.98619116621487\n", "20200203 98.57885257476376\n", "20200204 99.5066540094461\n", "20200205 100.0\n", "20200207 98.6085249834941\n", "20200210 96.98733298425819\n", "20200211 94.35890684912344\n", "20200212 92.51632750637836\n", "20200213 90.94041114511042\n", "20200214 88.92634498871585\n", "20200218 87.55549093681263\n", "20200219 86.53506818214238\n", "20200224 88.63790765153645\n", "20200225 91.15118876541946\n", "20200227 92.21369570502361\n", "20200305 93.27145154353816\n" ] } ], "source": [ "nsdq = \\\n", "estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F161130\": 100}), (\"est\", {\"20\": 94}), float_holdings=True)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.028541250402246907 \n", "标准差偏离: 0.058683335315467273\n", "\n", "预测偏差分位: \n", "1% 分位: -0.211 \n", "5% 分位: -0.087 \n", "25% 分位: -0.014 \n", "50% 分位: -0.004 \n", "75% 分位: 0.009 \n", "95% 分位: 0.055 \n", "99% 分位: 0.127\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.23 \n", "预测涨的比实际多: 0.37 \n", "预测跌的比实际多: 0.16 \n", "预测跌的比实际少: 0.24\n" ] } ], "source": [ "analyse_all(nsdq, \"diff_real_est\") ## 考虑仓位调节后,跟踪效果明显加强!!!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 日经225" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "rj = estimate_table(\"20191001\", \"20200305\", (\"real\", {\"F513880\": 100}), (\"est\", {\"178\": 95}))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.06843507292208129 \n", "标准差偏离: 0.10318900780357843\n", "\n", "预测偏差分位: \n", "1% 分位: -0.312 \n", "5% 分位: -0.161 \n", "25% 分位: -0.052 \n", "50% 分位: -0.002 \n", "75% 分位: 0.037 \n", "95% 分位: 0.129 \n", "99% 分位: 0.329\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.28 \n", "预测涨的比实际多: 0.24 \n", "预测跌的比实际多: 0.21 \n", "预测跌的比实际少: 0.27\n" ] } ], "source": [ "analyse_all(rj, \"diff_real_est\") " ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "warning: this fund has no data for rate\n", "20191009 96.19034492306771\n", "20191015 95.15794452830187\n", "20191023 96.23357657135672\n", "20191024 94.80183359605346\n", "20191025 92.98009826635071\n", "20191028 91.40047248386391\n", "20191104 91.70338822259805\n", "20191105 92.7274733259754\n", "20191106 95.73506377472368\n", "20191112 94.575907008482\n", "20191122 91.47366194430906\n", "20191125 89.50988330484543\n", "20191128 91.92576397505816\n", "20191203 92.6450548611614\n", "20191205 93.85054364222272\n", "20191206 95.9948744769812\n", "20191211 97.34371580286457\n", "20191212 99.08426593617219\n", "20191213 100.0\n", "20191216 100.0\n", "20191218 98.23294822910302\n", "20191219 96.84840584847062\n", "20200106 98.6814957750291\n", "20200107 99.99894362852034\n", "20200110 97.54109915292014\n", "20200113 96.13031552214173\n", "20200115 96.91828338975952\n", "20200116 97.69304602000149\n", "20200122 98.36412010674104\n", "20200123 99.02219811688835\n", "20200204 96.86275714501069\n", "20200205 96.18319493470636\n", "20200207 96.92614378846483\n", "20200212 96.10176675892353\n", "20200213 96.54561333099599\n", "20200214 96.85080241275031\n", "20200218 97.44347834076076\n", "20200219 96.01104431565676\n", "20200302 97.07505224553589\n" ] } ], "source": [ "rj = estimate_table(\"20191001\", \"20200305\", (\"real\", {\"F513880\": 100}), (\"est\", {\"178\": 95}), float_holdings=True)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.07166306068302974 \n", "标准差偏离: 0.10592680450275156\n", "\n", "预测偏差分位: \n", "1% 分位: -0.31 \n", "5% 分位: -0.17 \n", "25% 分位: -0.051 \n", "50% 分位: 0.0 \n", "75% 分位: 0.038 \n", "95% 分位: 0.146 \n", "99% 分位: 0.33\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.28 \n", "预测涨的比实际多: 0.24 \n", "预测跌的比实际多: 0.22 \n", "预测跌的比实际少: 0.26\n" ] } ], "source": [ "analyse_all(rj, \"diff_real_est\") # 几乎持平" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 中国互联" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "zghl = estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F164906\": 100}), (\"est\", {\"954018\": 95}))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.1386085271800198 \n", "标准差偏离: 0.7894710041910674\n", "\n", "预测偏差分位: \n", "1% 分位: -0.327 \n", "5% 分位: -0.146 \n", "25% 分位: -0.054 \n", "50% 分位: 0.007 \n", "75% 分位: 0.06 \n", "95% 分位: 0.162 \n", "99% 分位: 0.274\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.31 \n", "预测涨的比实际多: 0.23 \n", "预测跌的比实际多: 0.22 \n", "预测跌的比实际少: 0.24\n" ] } ], "source": [ "analyse_all(zghl, \"diff_real_est\") # 貌似就是这个指数,但是不太准==" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20190103 94.84754425577417\n", "20190104 94.67983249363367\n", "20190107 94.46259787435622\n", "20190108 94.46259787435623\n", "20190109 94.13354068565776\n", "20190110 94.36642591768546\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "../lof/predict.py:108: RuntimeWarning: invalid value encountered in double_scalars\n", " ratio = realf / estf\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "20190111 94.49147061954886\n", "20190114 94.43346170513458\n", "20190115 94.27916358446538\n", "20190116 94.68314956298144\n", "20190117 94.32139439695084\n", "20190118 94.16376316019192\n", "20190121 94.36988883177338\n", "20190122 94.47504929030637\n", "20190123 95.69500017859473\n", "20190124 96.33377286569768\n", "20190125 95.77119626476872\n", "20190128 96.1943401664517\n", "20190129 97.18363260369748\n", "20190130 96.843732981561\n", "20190131 97.21686384364345\n", "20190201 97.39964711079051\n", "20190211 96.52547279870105\n", "20190212 96.00833936817388\n", "20190213 96.00833936817385\n", "20190214 95.5543665466728\n", "20190215 96.19384781379462\n", "20190218 96.50962244694742\n", "20190219 96.61182296429517\n", "20190220 96.03360501869197\n", "20190221 96.05645498220845\n", "20190222 95.994210994571\n", "20190225 95.07402763683615\n", "20190226 95.55019133335887\n", "20190227 96.39992738337689\n", "20190228 96.39992738337686\n", "20190301 97.53340693614994\n", "20190304 99.45632134144516\n", "20190305 100.0\n", "20190306 98.89287144488618\n", "20190307 98.1740385819862\n", "20190308 98.19902942907048\n", "20190311 97.95468811750216\n", "20190312 97.94782817701412\n", "20190313 97.70646648379014\n", "20190314 98.43186313021552\n", "20190315 98.55805266962257\n", "20190318 97.99996880131931\n", "20190319 97.7432406167195\n", "20190320 98.77285217952438\n", "20190321 99.31665688797463\n", "20190322 99.42529879922218\n", "20190325 99.14103754924527\n", "20190326 100.00000000000001\n", "20190327 98.31431528906835\n", "20190328 98.24301378945742\n", "20190329 97.83063334005878\n", "20190401 97.3718863745563\n", "20190402 98.01971760471659\n", "20190403 97.96835700947709\n", "20190404 97.17783924874571\n", "20190408 97.56264545816497\n", "20190409 96.65287579963467\n", "20190410 96.19777225932644\n", "20190411 97.28204608735467\n", "20190412 98.31453435286353\n", "20190415 96.56067896301957\n", "20190416 97.57492259224465\n", "20190417 97.45915964360728\n", "20190418 97.96728727124504\n", "20190419 97.99811940847259\n", "20190422 97.99811940847256\n", "20190423 99.3410650449284\n", "20190424 100.00000000000001\n", "20190425 99.12861856339663\n", "20190426 96.55268124372816\n", "20190429 95.96163817562774\n", "20190430 95.86542089971377\n", "20190506 94.49251321550476\n", "20190507 97.4326552790498\n", "20190508 97.71130194768625\n", "20190509 95.95148385523477\n", "20190510 95.569832435279\n", "20190513 95.45923110117376\n", "20190514 98.207422434096\n", "20190515 98.90424585475986\n", "20190516 96.06364625794559\n", "20190517 96.25089270439324\n", "20190520 96.47373137205122\n", "20190521 94.83500607270672\n", "20190522 95.97065745909568\n", "20190523 95.99911963728317\n", "20190524 95.99737639293629\n", "20190527 96.36076889766954\n", "20190528 96.10941564259933\n", "20190529 95.6588501878214\n", "20190530 96.05600068462729\n", "20190531 94.65192881377051\n", "20190603 93.20420234696067\n", "20190604 94.89383984828916\n", "20190605 92.2521457252949\n", "20190606 91.79552654785317\n", "20190610 92.67873125660722\n", "20190611 93.67007215727935\n", "20190612 94.59629677877155\n", "20190614 91.88550330740337\n", "20190617 92.11701131318692\n", "20190618 91.52612012176382\n", "20190619 90.68407852926623\n", "20190620 89.52687295189364\n", "20190621 90.83390800082823\n", "20190624 90.83390800082822\n", "20190625 89.84728538023293\n", "20190626 90.34223438153305\n", "20190627 90.81312818444404\n", "20190628 90.67667880999049\n", "20190701 90.81031573829492\n", "20190702 89.35994795133495\n", "20190703 88.5020474060669\n", "20190704 89.3167343482618\n", "20190705 86.48394275281282\n", "20190708 86.17407998036447\n", "20190709 82.32481254883389\n", "20190710 83.43974806084621\n", "20190711 85.18163506653772\n", "20190712 83.44664555471917\n", "20190715 84.37810629112221\n", "20190716 85.77197157148099\n", "20190717 85.65220497636524\n", "20190718 85.75320965498314\n", "20190719 85.75914969270981\n", "20190722 87.59046474352698\n", "20190723 87.35565337716814\n", "20190724 87.73655445296187\n", "20190725 87.0375955605157\n", "20190726 88.35964430027592\n", "20190729 88.23217255816084\n", "20190730 87.59562370374776\n", "20190731 87.52899705236587\n", "20190801 87.89857183593841\n", "20190802 89.52466928361176\n", "20190805 89.64354544276881\n", "20190806 90.05657572469586\n", "20190807 90.15233135131261\n", "20190808 90.35113412221517\n", "20190809 90.82180529269945\n", "20190812 88.55530234161871\n", "20190813 89.04143510768914\n", "20190814 87.993107086633\n", "20190815 86.55423438464761\n", "20190816 88.5470643974929\n", "20190819 87.93499931825902\n", "20190820 88.2409102541861\n", "20190821 89.024069184924\n", "20190822 90.0911668639337\n", "20190823 91.33406014812162\n", "20190826 89.84968658642136\n", "20190827 90.49519004059304\n", "20190828 91.13212666817122\n", "20190829 91.17875959306544\n", "20190830 91.10073532831419\n", "20190902 92.57842906130678\n", "20190903 94.3895604728327\n", "20190904 95.57592916209158\n", "20190905 96.0109908619381\n", "20190906 94.99328361356298\n", "20190909 94.67268583958268\n", "20190910 95.63608994229573\n", "20190911 94.96564310012441\n", "20190912 95.9486820997365\n", "20190916 95.18092965523479\n", "20190917 96.918946946902\n", "20190918 97.78966724507153\n", "20190920 100.0\n", "20190923 100.0\n", "20190924 99.38231541389986\n", "20190925 99.43175085317682\n", "20190926 99.18391918101239\n", "20190927 99.55143977398855\n", "20190930 99.32002008376337\n", "20191008 97.94366028721907\n", "20191009 97.13772573477135\n", "20191010 96.01323352869471\n", "20191011 95.77177855354371\n", "20191014 96.20017146344844\n", "20191015 94.925942701588\n", "20191016 94.19719554085432\n", "20191017 92.54095998889414\n", "20191018 95.3751057977783\n", "20191021 95.15930508887702\n", "20191022 94.43445374715597\n", "20191023 95.1167303994734\n", "20191024 96.4757602125075\n", "20191025 96.80532690664567\n", "20191028 97.21217977782274\n", "20191029 96.1974159203353\n", "20191030 96.91849742341661\n", "20191031 96.51634077494994\n", "20191101 96.45075757415894\n", "20191104 95.3425743066461\n", "20191105 95.10636820251601\n", "20191106 95.9486559824377\n", "20191107 95.73264563566086\n", "20191108 95.43100921811403\n", "20191111 96.5643377149036\n", "20191112 97.65670502718488\n", "20191113 98.46715140764711\n", "20191114 98.09733211887567\n", "20191115 99.25775086605705\n", "20191118 97.77925610200356\n", "20191120 97.70182806497057\n", "20191121 96.66328680990222\n", "20191122 95.02726727342771\n", "20191125 95.27416278308358\n", "20191126 93.43595029086988\n", "20191127 94.67657187716914\n", "20191128 93.09617038510225\n", "20191129 92.66506130739333\n", "20191202 94.49636478246923\n", "20191203 94.46237305406143\n", "20191204 94.29781700751661\n", "20191205 91.31507446154491\n", "20191206 94.22791827186359\n", "20191209 94.5988297961521\n", "20191210 95.22417287729739\n", "20191211 94.84081405829484\n", "20191212 96.29458524074457\n", "20191213 96.67325236560475\n", "20191216 96.32949221498619\n", "20191217 96.55894538655909\n", "20191218 96.17796429496624\n", "20191220 97.03926624712518\n", "20191223 94.67753682073952\n", "20191224 94.6775368207395\n", "20191225 92.8276200676847\n", "20191226 93.16794283041598\n", "20191230 95.86700777802857\n", "20191231 92.87376519058849\n", "20200102 91.26425311870491\n", "20200103 95.74281976872118\n", "20200106 95.15666749340929\n", "20200107 93.73331611604708\n", "20200108 93.73331611604709\n", "20200109 94.01548892625658\n", "20200110 96.29361454939777\n", "20200113 95.68664873022165\n", "20200114 96.27777093758903\n", "20200115 98.0295529121203\n", "20200116 95.57901502574164\n", "20200117 94.84854660896245\n", "20200120 94.60570082522743\n", "20200121 93.27338134164432\n", "20200122 96.6383989455763\n", "20200123 97.84849218432007\n", "20200203 95.38470098844154\n", "20200204 97.32860998552896\n", "20200205 96.05208313722135\n", "20200206 96.10234085657407\n", "20200207 96.86519815068448\n", "20200210 97.79863324523302\n", "20200211 97.56931226436764\n", "20200212 98.11639591569448\n", "20200213 99.40083373672516\n", "20200214 97.56792052535293\n", "20200217 97.72890127413469\n", "20200218 97.9809323042748\n", "20200219 98.31233790899216\n", "20200220 99.62610682924152\n", "20200221 100.0\n", "20200224 98.71234247487308\n", "20200225 99.2816893249074\n", "20200226 97.25082447333125\n", "20200227 97.09667310461263\n", "20200228 96.28196790517794\n", "20200302 96.91760259751379\n", "20200303 97.29277658084322\n", "20200304 97.40280856916091\n", "20200305 97.29953483072296\n" ] } ], "source": [ "zghl = estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F164906\": 100}), (\"est\", {\"954018\": 95}),\n", " float_holdings=True, window=4, decay=0.65, smooth=0.2)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.134815673182512 \n", "标准差偏离: 0.7368848108187612\n", "\n", "预测偏差分位: \n", "1% 分位: -0.313 \n", "5% 分位: -0.158 \n", "25% 分位: -0.045 \n", "50% 分位: 0.007 \n", "75% 分位: 0.06 \n", "95% 分位: 0.163 \n", "99% 分位: 0.283\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.31 \n", "预测涨的比实际多: 0.23 \n", "预测跌的比实际多: 0.22 \n", "预测跌的比实际少: 0.24\n" ] } ], "source": [ "analyse_all(zghl, \"diff_real_est\") # 几乎一致,稍有一丝丝改善" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 鹏华互联" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "from lof.holdings import holdings_160644_19hy, holdings_160644_19s4\n", "from lof.utils import scale_dict" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def cutoff(hdict, threhold=1):\n", " d = {}\n", " aim = 0\n", " for k, v in hdict.items():\n", " if v>threhold:\n", " d[k] = v\n", " aim += v\n", " return scale_dict(d.copy(), aim=aim)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "d=cutoff(holdings_160644_19hy, threhold=2.5)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "hl = estimate_table(\"20200101\", \"20200325\", (\"real\", {\"F160644\": 100}), (\"est\", d))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.4123143196570495 \n", "标准差偏离: 0.553320687236681\n", "\n", "预测偏差分位: \n", "1% 分位: -1.143 \n", "5% 分位: -0.896 \n", "25% 分位: -0.105 \n", "50% 分位: 0.106 \n", "75% 分位: 0.327 \n", "95% 分位: 1.062 \n", "99% 分位: 1.248\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.4 \n", "预测涨的比实际多: 0.11 \n", "预测跌的比实际多: 0.28 \n", "预测跌的比实际少: 0.21\n" ] } ], "source": [ "analyse_all(hl, \"diff_real_est\") ## full 190101 200305 0.31 0.429" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/shixin/Documents/newwork/quant finance/xalpha/xalpha/universal.py:146: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df[k] = df[k].apply(_float)\n" ] } ], "source": [ "hl = estimate_table(\"20200101\", \"20200325\", (\"real\", {\"F160644\": 100}), (\"est\", holdings_160644_19s4))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.46212656420615356 \n", "标准差偏离: 0.61200792219348\n", "\n", "预测偏差分位: \n", "1% 分位: -1.432 \n", "5% 分位: -0.797 \n", "25% 分位: -0.282 \n", "50% 分位: 0.046 \n", "75% 分位: 0.42 \n", "95% 分位: 1.018 \n", "99% 分位: 1.482\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.32 \n", "预测涨的比实际多: 0.19 \n", "预测跌的比实际多: 0.23 \n", "预测跌的比实际少: 0.26\n" ] } ], "source": [ "analyse_all(hl, \"diff_real_est\") ## 预测效果拙计" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "hl = estimate_table_with_pos(\"20200101\", \"20200325\", (\"real\", {\"F160644\": 100}), (\"est\", holdings_160644_19s4))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
daterealestestposestposlddiff_real_est
02020-01-03-0.490913-0.4374360.9500000.950000-0.053477
12020-01-060.4093630.4634100.9584860.982353-0.054047
22020-01-070.7108510.7240810.9508770.912900-0.013230
32020-01-08-0.539755-0.9030400.9467680.9335020.363285
42020-01-092.0037571.6709340.8509570.6509910.332823
52020-01-100.2967060.6662570.8583181.000000-0.369552
62020-01-131.9789861.6234470.8112650.8257730.355539
72020-01-14-1.510453-1.3768720.9399740.988934-0.133581
82020-01-150.3757871.0229300.9249681.000000-0.647143
92020-01-160.6374580.7401690.9365130.775245-0.102711
102020-01-171.5885781.2959500.8755700.8065560.292628
112020-01-20-0.504751-0.6187000.8444631.0000000.113950
122020-01-21-2.317716-2.0380510.8654500.789285-0.279665
132020-01-222.0366601.0436520.9267710.9842090.993008
142020-01-23-1.906188-1.6400710.9084641.000000-0.266117
152020-02-03-2.085665-2.1374430.9935281.0000000.051778
162020-02-044.2809644.2817000.9919900.969460-0.000735
172020-02-05-0.338780-0.2927820.9878410.991819-0.045998
182020-02-061.9496101.5965050.9835860.9953670.353105
192020-02-070.2451700.1786530.9951281.0000000.066517
202020-02-100.821757-0.1974460.9981011.0000001.019203
212020-02-11-0.3493110.4863691.0000001.000000-0.835680
222020-02-120.8179161.6431151.0000001.000000-0.825199
232020-02-130.347692-0.4827260.9102950.6579980.830417
242020-02-140.1636190.2099320.7981630.657998-0.046313
252020-02-170.8840200.1265870.6550370.6467090.757434
262020-02-18-0.1523950.3541210.7440021.000000-0.506517
272020-02-191.0779360.9319680.8552091.0000000.145967
282020-02-20-0.670064-0.6630010.9971550.989154-0.007063
292020-02-21-1.729216-1.6515930.9964441.000000-0.077623
302020-02-24-1.691966-3.3858800.9955551.0000001.693914
312020-02-25-0.816286-0.1943440.9102340.657766-0.621942
322020-02-26-0.039663-0.4668470.8877921.0000000.427184
332020-02-27-2.390636-1.1541100.8053780.792746-1.236526
342020-02-28-0.934959-0.6875040.9320481.000000-0.247455
352020-03-022.8108331.9309190.9150601.0000000.879914
362020-03-03-2.015566-1.3564851.0000001.000000-0.659081
372020-03-042.1588591.7747471.0000001.0000000.384113
382020-03-05-0.328947-0.5234621.0000001.0000000.194515
392020-03-06-1.840184-2.5918140.9586170.8422290.751630
402020-03-09-4.758023-4.7301080.8644990.680617-0.027916
412020-03-103.2306383.4589350.7964210.869601-0.228297
422020-03-11-1.025907-2.3720340.7591660.7438551.346127
432020-03-12-5.570097-5.0433420.7347380.512619-0.526755
442020-03-134.7788003.8290340.6857770.8114780.949766
452020-03-16-8.275132-7.2625400.7006410.855880-1.012593
462020-03-172.6765112.9719500.8225870.798329-0.295439
472020-03-18-3.955056-3.9861730.8068300.7408150.031117
482020-03-191.2634531.0952710.7800500.8005310.168182
492020-03-201.2707952.7737970.8021030.899829-1.503002
502020-03-23-1.220625-1.1598870.7669690.548452-0.060738
512020-03-245.9013744.8661210.7603090.8071321.035253
522020-03-251.9302071.3635230.7312610.9220630.566684
\n", "
" ], "text/plain": [ " date real est estpos estposld diff_real_est\n", "0 2020-01-03 -0.490913 -0.437436 0.950000 0.950000 -0.053477\n", "1 2020-01-06 0.409363 0.463410 0.958486 0.982353 -0.054047\n", "2 2020-01-07 0.710851 0.724081 0.950877 0.912900 -0.013230\n", "3 2020-01-08 -0.539755 -0.903040 0.946768 0.933502 0.363285\n", "4 2020-01-09 2.003757 1.670934 0.850957 0.650991 0.332823\n", "5 2020-01-10 0.296706 0.666257 0.858318 1.000000 -0.369552\n", "6 2020-01-13 1.978986 1.623447 0.811265 0.825773 0.355539\n", "7 2020-01-14 -1.510453 -1.376872 0.939974 0.988934 -0.133581\n", "8 2020-01-15 0.375787 1.022930 0.924968 1.000000 -0.647143\n", "9 2020-01-16 0.637458 0.740169 0.936513 0.775245 -0.102711\n", "10 2020-01-17 1.588578 1.295950 0.875570 0.806556 0.292628\n", "11 2020-01-20 -0.504751 -0.618700 0.844463 1.000000 0.113950\n", "12 2020-01-21 -2.317716 -2.038051 0.865450 0.789285 -0.279665\n", "13 2020-01-22 2.036660 1.043652 0.926771 0.984209 0.993008\n", "14 2020-01-23 -1.906188 -1.640071 0.908464 1.000000 -0.266117\n", "15 2020-02-03 -2.085665 -2.137443 0.993528 1.000000 0.051778\n", "16 2020-02-04 4.280964 4.281700 0.991990 0.969460 -0.000735\n", "17 2020-02-05 -0.338780 -0.292782 0.987841 0.991819 -0.045998\n", "18 2020-02-06 1.949610 1.596505 0.983586 0.995367 0.353105\n", "19 2020-02-07 0.245170 0.178653 0.995128 1.000000 0.066517\n", "20 2020-02-10 0.821757 -0.197446 0.998101 1.000000 1.019203\n", "21 2020-02-11 -0.349311 0.486369 1.000000 1.000000 -0.835680\n", "22 2020-02-12 0.817916 1.643115 1.000000 1.000000 -0.825199\n", "23 2020-02-13 0.347692 -0.482726 0.910295 0.657998 0.830417\n", "24 2020-02-14 0.163619 0.209932 0.798163 0.657998 -0.046313\n", "25 2020-02-17 0.884020 0.126587 0.655037 0.646709 0.757434\n", "26 2020-02-18 -0.152395 0.354121 0.744002 1.000000 -0.506517\n", "27 2020-02-19 1.077936 0.931968 0.855209 1.000000 0.145967\n", "28 2020-02-20 -0.670064 -0.663001 0.997155 0.989154 -0.007063\n", "29 2020-02-21 -1.729216 -1.651593 0.996444 1.000000 -0.077623\n", "30 2020-02-24 -1.691966 -3.385880 0.995555 1.000000 1.693914\n", "31 2020-02-25 -0.816286 -0.194344 0.910234 0.657766 -0.621942\n", "32 2020-02-26 -0.039663 -0.466847 0.887792 1.000000 0.427184\n", "33 2020-02-27 -2.390636 -1.154110 0.805378 0.792746 -1.236526\n", "34 2020-02-28 -0.934959 -0.687504 0.932048 1.000000 -0.247455\n", "35 2020-03-02 2.810833 1.930919 0.915060 1.000000 0.879914\n", "36 2020-03-03 -2.015566 -1.356485 1.000000 1.000000 -0.659081\n", "37 2020-03-04 2.158859 1.774747 1.000000 1.000000 0.384113\n", "38 2020-03-05 -0.328947 -0.523462 1.000000 1.000000 0.194515\n", "39 2020-03-06 -1.840184 -2.591814 0.958617 0.842229 0.751630\n", "40 2020-03-09 -4.758023 -4.730108 0.864499 0.680617 -0.027916\n", "41 2020-03-10 3.230638 3.458935 0.796421 0.869601 -0.228297\n", "42 2020-03-11 -1.025907 -2.372034 0.759166 0.743855 1.346127\n", "43 2020-03-12 -5.570097 -5.043342 0.734738 0.512619 -0.526755\n", "44 2020-03-13 4.778800 3.829034 0.685777 0.811478 0.949766\n", "45 2020-03-16 -8.275132 -7.262540 0.700641 0.855880 -1.012593\n", "46 2020-03-17 2.676511 2.971950 0.822587 0.798329 -0.295439\n", "47 2020-03-18 -3.955056 -3.986173 0.806830 0.740815 0.031117\n", "48 2020-03-19 1.263453 1.095271 0.780050 0.800531 0.168182\n", "49 2020-03-20 1.270795 2.773797 0.802103 0.899829 -1.503002\n", "50 2020-03-23 -1.220625 -1.159887 0.766969 0.548452 -0.060738\n", "51 2020-03-24 5.901374 4.866121 0.760309 0.807132 1.035253\n", "52 2020-03-25 1.930207 1.363523 0.731261 0.922063 0.566684" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hl ## 放弃预测了,调仓估计太猛。。。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 中概互联" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "warning: this fund has no data for rate\n" ] } ], "source": [ "zghl = estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F513050\": 100}), (\"est\", {\"954018\": 95}))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.38391103684749234 \n", "标准差偏离: 0.9363378102198336\n", "\n", "预测偏差分位: \n", "1% 分位: -1.363 \n", "5% 分位: -0.55 \n", "25% 分位: -0.248 \n", "50% 分位: 0.022 \n", "75% 分位: 0.231 \n", "95% 分位: 0.721 \n", "99% 分位: 1.143\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.27 \n", "预测涨的比实际多: 0.27 \n", "预测跌的比实际多: 0.25 \n", "预测跌的比实际少: 0.22\n" ] } ], "source": [ "analyse_all(zghl, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20190103 95.05977782839773\n", "20190104 95.03628642204424\n", "20190107 95.00674155857332\n", "20190108 95.74219561435346\n", "20190109 95.99842755198297\n", "20190110 97.59106099525168\n", "20190111 99.07877969071899\n", "20190114 97.36853792766284\n", "20190115 98.1092696894567\n", "20190116" ] }, { "name": "stderr", "output_type": "stream", "text": [ "../lof/predict.py:108: RuntimeWarning: invalid value encountered in double_scalars\n", " ratio = realf / estf\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " 95.72285254006678\n", "20190117 96.21383510605598\n", "20190118 95.65858077897346\n", "20190122 93.07816445028465\n", "20190123 90.86363614676328\n", "20190124 92.77878339063216\n", "20190125 92.13739014461872\n", "20190128 89.6284660878274\n", "20190129 89.69693797830962\n", "20190130 92.5857542878532\n", "20190131 91.45092672149805\n", "20190201 91.94124335324895\n", "20190211 92.0371018674194\n", "20190212 89.29870915293931\n", "20190213 89.47447717491525\n", "20190214 87.99165812818995\n", "20190215 87.61597855139316\n", "20190219 87.66600627505248\n", "20190220 86.0400337947778\n", "20190221 88.46601958527867\n", "20190222 86.99926383760953\n", "20190225 82.53457675462604\n", "20190226 82.50080243747061\n", "20190227 83.4558185113491\n", "20190228 84.01686586110883\n", "20190301 82.7695693068644\n", "20190304 82.2555247690458\n", "20190305 82.52205048273954\n", "20190306 81.04372756885743\n", "20190307 84.26530257624395\n", "20190308 82.39627797270798\n", "20190311 80.30663490728003\n", "20190312 83.63994250604998\n", "20190313 83.91033681139061\n", "20190314 82.40036272535035\n", "20190315 81.15443813899603\n", "20190318 82.4316760655865\n", "20190319 82.43062528297399\n", "20190320 82.31263549050176\n", "20190321 83.23522741325728\n", "20190322 82.69481404212132\n", "20190325 82.42748746581356\n", "20190326 84.17141571007629\n", "20190327 81.54865005551187\n", "20190328 80.26450552233923\n", "20190329 81.85614624228347\n", "20190401 81.11533688446337\n", "20190402 80.13839097027254\n", "20190403 79.79567270560588\n", "20190404 78.5762152717624\n", "20190408 77.95023925708234\n", "20190409 79.25487131491676\n", "20190410 77.44729572082234\n", "20190411 80.7380961740686\n", "20190412 79.1156950818252\n", "20190415 80.72446059746473\n", "20190416 81.32145571705229\n", "20190417 83.01422199807415\n", "20190418 83.83926728334508\n", "20190419 83.8392672833451\n", "20190423 84.77557385087225\n", "20190424 84.95338868537081\n", "20190425 87.68400537896785\n", "20190426 86.87936649112278\n", "20190429 86.47117356320503\n", "20190430 85.40868764846614\n", "20190506 86.25827074462002\n", "20190507 86.7624385281962\n", "20190508 85.8547780434441\n", "20190509 85.57051451933405\n", "20190510 84.9923042796103\n", "20190514 85.11525869227088\n", "20190515 86.3078610426966\n", "20190516 85.93392392771538\n", "20190517 86.76410131296174\n", "20190520 88.33371707943441\n", "20190521 88.50568259963232\n", "20190522 90.37174672234751\n", "20190523 90.29412729770111\n", "20190524 89.72389352611616\n", "20190527 89.72389352611614\n", "20190528 89.95655323669412\n", "20190529 89.21624696048661\n", "20190530 85.67472730646605\n", "20190531 85.77530089128273\n", "20190603 87.56311000687452\n", "20190604 82.09835309661536\n", "20190605 82.71637189960317\n", "20190606 81.63231951903772\n", "20190610 84.29228216107211\n", "20190611 82.6379109666697\n", "20190612 83.14474278748287\n", "20190613 84.56422882188569\n", "20190614 84.69581295701308\n", "20190617 83.38142621776541\n", "20190618 81.94669722485759\n", "20190619 86.49498499092091\n", "20190620 87.7640838333683\n", "20190621 85.93439083374457\n", "20190624 87.72242591927711\n", "20190625 87.76131645428687\n", "20190626 86.27633478077298\n", "20190627 88.1646790049758\n", "20190628 86.6007207864921\n", "20190701 86.60072078649212\n", "20190702 87.73386854682403\n", "20190703 88.19288746896059\n", "20190704 88.1928874689606\n", "20190705 89.60391359719156\n", "20190708 87.0628978537872\n", "20190709 82.75233172231209\n", "20190710 82.89701532378739\n", "20190711 84.34191154131364\n", "20190712 79.12272758451569\n", "20190715 79.4599621815806\n", "20190716 82.16834650203162\n", "20190717 83.20441573929641\n", "20190718 83.09904695021525\n", "20190719 82.63463365340928\n", "20190722 83.95279586096935\n", "20190723 85.05339053245558\n", "20190724 83.1604572884211\n", "20190725 80.52588030638508\n", "20190726 82.57118631425129\n", "20190729 81.99159152912002\n", "20190730 77.50935623268927\n", "20190731 78.65113772816018\n", "20190801 82.57842769657731\n", "20190802 83.55934010716457\n", "20190805 83.5413751670636\n", "20190806 85.09216983475291\n", "20190807 87.61240922912542\n", "20190808 87.25452448472119\n", "20190809 88.83559960616782\n", "20190812 88.30061157599431\n", "20190813 86.35899675851911\n", "20190814 85.17945687534454\n", "20190815 81.23764855246921\n", "20190816 81.43461019409226\n", "20190819 79.15653443562118\n", "20190820 80.12315656818971\n", "20190821 81.81807567656645\n", "20190822 82.5837564693431\n", "20190823 83.21941870076944\n", "20190826 81.59684853720502\n", "20190827 83.19065603220115\n", "20190828 83.4071884900209\n", "20190829 81.38751764622646\n", "20190830 85.70689921886952\n", "20190903 84.2237548903551\n", "20190904 85.54898130359554\n", "20190905 85.10752664525786\n", "20190906 84.03763605596718\n", "20190909 86.01121196784845\n", "20190910 84.21416030435095\n", "20190911 84.06183285446481\n", "20190912 84.02323464629706\n", "20190916 88.18405764125166\n", "20190917 86.57928551027044\n", "20190918 90.68333265218045\n", "20190919 92.09776310736503\n", "20190920 93.7685003378053\n", "20190923 94.03147815048935\n", "20190924 94.68003715568472\n", "20190925 92.95186518263074\n", "20190926 93.5117003821691\n", "20190927 91.17453869647476\n", "20190930 92.82733062548884\n", "20191008 94.78621183177842\n", "20191009 95.2611871531049\n", "20191010 95.23063348382243\n", "20191011 94.56647848241398\n", "20191014 90.84401561913043\n", "20191015 92.31438670731299\n", "20191016 94.44120431894956\n", "20191017 90.52155480326587\n", "20191018 93.19529800875279\n", "20191021 94.11204014846302\n", "20191022 92.83610005210578\n", "20191023 92.49834121782595\n", "20191024 90.27770586936087\n", "20191025 91.73138605308976\n", "20191028 91.86072994053984\n", "20191029 87.23274149642879\n", "20191030 87.73508189206181\n", "20191031 89.48310445306934\n", "20191101 87.22031285479783\n", "20191104 83.97955006033162\n", "20191105 81.91636631847358\n", "20191106 82.40192542548476\n", "20191107 82.68389451085935\n", "20191108 81.05230141493745\n", "20191111 79.43594305037148\n", "20191112 83.23859491378978\n", "20191113 86.13029516039168\n", "20191114 84.41695963228024\n", "20191115 84.93768651112092\n", "20191118 85.11978613042177\n", "20191119 83.85799050312228\n", "20191120 84.08470499537617\n", "20191121 82.93320865350027\n", "20191122 81.65201004711952\n", "20191125 79.4794826782809\n", "20191126 80.41844874835475\n", "20191127 83.5789005432717\n", "20191128 83.81911528061168\n", "20191129 85.32161937649389\n", "20191202 86.38147095792378\n", "20191203 87.14354501109791\n", "20191204 88.95208624052184\n", "20191205 89.23854398015004\n", "20191206 88.46205761887528\n", "20191209 88.2586953645105\n", "20191210 91.94731348751833\n", "20191211 91.62090641732\n", "20191212 91.95315904839013\n", "20191213 93.68637751040286\n", "20191216 96.72634513321702\n", "20191217 96.44815953383288\n", "20191218 96.76184771717477\n", "20191219 95.95011516281228\n", "20191220 99.69631171706749\n", "20191223 98.94366978075793\n", "20191224 96.4219682906778\n", "20191225 96.73416318052396\n", "20191226 99.80538672013364\n", "20191227 97.65238642313246\n", "20191230 97.16957215410612\n", "20191231 95.41032789064424\n", "20200102 99.59368237733851\n", "20200103 100.0\n", "20200106 99.64779759183081\n", "20200107 97.9027972082973\n", "20200108 100.0\n", "20200109 99.1296815397325\n", "20200110 99.99999999999999\n", "20200113 99.98800685455286\n", "20200114 99.99999999999999\n", "20200115 100.00000000000001\n", "20200116 98.15149913014469\n", "20200117 100.00000000000001\n", "20200120 100.0\n", "20200121 97.48352578780965\n", "20200122 100.00000000000001\n", "20200123 99.45845909147945\n", "20200203 96.02921468808881\n", "20200204 95.99412870695689\n", "20200205 95.92786898081465\n", "20200206 93.55927431696175\n", "20200207 94.08673253577456\n", "20200210 92.75119658566639\n", "20200211 92.7137832225167\n", "20200212 94.16679639869157\n", "20200213 93.70070316127686\n", "20200214 92.78063375730268\n", "20200217 90.18083184689543\n", "20200218 88.18081003961643\n", "20200219 89.41963597836396\n", "20200220 91.03837384387094\n", "20200221 90.26912894162687\n", "20200224 90.54881566134199\n", "20200225 94.2030430162798\n", "20200226 93.68335376927017\n", "20200227 92.99305332055847\n", "20200228 92.45730313461226\n", "20200302 93.3586589992516\n", "20200303 91.31696670181668\n", "20200304 91.59432496466799\n", "20200305 90.47912798776595\n" ] } ], "source": [ "zghl = estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F513050\": 100}), (\"est\", {\"954018\": 95}),\n", " float_holdings=True, window=4, decay=0.65, smooth=0.2)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.3729667577703976 \n", "标准差偏离: 0.8792921342073579\n", "\n", "预测偏差分位: \n", "1% 分位: -1.355 \n", "5% 分位: -0.554 \n", "25% 分位: -0.236 \n", "50% 分位: -0.005 \n", "75% 分位: 0.239 \n", "95% 分位: 0.717 \n", "99% 分位: 1.112\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.3 \n", "预测涨的比实际多: 0.23 \n", "预测跌的比实际多: 0.19 \n", "预测跌的比实际少: 0.28\n" ] } ], "source": [ "analyse_all(zghl, \"diff_real_est\") ## 效果整体不佳,毕竟只是替代指数预测,是用中国互联在预测中概互联" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "warning: this fund has no data for rate\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/shixin/Documents/newwork/quant finance/xalpha/xalpha/universal.py:111: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df[k] = df[k].apply(_float)\n" ] } ], "source": [ "zghl = estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F513050\": 100}), (\"est\", holdings[\"513050\"]))\n", "# 直接按股票持仓回测" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.23933544281723101 \n", "标准差偏离: 0.3804756811121884\n", "\n", "预测偏差分位: \n", "1% 分位: -0.861 \n", "5% 分位: -0.547 \n", "25% 分位: -0.179 \n", "50% 分位: -0.023 \n", "75% 分位: 0.141 \n", "95% 分位: 0.481 \n", "99% 分位: 0.825\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.35 \n", "预测涨的比实际多: 0.19 \n", "预测跌的比实际多: 0.1 \n", "预测跌的比实际少: 0.36\n" ] } ], "source": [ "analyse_all(zghl, \"diff_real_est\") # 效果明显好于用中国互联替代预测,但精度还是处于预测的下游" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 富时100" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "fs = estimate_table(\"20200210\", \"20200310\", (\"real\", {\"F539003\": 100}), (\"est\", {\"27\": 90}))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.5283972528064959 \n", "标准差偏离: 0.7386660564739429\n", "\n", "预测偏差分位: \n", "1% 分位: -0.488 \n", "5% 分位: -0.47 \n", "25% 分位: -0.264 \n", "50% 分位: 0.242 \n", "75% 分位: 0.593 \n", "95% 分位: 1.1 \n", "99% 分位: 2.244\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.24 \n", "预测涨的比实际多: 0.19 \n", "预测跌的比实际多: 0.38 \n", "预测跌的比实际少: 0.19\n" ] } ], "source": [ "analyse_all(fs, \"diff_real_est\") ## 这也算跟踪?" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20200211 90.69251051123888\n", "20200212 91.82780658000073\n", "20200213 92.36374912425\n", "20200214 93.91310721733443\n", "20200217 92.59854258721278\n", "20200218 89.37083140167216\n", "20200219 90.6704612198551\n", "20200220 88.60876119620922\n", "20200221 85.13194068592846\n", "20200224 83.93483146866896\n", "20200225 85.24384764935226\n", "20200226 86.1241074875769\n", "20200227 84.00510647475909\n", "20200228 83.22577290949985\n", "20200302 81.18712928277694\n", "20200303 83.54102738460666\n", "20200304 84.65645864940409\n", "20200305 84.53867383376162\n", "20200306 81.55699857796287\n", "20200309 81.60885728246151\n", "20200310 79.82079347815389\n" ] } ], "source": [ "fs = estimate_table(\"20200210\", \"20200310\", (\"real\", {\"F539003\": 100}), (\"est\", {\"27\": 90}), \n", " float_holdings=True, window=4, decay=0.65, smooth=0.2)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.4695590992529565 \n", "标准差偏离: 0.6669560977885407\n", "\n", "预测偏差分位: \n", "1% 分位: -0.49 \n", "5% 分位: -0.462 \n", "25% 分位: -0.313 \n", "50% 分位: 0.236 \n", "75% 分位: 0.529 \n", "95% 分位: 0.779 \n", "99% 分位: 2.071\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.29 \n", "预测涨的比实际多: 0.14 \n", "预测跌的比实际多: 0.38 \n", "预测跌的比实际少: 0.19\n" ] } ], "source": [ "analyse_all(fs, \"diff_real_est\") # 虽然略有改善,但也是很差" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 华宝中小" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "hbzx = estimate_table(\"20190101\", \"20200310\", (\"real\", {\"F501021\": 100}), (\"est\", {\".SPHCMSHP\": 92}))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.12873881115102398 \n", "标准差偏离: 0.1794073681739216\n", "\n", "预测偏差分位: \n", "1% 分位: -0.487 \n", "5% 分位: -0.302 \n", "25% 分位: -0.065 \n", "50% 分位: 0.02 \n", "75% 分位: 0.106 \n", "95% 分位: 0.278 \n", "99% 分位: 0.516\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.37 \n", "预测涨的比实际多: 0.22 \n", "预测跌的比实际多: 0.19 \n", "预测跌的比实际少: 0.23\n" ] } ], "source": [ "analyse_all(hbzx, \"diff_real_est\") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 标普科技" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "bp = estimate_table(\"20190301\", \"20200301\", (\"real\", {\"F161128\": 100}), (\"est\", {\"962700\": 95}) )" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.03005764396976259 \n", "标准差偏离: 0.04837197894916738\n", "\n", "预测偏差分位: \n", "1% 分位: -0.103 \n", "5% 分位: -0.069 \n", "25% 分位: -0.024 \n", "50% 分位: -0.006 \n", "75% 分位: 0.014 \n", "95% 分位: 0.054 \n", "99% 分位: 0.139\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.09 \n", "预测涨的比实际多: 0.49 \n", "预测跌的比实际多: 0.32 \n", "预测跌的比实际少: 0.1\n" ] } ], "source": [ "analyse_all(bp, \"diff_real_est\") " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "bp = estimate_table_with_pos(\"20190301\", \"20200301\", (\"real\", {\"F161128\": 100}), (\"est\", {\"962700\": 90}) )" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.021718872044452726 \n", "标准差偏离: 0.041554018312517586\n", "\n", "预测偏差分位: \n", "1% 分位: -0.105 \n", "5% 分位: -0.049 \n", "25% 分位: -0.011 \n", "50% 分位: -0.003 \n", "75% 分位: 0.008 \n", "95% 分位: 0.063 \n", "99% 分位: 0.094\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.27 \n", "预测涨的比实际多: 0.31 \n", "预测跌的比实际多: 0.14 \n", "预测跌的比实际少: 0.28\n" ] } ], "source": [ "analyse_all(bp, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bp.plot(x=\"date\", y=\"estpos\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 诺安油气" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "nayq = estimate_table(\"20190101\", \"20200310\", (\"real\", {\"F163208\": 100}), (\"est\", holdings[\"163208\"]))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.13208577471850988 \n", "标准差偏离: 0.22785982345126046\n", "\n", "预测偏差分位: \n", "1% 分位: -0.362 \n", "5% 分位: -0.24 \n", "25% 分位: -0.095 \n", "50% 分位: -0.009 \n", "75% 分位: 0.073 \n", "95% 分位: 0.345 \n", "99% 分位: 0.565\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.18 \n", "预测涨的比实际多: 0.36 \n", "预测跌的比实际多: 0.28 \n", "预测跌的比实际少: 0.19\n" ] } ], "source": [ "analyse_all(nayq, \"diff_real_est\") # 预测一般,但不是特别差" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20190109 90.12407704517783\n", "20190110 90.43832932363975\n", "20190111 90.80916214589467\n", "20190114 91.75772964993098\n", "20190116 89.98302301063102\n", "20190117 88.4585862070784\n", "20190118 86.28018736544071\n", "20190121 84.50811217824572\n", "20190122 83.34368318060753\n", "20190124 82.34778682050076\n", "20190125 81.30324534742645\n", "20190201 79.70574831926663\n", "20190211 78.00607798476406\n", "20190212 76.8562995036019\n", "20190222 76.26879557510891\n", "20190307 76.46274054913988\n", "20190308 78.29745553486339\n", "20190401 79.91683678491083\n", "20190402 81.61377984875169\n", "20190403 82.64121397670125\n", "20190404 83.36494717851892\n", "20190408 84.13441411148536\n", "20190411 83.15843115013897\n", "20190412 82.1806584252174\n", "20190415 80.77963946696569\n", "20190416 79.42267569952918\n", "20190417 78.54651901050448\n", "20190419 77.73982483246162\n", "20190424 78.21042094934018\n", "20190426 78.88767203784838\n", "20190510 78.32777539101468\n", "20190513 77.5732539037026\n", "20190515 79.64755529969275\n", "20190524 78.76129907341681\n", "20190604 78.321881204943\n", "20190610 78.82437355798501\n", "20190624 79.82894043272621\n", "20190625 81.34302770290101\n", "20190626 83.39738153340751\n", "20190627 86.77990017592177\n", "20190628 89.51311825896367\n", "20190701 91.91452345576053\n", "20190702 93.01568648992476\n", "20190703 93.97897479042777\n", "20190704 94.81491555363318\n", "20190709 92.06181050277551\n", "20190710 89.55384466978396\n", "20190711 87.6566678866587\n", "20190712 86.17400811626706\n", "20190719 85.1540603628906\n", "20190730 87.77549339065237\n", "20190816 86.78655093981286\n", "20190826 85.46087969609856\n", "20190827 84.93879289245014\n", "20190828 84.3045814000053\n", "20190829 83.87844609978144\n", "20190903 82.48561309767996\n", "20190904 80.89941917008524\n", "20190905 80.21491517657721\n", "20190910 81.31859885524185\n", "20190911 82.24719155624906\n", "20190917 81.57099244396906\n", "20190918 79.72269941264337\n", "20190920 80.62804020203616\n", "20190923 81.37517041803314\n", "20190926 80.45653014502965\n", "20190927 81.03559854325073\n", "20190930 81.62326079550274\n", "20191008 82.76936863845096\n", "20191009 83.60300121561683\n", "20191015 84.2192018430088\n", "20191016 85.32642634446039\n", "20191017 87.03727766283689\n", "20191018 89.15818302891937\n", "20191021 89.81513059321556\n", "20191023 88.20675728896435\n", "20191025 89.14969462884349\n", "20191028 90.68065938612995\n", "20191101 89.6774197130793\n", "20191105 90.80667036227673\n", "20191106 91.8376412275338\n", "20191107 92.72789229537223\n", "20191206 91.51555982979154\n", "20191209 90.56109445203488\n", "20191210 88.06903453871331\n", "20191211 85.91527583633278\n", "20200102 87.37446718115473\n", "20200103 88.17280363494083\n", "20200109 89.27207857764417\n", "20200110 90.33042928629098\n", "20200113 91.46179927564316\n", "20200114 92.31659597759563\n", "20200121 93.54704289430265\n", "20200203 92.3497035019482\n", "20200204 90.99009906578198\n", "20200210 89.96684581718458\n", "20200214 90.65777701087546\n", "20200219 91.83101987633192\n", "20200227 90.11683767005228\n", "20200228 88.16750247345384\n", "20200302 87.00922331251489\n", "20200303 85.49730309149928\n", "20200304 83.78457319899616\n", "20200306 82.91777998504327\n" ] } ], "source": [ "nayq = estimate_table(\"20190101\", \"20200310\", (\"real\", {\"F163208\": 100}), (\"est\", holdings[\"163208\"]), float_holdings=True)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.10036739554677777 \n", "标准差偏离: 0.15816222684666867\n", "\n", "预测偏差分位: \n", "1% 分位: -0.409 \n", "5% 分位: -0.229 \n", "25% 分位: -0.071 \n", "50% 分位: -0.008 \n", "75% 分位: 0.065 \n", "95% 分位: 0.195 \n", "99% 分位: 0.442\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.25 \n", "预测涨的比实际多: 0.28 \n", "预测跌的比实际多: 0.2 \n", "预测跌的比实际少: 0.27\n" ] } ], "source": [ "analyse_all(nayq, \"diff_real_est\") # 预测明显改善" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 白银基金" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "by = estimate_table(\"20190101\", \"20200310\", (\"real\", {\"F161226\": 100}), (\"est\", {\"996734\": 95}))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.44999715731539347 \n", "标准差偏离: 0.7661396339450135\n", "\n", "预测偏差分位: \n", "1% 分位: -3.04 \n", "5% 分位: -1.127 \n", "25% 分位: -0.184 \n", "50% 分位: -0.004 \n", "75% 分位: 0.236 \n", "95% 分位: 0.936 \n", "99% 分位: 2.563\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.3 \n", "预测涨的比实际多: 0.26 \n", "预测跌的比实际多: 0.17 \n", "预测跌的比实际少: 0.27\n" ] } ], "source": [ "analyse_all(by, \"diff_real_est\") #偏差过大。。。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 信诚商品" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "xc = estimate_table(\"20190301\", \"20200310\", (\"real\", {\"F165513\": 100}), (\"est\", holdings[\"165513\"]))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.1595405294858351 \n", "标准差偏离: 0.29326077238744225\n", "\n", "预测偏差分位: \n", "1% 分位: -0.808 \n", "5% 分位: -0.31 \n", "25% 分位: -0.093 \n", "50% 分位: -0.013 \n", "75% 分位: 0.107 \n", "95% 分位: 0.278 \n", "99% 分位: 0.975\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.31 \n", "预测涨的比实际多: 0.29 \n", "预测跌的比实际多: 0.16 \n", "预测跌的比实际少: 0.23\n" ] } ], "source": [ "analyse_all(xc, \"diff_real_est\") " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "xc = estimate_table(\"20200101\", \"20200320\", (\"real\", {\"F165513\": 100}), (\"est\", holdings[\"165513\"]))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.4045582553634708 \n", "标准差偏离: 0.7429430088592022\n", "\n", "预测偏差分位: \n", "1% 分位: -1.638 \n", "5% 分位: -0.797 \n", "25% 分位: -0.096 \n", "50% 分位: 0.042 \n", "75% 分位: 0.166 \n", "95% 分位: 1.653 \n", "99% 分位: 2.427\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.26 \n", "预测涨的比实际多: 0.2 \n", "预测跌的比实际多: 0.34 \n", "预测跌的比实际少: 0.2\n" ] } ], "source": [ "analyse_all(xc, \"diff_real_est\") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 银华通胀" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "yh = estimate_table(\"20200101\", \"20200301\", (\"real\", {\"F161815\": 100}), (\"est\", holdings[\"161815\"]))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.15532680999625523 \n", "标准差偏离: 0.18436609999596307\n", "\n", "预测偏差分位: \n", "1% 分位: -0.313 \n", "5% 分位: -0.276 \n", "25% 分位: -0.139 \n", "50% 分位: -0.01 \n", "75% 分位: 0.138 \n", "95% 分位: 0.298 \n", "99% 分位: 0.316\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.26 \n", "预测涨的比实际多: 0.26 \n", "预测跌的比实际多: 0.23 \n", "预测跌的比实际少: 0.26\n" ] } ], "source": [ "analyse_all(yh, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "yh = estimate_table(\"20200101\", \"20200301\", (\"real\", {\"F161815\": 100}), (\"est\", {\"1088675\": 70}))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.3171633251037427 \n", "标准差偏离: 0.40339062807400033\n", "\n", "预测偏差分位: \n", "1% 分位: -0.709 \n", "5% 分位: -0.669 \n", "25% 分位: -0.211 \n", "50% 分位: 0.048 \n", "75% 分位: 0.274 \n", "95% 分位: 0.65 \n", "99% 分位: 0.863\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.31 \n", "预测涨的比实际多: 0.2 \n", "预测跌的比实际多: 0.23 \n", "预测跌的比实际少: 0.26\n" ] } ], "source": [ "analyse_all(yh, \"diff_real_est\") #0.317, 0.403 70 (0.75, 0.82)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 易基黄金" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from lof.holdings import holdings_161116_19s4\n", "\n", "gld = estimate_table(\"20200101\", \"20200325\", (\"real\", {\"F161116\": 100}), (\"est\", holdings_161116_19s4))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "92.86999999999999" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum([v for _, v in holdings_161116_19s4.items()])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.6535647648387035 \n", "标准差偏离: 1.3088569523948343\n", "\n", "预测偏差分位: \n", "1% 分位: -4.815 \n", "5% 分位: -1.463 \n", "25% 分位: -0.181 \n", "50% 分位: 0.016 \n", "75% 分位: 0.27 \n", "95% 分位: 1.509 \n", "99% 分位: 3.543\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.28 \n", "预测涨的比实际多: 0.28 \n", "预测跌的比实际多: 0.26 \n", "预测跌的比实际少: 0.17\n" ] } ], "source": [ "analyse_all(gld, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gld.plot(x=\"date\", y=\"estpos\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 嘉实黄金" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# from lof.holdings import holdings_160719_19s4\n", "# xa.provider.set_proxy(\"http://127.0.0.1:8087\")\n", "gld = estimate_table(\"20191001\", \"20200225\", (\"real\", {\"F160719\": 100}), (\"est\", holdings[\"160719\"]),(\"bcest\", \n", " {\"commodities/gold\":90})\n", " )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.12140793326205283 \n", "标准差偏离: 0.15001160103371575\n", "\n", "预测偏差分位: \n", "1% 分位: -0.362 \n", "5% 分位: -0.24 \n", "25% 分位: -0.101 \n", "50% 分位: 0.0 \n", "75% 分位: 0.097 \n", "95% 分位: 0.227 \n", "99% 分位: 0.281\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.33 \n", "预测涨的比实际多: 0.28 \n", "预测跌的比实际多: 0.17 \n", "预测跌的比实际少: 0.22\n", "\n", "\n", "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.17300555954771488 \n", "标准差偏离: 0.23733166703645128\n", "\n", "预测偏差分位: \n", "1% 分位: -0.629 \n", "5% 分位: -0.395 \n", "25% 分位: -0.121 \n", "50% 分位: -0.005 \n", "75% 分位: 0.114 \n", "95% 分位: 0.359 \n", "99% 分位: 0.449\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.27 \n", "预测涨的比实际多: 0.34 \n", "预测跌的比实际多: 0.23 \n", "预测跌的比实际少: 0.16\n" ] } ], "source": [ "analyse_all(gld, \"diff_real_est\")\n", "print(\"\\n\")\n", "analyse_all(gld, \"diff_real_bcest\") # 还是真实持仓准" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "gld = estimate_table_with_pos(\"20191001\", \"20200225\", (\"real\", {\"F160719\": 100}), (\"est\", holdings_160719_19s4))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.13032855563168053 \n", "标准差偏离: 0.16198181760630712\n", "\n", "预测偏差分位: \n", "1% 分位: -0.402 \n", "5% 分位: -0.301 \n", "25% 分位: -0.117 \n", "50% 分位: -0.011 \n", "75% 分位: 0.112 \n", "95% 分位: 0.232 \n", "99% 分位: 0.262\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.37 \n", "预测涨的比实际多: 0.25 \n", "预测跌的比实际多: 0.1 \n", "预测跌的比实际少: 0.29\n" ] } ], "source": [ "analyse_all(gld, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gld.plot(x=\"date\", y=\"estpos\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "gld = estimate_table_with_pos(\"20200201\", \"20200325\", (\"real\", {\"F160719\": 100}), (\"est\", holdings_160719_19s4))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.2956363763900901 \n", "标准差偏离: 0.4074806463984154\n", "\n", "预测偏差分位: \n", "1% 分位: -0.719 \n", "5% 分位: -0.664 \n", "25% 分位: -0.181 \n", "50% 分位: 0.053 \n", "75% 分位: 0.203 \n", "95% 分位: 0.918 \n", "99% 分位: 1.034\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.43 \n", "预测涨的比实际多: 0.19 \n", "预测跌的比实际多: 0.14 \n", "预测跌的比实际少: 0.24\n" ] } ], "source": [ "analyse_all(gld, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "gld = estimate_table(\"20200201\", \"20200325\", (\"real\", {\"F160719\": 100}), (\"est\", holdings_160719_19s4))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.2880271666070501 \n", "标准差偏离: 0.3716035221774948\n", "\n", "预测偏差分位: \n", "1% 分位: -0.724 \n", "5% 分位: -0.658 \n", "25% 分位: -0.199 \n", "50% 分位: 0.062 \n", "75% 分位: 0.197 \n", "95% 分位: 0.585 \n", "99% 分位: 0.838\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.43 \n", "预测涨的比实际多: 0.19 \n", "预测跌的比实际多: 0.19 \n", "预测跌的比实际少: 0.19\n" ] } ], "source": [ "analyse_all(gld, \"diff_real_est\") ## 固定仓位好点" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 汇添富黄金" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "xa.provider.set_proxy(\"http://127.0.0.1:8087\")\n", "gld = estimate_table(\n", " \"20191001\",\n", " \"20200225\",\n", " (\"real\", {\"F164701\": 100}),\n", " (\"est\", holdings[\"164701\"]),\n", " (\"bcest\", {\"commodities/gold\": 90}),\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.05400003928906106 \n", "标准差偏离: 0.06995691407820687\n", "\n", "预测偏差分位: \n", "1% 分位: -0.162 \n", "5% 分位: -0.11 \n", "25% 分位: -0.054 \n", "50% 分位: -0.007 \n", "75% 分位: 0.04 \n", "95% 分位: 0.115 \n", "99% 分位: 0.153\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.28 \n", "预测涨的比实际多: 0.35 \n", "预测跌的比实际多: 0.15 \n", "预测跌的比实际少: 0.22\n", "\n", "\n", "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.1730015259474371 \n", "标准差偏离: 0.21976789496119645\n", "\n", "预测偏差分位: \n", "1% 分位: -0.531 \n", "5% 分位: -0.399 \n", "25% 分位: -0.126 \n", "50% 分位: 0.007 \n", "75% 分位: 0.13 \n", "95% 分位: 0.328 \n", "99% 分位: 0.462\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.33 \n", "预测涨的比实际多: 0.3 \n", "预测跌的比实际多: 0.19 \n", "预测跌的比实际少: 0.17\n" ] } ], "source": [ "analyse_all(gld, \"diff_real_est\")\n", "print(\"\\n\")\n", "analyse_all(gld, \"diff_real_bcest\")" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "gld = estimate_table_with_pos(\n", " \"20200101\",\n", " \"20200326\",\n", " (\"real\", {\"F164701\": 100}),\n", " (\"est\", holdings[\"164701\"]),\n", ")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.09796134999036103 \n", "标准差偏离: 0.13560257112270027\n", "\n", "预测偏差分位: \n", "1% 分位: -0.329 \n", "5% 分位: -0.207 \n", "25% 分位: -0.076 \n", "50% 分位: -0.005 \n", "75% 分位: 0.065 \n", "95% 分位: 0.199 \n", "99% 分位: 0.35\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.31 \n", "预测涨的比实际多: 0.31 \n", "预测跌的比实际多: 0.17 \n", "预测跌的比实际少: 0.2\n" ] } ], "source": [ "analyse_all(gld, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "gld = estimate_table(\n", " \"20200101\",\n", " \"20200326\",\n", " (\"real\", {\"F164701\": 100}),\n", " (\"est\", holdings[\"164701\"]),\n", ")" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.07935650312273379 \n", "标准差偏离: 0.10888561587283502\n", "\n", "预测偏差分位: \n", "1% 分位: -0.199 \n", "5% 分位: -0.166 \n", "25% 分位: -0.072 \n", "50% 分位: -0.002 \n", "75% 分位: 0.054 \n", "95% 分位: 0.168 \n", "99% 分位: 0.314\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.31 \n", "预测涨的比实际多: 0.31 \n", "预测跌的比实际多: 0.15 \n", "预测跌的比实际少: 0.22\n" ] } ], "source": [ "analyse_all(gld, \"diff_real_est\") ## 仓位固定效果略好,类似指数" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('');\n", " button.click(method_name, toolbar_event);\n", " button.mouseover(tooltip, toolbar_mouse_event);\n", " nav_element.append(button);\n", " }\n", "\n", " // Add the status bar.\n", " var status_bar = $('');\n", " nav_element.append(status_bar);\n", " this.message = status_bar[0];\n", "\n", " // Add the close button to the window.\n", " var buttongrp = $('
');\n", " var button = $('');\n", " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", " buttongrp.append(button);\n", " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", " titlebar.prepend(buttongrp);\n", "}\n", "\n", "mpl.figure.prototype._root_extra_style = function(el){\n", " var fig = this\n", " el.on(\"remove\", function(){\n", "\tfig.close_ws(fig, {});\n", " });\n", "}\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(el){\n", " // this is important to make the div 'focusable\n", " el.attr('tabindex', 0)\n", " // reach out to IPython and tell the keyboard manager to turn it's self\n", " // off when our div gets focus\n", "\n", " // location in version 3\n", " if (IPython.notebook.keyboard_manager) {\n", " IPython.notebook.keyboard_manager.register_events(el);\n", " }\n", " else {\n", " // location in version 2\n", " IPython.keyboard_manager.register_events(el);\n", " }\n", "\n", "}\n", "\n", "mpl.figure.prototype._key_event_extra = function(event, name) {\n", " var manager = IPython.notebook.keyboard_manager;\n", " if (!manager)\n", " manager = IPython.keyboard_manager;\n", "\n", " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", " event.shiftKey = false;\n", " // Send a \"J\" for go to next cell\n", " event.which = 74;\n", " event.keyCode = 74;\n", " manager.command_mode();\n", " manager.handle_keydown(event);\n", " }\n", "}\n", "\n", "mpl.figure.prototype.handle_save = function(fig, msg) {\n", " fig.ondownload(fig, null);\n", "}\n", "\n", "\n", "mpl.find_output_cell = function(html_output) {\n", " // Return the cell and output element which can be found *uniquely* in the notebook.\n", " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", " // IPython event is triggered only after the cells have been serialised, which for\n", " // our purposes (turning an active figure into a static one), is too late.\n", " var cells = IPython.notebook.get_cells();\n", " var ncells = cells.length;\n", " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", " data = data.data;\n", " }\n", " if (data['text/html'] == html_output) {\n", " return [cell, data, j];\n", " }\n", " }\n", " }\n", " }\n", "}\n", "\n", "// Register the function which deals with the matplotlib target/channel.\n", "// The kernel may be null if the page has been refreshed.\n", "if (IPython.notebook.kernel != null) {\n", " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", "}\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xa.Compare(\"F164701\", (\"commodities/gold\", \"USD\"),\"F518880\", start=\"20190601\").v()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 华安标普预测替代尝试" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "haoil = estimate_table(\"20190101\", \"20200310\", (\"real\", {\"F160416\": 100}), (\"est\", {\"520\": 88}))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.2207225009490602 \n", "标准差偏离: 0.32810089194879843\n", "\n", "预测偏差分位: \n", "1% 分位: -0.947 \n", "5% 分位: -0.398 \n", "25% 分位: -0.143 \n", "50% 分位: -0.003 \n", "75% 分位: 0.164 \n", "95% 分位: 0.437 \n", "99% 分位: 0.891\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.29 \n", "预测涨的比实际多: 0.24 \n", "预测跌的比实际多: 0.21 \n", "预测跌的比实际少: 0.27\n" ] } ], "source": [ "analyse_all(haoil, \"diff_real_est\") #" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20190103 90.50509834425999\n", "20190104 90.82870624536903\n", "20190107 91.535888888205\n", "20190108 92.41654224670755\n", "20190109 92.17991901311608\n", "20190110 92.17991901311606\n", "20190111 92.7164042022933\n", "20190114 92.65822489404091\n", "20190115 92.97020645646519\n", "20190116 92.79518057471901\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "../lof/predict.py:108: RuntimeWarning: invalid value encountered in double_scalars\n", " ratio = realf / estf\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "20190117 92.103604741907\n", "20190118 92.03459796307658\n", "20190121 90.27349561955084\n", "20190122 90.08613925310753\n", "20190123 91.17978608333203\n", "20190124 90.76197559236697\n", "20190125 91.27255334878238\n", "20190128 92.0525419006446\n", "20190129 91.38684703817515\n", "20190130 89.84960019985343\n", "20190131 90.51054912701738\n", "20190201 90.36498736305242\n", "20190211 90.8119470033529\n", "20190212 91.23589487087666\n", "20190213 91.06383651189665\n", "20190214 90.98902848285698\n", "20190215 90.35606177178528\n", "20190218 91.2795696366634\n", "20190219 89.4898384332408\n", "20190220 91.09599097951488\n", "20190221 90.85670749517124\n", "20190225 89.83067289335266\n", "20190226 91.49590556512155\n", "20190227 91.6326915926331\n", "20190228 92.48822014346973\n", "20190301 93.03861525933378\n", "20190304 93.16616174820653\n", "20190305 93.14263875443767\n", "20190306 92.78408411199813\n", "20190307 92.82669355755267\n", "20190308 93.2579193892459\n", "20190311 92.10068500281209\n", "20190312 93.38006401292347\n", "20190313 94.13015407683139\n", "20190314 93.41021298442934\n", "20190315 92.83640735839788\n", "20190318 92.08977541249511\n", "20190319 92.13158482653374\n", "20190320 90.86679807492924\n", "20190321 90.86866184015227\n", "20190322 89.58043509697472\n", "20190325 90.81097889982\n", "20190326 91.12560609275151\n", "20190327 90.89277465334045\n", "20190328 90.38101718087083\n", "20190329 88.94138494342513\n", "20190401 88.59370979444009\n", "20190402 87.69683816074621\n", "20190403 87.63381738832072\n", "20190404 87.63381738832071\n", "20190408 87.8951461922753\n", "20190409 86.80702603884723\n", "20190410 88.11276482205486\n", "20190411 88.12641300824464\n", "20190412 87.50558497665422\n", "20190415 88.20372288699508\n", "20190416 88.1936434284553\n", "20190417 86.52478649632138\n", "20190418 87.307110176129\n", "20190419 87.13153926910458\n", "20190422 85.19432952136434\n", "20190423 85.6129020960085\n", "20190424 85.48866420895983\n", "20190425 86.72157779136711\n", "20190426 87.33008851907341\n", "20190429 87.55726772425349\n", "20190430 88.79617520877422\n", "20190506 89.25427233089717\n", "20190507 89.68375743614882\n", "20190508 89.00021443918286\n", "20190509 88.41153755459015\n", "20190510 88.01910754233185\n", "20190513 87.85347516215957\n", "20190514 88.48708005436518\n", "20190515 88.03428232700273\n", "20190516 87.56471974605549\n", "20190517 88.98743518085922\n", "20190520 88.42976510834158\n", "20190521 89.18894509864806\n", "20190522 88.17184281223186\n", "20190523 87.92333413049504\n", "20190524 87.94787798843137\n", "20190527 87.23488835414634\n", "20190528 87.48940501982051\n", "20190529 86.07874405128766\n", "20190530 86.75344698167413\n", "20190531 86.3839935570655\n", "20190603 85.61117318830532\n", "20190604 86.23799709570281\n", "20190605 86.85963727275232\n", "20190606 86.08642654647626\n", "20190610 85.36470508953887\n", "20190611 86.21669823570906\n", "20190612 86.45416871534225\n", "20190613 87.44373958866774\n", "20190614 86.45919201098664\n", "20190617 86.24941940864389\n", "20190618 86.10617280248921\n", "20190619 86.10617280248923\n", "20190620 86.12243949632605\n", "20190621 86.83760711328678\n", "20190624 86.75923778332378\n", "20190625 86.42646524219136\n", "20190626 86.33079653227477\n", "20190627 85.35911263801857\n", "20190628 84.78543026681429\n", "20190701 85.41954546056478\n", "20190702 85.25642237189852\n", "20190703 84.96592879355889\n", "20190704 86.57062155094583\n", "20190705 85.65100605868604\n", "20190708 84.71362034228807\n", "20190709 84.71362034228808\n", "20190710 85.3081546025561\n", "20190711 84.76765896959735\n", "20190712 85.40229472644268\n", "20190715 86.12171619014437\n", "20190716 86.48711496349632\n", "20190717 86.26575738745473\n", "20190718 87.03367485781625\n", "20190719 87.5872215279559\n", "20190722 88.8959226136795\n", "20190723 88.96221351018589\n", "20190724 89.42129474607752\n", "20190725 88.81690934338008\n", "20190726 89.6719905835847\n", "20190729 90.16024377481357\n", "20190730 89.06729067144208\n", "20190731 89.24059056499058\n", "20190801 88.72186639434418\n", "20190802 87.58881337976358\n", "20190805 86.99289095968687\n", "20190806 87.2998384481279\n", "20190807 87.8885341109098\n", "20190808 86.31722073353819\n", "20190809 87.00877077912452\n", "20190812 88.7982915675762\n", "20190813 88.49464215849727\n", "20190814 88.53106774393507\n", "20190815 89.57326696221973\n", "20190816 88.9779231656083\n", "20190819 88.78170988588118\n", "20190820 89.09759960747891\n", "20190821 88.29183033592405\n", "20190822 89.16106536095047\n", "20190823 88.23895752459663\n", "20190826 88.23895752459661\n", "20190827 89.38341255405878\n", "20190828 89.4298591709258\n", "20190829 90.11316463663965\n", "20190830 91.24822355076181\n", "20190902 91.825554972874\n", "20190903 92.55695922524094\n", "20190904 93.33421972772778\n", "20190905 94.12729627856484\n", "20190906 94.43967516455022\n", "20190909 94.1076928204716\n", "20190910 95.00944025504192\n", "20190912 95.125747311586\n", "20190916 94.98453498238138\n", "20190917 96.1440679298888\n", "20190918 97.00600792975902\n", "20190919 97.09406754831497\n", "20190920 97.80190374241154\n", "20190923 97.84203902446966\n", "20190924 99.08007356756252\n", "20190925 99.1852328267244\n", "20190926 98.45607571176205\n", "20190927 97.949051891103\n", "20190930 98.77491816868152\n", "20191008 97.07099431039796\n", "20191009 96.17945068347935\n", "20191010 96.86923441528225\n", "20191011 95.80391390491592\n", "20191014 94.53562294172865\n", "20191015 94.39468536184093\n", "20191016 93.52282171312504\n", "20191017 91.02129102585891\n", "20191018 90.58313785165556\n", "20191021 90.50188996197195\n", "20191022 91.16349387847323\n", "20191023 91.00780825175636\n", "20191024 90.34851190948733\n", "20191025 90.63433588056502\n", "20191028 90.03988405828002\n", "20191029 90.03988405828004\n", "20191030 90.65944800215546\n", "20191031 90.43775996557866\n", "20191101 90.78849775822502\n", "20191104 89.84885457574065\n", "20191105 90.46270650163989\n", "20191106 90.53559206490758\n", "20191107 91.3069784229231\n", "20191108 91.3306221362368\n", "20191111 92.0973985298537\n", "20191112 92.19608989539786\n", "20191113 92.88648492628086\n", "20191114 93.14273670757\n", "20191115 92.46075373316332\n", "20191118 94.24163650308537\n", "20191119 93.2777659966394\n", "20191120 91.42795930774277\n", "20191121 90.99326088970187\n", "20191122 90.99326088970189\n", "20191125 89.8859326451289\n", "20191126 89.68267495014167\n", "20191127 90.47713664099177\n", "20191128 91.05057519596168\n", "20191129 92.31101254297535\n", "20191202 93.10434309774654\n", "20191203 93.42375440530526\n", "20191204 93.24621253658196\n", "20191205 93.62364160446761\n", "20191206 93.54381265227244\n", "20191209 93.12538620495438\n", "20191210 93.12538620495437\n", "20191211 93.45210857860071\n", "20191212 94.00339224969429\n", "20191213 92.61477817564834\n", "20191216 93.10516364787132\n", "20191217 93.61912572878789\n", "20191218 91.8112844783985\n", "20191219 93.22510566969224\n", "20191220 93.60869339933913\n", "20191223 92.40940464205566\n", "20191225 94.1716025008091\n", "20191226 94.44349555064893\n", "20191227 94.44349555064892\n", "20191230 94.0413420980644\n", "20191231 93.80967570198915\n", "20200102 93.81983037240808\n", "20200103 92.81594512762646\n", "20200106 93.2723065691104\n", "20200107 94.03221765930414\n", "20200108 92.6784697849656\n", "20200109 91.65236614471496\n", "20200110 91.46984357453756\n", "20200113 93.03330261221551\n", "20200114 93.42232452745564\n", "20200115 92.11474931849129\n", "20200116 94.15578234957489\n", "20200117 93.29004801198101\n", "20200120 92.90925905636425\n", "20200121 93.49404455218924\n", "20200122 93.34067750191323\n", "20200123 95.34990074495926\n", "20200203 94.80620100250492\n", "20200204 95.71915387872485\n", "20200205 95.18706976402457\n", "20200206 95.04691042531782\n", "20200207 95.34760562194886\n", "20200210 96.19612947177646\n", "20200211 96.88315194170218\n", "20200212 96.59428883342503\n", "20200213 96.50257092057193\n", "20200214 95.62702318278969\n", "20200217 95.35377487756303\n", "20200218 95.55462090413337\n", "20200219 96.49091226971589\n", "20200220 95.85965479045807\n", "20200221 96.32512218277523\n", "20200224 95.2686613860293\n", "20200225 94.51847132351318\n", "20200226 95.35305734353237\n", "20200227 93.98257223556836\n", "20200228 93.49100058110854\n", "20200302 92.90476665994518\n", "20200303 91.98486854175552\n", "20200304 90.53001207007524\n", "20200305 88.66471074552298\n", "20200306 88.66194273610886\n", "20200309 88.72122981986901\n", "20200310 88.42482746152646\n" ] } ], "source": [ "haoil = estimate_table(\"20190101\", \"20200310\", (\"real\", {\"F160416\": 100}), (\"est\", {\"520\": 90}),\n", " float_holdings=True, window=4, decay=0.8, smooth=0.1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.2316632707891009 \n", "标准差偏离: 0.3394105216535918\n", "\n", "预测偏差分位: \n", "1% 分位: -1.052 \n", "5% 分位: -0.411 \n", "25% 分位: -0.144 \n", "50% 分位: -0.004 \n", "75% 分位: 0.177 \n", "95% 分位: 0.489 \n", "99% 分位: 0.78\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.28 \n", "预测涨的比实际多: 0.24 \n", "预测跌的比实际多: 0.22 \n", "预测跌的比实际少: 0.26\n" ] } ], "source": [ "analyse_all(haoil, \"diff_real_est\") # 替代预测效果很一般,浮动还是固定仓都很一般" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "haoil = estimate_table(\"20190101\", \"20200310\", (\"real\", {\"F160416\": 100}), (\"est\", {\"SP5475707.2\": 90}),\n", " )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.10336445485275023 \n", "标准差偏离: 0.13535653835908334\n", "\n", "预测偏差分位: \n", "1% 分位: -0.34 \n", "5% 分位: -0.193 \n", "25% 分位: -0.07 \n", "50% 分位: 0.008 \n", "75% 分位: 0.094 \n", "95% 分位: 0.216 \n", "99% 分位: 0.346\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.33 \n", "预测涨的比实际多: 0.2 \n", "预测跌的比实际多: 0.2 \n", "预测跌的比实际少: 0.28\n" ] } ], "source": [ "analyse_all(haoil, \"diff_real_est\") # 还得是真实指数更靠谱" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "haoil = estimate_table(\"20200301\", \"20200320\", (\"real\", {\"F160416\": 100}), (\"est\", {\"SP5475707.2\": 70}),\n", " )" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 1.5458828787019208 \n", "标准差偏离: 2.2193865761580227\n", "\n", "预测偏差分位: \n", "1% 分位: -3.947 \n", "5% 分位: -3.401 \n", "25% 分位: -0.44 \n", "50% 分位: -0.025 \n", "75% 分位: 0.615 \n", "95% 分位: 3.82 \n", "99% 分位: 4.444\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.21 \n", "预测涨的比实际多: 0.21 \n", "预测跌的比实际多: 0.29 \n", "预测跌的比实际少: 0.29\n" ] } ], "source": [ "analyse_all(haoil, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20200204 89.92132688797578\n", "20200205 90.06334525873646\n", "20200206 90.19716053487194\n", "20200207 90.57167851883504\n", "20200210 90.99863370283633\n", "20200211 90.40637636173336\n", "20200212 90.14666893597725\n", "20200213 88.01434330854921\n", "20200214 88.22330047103434\n", "20200217 88.04983662440671\n", "20200218 86.34798651510654\n", "20200219 87.96547680684772\n", "20200220 89.1531185122205\n", "20200221 87.64133100549118\n", "20200224 87.20815211376346\n", "20200225 87.7095658833157\n", "20200226 89.82478211996911\n", "20200227 88.10417162396567\n", "20200228 87.98874272050105\n", "20200302 86.78685978084376\n", "20200303 86.45581600882458\n", "20200304 84.37036271557437\n", "20200305 85.16944967796478\n", "20200306 87.03338491656781\n", "20200309 86.41159528899864\n", "20200310 86.03814854417271\n", "20200311 86.32912099962472\n", "20200312 85.6553009674062\n", "20200313 83.38038616628951\n", "20200316 79.80389912361746\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "../lof/predict.py:105: RuntimeWarning: invalid value encountered in double_scalars\n", " ratio = realf / estf\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "20200318 77.50509134092462\n", "20200319 76.34762184544849\n", "20200320 74.85602023693059\n" ] } ], "source": [ "haoil = estimate_table(\"20200201\", \"20200320\", (\"real\", {\"F160416\": 100}), (\"est\", {\"SP5475707.2\": 90}),\n", " float_holdings=True)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.7649936814692555 \n", "标准差偏离: 1.7554039097702738\n", "\n", "预测偏差分位: \n", "1% 分位: -2.996 \n", "5% 分位: -0.715 \n", "25% 分位: -0.131 \n", "50% 分位: 0.016 \n", "75% 分位: 0.171 \n", "95% 分位: 4.636 \n", "99% 分位: 5.914\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.21 \n", "预测涨的比实际多: 0.18 \n", "预测跌的比实际多: 0.35 \n", "预测跌的比实际少: 0.26\n" ] } ], "source": [ "analyse_all(haoil, \"diff_real_est\") # 浮动仓没啥区别" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 测试浮动仓位的渐进行为" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20190103 98.998011150905\n", "20190104 99.13433420433203\n", "20190107 99.32331625311409\n", "20190108 99.691902335422\n", "20190109 100.0\n", "20190110 96.92323174237059\n", "20190111 97.1019425677503\n", "20190114 97.5798411421276\n", "20190115 97.71198255900691\n", "20190116 97.1365365226914\n", "20190117 97.38370239104492\n", "20190118 97.51482135780323\n", "20190121 97.60080362836045\n", "20190122 97.10693694452911\n", "20190123 97.23149449432826\n", "20190124 97.53955577789067\n", "20190125 98.20240064688953\n", "20190128 98.26865065565114\n", "20190129 98.81678255580867\n", "20190130 97.40995861312274\n", "20190131 98.8094389139682\n", "20190201 98.11076085899879\n", "20190211 97.91622660360242\n", "20190212 98.20763959378486\n", "20190213 98.76761533484756\n", "20190214 98.6675843689077\n", "20190215 98.05451454065582\n", "20190218 97.71865178050366\n", "20190219 97.8648340189131\n", "20190220 97.92001467231408\n", "20190221 96.41545072018585\n", "20190222 95.86874509375932\n", "20190225 95.51552799111002\n", "20190226 97.865549765494\n", "20190227 97.59879461047939\n", "20190228 96.73549848722095\n", "20190301 96.20679499080065\n", "20190304 95.85050798768998\n", "20190305 96.51710148399373\n", "20190306 95.61531279063942\n", "20190307 95.66181972720395\n", "20190308 94.72172054794582\n", "20190311 93.9921122067115\n", "20190312 96.17728390389627\n", "20190313 96.0067728258366\n", "20190315 95.67427416353172\n", "20190318 95.46123788995901" ] }, { "name": "stderr", "output_type": "stream", "text": [ "../lof/predict.py:108: RuntimeWarning: invalid value encountered in double_scalars\n", " ratio = realf / estf\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "20190319 95.1823615790699\n", "20190320 95.66499966773793\n", "20190321 95.14401647761864\n", "20190322 95.0787269888551\n", "20190325 94.40631744470342\n", "20190326 95.74319976611889\n", "20190327 95.80710111535338\n", "20190328 95.95306841568566\n", "20190329 95.87594618583041\n", "20190401 96.44916835341517\n", "20190402 96.87302063893122\n", "20190403 98.24421746283546\n", "20190404 97.59128615658129\n", "20190408 98.31583290523628\n", "20190409 99.90732477935885\n", "20190410 99.99999999999999\n", "20190411 98.68787251255692\n", "20190412 97.34673348581427\n", "20190415 97.2926635429898\n", "20190416 97.89089542143985\n", "20190417 98.23154904127846\n", "20190418 96.87378692011481\n", "20190419 96.69495672079772\n", "20190422 96.25902590607667\n", "20190423 94.42659451670407\n", "20190424 98.10505120134621\n", "20190425 97.70517227339467\n", "20190426 98.21451987169664\n", "20190429 97.26345179763447\n", "20190430 98.65915169666056\n", "20190506 97.61449240768482\n", "20190507 97.2977714978251\n", "20190508 97.81026821104214\n", "20190509 97.97235278262788\n", "20190510 98.8559335122075\n", "20190513 97.97518550008287\n", "20190514 98.35958747108299\n", "20190515 98.36869906298752\n", "20190516 99.01922292874488\n", "20190517 99.89230245681202\n", "20190520 97.35840373036316\n", "20190521 96.84323777675381\n", "20190522 96.84646288396627\n", "20190523 96.50890065020837\n", "20190524 98.39651681317189\n", "20190527 97.11202449990235\n", "20190528 97.15961760424284\n", "20190529 96.9985054040193\n", "20190530 97.20529782025815\n", "20190531 97.89772466077034\n", "20190603 95.079237765087\n", "20190604 95.75290210754066\n", "20190605 96.01968274324392\n", "20190606 95.81048099152949\n", "20190610 94.98136508328678\n", "20190611 94.20318773114975\n", "20190612 94.44339651679529\n", "20190613 94.19148058071984\n", "20190614 92.9379228037595\n", "20190617 94.00042623111857\n", "20190618 94.24416076009172\n", "20190619 93.4132032592097\n", "20190620 92.8223672868978\n", "20190621 92.37342837172643\n", "20190624 91.47635593269149\n", "20190625 89.25362619144192\n", "20190626 91.47233177671075\n", "20190627 89.40642342224649\n", "20190628 88.28495959967823\n", "20190701 89.3444740708391\n", "20190702 90.06218221505881\n", "20190703 89.64570545529821\n", "20190704 92.88990049732777\n", "20190705 92.29320472932389\n", "20190708 91.12421316846637\n", "20190709 91.28087180009618\n", "20190710 91.92788546614084\n", "20190711 93.62059133685248\n", "20190712 93.29561262952542\n", "20190715 94.7558660199951\n", "20190716 93.53204995197756\n", "20190717 94.09948818980956\n", "20190718 96.01824685816759\n", "20190719 95.95742492427736\n", "20190722 95.68681905905666\n", "20190723 94.36680325744679\n", "20190724 94.66744514978143\n", "20190725 95.07721739168942\n", "20190726 96.87711465361386\n", "20190729 98.11890520654437\n", "20190730 100.0\n", "20190731 99.3555310842712\n", "20190801 99.47957830769663\n", "20190802 97.84513427110195\n", "20190805 97.07791330055645\n", "20190806 96.03227117899891\n", "20190807 96.18023890674618\n", "20190808 94.23935606901794\n", "20190809 95.5659993844393\n", "20190812 97.4663116439416\n", "20190813 96.06179517624155\n", "20190814 96.57520203559808\n", "20190815 96.5837420381924\n", "20190816 97.42808920027322\n", "20190819 96.09047594994678\n", "20190820 95.69052441102286\n", "20190821 95.37520169452475\n", "20190822 95.69774799171263\n", "20190823 95.52803287061758\n", "20190826 93.20233767253818\n", "20190827 94.73963104862277\n", "20190828 97.12583705526335\n", "20190829 95.53457129707357\n", "20190830 92.62723217081087\n", "20190902 95.97458604177231\n", "20190903 96.09954329979504\n", "20190904 97.17555481580845\n", "20190905 94.72846498210103\n", "20190906 93.82102615444056\n", "20190909 95.48942347992543\n", "20190910 95.48942347992542\n", "20190911 93.93109014843797\n", "20190912 92.87227328428004\n", "20190916 92.82763954420929\n", "20190917 95.05172818042588\n", "20190918 95.66522549388704\n", "20190919 94.97137076149995\n", "20190920 94.89758030683682\n", "20190923 95.35327569296126\n", "20190924 96.12807822056983\n", "20190925 96.07143459210988\n", "20190926 95.62876525332753\n", "20190927 96.37452982149532\n", "20190930 95.82632855518864\n", "20191008 95.58258509084742\n", "20191009 95.51647171053949\n", "20191010 98.75380108403442\n", "20191011 96.57224720225125\n", "20191014 96.16056531671477\n", "20191015 95.92361382183962\n", "20191016 97.00539284221581\n", "20191017 96.4163749468147\n", "20191018 97.18488314718402\n", "20191021 98.76533592556817\n", "20191022 97.65038273599167\n", "20191023 98.90548275492318\n", "20191024 96.03856269068196\n", "20191025 95.83833679420597\n", "20191028 97.47213656899109\n", "20191029 98.39597653617899\n", "20191030 97.94376532681734\n", "20191031 98.56215756545743\n", "20191101 99.8644462313409\n", "20191104 99.71077007647662\n", "20191105 98.38987774096073\n", "20191106 98.50019898578817\n", "20191107 98.71994329203514\n", "20191108 98.07941609276853\n", "20191111 98.30947550140378\n", "20191112 96.29207528652896\n", "20191113 95.4489895446411\n", "20191114 93.77118450152136\n", "20191115 95.42906128769687\n", "20191118 93.8036393762692\n", "20191119 96.03667924505316\n", "20191120 95.01186755973671\n", "20191121 94.99148227234855\n", "20191122 95.61349385380937\n", "20191125 95.21423320422713\n", "20191126 95.41771136568647\n", "20191127 95.32893924051837\n", "20191128 94.24915052590671\n", "20191129 93.13442767845231\n", "20191202 96.75327034159778\n", "20191203 95.7835577443651\n", "20191204 92.229167032951\n", "20191205 95.23872790031396\n", "20191206 95.47172505864944\n", "20191209 97.62344767179466\n", "20191210 94.81442142615144\n", "20191211 93.89301870968062\n", "20191212 95.08271148654492\n", "20191213 95.13511739070832\n", "20191216 95.66472951451563\n", "20191217 95.18153411410228\n", "20191218 94.9297240039535\n", "20191219 95.03964740682322\n", "20191220 94.65778492170817\n", "20191223 96.11805102606617\n", "20191224 96.24667671564349\n", "20191225 96.76143547635543\n", "20191226 96.83354468740856\n", "20191227 97.14742116380549\n", "20191230 94.9070401904274\n", "20191231 94.48877472816402\n", "20200102 92.89774670667182\n", "20200103 95.88497133063368\n", "20200106 95.36128333799104\n", "20200107 93.91622045827137\n", "20200108 92.6461331742685\n", "20200109 93.87868873285372\n", "20200110 94.00948840341148\n", "20200113 93.74344025838313\n", "20200114 93.07473386900506\n", "20200115 92.60551133395929\n", "20200116 93.2920440864787\n", "20200117 93.25273098995116\n", "20200120 93.13583617742864\n", "20200121 94.45195360331434\n", "20200122 95.23552796261242\n", "20200123 96.79584526015093\n", "20200203 97.08368785797877\n", "20200204 95.50110891193394\n", "20200205 94.50247498171147\n", "20200206 94.39689122447953\n", "20200207 93.96526660306931\n", "20200210 92.57102186527716\n", "20200211 91.0391361879991\n", "20200212 89.14273465512001\n", "20200213 90.5652291149273\n", "20200214 89.16266672913679\n", "20200217 87.02366960314446\n", "20200218 86.93582921411002\n", "20200219 86.38452345836215\n", "20200220 86.62199334935622\n", "20200221 85.98498661524492\n", "20200224 89.05230555956066\n", "20200225 87.06568691203636\n", "20200226 87.46321280180729\n", "20200227 86.5990387450102\n", "20200228 87.17118123229557\n", "20200302 86.71389630767548\n", "20200303 86.27409887893536\n", "20200304 86.41934747150903\n", "20200305 87.00902316848197\n", "20200306 88.61143638018807\n", "20200309 90.00418632128252\n", "20200310 88.15993653658315\n", "20200311 87.30281952012277\n", "20200312 86.23128722334133\n", "20200313 84.58437537616395\n", "20200316 81.49969255269298\n", "20200317 79.22571829980201\n" ] } ], "source": [ "jsyy=\\\n", "estimate_table(\"20190101\", \"20200317\", (\"real\", {\"F160723\": 100}), (\"est\", holdings[\"160723\"]), float_holdings=True)\n", "## 79" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "jsyy=\\\n", "estimate_table(\"20200101\", \"20200317\", (\"real\", {\"F160723\": 100}), (\"est\", holdings[\"160723\"]), float_holdings=True)\n", "# 79 所以有两个月足够了" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 调节浮动仓位的超参" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.3102546169014142 \n", "标准差偏离: 0.6595336841739304\n", "\n", "预测偏差分位: \n", "1% 分位: -1.383 \n", "5% 分位: -1.068 \n", "25% 分位: -0.07 \n", "50% 分位: -0.007 \n", "75% 分位: 0.069 \n", "95% 分位: 0.951 \n", "99% 分位: 2.482\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.06 \n", "预测涨的比实际多: 0.3 \n", "预测跌的比实际多: 0.34 \n", "预测跌的比实际少: 0.3\n" ] } ], "source": [ "analyse_all(jsyy, \"diff_real_est\") # 在仓位巨变的情况下,浮动仓位预测准确性大幅上升" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "jsyy=\\\n", "estimate_table(\"20200101\", \"20200317\", (\"real\", {\"F160723\": 100}), (\"est\", holdings[\"160723\"]))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.5139938766873343 \n", "标准差偏离: 1.0234878205057947\n", "\n", "预测偏差分位: \n", "1% 分位: -1.97 \n", "5% 分位: -0.574 \n", "25% 分位: -0.079 \n", "50% 分位: 0.05 \n", "75% 分位: 0.25 \n", "95% 分位: 1.717 \n", "99% 分位: 3.925\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.02 \n", "预测涨的比实际多: 0.34 \n", "预测跌的比实际多: 0.53 \n", "预测跌的比实际少: 0.11\n" ] } ], "source": [ "analyse_all(jsyy, \"diff_real_est\")" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20200103 98.40920667693318\n", "20200106 97.64853514951241\n", "20200107 96.62523556281496\n", "20200108 95.15401686708452\n", "20200109 93.0823814868199\n", "20200110 93.36443546932975\n", "20200113 93.11216029576838\n", "20200114 91.27013083061011\n", "20200115 92.13050838334848\n", "20200116 91.59142148486188\n", "20200117 91.06325234221197\n", "20200120 96.16646465319991\n", "20200121 99.84629272643697\n", "20200122 99.99999999999999\n", "20200123 100.00000000000003\n", "20200203 96.29661772963763\n", "20200204 92.26665630352407\n", "20200205 93.50409512818467\n", "20200206 89.94835094303721\n", "20200207 85.88148943436546\n", "20200210 83.4463279162062\n", "20200211 78.78541461154028\n", "20200212 83.13619172865776\n", "20200213 79.95897641886171\n", "20200214 76.81147545782146\n", "20200217 75.55999514008762\n", "20200218 76.4799222401603\n", "20200219 75.6369490310907\n", "20200220 75.623577001374\n", "20200221 86.32632227780684\n", "20200224 83.59299231836273\n", "20200225 85.43632206375919\n", "20200226 83.91234854574422\n", "20200227 86.9878170010383\n", "20200228 83.10182678087973\n", "20200302 82.47401722714106\n", "20200303 83.38535021874404\n", "20200304 86.13184793730808\n", "20200305 90.40385072561526\n", "20200306 95.30399675201251\n", "20200309 91.9347292911875\n", "20200310 89.8274011221422\n", "20200311 86.08059020624405\n", "20200312 80.03967138790792\n", "20200313 70.07407118095644\n", "20200316 64.53761156946534\n", "20200317 62.16971931143681\n" ] } ], "source": [ "jsyy=\\\n", "estimate_table(\"20200101\", \"20200317\", (\"real\", {\"F160723\": 100}),\n", " (\"est\", holdings[\"160723\"]), float_holdings=True, window=4, decay=0.65, smooth=0.6)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.3030265858700378 \n", "标准差偏离: 0.6305422687037667\n", "\n", "预测偏差分位: \n", "1% 分位: -1.448 \n", "5% 分位: -0.29 \n", "25% 分位: -0.071 \n", "50% 分位: -0.006 \n", "75% 分位: 0.111 \n", "95% 分位: 1.297 \n", "99% 分位: 2.282\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.11 \n", "预测涨的比实际多: 0.26 \n", "预测跌的比实际多: 0.38 \n", "预测跌的比实际少: 0.26\n" ] } ], "source": [ "analyse_all(jsyy, \"diff_real_est\") # 2020 SOTA 0.26 0.53 window=3, decay=0.7, smooth=0.6\n", "#2019 SOTA 0.09 0.135 window=4, decay=0.65, smooth=0.2" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20200103 93.45665232707752\n", "20200106 93.51791428472517\n", "20200107 93.5520801679379\n", "20200108 93.66822034810588\n", "20200109 93.30883359089702\n", "20200110 92.58731785691867\n", "20200113 91.69075781817025\n", "20200114 89.60552641051424\n", "20200115 88.69825004960039\n", "20200116 88.2053335988735\n", "20200117 87.91742762155187\n", "20200120 90.39437404355327\n", "20200121 91.96893139296083\n", "20200122 92.95730053434691\n", "20200123 95.8519902704606\n", "20200203 95.26656120900611\n", "20200204 92.29800617194215\n", "20200205 92.37057678788422\n", "20200206 90.81708290720317\n", "20200207 87.13760505389847\n", "20200210 85.85909407258791\n", "20200211 82.9640141805221\n", "20200212 83.06792602093726\n", "20200213 79.60366262516155\n", "20200214 77.42560684761044\n", "20200217 76.49471700799037\n", "20200218 77.00261147197513\n", "20200219 77.75406518838784\n", "20200220 80.54352300480772\n", "20200221 82.47785873914064\n", "20200224 81.06854400234613\n", "20200225 83.30907585665355\n", "20200226 82.21006333173082\n", "20200227 84.45929050470774\n", "20200228 88.04085927902116\n", "20200302 86.828801560879\n", "20200303 87.06842714989392\n", "20200304 87.20393662286146\n", "20200305 85.5647472762889\n", "20200306 86.10974509660099\n", "20200309 85.65184977513036\n", "20200310 83.40536321307515\n", "20200311 80.80832316630564\n", "20200312 78.9781298662052\n", "20200313 73.55456223974132\n", "20200316 73.28924350637332\n", "20200317 74.40963966730133\n", "20200318 75.86500405727163\n" ] } ], "source": [ "nfyy=\\\n", "estimate_table(\"20200101\", \"20200318\", (\"real\", {\"F501018\": 100}),\n", " (\"est\", holdings[\"501018\"]), float_holdings=True, window=4, decay=0.65, smooth=0.2)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.42852850497808187 \n", "标准差偏离: 0.870009892781402\n", "\n", "预测偏差分位: \n", "1% 分位: -3.637 \n", "5% 分位: -0.763 \n", "25% 分位: -0.219 \n", "50% 分位: 0.006 \n", "75% 分位: 0.123 \n", "95% 分位: 0.825 \n", "99% 分位: 1.407\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.19 \n", "预测涨的比实际多: 0.21 \n", "预测跌的比实际多: 0.35 \n", "预测跌的比实际少: 0.25\n" ] } ], "source": [ "analyse_all(nfyy, \"diff_real_est\") # SOTA 2019 0.129 0.188 window = 5\n", "# SOTA 2020 0.41 0.83 window=5/4, decay=0.65, smooth=0.2" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [], "source": [ "jsyy=\\\n", "estimate_table_with_pos(\"20200101\", \"20200317\", (\"real\", {\"F160723\": 100}),\n", " (\"est\", holdings[\"160723\"]), pos=0.94, window=3, decay=0.8)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.2538763052395668 \n", "标准差偏离: 0.4870486171347755\n", "\n", "预测偏差分位: \n", "1% 分位: -1.225 \n", "5% 分位: -0.249 \n", "25% 分位: -0.056 \n", "50% 分位: -0.005 \n", "75% 分位: 0.132 \n", "95% 分位: 1.166 \n", "99% 分位: 1.507\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.15 \n", "预测涨的比实际多: 0.21 \n", "预测跌的比实际多: 0.34 \n", "预测跌的比实际少: 0.3\n" ] } ], "source": [ "analyse_all(jsyy, \"diff_real_est\") \n", "# 20200101 0317 SOTA 0.26 0.53 window=3, decay=0.7, smooth=0.6\n", "#2019 SOTA 0.09 0.135 window=4, decay=0.65, smooth=0.2\n", "\n", "## new approach 2020 0.254 0.487 window=3, decay=0.8" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "def smooth_pos(r, e, o):\n", " pos = r/e\n", " if pos <= 0:\n", " return o\n", " if pos > 1:\n", " pos = 1\n", " elif pos < 0.5:\n", " pos = pos ** 0.6\n", "\n", " if abs(r) < 1:\n", " pos = (pos + (3-3*abs(r))*o)/(4-3*abs(r))\n", "\n", " return pos\n", "\n", "gtsp=\\\n", "estimate_table_with_pos(\"20200101\", \"20200324\", (\"real\", {\"F160216\": 100}),\n", " (\"est\", holdings[\"160216\"]), pos=0.94, window=3, decay=0.8, smooth=smooth_pos)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "净值预测回测分析:\n", "\n", "\n", "平均偏离: 0.28339514803858534 \n", "标准差偏离: 0.4909722047292374\n", "\n", "预测偏差分位: \n", "1% 分位: -1.736 \n", "5% 分位: -0.682 \n", "25% 分位: -0.152 \n", "50% 分位: -0.053 \n", "75% 分位: 0.166 \n", "95% 分位: 0.498 \n", "99% 分位: 1.167\n", "\n", "涨跌偏差分析: \n", "预测涨的比实际少: 0.1 \n", "预测涨的比实际多: 0.27 \n", "预测跌的比实际多: 0.33 \n", "预测跌的比实际少: 0.31\n" ] } ], "source": [ "analyse_all(gtsp, \"diff_real_est\") # 0.283 0.49" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " fig.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('