{
"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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sns.distplot(list(nfyy[\"diff_real_est\"]))"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20190103 93.65447840120115\n",
"20190104 93.76823121561948\n",
"20190107 94.05076945846218\n",
"20190108 94.53989917677045\n",
"20190109 94.11820866519953\n",
"20190110 94.64001499429096\n",
"20190111 95.01318002397532\n",
"20190114 95.25781689416775\n",
"20190115 95.37636538508443\n",
"20190116 95.89909419328484\n",
"20190117 96.16955163653688\n",
"20190118 96.78493705275066\n",
"20190121 95.53522415568376\n",
"20190122 97.60285032195961\n",
"20190123 97.48150922483097\n",
"20190124 97.95451655511893\n",
"20190125 95.19108172819183\n",
"20190128 94.92971071063164\n",
"20190129 99.23867589460771\n",
"20190130 99.99999999999999\n",
"20190131 98.05699339075981\n",
"20190201 98.09306296337613\n",
"20190211 99.43323887302371\n",
"20190212 98.79898994873756\n",
"20190213 100.0\n",
"20190214 99.88131044941427\n",
"20190215 97.54548021289017\n",
"20190218 96.81871135841469\n",
"20190219 96.85780233934933\n",
"20190220 96.18607396169415\n",
"20190221 95.20892124329548\n",
"20190222 98.04444503632847\n",
"20190225 98.82618970060442\n",
"20190226 99.74365338954891\n",
"20190227 99.88341766288124\n",
"20190228 97.79486699295909\n",
"20190301 97.9267567344947\n",
"20190304 97.22425910330642\n",
"20190305 95.910365587022\n",
"20190306 99.677261324912\n",
"20190307 99.70610533639068\n",
"20190308 99.73006234036976\n",
"20190311 98.48235023896858\n",
"20190312 97.17880132302501\n",
"20190313 97.24206087176712\n",
"20190314 97.55092228258414\n",
"20190315 96.9968877421884\n",
"20190318 97.2642396934006\n",
"20190319 95.76353792613384\n",
"20190320 95.03649155186946\n",
"20190321 99.28667723693033\n",
"20190322 100.0\n",
"20190325 97.26758745518102\n",
"20190326 97.22956716530584\n",
"20190327 98.25122281496479\n",
"20190328 97.69119912494916\n",
"20190329 98.24944018530445\n",
"20190401 97.2655757717525\n",
"20190402 94.92986604939438\n",
"20190403 96.97053718680385\n",
"20190404 97.29466665083085\n",
"20190408 96.56394659904025\n",
"20190409 95.12076266114163\n",
"20190410 99.54866286929146\n",
"20190411 97.34492825871405\n",
"20190412 97.05319234562067\n",
"20190415 97.08903536812161\n",
"20190416 99.66931470619767\n",
"20190417 97.09807906328555\n",
"20190418 97.3674593564218\n",
"20190419 97.75067893718972\n",
"20190422 96.31677770505277\n",
"20190423 99.7456463679329\n",
"20190424 97.29559820684318\n",
"20190425 97.34794273496203\n",
"20190426 96.56678467597736\n",
"20190429 98.18792380617393\n",
"20190430 96.98676331581645\n",
"20190506 96.97707912513957\n",
"20190507 97.45493167445436\n",
"20190508 97.34696862103556\n",
"20190509 97.77563019023681\n",
"20190510 97.04496011674127\n",
"20190513 98.02975261251883\n",
"20190514 97.82441459690898\n",
"20190515 98.21099449242307\n",
"20190516 99.29871656216373\n",
"20190517 97.06110455867623\n",
"20190520 96.81916834152487\n",
"20190521 96.64960751612622\n",
"20190522 96.69020279852819\n",
"20190523 98.31308768611812\n",
"20190524 97.38832881255135\n",
"20190527 97.86828259790853\n",
"20190528 96.5912768154747\n",
"20190529 96.89881578774067\n",
"20190530 96.02619248542368\n",
"20190531 94.94339208437005\n",
"20190603 95.50038724369004\n",
"20190604 95.54016086505129\n",
"20190605 94.92232794114467\n",
"20190606 94.60054685289953\n",
"20190610 95.09069980155203\n",
"20190611 94.64074509798412\n",
"20190612 93.92377156151373\n",
"20190613 92.36677989687628\n",
"20190614 90.35476304146071\n",
"20190617 92.80031961840648\n",
"20190618 91.57712120181685\n",
"20190619 88.73651192026911\n",
"20190620 90.6266989984932\n",
"20190621 90.94905787491375\n",
"20190624 86.8984549750862\n",
"20190625 89.13707760107751\n",
"20190626 88.19573473409837\n",
"20190627 86.193553807514\n",
"20190628 87.31761706781599\n",
"20190701 88.7126826435418\n",
"20190702 86.99455046722262\n",
"20190703 91.61360179373837\n",
"20190704 91.20687271101387\n",
"20190705 89.88506640021727\n",
"20190708 89.49018822907516\n",
"20190709 90.5829462453055\n",
"20190710 91.45145770069323\n",
"20190711 91.76566273402348\n",
"20190712 93.01815381346235\n",
"20190715 92.0529107534956\n",
"20190716 92.39395313070543\n",
"20190717 95.4927001778484\n",
"20190718 95.72099521157816\n",
"20190719 94.37243547210491\n",
"20190722 93.37631319903207\n",
"20190723 94.29359518960005\n",
"20190724 94.40723944875124\n",
"20190725 93.2401060718849\n",
"20190726 94.97339927669387\n",
"20190729 95.67755132643181\n",
"20190730 96.95019995381554\n",
"20190731 95.64545374205018\n",
"20190801 97.47337469256973\n",
"20190802 95.34874853711882\n",
"20190805 92.46198670127994\n",
"20190806 93.6892092718156\n",
"20190807 90.96927337234254\n",
"20190808 92.597958601564\n",
"20190809 94.5280349695812\n",
"20190812 93.95185998709634\n",
"20190813 94.372670876075\n",
"20190814 95.56542205334783\n",
"20190815 95.4689973624791\n",
"20190816 94.37564982805695\n",
"20190819 94.37473776832783\n",
"20190820 92.57042893710461\n",
"20190821 93.92451406638433\n",
"20190822 94.1518889901149\n",
"20190823 93.54810925839877\n",
"20190826 92.3462625679791\n",
"20190827 96.1781884803884\n",
"20190828 94.29082369861767\n",
"20190829 92.0820053904515\n",
"20190830 96.73283452097574\n",
"20190902 95.84689202758707\n",
"20190903 96.6585506750083\n",
"20190904 94.53016196357505\n",
"20190905 95.06086545976878\n",
"20190906 95.63934716699639\n",
"20190909 93.9185564684255\n",
"20190910 91.33369685645081\n",
"20190911 92.04587774349261\n",
"20190912 92.29873488458942\n",
"20190916 89.8062935972953\n",
"20190917 92.84202798415694\n",
"20190918 92.43462942072114\n",
"20190919 92.65539111715827\n",
"20190920 92.0030470546091\n",
"20190923 91.99574275659674\n",
"20190924 91.30980344265903\n",
"20190925 90.31933336019264\n",
"20190926 91.6965813233175\n",
"20190927 93.77747478219858\n",
"20190930 93.93861267813871\n",
"20191008 95.05851983054474\n",
"20191009 95.55991963262576\n",
"20191010 94.70424775458869\n",
"20191011 94.6969707103485\n",
"20191014 94.29658193425988\n",
"20191015 96.6062426617909\n",
"20191016 94.43332767890877\n",
"20191017 97.1792516244434\n",
"20191018 98.12971030563334\n",
"20191021 97.23484104484713\n",
"20191022 95.78929298734353\n",
"20191023 93.06633431698712\n",
"20191024 95.55803640601529\n",
"20191025 96.11727742428688\n",
"20191028 96.97345160533871\n",
"20191029 95.1065951667566\n",
"20191030 97.77365492700207\n",
"20191031 98.90704936769589\n",
"20191101 96.7042475431054\n",
"20191104 96.69010848130661\n",
"20191105 97.29105955270792\n",
"20191106 97.42871187856204\n",
"20191107 96.14174595525786\n",
"20191108 96.31767336729915\n",
"20191111 95.23706242329341\n",
"20191112 94.14125511324706\n",
"20191113 91.31230590034812\n",
"20191114 92.7772778901933\n",
"20191115 96.32989503776075\n",
"20191118 95.53071837954214\n",
"20191119 93.94249844826365\n",
"20191120 95.897485484709\n",
"20191121 96.7880822033708\n",
"20191122 97.30504310982323\n",
"20191125 96.52198605165165\n",
"20191126 97.21906783717331\n",
"20191127 98.74032174508467\n",
"20191128 95.64329361518152\n",
"20191129 98.84223338520515\n",
"20191202 98.70002215364413\n",
"20191203 96.23953650463594\n",
"20191204 98.56818493020346\n",
"20191205 99.89892000006923\n",
"20191206 100.0\n",
"20191209 96.4295523744025\n",
"20191210 96.6857211169727\n",
"20191211 97.05476895234965\n",
"20191212 96.13421830760592\n",
"20191213 95.83913594233506\n",
"20191216 96.14701380033414\n",
"20191217 95.92273916839407\n",
"20191218 95.66556386285724\n",
"20191219 95.29111164999073\n",
"20191220 95.81072254375701\n",
"20191223 96.07298677177356\n",
"20191224 96.60003896437877\n",
"20191225 97.5819208138721\n",
"20191226 97.84674157882897\n",
"20191227 95.25260494493743\n",
"20191230 95.04374116451405\n",
"20191231 93.39947491687953\n",
"20200102 95.9293452802388\n",
"20200103 95.42913983761969\n",
"20200106 95.19868841266171\n",
"20200107 94.45171419684551\n",
"20200108 95.21723086463876\n",
"20200109 94.69168571149305\n",
"20200110 94.01272653305185\n",
"20200113 93.22840730938807\n",
"20200114 91.91960191526093\n",
"20200115 91.09947833666325\n",
"20200116 90.5875588334005\n",
"20200117 90.2306879696269\n",
"20200120 91.84029226085877\n",
"20200121 92.78298381270122\n",
"20200122 93.40135790797162\n",
"20200123 95.40129389475801\n",
"20200203 94.90541443824806\n",
"20200204 92.91172553278456\n",
"20200205 92.99172599134864\n",
"20200206 91.9742058693125\n",
"20200207 89.38378390276563\n",
"20200210 88.38204421968352\n",
"20200211 86.33783563050063\n",
"20200212 86.21365976322114\n",
"20200213 83.63014517928202\n",
"20200214 81.94941288202585\n",
"20200217 81.10958478007417\n",
"20200218 81.43897868407669\n",
"20200219 81.72217402800237\n",
"20200220 83.80321465736029\n",
"20200221 84.86542458438068\n",
"20200224 83.56560513230932\n",
"20200225 85.24253120634647\n",
"20200226 84.2307250076849\n",
"20200227 85.68627678672918\n",
"20200228 88.06714633732634\n",
"20200302 87.10920353481362\n",
"20200303 87.19342918748322\n",
"20200304 87.34939717091778\n",
"20200305 86.30072845206428\n"
]
}
],
"source": [
"nfyy = \\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F501018\": 100}), (\"est\", holdings[\"501018\"]),\n",
" float_holdings=True, window=4, decay=0.65, smooth=0.2)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.1311503341239221 \n",
"标准差偏离: 0.19041538962622517\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.614 \n",
"5% 分位: -0.331 \n",
"25% 分位: -0.086 \n",
"50% 分位: -0.001 \n",
"75% 分位: 0.081 \n",
"95% 分位: 0.292 \n",
"99% 分位: 0.408\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.29 \n",
"预测涨的比实际多: 0.26 \n",
"预测跌的比实际多: 0.2 \n",
"预测跌的比实际少: 0.25\n"
]
}
],
"source": [
"analyse_all(nfyy, \"diff_real_est\") ## 略改善"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"nfyy = \\\n",
"estimate_table_with_pos(\"20190101\", \"20200305\", (\"real\", {\"F501018\": 100}), (\"est\", holdings[\"501018\"]),\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.13673210395925103 \n",
"标准差偏离: 0.19784112535689902\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.717 \n",
"5% 分位: -0.305 \n",
"25% 分位: -0.098 \n",
"50% 分位: -0.005 \n",
"75% 分位: 0.09 \n",
"95% 分位: 0.325 \n",
"99% 分位: 0.463\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.31 \n",
"预测涨的比实际多: 0.24 \n",
"预测跌的比实际多: 0.17 \n",
"预测跌的比实际少: 0.28\n"
]
}
],
"source": [
"analyse_all(nfyy, \"diff_real_est\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 华宝油气"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"hbyq = \\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F162411\": 100}), (\"est\", holdings[\"162411\"]))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.11067137837382375 \n",
"标准差偏离: 0.1466540526054426\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.351 \n",
"5% 分位: -0.235 \n",
"25% 分位: -0.092 \n",
"50% 分位: -0.005 \n",
"75% 分位: 0.083 \n",
"95% 分位: 0.183 \n",
"99% 分位: 0.487\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.27 \n",
"预测涨的比实际多: 0.25 \n",
"预测跌的比实际多: 0.22 \n",
"预测跌的比实际少: 0.27\n"
]
}
],
"source": [
"analyse_all(hbyq, \"diff_real_est\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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": [
"hbyq.plot(x=\"date\", y=[\"estpos\", \"estposld\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 易方达原油"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"yfd = \\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F161129\": 100}), (\"est\", holdings[\"161129\"]))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.14551683914747404 \n",
"标准差偏离: 0.7442584240573993\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.868 \n",
"5% 分位: -0.187 \n",
"25% 分位: -0.035 \n",
"50% 分位: -0.006 \n",
"75% 分位: 0.019 \n",
"95% 分位: 0.182 \n",
"99% 分位: 0.867\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.31 \n",
"预测涨的比实际多: 0.27 \n",
"预测跌的比实际多: 0.13 \n",
"预测跌的比实际少: 0.29\n"
]
}
],
"source": [
"analyse_all(yfd, \"diff_real_est\") # 预测性超级差。。。"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20190103 94.35909958505378\n",
"20190104 94.29885283914926\n",
"20190107 94.10632834706732\n",
"20190108 93.89849682024727\n",
"20190109 94.15037825949103\n",
"20190110 93.00084911973276\n",
"20190111 92.80688500803618\n",
"20190114 91.89251253268004\n",
"20190115 90.7209254541996\n",
"20190116 93.85015008877576\n",
"20190117 93.30485137355123\n",
"20190118 93.0799361926993\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": [
"20190122 92.41406480317406\n",
"20190123 93.05723899047692\n",
"20190124 92.48351232237006\n",
"20190125 91.63841277969334\n",
"20190128 93.5172298621616\n",
"20190129 94.05332730641469\n",
"20190130 93.25565046779455\n",
"20190131 94.32575521391422\n",
"20190201 95.34466257496861\n",
"20190211 95.6196933962445\n",
"20190212 96.55421127268163\n",
"20190213 95.249406326432\n",
"20190214 95.32618006003132\n",
"20190215 95.51157085462339\n",
"20190218 95.5115708546234\n",
"20190219 96.33954801413765\n",
"20190220 96.31725936088765\n",
"20190221 97.8374073501235\n",
"20190222 99.86387197067124\n",
"20190225 98.54989786651848\n",
"20190226 99.99999999999999\n",
"20190227 100.00000000000001\n",
"20190228 98.15642549634896\n",
"20190301 99.10152029623447\n",
"20190304 97.20169470469563\n",
"20190305 96.22849433865704\n",
"20190306 98.14563572782856\n",
"20190307 97.45710954771694\n",
"20190308 95.14178881113892\n",
"20190311 96.47669566742192\n",
"20190312 96.86401749678036\n",
"20190313 96.28949704252715\n",
"20190314 95.042075350783\n",
"20190315 94.61042753395493\n",
"20190318 94.73237563959655\n",
"20190319 93.73461731412577\n",
"20190320 95.03048711287062\n",
"20190321 95.89286265462322\n",
"20190322 96.37743411169903\n",
"20190325 95.8203300047816\n",
"20190326 96.80177782429293\n",
"20190327 97.30953806778044\n",
"20190328 97.77804720699457\n",
"20190329 96.43607096549842\n",
"20190401 96.94866313730589\n",
"20190402 96.32083417523646\n",
"20190403 96.34280476591738\n",
"20190404 95.84543373988521\n",
"20190408 95.69950798285743\n",
"20190409 95.61370390373065\n",
"20190410 96.43941615016551\n",
"20190411 96.37673977430387\n",
"20190412 96.87893669514985\n",
"20190415 95.85852778255014\n",
"20190416 96.12931581177668\n",
"20190417 96.00455619854735\n",
"20190418 95.85258995695013\n",
"20190419 95.85258995695011\n",
"20190423 95.89176907256666\n",
"20190424 95.74638958039057\n",
"20190425 97.10102943372249\n",
"20190426 97.76110288580934\n",
"20190429 94.74679969089537\n",
"20190430 95.79928803916243\n",
"20190507 96.92430580427585\n",
"20190508 97.10644077360833\n",
"20190509 97.44583521878847\n",
"20190510 98.85795542204264\n",
"20190513 97.17126744328736\n",
"20190514 97.40781012699068\n",
"20190515 97.48073964903145\n",
"20190516 97.34338046843965\n",
"20190517 96.88095305914\n",
"20190520 96.60736733660401\n",
"20190521 96.12732710876901\n",
"20190522 95.6870435825128\n",
"20190523 96.18672340649447\n",
"20190524 96.07005486549615\n",
"20190527 96.07005486549613\n",
"20190528 95.18110550114392\n",
"20190529 94.72237322738344\n",
"20190530 94.34449963138448\n",
"20190531 93.31944473161617\n",
"20190603 94.84403646145948\n",
"20190604 94.61896579330052\n",
"20190605 94.27761484209582\n",
"20190606 94.28442416708002\n",
"20190610 94.60628196965233\n",
"20190611 94.33155212157145\n",
"20190612 93.97898232329172\n",
"20190613 93.75044037405998\n",
"20190614 93.81138741057767\n",
"20190617 93.9381569108144\n",
"20190618 93.8117594124685\n",
"20190619 93.42417056005314\n",
"20190620 93.77148632004061\n",
"20190621 93.20682106226752\n",
"20190624 92.43735135236844\n",
"20190625 93.40793733502444\n",
"20190626 93.20479176195798\n",
"20190627 93.37429953460345\n",
"20190628 93.56174450369166\n",
"20190701 93.22816147572496\n",
"20190702 93.15500688166445\n",
"20190705 94.29822092873485\n",
"20190708 94.40109561441739\n",
"20190709 95.21853794024501\n",
"20190710 96.12931493552033\n",
"20190711 94.43638978318181\n",
"20190712 95.3181012082776\n",
"20190715 95.31810120827762\n",
"20190716 96.34479473672125\n",
"20190717 97.0930440807886\n",
"20190718 98.51289398464245\n",
"20190719 98.83301947063522\n",
"20190722 96.86555794978257\n",
"20190723 96.98499870391429\n",
"20190724 97.43897820329185\n",
"20190725 96.95146807725\n",
"20190726 96.72226427660938\n",
"20190729 97.29184188167902\n",
"20190730 97.1452440997865\n",
"20190731 96.84685551678687\n",
"20190801 96.24545569887198\n",
"20190802 95.89416288292281\n",
"20190805 95.57089162340272\n",
"20190806 95.88251159746797\n",
"20190807 95.27411995301162\n",
"20190808 96.09977762820212\n",
"20190809 96.07525878877263\n",
"20190812 96.07525878877263\n",
"20190813 95.75938374244843\n",
"20190814 96.05652654800512\n",
"20190815 96.48877821148811\n",
"20190816 96.86989110662671\n",
"20190819 96.24703842938631\n",
"20190820 95.74343927199885\n",
"20190821 95.88833926258054\n",
"20190822 95.76260303083676\n",
"20190823 95.25093358180679\n",
"20190826 95.25093358180678\n",
"20190827 94.76445879207867\n",
"20190828 94.39804272166569\n",
"20190829 93.60009664493104\n",
"20190830 92.04883123175058\n",
"20190902 92.0488312317506\n",
"20190903 95.15738548588136\n",
"20190904 95.45374605728765\n",
"20190905 95.60509300113304\n",
"20190906 96.4555808861729\n",
"20190909 95.94133604690643\n",
"20190910 96.4184396951337\n",
"20190911 97.89104744898653\n",
"20190912 96.45789640593146\n",
"20190916 96.45789640593144\n",
"20190917 96.47737990684418\n",
"20190918 96.90875257307941\n",
"20190919 97.25341433460801\n",
"20190920 96.95611520097317\n",
"20190924 97.45357728941094\n",
"20190925 97.66423250459536\n",
"20190926 97.62652741605363\n",
"20190927 97.66502840951415\n",
"20190930 97.14721419342193\n",
"20191008 96.78433371784277\n",
"20191009 97.34096975146996\n",
"20191010 96.86083664624007\n",
"20191011 96.29631275972383\n",
"20191014 96.03394510236447\n",
"20191015 95.8944451977319\n",
"20191016 95.65605542696386\n",
"20191017 95.90502045358218\n",
"20191018 96.47199725606039\n",
"20191021 97.40570228583067\n",
"20191022 98.29775422436394\n",
"20191023 99.76010008006654\n",
"20191024 97.92495776254424\n",
"20191025 97.78337661508776\n",
"20191028 97.90196962854534\n",
"20191029 97.7126513969212\n",
"20191030 97.65930436689601\n",
"20191031 97.9191286569897\n",
"20191101 97.34406535311541\n",
"20191104 97.79078185626517\n",
"20191105 97.45084507710575\n",
"20191106 97.44875003920454\n",
"20191107 97.30418686298403\n",
"20191108 97.12808091153606\n",
"20191111 98.52144629173199\n",
"20191112 98.11728707553648\n",
"20191113 95.65583648133916\n",
"20191114 96.59209762703016\n",
"20191115 96.32651750312677\n",
"20191118 97.49231188424119\n",
"20191119 96.54443436282175\n",
"20191120 97.20342455079131\n",
"20191121 97.25952552216869\n",
"20191122 98.03313391555562\n",
"20191125 98.5927175430088\n",
"20191126 99.46659472029906\n",
"20191127 99.99999999999999\n",
"20191128 98.58133823047515\n",
"20191129 98.41807289499026\n",
"20191202 98.38302413074949\n",
"20191203 97.7949389127\n",
"20191204 96.10660689310619\n",
"20191205 94.31110435302892\n",
"20191206 97.39961105673119\n",
"20191209 95.79450150768811\n",
"20191210 94.53951335553364\n",
"20191211 94.72441280016075\n",
"20191212 95.00528507296845\n",
"20191213 95.0765961710922\n",
"20191216 94.73999726282348\n",
"20191217 94.37641993281208\n",
"20191218 94.39419542398882\n",
"20191219 94.18391638706828\n",
"20191220 94.89673405087817\n",
"20191223 95.49488959515517\n",
"20191224 95.7304844825557\n",
"20191225 95.76860686145623\n",
"20191226 96.59062343660524\n",
"20191227 93.56498838575334\n",
"20191230 92.86972299258287\n",
"20191231 94.77148877641264\n",
"20200102 95.12771238386411\n",
"20200103 94.42455847606539\n",
"20200106 94.04425148204508\n",
"20200107 94.75308318851387\n",
"20200108 94.8060046038879\n",
"20200109 94.53046659413491\n",
"20200110 94.68362225865964\n",
"20200113 94.72964490781683\n",
"20200114 94.6701582996827\n",
"20200115 94.84085363638393\n",
"20200116 94.74885934096335\n",
"20200117 94.51897180003246\n",
"20200120 94.60047161414033\n",
"20200121 94.86655359716906\n",
"20200122 95.03328182529155\n",
"20200123 96.33323217595849\n",
"20200203 94.88895387307295\n",
"20200204 95.55097367578851\n",
"20200205 96.02642103118973\n",
"20200206 95.9538501948689\n",
"20200207 95.87891175431673\n",
"20200210 93.34168332861374\n",
"20200211 91.80150986545213\n",
"20200212 93.24071644708069\n",
"20200213 93.11811494614359\n",
"20200214 92.27895110072691\n",
"20200217 91.22883572242804\n",
"20200218 90.63122091946846\n",
"20200219 90.63458670651661\n",
"20200220 89.8378833362987\n",
"20200221 92.0480330667887\n",
"20200224 90.99647356183755\n",
"20200225 91.27361223203862\n",
"20200226 91.55897733230482\n",
"20200227 91.99314687410877\n",
"20200228 91.36881685171706\n",
"20200302 91.10239704904794\n",
"20200303 90.25495471096008\n",
"20200304 90.25495471096006\n",
"20200305 89.26949937351488\n"
]
}
],
"source": [
"yfd = \\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F161129\": 100}), (\"est\", holdings[\"161129\"]), \n",
" float_holdings=True, window=4, decay=0.65, smooth=0.2)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.14552664698264714 \n",
"标准差偏离: 0.7525197347974537\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.852 \n",
"5% 分位: -0.2 \n",
"25% 分位: -0.037 \n",
"50% 分位: -0.007 \n",
"75% 分位: 0.015 \n",
"95% 分位: 0.186 \n",
"99% 分位: 0.876\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.24 \n",
"预测涨的比实际多: 0.34 \n",
"预测跌的比实际多: 0.17 \n",
"预测跌的比实际少: 0.25\n"
]
}
],
"source": [
"analyse_all(yfd, \"diff_real_est\") # 无明显变化"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"yfd = \\\n",
"estimate_table_with_pos(\"20190101\", \"20200305\", (\"real\", {\"F161129\": 100}), (\"est\", holdings[\"161129\"]))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.15115824932756056 \n",
"标准差偏离: 0.7545173942148973\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.871 \n",
"5% 分位: -0.22 \n",
"25% 分位: -0.039 \n",
"50% 分位: -0.011 \n",
"75% 分位: 0.019 \n",
"95% 分位: 0.183 \n",
"99% 分位: 0.867\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.26 \n",
"预测涨的比实际多: 0.32 \n",
"预测跌的比实际多: 0.12 \n",
"预测跌的比实际少: 0.3\n"
]
}
],
"source": [
"analyse_all(yfd, \"diff_real_est\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20200303 93.67356452261032\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"../lof/predict.py:119: RuntimeWarning: invalid value encountered in double_scalars\n",
" ratio = realf / estf\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"20200305 92.67795478801658\n",
"20200306 91.28124775416539\n",
"20200309 89.02570039922193\n",
"20200310 88.61025671965129\n",
"20200311 86.77313584841382\n",
"20200312 83.4295867629937\n",
"20200313 78.83150991223815\n",
"20200316 79.50797422018019\n",
"20200317 80.31698721386726\n",
"20200318 80.72404430865531\n",
"20200319 80.18286853978816\n"
]
}
],
"source": [
"yfd = \\\n",
"estimate_table(\"20200301\", \"20200319\", (\"real\", {\"F161129\": 100}), (\"est\", holdings[\"161129\"]),\n",
" float_holdings=True, window=4, decay=0.65, smooth=0.3)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.7886797773559008 \n",
"标准差偏离: 1.0781728431273583\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -2.325 \n",
"5% 分位: -2.237 \n",
"25% 分位: -0.427 \n",
"50% 分位: -0.059 \n",
"75% 分位: 0.476 \n",
"95% 分位: 1.039 \n",
"99% 分位: 1.123\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.0 \n",
"预测涨的比实际多: 0.31 \n",
"预测跌的比实际多: 0.46 \n",
"预测跌的比实际少: 0.23\n"
]
}
],
"source": [
"analyse_all(yfd, \"diff_real_est\") # 无明显变化"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 嘉实原油"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"jsyy=\\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F160723\": 100}), (\"est\", holdings[\"160723\"]))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.09408298326797263 \n",
"标准差偏离: 0.14917499656769317\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.456 \n",
"5% 分位: -0.213 \n",
"25% 分位: -0.064 \n",
"50% 分位: -0.012 \n",
"75% 分位: 0.044 \n",
"95% 分位: 0.215 \n",
"99% 分位: 0.53\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.17 \n",
"预测涨的比实际多: 0.37 \n",
"预测跌的比实际多: 0.25 \n",
"预测跌的比实际少: 0.22\n"
]
}
],
"source": [
"analyse_all(jsyy, \"diff_real_est\") # 原油基金中,预测性出奇的好!"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20190103 99.29822206419422\n",
"20190104 99.55141988127797\n",
"20190107 99.91715215427388\n",
"20190108 100.0\n",
"20190109 97.04302618289408\n",
"20190110 97.01306569271324\n",
"20190111 97.42733639604523\n",
"20190114 97.31777302131505\n",
"20190115 96.18560083382205\n",
"20190116 97.37304524645579\n",
"20190117 97.42899994466062\n",
"20190118 97.17151600200435\n",
"20190121 96.60258546561575\n",
"20190122 96.89622685352987\n",
"20190123 97.48382711824088\n",
"20190124 97.7158223583916\n",
"20190125 97.91814704297691\n",
"20190128 98.91134407088727\n",
"20190129 97.56537108756571\n",
"20190130 98.75953232487856\n",
"20190131 97.99707843647612\n",
"20190201 97.94145473010512\n",
"20190211 98.06772184145757\n",
"20190212 99.1147382314877\n",
"20190213 98.31819463430388\n",
"20190214 98.16233428359024\n",
"20190215 97.9456955014084\n",
"20190218 98.47354745038493\n",
"20190219 97.68249289374225\n",
"20190220 96.13891186443742\n",
"20190221 95.6777939971009\n",
"20190222 95.3225788116609\n",
"20190225 97.52902887402169\n",
"20190226 96.89526679764079\n",
"20190227 96.63591880698213\n",
"20190228 96.14939796309372\n",
"20190301 96.10610153546028\n",
"20190304 96.3429849359328\n",
"20190305 95.12982365228417\n",
"20190306 95.3217691072385\n",
"20190307 94.47219739359564\n",
"20190308 93.58853761126181\n",
"20190311 95.49106687117538\n",
"20190312 95.5744632070197\n",
"20190313 95.19613699385104\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": [
"20190314 95.19613699385104\n",
"20190315 95.18827485157277\n",
"20190318 95.61157032652739\n",
"20190319 95.22867173457877\n",
"20190320 94.71961657964133\n",
"20190321 94.78118433445081\n",
"20190322 94.25725292239981\n",
"20190325 95.40630461750901\n",
"20190326 95.41281910498653\n",
"20190327 95.47507865724744\n",
"20190328 95.69359766771962\n",
"20190329 96.7119258056437\n",
"20190401 96.65426854958397\n",
"20190402 97.82774113301839\n",
"20190403 97.52144720415048\n",
"20190404 98.74753157300331\n",
"20190408 100.00000000000003\n",
"20190409 99.99999999999996\n",
"20190410 98.19327132100236\n",
"20190411 97.63001463753244\n",
"20190412 97.55377492417888\n",
"20190415 97.70854685306479\n",
"20190416 98.14385388550076\n",
"20190417 96.50886340613107\n",
"20190418 96.73555384199035\n",
"20190419 95.94343687627003\n",
"20190422 94.01396360368217\n",
"20190423 96.97130889595313\n",
"20190424 97.45384717956773\n",
"20190425 97.99683903635812\n",
"20190426 97.15247139724033\n",
"20190429 99.7487869746339\n",
"20190430 97.22759983873404\n",
"20190506 97.37395800139527\n",
"20190507 97.73376060478941\n",
"20190508 98.55141388554233\n",
"20190509 98.5817331205865\n",
"20190510 97.681583993134\n",
"20190513 98.64749371462385\n",
"20190514 98.72956581501182\n",
"20190515 99.17373727716145\n",
"20190516 100.0\n",
"20190517 97.43967945611652\n",
"20190520 96.94338453749727\n",
"20190521 96.84476284504541\n",
"20190522 96.24793881263668\n",
"20190523 97.584554731748\n",
"20190524 96.59003648882724\n",
"20190527 97.25867694766724\n",
"20190528 96.909906612747\n",
"20190529 97.21294644895089\n",
"20190530 97.36800831849857\n",
"20190531 95.04079963524836\n",
"20190603 95.62076309986318\n",
"20190604 95.86137642452107\n",
"20190605 95.30264649377428\n",
"20190606 94.37447450328995\n",
"20190610 94.21647404543306\n",
"20190611 94.03894174325923\n",
"20190612 93.49898054491793\n",
"20190613 92.1535010700763\n",
"20190614 93.4379103909606\n",
"20190617 93.69353279125795\n",
"20190618 93.26515871023017\n",
"20190619 92.00249455347556\n",
"20190620 92.22691878921194\n",
"20190621 91.18789028015455\n",
"20190624 88.43224289784386\n",
"20190625 90.47170504024238\n",
"20190626 89.04011830387248\n",
"20190627 86.74948903728196\n",
"20190628 87.58934989025475\n",
"20190701 89.26468727046739\n",
"20190702 87.88155886030543\n",
"20190703 91.6912518598451\n",
"20190704 91.67273871098428\n",
"20190705 90.77700533906638\n",
"20190708 90.21948814189479\n",
"20190709 91.95485842392085\n",
"20190710 92.95570222001206\n",
"20190711 92.52378388617922\n",
"20190712 94.42084541907037\n",
"20190715 93.6135478210084\n",
"20190716 94.21607755593737\n",
"20190717 95.90134004210788\n",
"20190718 96.59643875739657\n",
"20190719 95.7998756567572\n",
"20190722 95.01113243624813\n",
"20190723 94.87244259108111\n",
"20190724 94.88800299260956\n",
"20190725 96.45767865530125\n",
"20190726 98.21587909991936\n",
"20190729 100.0\n",
"20190730 99.84611571457685\n",
"20190731 100.00000000000003\n",
"20190801 98.76121864283179\n",
"20190802 97.9344001354589\n",
"20190805 95.69162568170961\n",
"20190806 96.18782992095112\n",
"20190807 93.64315363252084\n",
"20190808 95.09139645146449\n",
"20190809 96.5558606179694\n",
"20190812 95.76247765454666\n",
"20190813 95.95875175953552\n",
"20190814 96.94334447089092\n",
"20190815 97.4956756958637\n",
"20190816 95.45939915279043\n",
"20190819 96.03702465886907\n",
"20190820 95.75944205620505\n",
"20190821 95.22666036236069\n",
"20190822 94.46708957353285\n",
"20190823 93.51359596750264\n",
"20190826 94.20178365136991\n",
"20190827 96.72617129304749\n",
"20190828 94.4708593380956\n",
"20190829 91.87407686011711\n",
"20190830 96.38957527593094\n",
"20190902 95.81570433485635\n",
"20190903 96.03968178951631\n",
"20190904 94.38474039271559\n",
"20190905 95.12296399603117\n",
"20190906 95.17389496679598\n",
"20190909 93.28083489082819\n",
"20190911 91.86558928764978\n",
"20190912 92.6290937841457\n",
"20190916 94.75626736899683\n",
"20190917 94.5457286159569\n",
"20190918 94.34504636859322\n",
"20190919 95.02985355096953\n",
"20190920 95.56826044289417\n",
"20190923 95.7137730914727\n",
"20190924 95.80632959206658\n",
"20190925 96.2080787172079\n",
"20190926 96.23549105073639\n",
"20190927 95.88915356164459\n",
"20190930 95.46602755230614\n",
"20191008 95.74600109910973\n",
"20191009 98.8866844357554\n",
"20191010 96.78070682556405\n",
"20191011 96.54417107516988\n",
"20191014 96.13535991358867\n",
"20191015 97.8547308065685\n",
"20191016 95.4073357508953\n",
"20191017 97.3059561729405\n",
"20191018 99.37117912954177\n",
"20191021 98.28033096356008\n",
"20191022 98.66896162246776\n",
"20191023 96.84247656171596\n",
"20191024 96.36121556446489\n",
"20191025 97.0958338705519\n",
"20191028 98.19458826458491\n",
"20191029 97.5771792059499\n",
"20191030 99.04291503375107\n",
"20191031 100.0\n",
"20191101 99.7730437665297\n",
"20191104 97.9964276525329\n",
"20191105 98.83490615052247\n",
"20191106 99.02443807899579\n",
"20191107 98.30900768250184\n",
"20191108 97.86860796506772\n",
"20191111 96.92633603410039\n",
"20191112 95.1823450212678\n",
"20191113 93.1435388648091\n",
"20191114 94.73994280215756\n",
"20191115 92.70576372077427\n",
"20191118 95.08118076555392\n",
"20191119 94.07291003989175\n",
"20191120 94.63107063687617\n",
"20191121 94.61049212616904\n",
"20191122 95.74937114493505\n",
"20191125 94.34886123334579\n",
"20191126 94.56606300850926\n",
"20191127 94.44703750735773\n",
"20191128 92.60812388124386\n",
"20191129 96.28594515404579\n",
"20191202 94.76280667266508\n",
"20191203 91.45402660114289\n",
"20191204 94.85459861878124\n",
"20191205 95.64920203121768\n",
"20191206 96.54057302500418\n",
"20191209 93.93163123125068\n",
"20191210 94.5511572902784\n",
"20191211 94.92208771181933\n",
"20191212 94.68952345472042\n",
"20191213 94.67389174224688\n",
"20191216 95.1700782410705\n",
"20191217 95.0450819120455\n",
"20191218 94.67506554188327\n",
"20191219 94.22641800742534\n",
"20191220 95.44218778468057\n",
"20191223 96.16231899175959\n",
"20191224 96.65539001971531\n",
"20191225 97.49445414473801\n",
"20191226 97.69367962375458\n",
"20191227 95.00528737016681\n",
"20191230 94.6204105963189\n",
"20191231 93.21734056561081\n",
"20200102 95.71367937813825\n",
"20200103 94.71917515571782\n",
"20200106 93.7973431529509\n",
"20200107 92.45527814859472\n",
"20200108 94.4102351227919\n",
"20200109 93.24927662450345\n",
"20200110 93.05013592864526\n",
"20200113 92.90711569686977\n",
"20200114 92.80757561173147\n",
"20200115 92.77220059994936\n",
"20200116 92.49469177957532\n",
"20200117 92.27006145496026\n",
"20200120 93.96748841396068\n",
"20200121 95.11583424828166\n",
"20200122 96.55657990107169\n",
"20200123 97.12961761204274\n",
"20200203 96.2359771183458\n",
"20200204 95.0439002206545\n",
"20200205 95.3598337304895\n",
"20200206 94.02477209581278\n",
"20200207 92.34799736275346\n",
"20200210 91.13334226617478\n",
"20200211 89.11715048591475\n",
"20200212 90.04743341675153\n",
"20200213 88.25984690673168\n",
"20200214 86.5095048297471\n",
"20200217 85.499639091913\n",
"20200218 85.60056361265934\n",
"20200219 84.67021178885287\n",
"20200220 84.2716552242511\n",
"20200221 87.76481573456985\n",
"20200224 85.97910536286238\n",
"20200225 86.41065489870658\n",
"20200226 85.78068120264076\n",
"20200227 87.06884015479594\n",
"20200228 85.3628315650881\n",
"20200302 85.11765894886592\n",
"20200303 85.33101018270851\n",
"20200304 86.28343937131608\n",
"20200305 87.4194588190247\n"
]
}
],
"source": [
"jsyy=\\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F160723\": 100}), (\"est\", holdings[\"160723\"]), float_holdings=True,\n",
" window=4, decay=0.65, smooth=0.2)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.09025478055273216 \n",
"标准差偏离: 0.1357400830918445\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.403 \n",
"5% 分位: -0.215 \n",
"25% 分位: -0.078 \n",
"50% 分位: -0.011 \n",
"75% 分位: 0.041 \n",
"95% 分位: 0.183 \n",
"99% 分位: 0.353\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.25 \n",
"预测涨的比实际多: 0.29 \n",
"预测跌的比实际多: 0.19 \n",
"预测跌的比实际少: 0.27\n"
]
}
],
"source": [
"analyse_all(jsyy, \"diff_real_est\") # 在本来就准的基础上,又略微改善"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 国泰商品"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"gtsp=\\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F160216\": 100}), (\"est\", holdings[\"160216\"]))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.09892164040568546 \n",
"标准差偏离: 0.13543481510543717\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.345 \n",
"5% 分位: -0.191 \n",
"25% 分位: -0.089 \n",
"50% 分位: -0.012 \n",
"75% 分位: 0.061 \n",
"95% 分位: 0.175 \n",
"99% 分位: 0.382\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.24 \n",
"预测涨的比实际多: 0.34 \n",
"预测跌的比实际多: 0.21 \n",
"预测跌的比实际少: 0.21\n"
]
}
],
"source": [
"analyse_all(gtsp, \"diff_real_est\") # 可预测性也极好"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20190103 80.2702944117856\n",
"20190104 79.83495975133566\n",
"20190107 79.22576396208636\n",
"20190108 78.47724534301975\n",
"20190109 78.74360294698806\n",
"20190110 78.74360294698808\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 78.9942948927742\n",
"20190114 79.3665022888935\n",
"20190115 79.34173531302416\n",
"20190116 79.73314015224061\n",
"20190117 80.5013589711213\n",
"20190118 80.78474608088835\n",
"20190121 80.78474608088837\n",
"20190122 81.03740845809476\n",
"20190123 82.28059718905827\n",
"20190124 80.91245607675934\n",
"20190125 80.61418020716302\n",
"20190128 81.88734883172489\n",
"20190129 81.27719388736816\n",
"20190130 80.00457539367515\n",
"20190131 80.50889479499325\n",
"20190201 80.3282372530879\n",
"20190211 80.52961323110256\n",
"20190212 80.37502132492328\n",
"20190213 80.41516091758463\n",
"20190214 80.59551298210076\n",
"20190215 80.27088655385529\n",
"20190218 80.97856424973104\n",
"20190219 80.39950513901316\n",
"20190220 80.5922662371534\n",
"20190221 81.35417082630728\n",
"20190222 79.31197325868325\n",
"20190225 82.27093710033559\n",
"20190226 82.87907408775469\n",
"20190227 82.71632379776572\n",
"20190228 81.31383487740028\n",
"20190301 81.9705166722793\n",
"20190304 80.55193307689726\n",
"20190306 79.60341609572546\n",
"20190307 81.35707635466373\n",
"20190308 81.01691208637\n",
"20190311 80.61465908930757\n",
"20190313 79.51874360336672\n",
"20190314 79.5187436033667\n",
"20190315 79.51874360336672\n",
"20190318 78.87594357663426\n",
"20190319 79.29885148268698\n",
"20190320 78.33402240683829\n",
"20190321 77.49931839009274\n",
"20190322 77.24269900955102\n",
"20190325 78.38727730970523\n",
"20190326 77.67731391291983\n",
"20190327 76.54139715924939\n",
"20190328 76.05890497769965\n",
"20190329 78.54097720687994\n",
"20190401 77.3756880991352\n",
"20190402 77.02889074048967\n",
"20190403 78.61010431460667\n",
"20190404 79.1865062773887\n",
"20190408 79.66770464233764\n",
"20190409 79.89181986041427\n",
"20190410 78.73668418277514\n",
"20190411 79.25092945890131\n",
"20190412 79.3758920659885\n",
"20190415 78.4293821898676\n",
"20190416 77.06458982320811\n",
"20190417 76.25543876412698\n",
"20190418 76.25543876412696\n",
"20190419 76.8602856255534\n",
"20190422 77.69564462771204\n",
"20190423 77.64225761215826\n",
"20190424 77.07217249472818\n",
"20190425 74.78334265032926\n",
"20190426 74.78901473082024\n",
"20190429 74.83547612953254\n",
"20190430 74.1767956895695\n",
"20190506 75.15225148590977\n",
"20190507 75.62359266083094\n",
"20190508 75.51868631578607\n",
"20190509 75.28794908372568\n",
"20190510 76.05160090615499\n",
"20190513 76.3156309409069\n",
"20190514 77.13276515435163\n",
"20190515 78.20962314598133\n",
"20190516 77.66123954594555\n",
"20190517 78.02777579889145\n",
"20190520 79.30142530288823\n",
"20190521 79.98286879142046\n",
"20190522 81.34122581236122\n",
"20190523 82.00771042965954\n",
"20190524 82.0615736313868\n",
"20190527 81.40779805527202\n",
"20190528 82.1408930573491\n",
"20190529 83.52932871423782\n",
"20190530 84.61898483702952\n",
"20190531 82.69578042949094\n",
"20190603 84.60182819051006\n",
"20190604 82.37275704924521\n",
"20190605 81.76927749048339\n",
"20190606 81.29577018278808\n",
"20190610 81.00134152861386\n",
"20190611 81.00134152861385\n",
"20190612 81.31580954621381\n",
"20190613 82.13271344718218\n",
"20190614 83.07326830051721\n",
"20190617 81.29409123375984\n",
"20190618 81.58797696819767\n",
"20190619 82.05950363024135\n",
"20190620 82.52806677433567\n",
"20190621 81.80158812049504\n",
"20190624 81.83198628717982\n",
"20190625 81.83198628717983\n",
"20190626 82.89700378941023\n",
"20190627 84.19514473034738\n",
"20190628 82.91497205543659\n",
"20190701 84.69631672635597\n",
"20190702 85.9822053000462\n",
"20190703 84.57989460539474\n",
"20190704 85.19375345783331\n",
"20190705 85.92061425550116\n",
"20190708 85.92061425550114\n",
"20190709 86.35114141219474\n",
"20190710 86.62778291476155\n",
"20190711 85.4171927651509\n",
"20190712 85.46121802093474\n",
"20190715 85.99098553658793\n",
"20190716 86.54426197282561\n",
"20190717 82.9076380495884\n",
"20190718 84.68946614259477\n",
"20190719 83.33242550102433\n",
"20190722 82.73013886563557\n",
"20190723 81.8699127219114\n",
"20190724 81.90219564466312\n",
"20190725 81.90219564466314\n",
"20190726 83.42101731322886\n",
"20190729 81.82199713043079\n",
"20190730 82.46258680043422\n",
"20190731 83.0910983669829\n",
"20190801 82.37718866388248\n",
"20190802 82.79157431490474\n",
"20190805 83.35931352896087\n",
"20190806 82.63344464897696\n",
"20190807 82.2244072613954\n",
"20190808 81.8409550128397\n",
"20190809 80.9969667119931\n",
"20190812 81.81093184213557\n",
"20190813 81.75005304482649\n",
"20190814 81.27308037276302\n",
"20190815 81.85967423960727\n",
"20190816 81.05383792683509\n",
"20190819 80.61068052828597\n",
"20190822 80.0754447983271\n",
"20190823 80.23361932710979\n",
"20190826 81.53429379916973\n",
"20190827 82.12168137775528\n",
"20190828 81.59736203801438\n",
"20190829 83.23506180917434\n",
"20190830 82.00276249133086\n",
"20190902 82.00276249133087\n",
"20190903 82.49887574756377\n",
"20190904 82.64766148880052\n",
"20190905 83.00137138410629\n",
"20190906 82.3410997425148\n",
"20190909 83.3615964334954\n",
"20190910 84.51968307081087\n",
"20190911 82.3636361381733\n",
"20190912 83.35894303591324\n",
"20190916 83.45394497768608\n",
"20190917 83.02012722330936\n",
"20190918 82.82018996365913\n",
"20190919 83.29609907021218\n",
"20190924 83.67340699456938\n",
"20190925 83.5963645612844\n",
"20190927 83.59527265195315\n",
"20190930 82.34672534590541\n",
"20191008 82.27598963536215\n",
"20191009 81.6783346825308\n",
"20191010 80.9927977753503\n",
"20191011 80.29171726781686\n",
"20191014 80.31356178527595\n",
"20191015 80.11464405678603\n",
"20191016 80.39255744347572\n",
"20191017 80.06193496690021\n",
"20191018 79.8063517003506\n",
"20191021 79.11980024520174\n",
"20191022 79.50974058021613\n",
"20191023 79.21434231035941\n",
"20191024 79.70814715961451\n",
"20191025 79.08730346974271\n",
"20191028 79.99354791107477\n",
"20191029 80.23351600382826\n",
"20191030 80.18816727162368\n",
"20191031 80.06536426682189\n",
"20191101 79.93258892522122\n",
"20191104 80.30602048708603\n",
"20191105 79.36771384824401\n",
"20191106 79.6647296643636\n",
"20191107 80.37911595668633\n",
"20191108 78.5238197358857\n",
"20191111 79.24876629420176\n",
"20191113 79.49354725246819\n",
"20191114 78.56745502069654\n",
"20191115 79.87320935704805\n",
"20191118 80.498900830866\n",
"20191119 80.29137474645957\n",
"20191120 79.84288706387885\n",
"20191121 79.93876390686435\n",
"20191122 80.19505397950692\n",
"20191126 80.01177760663033\n",
"20191127 80.16615850262725\n",
"20191128 81.5713963666743\n",
"20191129 81.06351504204679\n",
"20191202 82.17013642492132\n",
"20191203 83.35914920322134\n",
"20191204 82.05656160040682\n",
"20191205 82.22810765236841\n",
"20191206 82.48529520476995\n",
"20191209 82.48529520476994\n",
"20191210 80.89910220012851\n",
"20191211 79.9265044762031\n",
"20191212 79.8983831543559\n",
"20191213 79.67315615726152\n",
"20191216 80.35946845700259\n",
"20191217 80.33400574323908\n",
"20191218 79.85237994147664\n",
"20191219 81.82822681431456\n",
"20191220 81.19211289141583\n",
"20191223 83.42594572607166\n",
"20191224 83.00362433472911\n",
"20191225 84.9657641318684\n",
"20191226 86.24232594129641\n",
"20191227 86.08099546647476\n",
"20191230 85.39386326264535\n",
"20191231 83.95964167496552\n",
"20200103 84.19920473182287\n",
"20200106 83.63375816001663\n",
"20200107 86.55817490900988\n",
"20200108 85.80133829209817\n",
"20200109 87.02675930512336\n",
"20200110 85.79647188252731\n",
"20200113 84.28841663898427\n",
"20200114 83.62531849483237\n",
"20200115 84.26680213298077\n",
"20200116 83.99802386860044\n",
"20200117 83.46875280593943\n",
"20200120 83.07246502916303\n",
"20200121 82.16985678222449\n",
"20200122 81.41828195794959\n",
"20200123 81.20310948534922\n",
"20200203 81.7812548624717\n",
"20200204 81.78125486247171\n",
"20200205 80.61905435652172\n",
"20200206 80.09690779437973\n",
"20200207 78.71317116321242\n",
"20200210 77.87679753549111\n",
"20200211 76.62251769972889\n",
"20200212 77.37440433914261\n",
"20200213 76.95654375174624\n",
"20200214 76.40947279404146\n",
"20200217 75.6639422435171\n",
"20200218 77.1816187122732\n",
"20200219 80.06978760866834\n",
"20200220 80.24796081599058\n",
"20200221 79.31350597920746\n",
"20200224 78.67455201280613\n",
"20200225 78.9973856699437\n",
"20200226 78.85262017931608\n",
"20200227 79.14524794563822\n",
"20200228 78.39209675279315\n",
"20200302 77.59581129448863\n",
"20200303 77.82259313878247\n",
"20200304 77.20900230953139\n",
"20200305 77.00213275551326\n"
]
}
],
"source": [
"gtsp=\\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F160216\": 100}), (\"est\", holdings[\"160216\"]), \n",
" float_holdings=True, window=4, decay=0.65, smooth=0.2)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.10633261782727876 \n",
"标准差偏离: 0.14924600716283906\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.366 \n",
"5% 分位: -0.216 \n",
"25% 分位: -0.085 \n",
"50% 分位: -0.02 \n",
"75% 分位: 0.069 \n",
"95% 分位: 0.21 \n",
"99% 分位: 0.442\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.23 \n",
"预测涨的比实际多: 0.35 \n",
"预测跌的比实际多: 0.19 \n",
"预测跌的比实际少: 0.23\n"
]
}
],
"source": [
"analyse_all(gtsp, \"diff_real_est\") # 略微变差,几乎持平"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 广发道琼斯石油"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"gfdqs=\\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F162719\": 100}), (\"est\", holdings[\"162719\"]),\n",
" (\"est_bc\", {\"954528\": 95}))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.0611781800347652 \n",
"标准差偏离: 0.09176119165721837\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.315 \n",
"5% 分位: -0.135 \n",
"25% 分位: -0.044 \n",
"50% 分位: -0.003 \n",
"75% 分位: 0.038 \n",
"95% 分位: 0.122 \n",
"99% 分位: 0.231\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.3 \n",
"预测涨的比实际多: 0.2 \n",
"预测跌的比实际多: 0.18 \n",
"预测跌的比实际少: 0.31\n"
]
}
],
"source": [
"analyse_all(gfdqs, \"diff_real_est\") #跟踪不错"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.08768082694014524 \n",
"标准差偏离: 0.13186120013259217\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.305 \n",
"5% 分位: -0.224 \n",
"25% 分位: -0.051 \n",
"50% 分位: -0.006 \n",
"75% 分位: 0.058 \n",
"95% 分位: 0.186 \n",
"99% 分位: 0.397\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.11 \n",
"预测涨的比实际多: 0.4 \n",
"预测跌的比实际多: 0.36 \n",
"预测跌的比实际少: 0.13\n"
]
}
],
"source": [
"analyse_all(gfdqs, \"diff_real_est_bc\") #还是实际持仓预测更稳定"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20190103 88.91600918473242\n",
"20190104 89.10591367122217\n",
"20190107 89.39180478855486\n",
"20190108 89.84499077145038\n",
"20190109 89.99938079562716\n",
"20190110 89.1623409970855\n",
"20190111 88.75216784917028\n",
"20190114 88.20007600624996\n",
"20190115 86.789746987543\n",
"20190116 90.2418729185157\n",
"20190117 90.60441265288051\n",
"20190118 90.36783466611232\n",
"20190122 91.39648283317071\n",
"20190123 90.67407102999461\n",
"20190124 91.04481522165392\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": [
"20190125 91.03140681188775\n",
"20190128 91.89913819974026\n",
"20190129 92.1258909657445\n",
"20190130 91.48639559954407\n",
"20190131 91.90982521834854\n",
"20190201 91.9806066996971\n",
"20190211 92.59290860059302\n",
"20190212 93.00109554011368\n",
"20190213 92.1701701704497\n",
"20190214 92.17382542781985\n",
"20190215 92.58876413611189\n",
"20190219 91.1737423579153\n",
"20190220 90.64152505523748\n",
"20190221 90.0111738377685\n",
"20190222 88.26230133939472\n",
"20190225 90.99816659429005\n",
"20190226 90.00545745473342\n",
"20190227 89.31283414486063\n",
"20190228 89.39361898651968\n",
"20190301 89.46704626254903\n",
"20190304 88.06367338421393\n",
"20190305 89.39032955826531\n",
"20190306 89.62490229415614\n",
"20190307 89.64797749456089\n",
"20190308 89.79382172652268\n",
"20190311 90.39090689931945\n",
"20190312 90.58127487386416\n",
"20190313 90.65607396851661\n",
"20190314 90.8094327973861\n",
"20190315 91.4790967546525\n",
"20190318 91.47507034537786\n",
"20190319 91.82203028256943\n",
"20190320 92.93527741378398\n",
"20190321 91.78588988079531\n",
"20190322 91.77715525508404\n",
"20190325 91.41011255709321\n",
"20190326 91.26878390167224\n",
"20190327 91.76676043258763\n",
"20190328 91.63744447603939\n",
"20190329 91.41465314783999\n",
"20190401 91.1734549613606\n",
"20190402 90.98449326453867\n",
"20190403 91.06391704100031\n",
"20190404 91.57856760611891\n",
"20190408 91.95089153235848\n",
"20190409 92.07059589960376\n",
"20190410 92.4840452267615\n",
"20190411 92.80499032029736\n",
"20190412 92.63402643229072\n",
"20190415 92.44457414044281\n",
"20190416 91.95512661943548\n",
"20190417 92.35691148437445\n",
"20190418 92.99649643418046\n",
"20190419 93.39320332164029\n",
"20190422 94.57260076801954\n",
"20190423 93.95273657144378\n",
"20190424 93.5561219509454\n",
"20190425 93.14594267576126\n",
"20190426 93.01277331121298\n",
"20190429 93.23670712364142\n",
"20190430 92.161361612901\n",
"20190506 93.18249862290885\n",
"20190507 93.84169435394791\n",
"20190508 91.63624814276767\n",
"20190509 91.9862759717356\n",
"20190510 92.07274273803058\n",
"20190513 90.98816377778331\n",
"20190514 92.76175036084494\n",
"20190515 93.31218027331714\n",
"20190516 93.16320043497342\n",
"20190517 93.23964768085499\n",
"20190520 94.46199607238005\n",
"20190521 94.05767354637179\n",
"20190522 94.50943206206175\n",
"20190523 96.21863556602142\n",
"20190524 93.14209291732854\n",
"20190527 93.35974698931001\n",
"20190528 92.94786747612903\n",
"20190529 91.89242707864844\n",
"20190530 94.72846556708498\n",
"20190531 92.77893137615197\n",
"20190603 92.06701789892242\n",
"20190604 92.57648568057984\n",
"20190605 92.86506341312453\n",
"20190606 91.68649023064924\n",
"20190610 91.42205140740472\n",
"20190611 91.48015699837416\n",
"20190612 91.1282297835265\n",
"20190613 90.54862720469905\n",
"20190614 91.39162487130832\n",
"20190617 90.22623012129887\n",
"20190618 89.86342199609146\n",
"20190619 89.72724868861857\n",
"20190620 89.57170263879385\n",
"20190621 89.63577001168407\n",
"20190624 90.0811189963516\n",
"20190625 89.52854093900159\n",
"20190626 89.93018196822332\n",
"20190627 88.66851236123608\n",
"20190628 88.00961443814707\n",
"20190701 87.62547374363709\n",
"20190702 86.89711430532913\n",
"20190703 86.40761064652375\n",
"20190704 84.84452772308812\n",
"20190705 87.12502408151342\n",
"20190708 87.01940441508877\n",
"20190709 87.09485502922499\n",
"20190710 88.37897430441585\n",
"20190711 89.82865095514289\n",
"20190712 88.75807425339123\n",
"20190715 87.55505420777834\n",
"20190716 87.67903058088154\n",
"20190717 88.15001333194625\n",
"20190718 87.90049041202032\n",
"20190719 87.51238286965939\n",
"20190722 88.55780547602282\n",
"20190723 88.72656695253464\n",
"20190724 89.3360726451651\n",
"20190725 89.07841572952785\n",
"20190726 89.14493448406094\n",
"20190729 89.4546965042221\n",
"20190730 88.62912988820055\n",
"20190731 88.57065649689338\n",
"20190801 89.70094146390763\n",
"20190802 90.16761737846277\n",
"20190805 90.89366663743134\n",
"20190806 89.64067384860748\n",
"20190807 91.31324762676587\n",
"20190808 91.6523243662846\n",
"20190809 92.38369402552617\n",
"20190812 92.32800570579184\n",
"20190813 90.068971237659\n",
"20190814 89.91254846787338\n",
"20190815 90.27253804930758\n",
"20190816 89.67507435994895\n",
"20190819 88.76219737770981\n",
"20190820 88.06791388369899\n",
"20190821 88.91328391884859\n",
"20190822 88.14280691284863\n",
"20190823 87.30277813616644\n",
"20190826 86.75189094955837\n",
"20190827 86.43192716463533\n",
"20190828 85.46499452496042\n",
"20190829 84.47308990050838\n",
"20190830 85.99224429067043\n",
"20190902 85.99224429067041\n",
"20190903 85.68022035385034\n",
"20190904 85.16629165886069\n",
"20190905 84.57687011251096\n",
"20190906 86.17656629089306\n",
"20190909 86.3642698052913\n",
"20190910 87.36932567692214\n",
"20190911 87.97035981206596\n",
"20190912 86.5075766657823\n",
"20190916 87.0018386764806\n",
"20190917 86.49497613157843\n",
"20190918 86.67469700759871\n",
"20190919 85.84857746009853\n",
"20190920 85.82676462489825\n",
"20190923 86.34510873881345\n",
"20190924 86.79609715044822\n",
"20190925 88.18196956766968\n",
"20190926 89.01875125529659\n",
"20190927 87.9717483403206\n",
"20190930 88.81497740634896\n",
"20191008 89.39970111362153\n",
"20191009 90.86473716469449\n",
"20191010 88.87661894922374\n",
"20191011 89.52409256864811\n",
"20191014 89.81173844490142\n",
"20191015 89.78843070878536\n",
"20191016 89.07021510202719\n",
"20191017 89.23432066667787\n",
"20191018 89.02330794919484\n",
"20191021 88.47205631821947\n",
"20191022 89.45267203210406\n",
"20191023 88.16574692938933\n",
"20191024 88.62299334122683\n",
"20191025 88.87290083720741\n",
"20191028 89.52172889910948\n",
"20191029 90.30522676562815\n",
"20191030 88.87611236862911\n",
"20191031 89.29016647373412\n",
"20191101 90.00876771446642\n",
"20191104 91.00784762186393\n",
"20191105 90.53665086085039\n",
"20191106 88.92819835814142\n",
"20191107 88.22150189523876\n",
"20191108 85.8697734276169\n",
"20191111 88.10887303384898\n",
"20191112 86.44322568277192\n",
"20191113 84.91096553248988\n",
"20191114 84.9109655324899\n",
"20191115 88.26955120560197\n",
"20191118 88.43189297056047\n",
"20191119 87.86358859104496\n",
"20191120 88.16935700626988\n",
"20191121 88.48070116720818\n",
"20191122 88.5112492596272\n",
"20191125 88.283835035927\n",
"20191126 88.56307852491517\n",
"20191127 88.30764874607061\n",
"20191128 88.50678617056954\n",
"20191129 88.78801068225668\n",
"20191202 89.76578160085643\n",
"20191203 90.84078220237029\n",
"20191204 90.86962723956078\n",
"20191205 91.95810054155892\n",
"20191206 90.06589015332172\n",
"20191209 90.37545888396663\n",
"20191210 90.86452122997693\n",
"20191211 91.77550794497839\n",
"20191212 92.41168308712234\n",
"20191213 92.81199033792734\n",
"20191216 92.72290609036055\n",
"20191217 91.8482527668917\n",
"20191218 91.99603594540689\n",
"20191219 92.18958396635455\n",
"20191220 92.2587484685978\n",
"20191223 91.61613338899694\n",
"20191224 92.58974392085177\n",
"20191225 92.85239827812555\n",
"20191226 93.49176278032333\n",
"20191227 95.10309610840886\n",
"20191230 95.85813192290718\n",
"20191231 94.53305058175098\n",
"20200102 94.69895495204211\n",
"20200103 96.3856264925505\n",
"20200106 96.59102571121394\n",
"20200107 97.34637625110464\n",
"20200108 94.86937992162451\n",
"20200109 94.84083410608807\n",
"20200110 93.97140079598528\n",
"20200113 94.00216368410625\n",
"20200114 92.55864147625181\n",
"20200115 92.16335016194525\n",
"20200116 93.20966029969885\n",
"20200117 92.90579824517135\n",
"20200120 95.90359828583807\n",
"20200121 96.11119022207768\n",
"20200122 94.46517903144843\n",
"20200123 94.41541945563924\n",
"20200203 94.25355073142607\n",
"20200204 94.01909922750293\n",
"20200205 94.00982019241013\n",
"20200206 93.71415026424133\n",
"20200207 93.52122822620852\n",
"20200210 91.42624780619239\n",
"20200211 90.31776443889022\n",
"20200212 89.87931271056208\n",
"20200213 88.82049962608266\n",
"20200214 88.57739438284605\n",
"20200217 87.11146130268466\n",
"20200218 87.18061838827916\n",
"20200219 89.97892017150454\n",
"20200220 91.53454692672123\n",
"20200221 90.2704073717158\n",
"20200224 91.25763638944586\n",
"20200225 93.45787928113134\n",
"20200226 91.81971335066996\n",
"20200227 91.95321355764929\n",
"20200228 92.29377732340454\n",
"20200302 92.48341583791715\n",
"20200303 92.31435120318643\n",
"20200304 91.81523098055037\n",
"20200305 92.56189553445614\n"
]
}
],
"source": [
"gfdqs=\\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F162719\": 100}), (\"est\", holdings[\"162719\"]),\n",
" float_holdings=True, window=4, decay=0.65, smooth=0.2)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.06163217662538718 \n",
"标准差偏离: 0.09118138464047547\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.243 \n",
"5% 分位: -0.129 \n",
"25% 分位: -0.042 \n",
"50% 分位: -0.0 \n",
"75% 分位: 0.036 \n",
"95% 分位: 0.139 \n",
"99% 分位: 0.262\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.25 \n",
"预测涨的比实际多: 0.25 \n",
"预测跌的比实际多: 0.24 \n",
"预测跌的比实际少: 0.25\n"
]
}
],
"source": [
"analyse_all(gfdqs, \"diff_real_est\") # 好多了"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 华安德国"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"warning: this fund has no data for rate\n"
]
}
],
"source": [
"dax = \\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F513030\": 100}), (\"est\", holdings[\"513030\"]))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.041622922621231295 \n",
"标准差偏离: 0.052359059264518816\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.144 \n",
"5% 分位: -0.094 \n",
"25% 分位: -0.039 \n",
"50% 分位: -0.009 \n",
"75% 分位: 0.028 \n",
"95% 分位: 0.076 \n",
"99% 分位: 0.117\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.34 \n",
"预测涨的比实际多: 0.22 \n",
"预测跌的比实际多: 0.09 \n",
"预测跌的比实际少: 0.36\n"
]
}
],
"source": [
"analyse_all(dax, \"diff_real_est\") ## 精度也算逆天"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"warning: this fund has no data for rate\n",
"20190103 94.78009282166988\n",
"20190104 94.77859353435717\n",
"20190107 95.48728734367326\n",
"20190108 96.084298394594\n",
"20190109 96.84278698858081\n",
"20190110 98.02883134656308\n",
"20190111 96.02064481323588\n",
"20190114 95.56067965358507\n",
"20190115 96.94461095834129\n",
"20190116 97.93568747908293\n",
"20190117 98.74830105252403\n",
"20190118 99.99999999999999\n",
"20190121 100.00000000000001\n",
"20190122 99.25520602010282\n",
"20190123 98.73132983442622\n",
"20190124 99.73164289331211\n",
"20190125 99.12014703094799\n",
"20190128 100.0\n",
"20190129 98.71870959891163\n",
"20190130 99.52890964478524\n",
"20190131 100.0\n",
"20190201 100.0\n",
"20190211 98.30647204233175\n",
"20190212 100.0\n",
"20190213 98.72454801419771\n",
"20190214 98.82092282185205\n",
"20190215 98.15301855832794\n",
"20190218 98.15301855832791\n",
"20190219 97.45060380952742\n",
"20190220 97.9621628773251\n",
"20190221 98.59695113126624\n",
"20190222 97.63908298554\n",
"20190225 97.43449712852494\n",
"20190226"
]
},
{
"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": [
" 97.61444132671564\n",
"20190227 95.55117793146698\n",
"20190228 97.81686830716028\n",
"20190301 96.22838535384197\n",
"20190304 95.72622149925418\n",
"20190305 94.49782930223165\n",
"20190306 96.53710522409419\n",
"20190307 96.99365467700743\n",
"20190308 95.08092605744481\n",
"20190311 96.9464084770509\n",
"20190312 96.94640847705092\n",
"20190313 97.75713782571884\n",
"20190314 96.77678180012111\n",
"20190315 96.23683986280824\n",
"20190318 96.16314053264566\n",
"20190319 95.7980434260631\n",
"20190320 96.1256476787763\n",
"20190321 96.18723713026354\n",
"20190322 97.00970798049883\n",
"20190325 97.20544406038857\n",
"20190326 97.3645937215426\n",
"20190327 96.92755410946017\n",
"20190328 96.9275541094602\n",
"20190329 97.62900351730342\n",
"20190401 97.2347286786578\n",
"20190402 96.42409778601245\n",
"20190403 96.88954221601738\n",
"20190404 95.58475225028378\n",
"20190408 95.23491426861764\n",
"20190409 96.87464209449004\n",
"20190410 96.38905700412252\n",
"20190411 96.97890657841288\n",
"20190412 94.94709334833033\n",
"20190415 95.72652412908437\n",
"20190416 96.89188161976895\n",
"20190417 96.46007981274032\n",
"20190418 96.17123061573854\n",
"20190419 95.82234314320978\n",
"20190423 96.42530807111596\n",
"20190424 94.52559089757902\n",
"20190425 96.14081938767062\n",
"20190426 97.01273705636288\n",
"20190429 95.49506978157983\n",
"20190430 94.88345726628387\n",
"20190506 95.60284047886027\n",
"20190507 93.74273005395062\n",
"20190508 96.55483337312722\n",
"20190509 97.43595291070248\n",
"20190510 95.5045515450427\n",
"20190513 95.1944520179961\n",
"20190514 96.65334842247654\n",
"20190515 95.98848052923668\n",
"20190516 95.53819784804608\n",
"20190517 95.33446858403634\n",
"20190520 95.34482183915368\n",
"20190521 95.21036619692825\n",
"20190522 95.06953080894326\n",
"20190523 95.61460440187447\n",
"20190524 94.75302473867994\n",
"20190527 93.86935800917585\n",
"20190528 95.45709345868568\n",
"20190529 95.19314282864448\n",
"20190530 96.22990175924397\n",
"20190531 96.23017410464982\n",
"20190603 96.39400094760254\n",
"20190604 96.7853865322902\n",
"20190605 97.24779581366874\n",
"20190606 98.22434078210183\n",
"20190610 98.56624728034673\n",
"20190611 99.99999999999999\n",
"20190612 99.49961036703192\n",
"20190613 97.38430591497305\n",
"20190614 96.58982554041545\n",
"20190617 95.90434777917193\n",
"20190618 94.93538375219435\n",
"20190619 96.16081444395115\n",
"20190620 96.62689478254933\n",
"20190621 96.2694128726075\n",
"20190624 97.09345202057386\n",
"20190625 97.97084224955476\n",
"20190626 97.9708422495548\n",
"20190627 98.98157648724627\n",
"20190628 99.8210108097451\n",
"20190701 99.6766508695825\n",
"20190702 99.75618218970037\n",
"20190703 98.6641646823095\n",
"20190704 98.81410774164351\n",
"20190705 98.66996726717171\n",
"20190708 98.22030052044595\n",
"20190709 97.93073438008194\n",
"20190710 98.36828009060274\n",
"20190711 98.5521315122385\n",
"20190712 98.23885126621015\n",
"20190715 98.88903540340047\n",
"20190716 97.3681980404896\n",
"20190717 99.4874792013687\n",
"20190718 98.14497200544892\n",
"20190719 96.61076338117371\n",
"20190722 97.64254224208943\n",
"20190723 97.99564809358029\n",
"20190724 97.5147679600899\n",
"20190725 100.00000000000001\n",
"20190726 98.06891999685885\n",
"20190729 98.43066117662585\n",
"20190730 97.91287960828778\n",
"20190731 97.95777809762602\n",
"20190801 97.72530446782768\n",
"20190802 98.84445521929288\n",
"20190805 99.40802877026103\n",
"20190806 100.00000000000001\n",
"20190807 97.46354953709637\n",
"20190808 98.18805101037725\n",
"20190809 98.19679295641212\n",
"20190812 98.41498063221503\n",
"20190813 97.91275366725313\n",
"20190814 97.73292152525113\n",
"20190815 97.45667167982654\n",
"20190816 96.92431399049198\n",
"20190819 97.43368305574629\n",
"20190820 97.28117158718834\n",
"20190821 97.10598109960088\n",
"20190822 97.6673356924443\n",
"20190823 97.87892887793656\n",
"20190826 97.59922343199653\n",
"20190827 97.56818822525014\n",
"20190828 96.76578478548782\n",
"20190829 97.31078004297198\n",
"20190830 98.33899649651026\n",
"20190902 98.1764783187476\n",
"20190903 97.08107610004548\n",
"20190904 96.87804734998274\n",
"20190905 97.52274661532353\n",
"20190906 97.72466349943717\n",
"20190910 97.11855541848477\n",
"20190911 97.38079209936629\n",
"20190912 97.67816685395242\n",
"20190916 98.49713814255246\n",
"20190917 99.09371035756645\n",
"20190918 100.00000000000001\n",
"20190919 100.0\n",
"20190920 97.42774397842598\n",
"20190923 98.1366210035222\n",
"20190924 97.78510887626724\n",
"20190925 97.8986319502209\n",
"20190926 97.80354895298906\n",
"20190927 97.4312182434616\n",
"20190930 96.0508757356511\n",
"20191008 95.82845571578265\n",
"20191009 97.27031399811861\n",
"20191010 95.80139869793611\n",
"20191011 96.78413503772165\n",
"20191014 97.39914609270993\n",
"20191015 97.84854065621168\n",
"20191016 98.25568301287889\n",
"20191017 99.0968897591683\n",
"20191018 98.37291184593006\n",
"20191021 98.25836789656437\n",
"20191023 99.17162692349322\n",
"20191024 100.0\n",
"20191025 98.20076273600039\n",
"20191028 99.62136074505635\n",
"20191029 100.0\n",
"20191030 100.0\n",
"20191031 97.7558805867726\n",
"20191101 99.2927745077816\n",
"20191104 97.50543514495799\n",
"20191105 100.0\n",
"20191106 96.69669686243236\n",
"20191107 97.08510940364464\n",
"20191108 96.62622205227818\n",
"20191111 98.18918658488974\n",
"20191112 97.41599456184576\n",
"20191113 98.01720995591468\n",
"20191114 98.62335810929515\n",
"20191115 98.48910837075441\n",
"20191118 99.43873061785857\n",
"20191119 100.00000000000001\n",
"20191120 100.0\n",
"20191121 99.99999999999997\n",
"20191122 100.0\n",
"20191125 99.0588574187791\n",
"20191126 99.05881445972238\n",
"20191127 99.59692291888639\n",
"20191128 99.99946376943188\n",
"20191202 100.0\n",
"20191203 98.74903454963417\n",
"20191204 98.76421693887414\n",
"20191205 99.23460304182362\n",
"20191206 99.54458599736638\n",
"20191209 99.19961653506623\n",
"20191210 99.62315244277823\n",
"20191211 99.3841362313074\n",
"20191212 98.92256309074216\n",
"20191213 97.90537946707694\n",
"20191216 97.90537946707697\n",
"20191217 99.2257224282727\n",
"20191218 98.79775313581116\n",
"20191219 99.24196356204646\n",
"20191220 98.9515931660646\n",
"20191223 99.57637017021028\n",
"20191224 100.0\n",
"20191225 100.0\n",
"20191226 100.0\n",
"20191227 100.0\n",
"20191230 100.0\n",
"20191231 97.90631297989685\n",
"20200102 98.91706688015068\n",
"20200103 100.0\n",
"20200106 96.97382110844018\n",
"20200107 97.20802025510278\n",
"20200109 98.169547143952\n",
"20200110 98.95559969206595\n",
"20200113 97.99768146972252\n",
"20200114 98.72622708497684\n",
"20200115 99.2099079004798\n",
"20200116 98.19184315828225\n",
"20200117 98.08960230417706\n",
"20200120 100.0\n",
"20200121 99.99999999999997\n",
"20200122 100.0\n",
"20200123 98.18746275388047\n",
"20200203 100.00000000000001\n",
"20200204 100.0\n",
"20200205 97.56785869079555\n",
"20200206 97.08449256147311\n",
"20200207 97.63128860142466\n",
"20200210 97.69459177344295\n",
"20200211 96.74728047336077\n",
"20200212 97.92858256775992\n",
"20200213 98.21629862935094\n",
"20200214 98.48035465657686\n",
"20200217 97.85603392810339\n",
"20200218 98.24191984942075\n",
"20200219 98.50439477638129\n",
"20200220 96.71011423502081\n",
"20200221 98.31466749743984\n",
"20200224 99.01661692886306\n",
"20200225 98.82089530177683\n",
"20200226 98.82089530177686\n",
"20200227 97.81369460736299\n",
"20200228 98.70494922555493\n",
"20200302 99.55684722987077\n",
"20200303 99.69041916536989\n",
"20200304 99.99999999999999\n",
"20200305 100.00000000000001\n"
]
}
],
"source": [
"dax = \\\n",
"estimate_table(\"20190101\", \"20200305\", (\"real\", {\"F513030\": 100}), (\"est\", holdings[\"513030\"]), \n",
" float_holdings=True, window=4, decay=0.65, smooth=0.2)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.03750258301849859 \n",
"标准差偏离: 0.0454188130802292\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.104 \n",
"5% 分位: -0.081 \n",
"25% 分位: -0.04 \n",
"50% 分位: -0.011 \n",
"75% 分位: 0.027 \n",
"95% 分位: 0.063 \n",
"99% 分位: 0.085\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.25 \n",
"预测涨的比实际多: 0.3 \n",
"预测跌的比实际多: 0.17 \n",
"预测跌的比实际少: 0.28\n"
]
}
],
"source": [
"analyse_all(dax, \"diff_real_est\") # 在本来就准的基础上,进一步改善"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"warning: this fund has no data for rate\n"
]
}
],
"source": [
"dax = \\\n",
"estimate_table_with_pos(\"20190101\", \"20200305\", (\"real\", {\"F513030\": 100}), (\"est\", holdings[\"513030\"]))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.040043320409176374 \n",
"标准差偏离: 0.05096457858857964\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.122 \n",
"5% 分位: -0.092 \n",
"25% 分位: -0.039 \n",
"50% 分位: -0.012 \n",
"75% 分位: 0.027 \n",
"95% 分位: 0.068 \n",
"99% 分位: 0.094\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.29 \n",
"预测涨的比实际多: 0.27 \n",
"预测跌的比实际多: 0.13 \n",
"预测跌的比实际少: 0.31\n"
]
}
],
"source": [
"analyse_all(dax, \"diff_real_est\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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",
" date | \n",
" real | \n",
" est | \n",
" estpos | \n",
" estposld | \n",
" diff_real_est | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 2020-01-03 | \n",
" -0.490913 | \n",
" -0.437436 | \n",
" 0.950000 | \n",
" 0.950000 | \n",
" -0.053477 | \n",
"
\n",
" \n",
" | 1 | \n",
" 2020-01-06 | \n",
" 0.409363 | \n",
" 0.463410 | \n",
" 0.958486 | \n",
" 0.982353 | \n",
" -0.054047 | \n",
"
\n",
" \n",
" | 2 | \n",
" 2020-01-07 | \n",
" 0.710851 | \n",
" 0.724081 | \n",
" 0.950877 | \n",
" 0.912900 | \n",
" -0.013230 | \n",
"
\n",
" \n",
" | 3 | \n",
" 2020-01-08 | \n",
" -0.539755 | \n",
" -0.903040 | \n",
" 0.946768 | \n",
" 0.933502 | \n",
" 0.363285 | \n",
"
\n",
" \n",
" | 4 | \n",
" 2020-01-09 | \n",
" 2.003757 | \n",
" 1.670934 | \n",
" 0.850957 | \n",
" 0.650991 | \n",
" 0.332823 | \n",
"
\n",
" \n",
" | 5 | \n",
" 2020-01-10 | \n",
" 0.296706 | \n",
" 0.666257 | \n",
" 0.858318 | \n",
" 1.000000 | \n",
" -0.369552 | \n",
"
\n",
" \n",
" | 6 | \n",
" 2020-01-13 | \n",
" 1.978986 | \n",
" 1.623447 | \n",
" 0.811265 | \n",
" 0.825773 | \n",
" 0.355539 | \n",
"
\n",
" \n",
" | 7 | \n",
" 2020-01-14 | \n",
" -1.510453 | \n",
" -1.376872 | \n",
" 0.939974 | \n",
" 0.988934 | \n",
" -0.133581 | \n",
"
\n",
" \n",
" | 8 | \n",
" 2020-01-15 | \n",
" 0.375787 | \n",
" 1.022930 | \n",
" 0.924968 | \n",
" 1.000000 | \n",
" -0.647143 | \n",
"
\n",
" \n",
" | 9 | \n",
" 2020-01-16 | \n",
" 0.637458 | \n",
" 0.740169 | \n",
" 0.936513 | \n",
" 0.775245 | \n",
" -0.102711 | \n",
"
\n",
" \n",
" | 10 | \n",
" 2020-01-17 | \n",
" 1.588578 | \n",
" 1.295950 | \n",
" 0.875570 | \n",
" 0.806556 | \n",
" 0.292628 | \n",
"
\n",
" \n",
" | 11 | \n",
" 2020-01-20 | \n",
" -0.504751 | \n",
" -0.618700 | \n",
" 0.844463 | \n",
" 1.000000 | \n",
" 0.113950 | \n",
"
\n",
" \n",
" | 12 | \n",
" 2020-01-21 | \n",
" -2.317716 | \n",
" -2.038051 | \n",
" 0.865450 | \n",
" 0.789285 | \n",
" -0.279665 | \n",
"
\n",
" \n",
" | 13 | \n",
" 2020-01-22 | \n",
" 2.036660 | \n",
" 1.043652 | \n",
" 0.926771 | \n",
" 0.984209 | \n",
" 0.993008 | \n",
"
\n",
" \n",
" | 14 | \n",
" 2020-01-23 | \n",
" -1.906188 | \n",
" -1.640071 | \n",
" 0.908464 | \n",
" 1.000000 | \n",
" -0.266117 | \n",
"
\n",
" \n",
" | 15 | \n",
" 2020-02-03 | \n",
" -2.085665 | \n",
" -2.137443 | \n",
" 0.993528 | \n",
" 1.000000 | \n",
" 0.051778 | \n",
"
\n",
" \n",
" | 16 | \n",
" 2020-02-04 | \n",
" 4.280964 | \n",
" 4.281700 | \n",
" 0.991990 | \n",
" 0.969460 | \n",
" -0.000735 | \n",
"
\n",
" \n",
" | 17 | \n",
" 2020-02-05 | \n",
" -0.338780 | \n",
" -0.292782 | \n",
" 0.987841 | \n",
" 0.991819 | \n",
" -0.045998 | \n",
"
\n",
" \n",
" | 18 | \n",
" 2020-02-06 | \n",
" 1.949610 | \n",
" 1.596505 | \n",
" 0.983586 | \n",
" 0.995367 | \n",
" 0.353105 | \n",
"
\n",
" \n",
" | 19 | \n",
" 2020-02-07 | \n",
" 0.245170 | \n",
" 0.178653 | \n",
" 0.995128 | \n",
" 1.000000 | \n",
" 0.066517 | \n",
"
\n",
" \n",
" | 20 | \n",
" 2020-02-10 | \n",
" 0.821757 | \n",
" -0.197446 | \n",
" 0.998101 | \n",
" 1.000000 | \n",
" 1.019203 | \n",
"
\n",
" \n",
" | 21 | \n",
" 2020-02-11 | \n",
" -0.349311 | \n",
" 0.486369 | \n",
" 1.000000 | \n",
" 1.000000 | \n",
" -0.835680 | \n",
"
\n",
" \n",
" | 22 | \n",
" 2020-02-12 | \n",
" 0.817916 | \n",
" 1.643115 | \n",
" 1.000000 | \n",
" 1.000000 | \n",
" -0.825199 | \n",
"
\n",
" \n",
" | 23 | \n",
" 2020-02-13 | \n",
" 0.347692 | \n",
" -0.482726 | \n",
" 0.910295 | \n",
" 0.657998 | \n",
" 0.830417 | \n",
"
\n",
" \n",
" | 24 | \n",
" 2020-02-14 | \n",
" 0.163619 | \n",
" 0.209932 | \n",
" 0.798163 | \n",
" 0.657998 | \n",
" -0.046313 | \n",
"
\n",
" \n",
" | 25 | \n",
" 2020-02-17 | \n",
" 0.884020 | \n",
" 0.126587 | \n",
" 0.655037 | \n",
" 0.646709 | \n",
" 0.757434 | \n",
"
\n",
" \n",
" | 26 | \n",
" 2020-02-18 | \n",
" -0.152395 | \n",
" 0.354121 | \n",
" 0.744002 | \n",
" 1.000000 | \n",
" -0.506517 | \n",
"
\n",
" \n",
" | 27 | \n",
" 2020-02-19 | \n",
" 1.077936 | \n",
" 0.931968 | \n",
" 0.855209 | \n",
" 1.000000 | \n",
" 0.145967 | \n",
"
\n",
" \n",
" | 28 | \n",
" 2020-02-20 | \n",
" -0.670064 | \n",
" -0.663001 | \n",
" 0.997155 | \n",
" 0.989154 | \n",
" -0.007063 | \n",
"
\n",
" \n",
" | 29 | \n",
" 2020-02-21 | \n",
" -1.729216 | \n",
" -1.651593 | \n",
" 0.996444 | \n",
" 1.000000 | \n",
" -0.077623 | \n",
"
\n",
" \n",
" | 30 | \n",
" 2020-02-24 | \n",
" -1.691966 | \n",
" -3.385880 | \n",
" 0.995555 | \n",
" 1.000000 | \n",
" 1.693914 | \n",
"
\n",
" \n",
" | 31 | \n",
" 2020-02-25 | \n",
" -0.816286 | \n",
" -0.194344 | \n",
" 0.910234 | \n",
" 0.657766 | \n",
" -0.621942 | \n",
"
\n",
" \n",
" | 32 | \n",
" 2020-02-26 | \n",
" -0.039663 | \n",
" -0.466847 | \n",
" 0.887792 | \n",
" 1.000000 | \n",
" 0.427184 | \n",
"
\n",
" \n",
" | 33 | \n",
" 2020-02-27 | \n",
" -2.390636 | \n",
" -1.154110 | \n",
" 0.805378 | \n",
" 0.792746 | \n",
" -1.236526 | \n",
"
\n",
" \n",
" | 34 | \n",
" 2020-02-28 | \n",
" -0.934959 | \n",
" -0.687504 | \n",
" 0.932048 | \n",
" 1.000000 | \n",
" -0.247455 | \n",
"
\n",
" \n",
" | 35 | \n",
" 2020-03-02 | \n",
" 2.810833 | \n",
" 1.930919 | \n",
" 0.915060 | \n",
" 1.000000 | \n",
" 0.879914 | \n",
"
\n",
" \n",
" | 36 | \n",
" 2020-03-03 | \n",
" -2.015566 | \n",
" -1.356485 | \n",
" 1.000000 | \n",
" 1.000000 | \n",
" -0.659081 | \n",
"
\n",
" \n",
" | 37 | \n",
" 2020-03-04 | \n",
" 2.158859 | \n",
" 1.774747 | \n",
" 1.000000 | \n",
" 1.000000 | \n",
" 0.384113 | \n",
"
\n",
" \n",
" | 38 | \n",
" 2020-03-05 | \n",
" -0.328947 | \n",
" -0.523462 | \n",
" 1.000000 | \n",
" 1.000000 | \n",
" 0.194515 | \n",
"
\n",
" \n",
" | 39 | \n",
" 2020-03-06 | \n",
" -1.840184 | \n",
" -2.591814 | \n",
" 0.958617 | \n",
" 0.842229 | \n",
" 0.751630 | \n",
"
\n",
" \n",
" | 40 | \n",
" 2020-03-09 | \n",
" -4.758023 | \n",
" -4.730108 | \n",
" 0.864499 | \n",
" 0.680617 | \n",
" -0.027916 | \n",
"
\n",
" \n",
" | 41 | \n",
" 2020-03-10 | \n",
" 3.230638 | \n",
" 3.458935 | \n",
" 0.796421 | \n",
" 0.869601 | \n",
" -0.228297 | \n",
"
\n",
" \n",
" | 42 | \n",
" 2020-03-11 | \n",
" -1.025907 | \n",
" -2.372034 | \n",
" 0.759166 | \n",
" 0.743855 | \n",
" 1.346127 | \n",
"
\n",
" \n",
" | 43 | \n",
" 2020-03-12 | \n",
" -5.570097 | \n",
" -5.043342 | \n",
" 0.734738 | \n",
" 0.512619 | \n",
" -0.526755 | \n",
"
\n",
" \n",
" | 44 | \n",
" 2020-03-13 | \n",
" 4.778800 | \n",
" 3.829034 | \n",
" 0.685777 | \n",
" 0.811478 | \n",
" 0.949766 | \n",
"
\n",
" \n",
" | 45 | \n",
" 2020-03-16 | \n",
" -8.275132 | \n",
" -7.262540 | \n",
" 0.700641 | \n",
" 0.855880 | \n",
" -1.012593 | \n",
"
\n",
" \n",
" | 46 | \n",
" 2020-03-17 | \n",
" 2.676511 | \n",
" 2.971950 | \n",
" 0.822587 | \n",
" 0.798329 | \n",
" -0.295439 | \n",
"
\n",
" \n",
" | 47 | \n",
" 2020-03-18 | \n",
" -3.955056 | \n",
" -3.986173 | \n",
" 0.806830 | \n",
" 0.740815 | \n",
" 0.031117 | \n",
"
\n",
" \n",
" | 48 | \n",
" 2020-03-19 | \n",
" 1.263453 | \n",
" 1.095271 | \n",
" 0.780050 | \n",
" 0.800531 | \n",
" 0.168182 | \n",
"
\n",
" \n",
" | 49 | \n",
" 2020-03-20 | \n",
" 1.270795 | \n",
" 2.773797 | \n",
" 0.802103 | \n",
" 0.899829 | \n",
" -1.503002 | \n",
"
\n",
" \n",
" | 50 | \n",
" 2020-03-23 | \n",
" -1.220625 | \n",
" -1.159887 | \n",
" 0.766969 | \n",
" 0.548452 | \n",
" -0.060738 | \n",
"
\n",
" \n",
" | 51 | \n",
" 2020-03-24 | \n",
" 5.901374 | \n",
" 4.866121 | \n",
" 0.760309 | \n",
" 0.807132 | \n",
" 1.035253 | \n",
"
\n",
" \n",
" | 52 | \n",
" 2020-03-25 | \n",
" 1.930207 | \n",
" 1.363523 | \n",
" 0.731261 | \n",
" 0.922063 | \n",
" 0.566684 | \n",
"
\n",
" \n",
"
\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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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": [
"bp.plot(x=\"date\", y=\"estpos\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"bp = estimate_table(\"20200201\", \"20200325\", (\"real\", {\"F161128\": 100}), (\"est\", {\"962700\": 95}) )"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.10711800037902634 \n",
"标准差偏离: 0.18298292891054715\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.555 \n",
"5% 分位: -0.316 \n",
"25% 分位: -0.071 \n",
"50% 分位: -0.025 \n",
"75% 分位: 0.013 \n",
"95% 分位: 0.151 \n",
"99% 分位: 0.422\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.27 \n",
"预测涨的比实际多: 0.16 \n",
"预测跌的比实际多: 0.16 \n",
"预测跌的比实际少: 0.41\n"
]
}
],
"source": [
"analyse_all(bp, \"diff_real_est\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"bp = estimate_table_with_pos(\"20200201\", \"20200325\", (\"real\", {\"F161128\": 100}), (\"est\", {\"962700\": 95}) )"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.1178957348472328 \n",
"标准差偏离: 0.20216755173213008\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.703 \n",
"5% 分位: -0.366 \n",
"25% 分位: -0.094 \n",
"50% 分位: -0.009 \n",
"75% 分位: 0.024 \n",
"95% 分位: 0.143 \n",
"99% 分位: 0.357\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.24 \n",
"预测涨的比实际多: 0.19 \n",
"预测跌的比实际多: 0.22 \n",
"预测跌的比实际少: 0.35\n"
]
}
],
"source": [
"analyse_all(bp, \"diff_real_est\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xa.Compare(\"F161116\", (\"commodities/gold\",\"USD\"), start=\"20180101\").v()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" F161116 | \n",
" commodities/gold | \n",
"
\n",
" \n",
" \n",
" \n",
" | F161116 | \n",
" 1.00000 | \n",
" 0.59971 | \n",
"
\n",
" \n",
" | commodities/gold | \n",
" 0.59971 | \n",
" 1.00000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" F161116 commodities/gold\n",
"F161116 1.00000 0.59971\n",
"commodities/gold 0.59971 1.00000"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xa.Compare(\"F161116\", (\"commodities/gold\",\"USD\"), start=\"20180101\").corr() # 和黄金相关系数不及格。。。。"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"gld = estimate_table_with_pos(\"20191001\", \"20200225\", (\"real\", {\"F161116\": 100}), (\"est\", holdings_161116_19s4),\n",
" window=3, decay=0.8)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.19134468565719392 \n",
"标准差偏离: 0.2517831260198441\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.621 \n",
"5% 分位: -0.452 \n",
"25% 分位: -0.105 \n",
"50% 分位: 0.02 \n",
"75% 分位: 0.162 \n",
"95% 分位: 0.447 \n",
"99% 分位: 0.578\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.41 \n",
"预测涨的比实际多: 0.16 \n",
"预测跌的比实际多: 0.13 \n",
"预测跌的比实际少: 0.3\n"
]
}
],
"source": [
"analyse_all(gld, \"diff_real_est\") # 0.49, 0.92 20 年 平稳时期"
]
},
{
"cell_type": "code",
"execution_count": 15,
"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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xa.Compare(\"F160719\", \"F159937\",(\"commodities/gold\",\"USD\"), start=\"20180601\").v() # 跟不上啊"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" F160719 | \n",
" commodities/gold | \n",
"
\n",
" \n",
" \n",
" \n",
" | F160719 | \n",
" 1.000000 | \n",
" 0.716482 | \n",
"
\n",
" \n",
" | commodities/gold | \n",
" 0.716482 | \n",
" 1.000000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" F160719 commodities/gold\n",
"F160719 1.000000 0.716482\n",
"commodities/gold 0.716482 1.000000"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xa.Compare(\"F160719\", (\"commodities/gold\",\"USD\"), start=\"20180101\").corr() "
]
},
{
"cell_type": "code",
"execution_count": 17,
"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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gld.plot(x=\"date\", y=\"estpos\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/shixin/Documents/newwork/quant finance/xalpha/xalpha/universal.py:152: 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": [
"warning: this fund has no data for rate\n"
]
},
{
"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.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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 = $('');\n",
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $('');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $('');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $('');\n",
"\n",
" var fmt_picker = $('');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" '', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html('
');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = '
';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\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) { 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": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gtsp.plot(x=\"date\", y=[\"estpos\", \"estposld\"])"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"gtsp=\\\n",
"estimate_table_with_pos(\"20190101\", \"20200305\", (\"real\", {\"F160216\": 100}), (\"est\", holdings[\"160216\"]),\n",
" pos=0.9, window=3, decay=0.8, smooth=smooth_pos)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"净值预测回测分析:\n",
"\n",
"\n",
"平均偏离: 0.11508219285292358 \n",
"标准差偏离: 0.15686811933164138\n",
"\n",
"预测偏差分位: \n",
"1% 分位: -0.348 \n",
"5% 分位: -0.22 \n",
"25% 分位: -0.105 \n",
"50% 分位: -0.022 \n",
"75% 分位: 0.079 \n",
"95% 分位: 0.225 \n",
"99% 分位: 0.403\n",
"\n",
"涨跌偏差分析: \n",
"预测涨的比实际少: 0.23 \n",
"预测涨的比实际多: 0.35 \n",
"预测跌的比实际多: 0.19 \n",
"预测跌的比实际少: 0.23\n"
]
}
],
"source": [
"# 0.09 0.13\n",
"analyse_all(gtsp, \"diff_real_est\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}