{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(root) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " var force = true;\n", "\n", " if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n", " root._bokeh_onload_callbacks = [];\n", " root._bokeh_is_loading = undefined;\n", " }\n", "\n", " var JS_MIME_TYPE = 'application/javascript';\n", " var HTML_MIME_TYPE = 'text/html';\n", " var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n", " var CLASS_NAME = 'output_bokeh rendered_html';\n", "\n", " /**\n", " * Render data to the DOM node\n", " */\n", " function render(props, node) {\n", " var script = document.createElement(\"script\");\n", " node.appendChild(script);\n", " }\n", "\n", " /**\n", " * Handle when an output is cleared or removed\n", " */\n", " function handleClearOutput(event, handle) {\n", " var cell = handle.cell;\n", "\n", " var id = cell.output_area._bokeh_element_id;\n", " var server_id = cell.output_area._bokeh_server_id;\n", " // Clean up Bokeh references\n", " if (id !== undefined) {\n", " Bokeh.index[id].model.document.clear();\n", " delete Bokeh.index[id];\n", " }\n", "\n", " if (server_id !== undefined) {\n", " // Clean up Bokeh references\n", " var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n", " cell.notebook.kernel.execute(cmd, {\n", " iopub: {\n", " output: function(msg) {\n", " var element_id = msg.content.text.trim();\n", " Bokeh.index[element_id].model.document.clear();\n", " delete Bokeh.index[element_id];\n", " }\n", " }\n", " });\n", " // Destroy server and session\n", " var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n", " cell.notebook.kernel.execute(cmd);\n", " }\n", " }\n", "\n", " /**\n", " * Handle when a new output is added\n", " */\n", " function handleAddOutput(event, handle) {\n", " var output_area = handle.output_area;\n", " var output = handle.output;\n", "\n", " // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n", " if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n", " return\n", " }\n", "\n", " var toinsert = output_area.element.find(`.${CLASS_NAME.split(' ')[0]}`);\n", "\n", " if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n", " toinsert[0].firstChild.textContent = output.data[JS_MIME_TYPE];\n", " // store reference to embed id on output_area\n", " output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n", " }\n", " if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n", " var bk_div = document.createElement(\"div\");\n", " bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n", " var script_attrs = bk_div.children[0].attributes;\n", " for (var i = 0; i < script_attrs.length; i++) {\n", " toinsert[0].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n", " }\n", " // store reference to server id on output_area\n", " output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n", " }\n", " }\n", "\n", " function register_renderer(events, OutputArea) {\n", "\n", " function append_mime(data, metadata, element) {\n", " // create a DOM node to render to\n", " var toinsert = this.create_output_subarea(\n", " metadata,\n", " CLASS_NAME,\n", " EXEC_MIME_TYPE\n", " );\n", " this.keyboard_manager.register_events(toinsert);\n", " // Render to node\n", " var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n", " render(props, toinsert[0]);\n", " element.append(toinsert);\n", " return toinsert\n", " }\n", "\n", " /* Handle when an output is cleared or removed */\n", " events.on('clear_output.CodeCell', handleClearOutput);\n", " events.on('delete.Cell', handleClearOutput);\n", "\n", " /* Handle when a new output is added */\n", " events.on('output_added.OutputArea', handleAddOutput);\n", "\n", " /**\n", " * Register the mime type and append_mime function with output_area\n", " */\n", " OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n", " /* Is output safe? */\n", " safe: true,\n", " /* Index of renderer in `output_area.display_order` */\n", " index: 0\n", " });\n", " }\n", "\n", " // register the mime type if in Jupyter Notebook environment and previously unregistered\n", " if (root.Jupyter !== undefined) {\n", " var events = require('base/js/events');\n", " var OutputArea = require('notebook/js/outputarea').OutputArea;\n", "\n", " if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n", " register_renderer(events, OutputArea);\n", " }\n", " }\n", "\n", " \n", " if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n", " root._bokeh_timeout = Date.now() + 5000;\n", " root._bokeh_failed_load = false;\n", " }\n", "\n", " var NB_LOAD_WARNING = {'data': {'text/html':\n", " \"
\\n\"+\n", " \"

\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"

\\n\"+\n", " \"\\n\"+\n", " \"\\n\"+\n", " \"from bokeh.resources import INLINE\\n\"+\n", " \"output_notebook(resources=INLINE)\\n\"+\n", " \"\\n\"+\n", " \"
\"}};\n", "\n", " function display_loaded() {\n", " var el = document.getElementById(\"7caf8012-e1db-4332-be5e-7e8b3eb8e9ed\");\n", " if (el != null) {\n", " el.textContent = \"BokehJS is loading...\";\n", " }\n", " if (root.Bokeh !== undefined) {\n", " if (el != null) {\n", " el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n", " }\n", " } else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(display_loaded, 100)\n", " }\n", " }\n", "\n", "\n", " function run_callbacks() {\n", " try {\n", " root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n", " }\n", " finally {\n", " delete root._bokeh_onload_callbacks\n", " }\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " root._bokeh_onload_callbacks.push(callback);\n", " if (root._bokeh_is_loading > 0) {\n", " console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " root._bokeh_is_loading = js_urls.length;\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var s = document.createElement('script');\n", " s.src = url;\n", " s.async = false;\n", " s.onreadystatechange = s.onload = function() {\n", " root._bokeh_is_loading--;\n", " if (root._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: all BokehJS libraries loaded\");\n", " run_callbacks()\n", " }\n", " };\n", " s.onerror = function() {\n", " console.warn(\"failed to load library \" + url);\n", " };\n", " console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", " }\n", " };var element = document.getElementById(\"7caf8012-e1db-4332-be5e-7e8b3eb8e9ed\");\n", " if (element == null) {\n", " console.log(\"Bokeh: ERROR: autoload.js configured with elementid '7caf8012-e1db-4332-be5e-7e8b3eb8e9ed' but no matching script tag was found. \")\n", " return false;\n", " }\n", "\n", " var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.10.min.js\"];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " \n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if ((root.Bokeh !== undefined) || (force === true)) {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i].call(root, root.Bokeh);\n", " }if (force === true) {\n", " display_loaded();\n", " }} else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!root._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " root._bokeh_failed_load = true;\n", " } else if (force !== true) {\n", " var cell = $(document.getElementById(\"7caf8012-e1db-4332-be5e-7e8b3eb8e9ed\")).parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", " }\n", "\n", " }\n", "\n", " if (root._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(js_urls, function() {\n", " console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(window));" ], "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n\n if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n \n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n var NB_LOAD_WARNING = {'data': {'text/html':\n \"
\\n\"+\n \"

\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"

\\n\"+\n \"\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"\\n\"+\n \"
\"}};\n\n function display_loaded() {\n var el = document.getElementById(\"7caf8012-e1db-4332-be5e-7e8b3eb8e9ed\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n }\n finally {\n delete root._bokeh_onload_callbacks\n }\n console.info(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(js_urls, callback) {\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n }\n if (js_urls == null || js_urls.length === 0) {\n run_callbacks();\n return null;\n }\n console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = js_urls.length;\n for (var i = 0; i < js_urls.length; i++) {\n var url = js_urls[i];\n var s = document.createElement('script');\n s.src = url;\n s.async = false;\n s.onreadystatechange = s.onload = function() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.log(\"Bokeh: all BokehJS libraries loaded\");\n run_callbacks()\n }\n };\n s.onerror = function() {\n console.warn(\"failed to load library \" + url);\n };\n console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.getElementsByTagName(\"head\")[0].appendChild(s);\n }\n };var element = document.getElementById(\"7caf8012-e1db-4332-be5e-7e8b3eb8e9ed\");\n if (element == null) {\n console.log(\"Bokeh: ERROR: autoload.js configured with elementid '7caf8012-e1db-4332-be5e-7e8b3eb8e9ed' but no matching script tag was found. \")\n return false;\n }\n\n var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.10.min.js\"];\n\n var inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n \n function(Bokeh) {\n \n },\n function(Bokeh) {\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n }\n ];\n\n function run_inline_js() {\n \n if ((root.Bokeh !== undefined) || (force === true)) {\n for (var i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n var cell = $(document.getElementById(\"7caf8012-e1db-4332-be5e-7e8b3eb8e9ed\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(js_urls, function() {\n console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from bokeh.io import output_notebook\n", "from bokeh.plotting import figure, show\n", "import scipy.optimize as sco\n", "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 基本分散投資はマーコビッツの平均分散モデルが使われます。\n", "* 各資産の収益率、リスク、相関、共分散を使い、各資産にどのように投資したらどのようなリスクリターンの運用になるかを作ります。 \n", "* 1952年に発表された古い理論です。まぁ単純で簡単に作れるから使われているのだろう。\n", "* 難点 リターンの予想が難しいが、そのリターン予想にポートフォリオの決定が大きく影響される。\n", "* Scipyで2次計画問題を解く \n", "https://qiita.com/ryoshi81/items/b323a363e5442a15db6d \n", "* 最近の理論になるとブラック・リッターマンとかリスクパリティとかあります。 \n", " \n", "### ブラックリッターマン\n", "* qiitaの記事 https://qiita.com/nokomitch/items/0d1812763114e6266bf3 \n", "* サイト  http://www.blacklitterman.org/ \n", "* なんかすげーみたいな評価がされていますが、色々パラメーター決めるの難しそう。\n", " \n", "### リスクパリティ \n", "* 今までのやり方は結局リスクが大きい商品のポートフォリオへの影響が大きい。\n", "* 資産のリスクはあまり変化しない。\n", "* 各資産のリスクを同一にする。\n", "* そうすると基本債券のウェイトが高くなり、リターンが下がるがその分、レバレッジを掛ける。\n", "* 世界最大のヘッジファンド:ブリッジウォーターが提唱したやり方で、徐々に取り入れられ始めているのではないか?\n", "* しかし、現在債券バブルと言われる環境下にあるので、それほどおすすめでない。\n", "* Bridgewater /Research_Library/Risk_parity: https://www.bridgewater.com/research-library/risk-parity/ " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## さて進めましょう!\n", "* 今回はロボアドバイザー大手のウェルスナビのモデルを使います。 \n", "https://www.wealthnavi.com/image/WealthNavi_WhitePaper.pdf \n", "* リターンの推測にはブラック・リターマンが使われている。 \n", "* 上記のPDFにウェルスナビの考えるリスクリターンと相関が掲載されています。\n", "* 資産クラスは米国株、欧日株、新興株、米国債券、物価連動債、金、不動産の7資産となっています。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rtns = np.array([0.065, 0.075, 0.085, 0.019, 0.023, 0.039, 0.058]) # 収益率(年率)\n", "rsks = np.array([0.124, 0.148, 0.184, 0.028, 0.048, 0.179, 0.149]) # リスク(年率)\n", "corrs = np.array([\n", " [1.0, 0.9, 0.8, -0.1, 0, 0, 0.7],\n", " [0.9, 1.0, 0.8, -0.1, 0.1, 0.1, 0.6],\n", " [0.8, 0.8, 1.0, 0.1, 0.2, 0.3, 0.6],\n", " [-0.1, -0.1, 0.1, 1.0, 0.8, 0.4, 0.4],\n", " [0, 0.1, 0.2, 0.8, 1.0, 0.5, 0.4],\n", " [0, 0.1, 0.3, 0.4, 0.4, 1.0, 0.1],\n", " [0.7, 0.6, 0.6, 0.4, 0.4, 0.1, 1.0]\n", "]) # 相関" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " var docs_json = {\"8d0f882c-00d0-47b5-8e70-3b3f88089856\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"b01f33c9-33a6-462d-a9b8-345616b0198a\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"fa0233ab-2d1e-4317-ab83-757f1ce5841a\",\"type\":\"LinearAxis\"}],\"plot_height\":500,\"plot_width\":800,\"renderers\":[{\"id\":\"b01f33c9-33a6-462d-a9b8-345616b0198a\",\"type\":\"LinearAxis\"},{\"id\":\"b32eb51e-8597-4227-860d-d7837c1e0317\",\"type\":\"Grid\"},{\"id\":\"fa0233ab-2d1e-4317-ab83-757f1ce5841a\",\"type\":\"LinearAxis\"},{\"id\":\"8e3cb2b3-612f-4680-b541-0064ba49c43b\",\"type\":\"Grid\"},{\"id\":\"8efb1e3b-2113-4999-8747-067db6027615\",\"type\":\"BoxAnnotation\"},{\"id\":\"d2875913-c0a3-4b71-a396-a55ffb34b2de\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"352cc132-789c-4e3f-9a44-c40a21c61fff\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"28081540-0e9d-4722-a9a0-18f7b99426e9\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"855b17fd-1c00-4027-85da-0c7c292240eb\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"1ed0941e-f0c9-465a-b926-e86304f90e9f\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"905c8f3b-02dc-459c-ab14-ae76a4710e7b\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"5c259982-ec6e-46a9-8914-45d1652399a2\",\"type\":\"LinearScale\"}},\"id\":\"537621f4-1fe3-4d35-8f40-266c3c6532c5\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":{\"__ndarray__\":\"WDm0yHa+vz+LbOf7qfHCP1pkO99Pjcc/eekmMQisnD/6fmq8dJOoP7bz/dR46cY/30+Nl24Swz8=\",\"dtype\":\"float64\",\"shape\":[7]},\"y\":{\"__ndarray__\":\"pHA9CtejsD8zMzMzMzOzP8P1KFyPwrU/2/l+arx0kz9aZDvfT42XPyuHFtnO96M/GQRWDi2yrT8=\",\"dtype\":\"float64\",\"shape\":[7]}}},\"id\":\"728f7a49-fcbb-4bef-8aec-080ce4dd7374\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"plot\":null,\"text\":\"\\u5404\\u8cc7\\u7523\\u306e\\u30ea\\u30b9\\u30af\\u30ea\\u30bf\\u30fc\\u30f3\"},\"id\":\"352cc132-789c-4e3f-9a44-c40a21c61fff\",\"type\":\"Title\"},{\"attributes\":{\"callback\":null},\"id\":\"855b17fd-1c00-4027-85da-0c7c292240eb\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1d090f41-8914-46ca-b6ba-62f5103675a5\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"a1732353-f198-4bed-a13a-96a91bd47174\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"594c62f8-c7a5-4cfb-bbfb-7ccb4fadd3b1\",\"type\":\"PanTool\"},{\"id\":\"a4fb7bdb-f67b-4474-aac3-f1ef150375b4\",\"type\":\"WheelZoomTool\"},{\"id\":\"968ab90b-b828-471a-97ef-591c5dd7d655\",\"type\":\"BoxZoomTool\"},{\"id\":\"5fbc76a7-96b8-45b7-87ef-06a4f634f8e0\",\"type\":\"SaveTool\"},{\"id\":\"956814e6-1795-40fb-9957-ca1ecee40333\",\"type\":\"ResetTool\"},{\"id\":\"ac41fed4-5bdb-4607-bc02-9d4fa7c88dc9\",\"type\":\"HelpTool\"}]},\"id\":\"28081540-0e9d-4722-a9a0-18f7b99426e9\",\"type\":\"Toolbar\"},{\"attributes\":{\"axis_label\":\"\\u30ea\\u30b9\\u30af\",\"formatter\":{\"id\":\"1d090f41-8914-46ca-b6ba-62f5103675a5\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"537621f4-1fe3-4d35-8f40-266c3c6532c5\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"bcd162c3-d698-4a1d-b4d3-72299d093f74\",\"type\":\"BasicTicker\"}},\"id\":\"b01f33c9-33a6-462d-a9b8-345616b0198a\",\"type\":\"LinearAxis\"},{\"attributes\":{\"callback\":null},\"id\":\"905c8f3b-02dc-459c-ab14-ae76a4710e7b\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1ed0941e-f0c9-465a-b926-e86304f90e9f\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"5c259982-ec6e-46a9-8914-45d1652399a2\",\"type\":\"LinearScale\"},{\"attributes\":{\"plot\":{\"id\":\"537621f4-1fe3-4d35-8f40-266c3c6532c5\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"bcd162c3-d698-4a1d-b4d3-72299d093f74\",\"type\":\"BasicTicker\"}},\"id\":\"b32eb51e-8597-4227-860d-d7837c1e0317\",\"type\":\"Grid\"},{\"attributes\":{\"axis_label\":\"\\u30ea\\u30bf\\u30fc\\u30f3\",\"formatter\":{\"id\":\"a1732353-f198-4bed-a13a-96a91bd47174\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"537621f4-1fe3-4d35-8f40-266c3c6532c5\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"a3e33b70-ed3b-40aa-ae6b-04f969a29c8c\",\"type\":\"BasicTicker\"}},\"id\":\"fa0233ab-2d1e-4317-ab83-757f1ce5841a\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"a3e33b70-ed3b-40aa-ae6b-04f969a29c8c\",\"type\":\"BasicTicker\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"537621f4-1fe3-4d35-8f40-266c3c6532c5\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"a3e33b70-ed3b-40aa-ae6b-04f969a29c8c\",\"type\":\"BasicTicker\"}},\"id\":\"8e3cb2b3-612f-4680-b541-0064ba49c43b\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"594c62f8-c7a5-4cfb-bbfb-7ccb4fadd3b1\",\"type\":\"PanTool\"},{\"attributes\":{\"source\":{\"id\":\"728f7a49-fcbb-4bef-8aec-080ce4dd7374\",\"type\":\"ColumnDataSource\"}},\"id\":\"726673b0-f471-4db6-8576-8264e92467a1\",\"type\":\"CDSView\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"8efb1e3b-2113-4999-8747-067db6027615\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"data_source\":{\"id\":\"728f7a49-fcbb-4bef-8aec-080ce4dd7374\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1c857710-6202-4402-8d65-806a0e200efc\",\"type\":\"Cross\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"63b03167-493e-4f9d-adcc-1ca440c4079c\",\"type\":\"Cross\"},\"selection_glyph\":null,\"view\":{\"id\":\"726673b0-f471-4db6-8576-8264e92467a1\",\"type\":\"CDSView\"}},\"id\":\"d2875913-c0a3-4b71-a396-a55ffb34b2de\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"a4fb7bdb-f67b-4474-aac3-f1ef150375b4\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":20},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1c857710-6202-4402-8d65-806a0e200efc\",\"type\":\"Cross\"},{\"attributes\":{\"overlay\":{\"id\":\"8efb1e3b-2113-4999-8747-067db6027615\",\"type\":\"BoxAnnotation\"}},\"id\":\"968ab90b-b828-471a-97ef-591c5dd7d655\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"5fbc76a7-96b8-45b7-87ef-06a4f634f8e0\",\"type\":\"SaveTool\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":20},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"63b03167-493e-4f9d-adcc-1ca440c4079c\",\"type\":\"Cross\"},{\"attributes\":{},\"id\":\"956814e6-1795-40fb-9957-ca1ecee40333\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"bcd162c3-d698-4a1d-b4d3-72299d093f74\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"ac41fed4-5bdb-4607-bc02-9d4fa7c88dc9\",\"type\":\"HelpTool\"}],\"root_ids\":[\"537621f4-1fe3-4d35-8f40-266c3c6532c5\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.10\"}};\n", " var render_items = [{\"docid\":\"8d0f882c-00d0-47b5-8e70-3b3f88089856\",\"elementid\":\"f3694a55-b6fa-49f7-8f4e-7af60cce9314\",\"modelid\":\"537621f4-1fe3-4d35-8f40-266c3c6532c5\"}];\n", "\n", " root.Bokeh.embed.embed_items(docs_json, render_items);\n", " }\n", "\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " var attempts = 0;\n", " var timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " clearInterval(timer);\n", " }\n", " attempts++;\n", " if (attempts > 100) {\n", " console.log(\"Bokeh: ERROR: Unable to embed document because BokehJS library is missing\")\n", " clearInterval(timer);\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "537621f4-1fe3-4d35-8f40-266c3c6532c5" } }, "output_type": "display_data" } ], "source": [ "p = figure(width= 800, height = 500, title='各資産のリスクリターン')\n", "p.cross(rsks, rtns, size=20)\n", "p.xaxis.axis_label='リスク'\n", "p.yaxis.axis_label='リターン'\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# 相関行列と標準偏差から共分散行列を作成\n", "# 相関係数(x, y) = 共分散 / (標準偏差(x) * 標準偏差(y) )\n", "# 共分散 = 相関係数(x, y) * (標準偏差(x) * 標準偏差(y))\n", "# これを作る部分が難しい。なんか簡単な方法ないのかなぁ。\n", "\n", "a1 = []\n", "a2 = []\n", "a3 = []\n", "a4 = []\n", "a5 = []\n", "a6 = []\n", "a7 = []\n", "\n", "def testi(r = 0.065, box = a1, volls = rsks):\n", " for i in range(len(rsks)):\n", " box.append(np.dot(r ,rsks[i]))\n", " return np.array(box)\n", "\n", "b1 = testi()\n", "b2 = testi(r = rsks[1], box = a2)\n", "b3 = testi(r = rsks[2], box = a3)\n", "b4 = testi(r = rsks[3], box = a4)\n", "b5 = testi(r = rsks[4], box = a5)\n", "b6 = testi(r = rsks[5], box = a6)\n", "b7 = testi(r = rsks[6], box = a7)\n", "\n", "SIgma = [b1 * np.array(corrs[0]), b2 * np.array(corrs[1]), b3 * np.array(corrs[2]), \\\n", " b4 * np.array(corrs[3]), b5 * np.array(corrs[4]), b6 * np.array(corrs[5]), b7 * np.array(corrs[6])]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 以上で各資産のリスクリターン共分散を作成しました。\n", "## 以下ではscipy.optimize.minimize()を解くための引数などを作ります。\n", "* 目的関数、初期解、ソルバー、資産配分の上下制約、制約条件が必要です。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 目的関数\n", "def min_func_var(weights):\n", " return np.dot(weights.T, np.dot(SIgma, weights))\n", "\n", "# 初期解\n", "x0 = [1.0 / len(rtns)] * len(rtns)\n", "\n", "# 目標リターン(各資産の最高、最低で作る。レバレッジを掛けない限り採用資産のリターンの最高がポートフォリオのリターンの上限となる)\n", "\n", "max_ret = rtns.max()\n", "min_ret = rtns.min()\n", "trets = np.linspace(min_ret, max_ret, 100)\n", "\n", "# 投資比率制限(空売りを出来なくする)\n", "bnds = [(0, None)] * len(rtns)\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "tvols = []\n", "\n", "for tret in trets:\n", " cons = [{'type': 'eq', 'fun': lambda x: np.sum(x) - 1},\n", " {'type': 'ineq', 'fun': lambda x: np.sum(rtns * x) - tret}]\n", " res = sco.minimize(fun=min_func_var, x0 = x0, method = 'SLSQP', bounds = bnds, constraints = cons)\n", " tvols.append((np.sqrt(res['fun'])))\n", "\n", "tvols = np.array(tvols)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " var docs_json = {\"badaed05-7fb7-4e80-b061-05a58c53b61a\":{\"roots\":{\"references\":[{\"attributes\":{},\"id\":\"5bf389a1-aea0-482e-8bfb-deb28f847b7c\",\"type\":\"BasicTicker\"},{\"attributes\":{\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"line_width\":{\"value\":3},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"c84d588a-6237-4aea-a137-d723f04ac752\",\"type\":\"Line\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"0c4e08c4-97fc-4b47-a732-d0bfd60929bc\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"5bf389a1-aea0-482e-8bfb-deb28f847b7c\",\"type\":\"BasicTicker\"}},\"id\":\"3303f131-215d-4a1c-bb45-2c27c61e2691\",\"type\":\"Grid\"},{\"attributes\":{\"line_color\":{\"value\":\"blue\"},\"line_width\":{\"value\":3},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"ffa93d07-84bc-4248-a5e0-13796757c518\",\"type\":\"Line\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":{\"__ndarray__\":\"WDm0yHa+vz+LbOf7qfHCP1pkO99Pjcc/eekmMQisnD/6fmq8dJOoP7bz/dR46cY/30+Nl24Swz8=\",\"dtype\":\"float64\",\"shape\":[7]},\"y\":{\"__ndarray__\":\"pHA9CtejsD8zMzMzMzOzP8P1KFyPwrU/2/l+arx0kz9aZDvfT42XPyuHFtnO96M/GQRWDi2yrT8=\",\"dtype\":\"float64\",\"shape\":[7]}}},\"id\":\"4a422ad0-861a-4787-a60c-f48590d53901\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1161c4df-662b-4693-8469-e6c61a3b2043\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"axis_label\":\"\\u30dd\\u30fc\\u30c8\\u30d5\\u30a9\\u30ea\\u30aa\\u306e\\u30ea\\u30b9\\u30af\",\"formatter\":{\"id\":\"460190e2-7717-4905-84a5-78c10a96e9a6\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"0c4e08c4-97fc-4b47-a732-d0bfd60929bc\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"a8a358b9-a094-4837-926a-f16aa1beb0ef\",\"type\":\"BasicTicker\"}},\"id\":\"9ee6767d-d85b-4810-b014-1f754de6036f\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"a8a358b9-a094-4837-926a-f16aa1beb0ef\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"9603adcb-b50a-4437-9eb4-5cf0d68ed660\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"a59fe90a-22f6-43e4-8045-ebf056e13551\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"460190e2-7717-4905-84a5-78c10a96e9a6\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"ad7e84dc-7157-4c2b-bf46-363207d4869d\",\"type\":\"LinearScale\"},{\"attributes\":{\"source\":{\"id\":\"b877faee-8e5f-4727-99ca-c8d7d779308c\",\"type\":\"ColumnDataSource\"}},\"id\":\"b4f78b40-702d-45a1-b6b1-f0afbae80387\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"934978fa-6925-4dd5-8462-391b470045be\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1afe4c1c-920b-4da5-b9e0-506f5b362183\",\"type\":\"LinearScale\"},{\"attributes\":{\"overlay\":{\"id\":\"1161c4df-662b-4693-8469-e6c61a3b2043\",\"type\":\"BoxAnnotation\"}},\"id\":\"2dd31b1e-aaa8-43a1-b08e-21ded0d5ed06\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"7ac7a48c-9274-4b7d-8dcb-b52823feda0c\",\"type\":\"SaveTool\"},{\"attributes\":{\"callback\":null},\"id\":\"db48578c-7140-44f5-a794-2494ef87529f\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"2ce66e67-7879-4c62-8ae2-c6fe71544e0f\",\"type\":\"ResetTool\"},{\"attributes\":{\"callback\":null},\"id\":\"ff257af5-f743-44e6-87c8-3b8ca856e681\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"fec5054f-91e4-4a00-8295-acffea391d1e\",\"type\":\"HelpTool\"},{\"attributes\":{\"source\":{\"id\":\"4a422ad0-861a-4787-a60c-f48590d53901\",\"type\":\"ColumnDataSource\"}},\"id\":\"fdd45824-3288-4382-af45-5a3a7afcea40\",\"type\":\"CDSView\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":20},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"938d33a1-0313-4f10-b0d6-f22b6b41b0f2\",\"type\":\"Cross\"},{\"attributes\":{\"data_source\":{\"id\":\"4a422ad0-861a-4787-a60c-f48590d53901\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"f9931e26-5e0e-44fe-9843-fc73f54e44a7\",\"type\":\"Cross\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"938d33a1-0313-4f10-b0d6-f22b6b41b0f2\",\"type\":\"Cross\"},\"selection_glyph\":null,\"view\":{\"id\":\"fdd45824-3288-4382-af45-5a3a7afcea40\",\"type\":\"CDSView\"}},\"id\":\"bff156b6-d80e-4612-a68f-bb79c939735a\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"data_source\":{\"id\":\"b877faee-8e5f-4727-99ca-c8d7d779308c\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"ffa93d07-84bc-4248-a5e0-13796757c518\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"c84d588a-6237-4aea-a137-d723f04ac752\",\"type\":\"Line\"},\"selection_glyph\":null,\"view\":{\"id\":\"b4f78b40-702d-45a1-b6b1-f0afbae80387\",\"type\":\"CDSView\"}},\"id\":\"87904e2e-e901-468d-afac-a46d0d94d757\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"below\":[{\"id\":\"9ee6767d-d85b-4810-b014-1f754de6036f\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"c0325e90-a60c-47bd-824d-71b04c1f6e64\",\"type\":\"LinearAxis\"}],\"plot_height\":500,\"plot_width\":800,\"renderers\":[{\"id\":\"9ee6767d-d85b-4810-b014-1f754de6036f\",\"type\":\"LinearAxis\"},{\"id\":\"b495cb8c-d6dc-438a-aa94-0f18da0f42fd\",\"type\":\"Grid\"},{\"id\":\"c0325e90-a60c-47bd-824d-71b04c1f6e64\",\"type\":\"LinearAxis\"},{\"id\":\"3303f131-215d-4a1c-bb45-2c27c61e2691\",\"type\":\"Grid\"},{\"id\":\"1161c4df-662b-4693-8469-e6c61a3b2043\",\"type\":\"BoxAnnotation\"},{\"id\":\"87904e2e-e901-468d-afac-a46d0d94d757\",\"type\":\"GlyphRenderer\"},{\"id\":\"bff156b6-d80e-4612-a68f-bb79c939735a\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"122e73bc-ef15-411f-a331-aa0d11e3c85d\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"2db66db2-94f8-4b53-b7de-3f2ec782809b\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"ff257af5-f743-44e6-87c8-3b8ca856e681\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"1afe4c1c-920b-4da5-b9e0-506f5b362183\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"db48578c-7140-44f5-a794-2494ef87529f\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"ad7e84dc-7157-4c2b-bf46-363207d4869d\",\"type\":\"LinearScale\"}},\"id\":\"0c4e08c4-97fc-4b47-a732-d0bfd60929bc\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":{\"__ndarray__\":\"2Ugu3qZwmj/ZSC7epnCaP9lILt6mcJo/2Ugu3qZwmj/ZSC7epnCaPxdfLt6mcJo/bdxGse1xmj8LcCk3LHGaP9r+PZ5wcZo/5pJL8waBmj8HjW+XcaSaP6qieRhi25o/C4y97mElmz+HgGZJ14GbP0EbGeUK8Js/bFRveC5vnD9AX6M3Y/6cP8/oigvAnJ0/G7GyIOgIoT98/QsvOwOfPx6z0omEbaE/DP84jgmuoT+BwnJaHPehP5pkMfPyR6I/EKSQz8Ofoj9hHZG1jiGiP88W6vpeY6M/2BwkQTYkoz9cRrICSUCkPwhProL4s6Q/HVjK6oovpT+T12cTNKqlP1SEPYBaK6Y/A04ZrZ6upj9oH6hwDDWnPxvxqdatvac/4w7WiMZIqD/jSVo9stWoP8uCr7E3Zak/laQlBTT3qT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j/1b4QyelumP4AjlNHbsqY/CtejcD0Kpz+UirMPn2GnPx8+w64Auac/qvHSTWIQqD80peLsw2eoP79Y8oslv6g/SgwCK4cWqT/UvxHK6G2pP15zIWlKxak/6iYxCKwcqj902kCnDXSqP/6NUEZvy6o/iEFg5dAiqz8U9W+EMnqrP56ofyOU0as/KFyPwvUorD+0D59hV4CsPz7DrgC516w/yHa+nxovrT9UKs4+fIatP97d3d3d3a0/aJHtfD81rj/yRP0boYyuP374DLsC5K4/CKwcWmQ7rz+SXyz5xZKvPx4TPJgn6q8/VOOlm8QgsD8ZvS1rdUywP96WtTomeLA/pHA9CtejsD9pSsXZh8+wPy4kTak4+7A/9P3UeOkmsT+511xImlKxP36x5BdLfrE/Q4ts5/upsT8JZfS2rNWxP84+fIZdAbI/kxgEVg4tsj9Z8oslv1iyPx7ME/VvhLI/46WbxCCwsj+ofyOU0duyP25Zq2OCB7M/MzMzMzMzsz/4DLsC5F6zP77mQtKUirM/g8DKoUW2sz9ImlJx9uGzPw102kCnDbQ/001iEFg5tD+YJ+rfCGW0P10Bcq+5kLQ/I9v5fmq8tD/otIFOG+i0P62OCR7ME7U/c2iR7Xw/tT84Qhm9LWu1P/0boYzelrU/w/UoXI/CtT8=\",\"dtype\":\"float64\",\"shape\":[100]}}},\"id\":\"b877faee-8e5f-4727-99ca-c8d7d779308c\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":20},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"f9931e26-5e0e-44fe-9843-fc73f54e44a7\",\"type\":\"Cross\"},{\"attributes\":{\"plot\":null,\"text\":\"\\u30dd\\u30fc\\u30c8\\u30d5\\u30a9\\u30ea\\u30aa\\u306e\\u30ea\\u30b9\\u30af\\u30ea\\u30bf\\u30fc\\u30f3\"},\"id\":\"122e73bc-ef15-411f-a331-aa0d11e3c85d\",\"type\":\"Title\"},{\"attributes\":{\"axis_label\":\"\\u30dd\\u30fc\\u30c8\\u30d5\\u30a9\\u30ea\\u30aa\\u306e\\u30ea\\u30bf\\u30fc\\u30f3\",\"formatter\":{\"id\":\"9603adcb-b50a-4437-9eb4-5cf0d68ed660\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"0c4e08c4-97fc-4b47-a732-d0bfd60929bc\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"5bf389a1-aea0-482e-8bfb-deb28f847b7c\",\"type\":\"BasicTicker\"}},\"id\":\"c0325e90-a60c-47bd-824d-71b04c1f6e64\",\"type\":\"LinearAxis\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"a59fe90a-22f6-43e4-8045-ebf056e13551\",\"type\":\"PanTool\"},{\"id\":\"934978fa-6925-4dd5-8462-391b470045be\",\"type\":\"WheelZoomTool\"},{\"id\":\"2dd31b1e-aaa8-43a1-b08e-21ded0d5ed06\",\"type\":\"BoxZoomTool\"},{\"id\":\"7ac7a48c-9274-4b7d-8dcb-b52823feda0c\",\"type\":\"SaveTool\"},{\"id\":\"2ce66e67-7879-4c62-8ae2-c6fe71544e0f\",\"type\":\"ResetTool\"},{\"id\":\"fec5054f-91e4-4a00-8295-acffea391d1e\",\"type\":\"HelpTool\"}]},\"id\":\"2db66db2-94f8-4b53-b7de-3f2ec782809b\",\"type\":\"Toolbar\"},{\"attributes\":{\"plot\":{\"id\":\"0c4e08c4-97fc-4b47-a732-d0bfd60929bc\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"a8a358b9-a094-4837-926a-f16aa1beb0ef\",\"type\":\"BasicTicker\"}},\"id\":\"b495cb8c-d6dc-438a-aa94-0f18da0f42fd\",\"type\":\"Grid\"}],\"root_ids\":[\"0c4e08c4-97fc-4b47-a732-d0bfd60929bc\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.10\"}};\n", " var render_items = [{\"docid\":\"badaed05-7fb7-4e80-b061-05a58c53b61a\",\"elementid\":\"d7dd0e7f-000f-4c5e-95dc-06f305d31eab\",\"modelid\":\"0c4e08c4-97fc-4b47-a732-d0bfd60929bc\"}];\n", "\n", " root.Bokeh.embed.embed_items(docs_json, render_items);\n", " }\n", "\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " var attempts = 0;\n", " var timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " clearInterval(timer);\n", " }\n", " attempts++;\n", " if (attempts > 100) {\n", " console.log(\"Bokeh: ERROR: Unable to embed document because BokehJS library is missing\")\n", " clearInterval(timer);\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "0c4e08c4-97fc-4b47-a732-d0bfd60929bc" } }, "output_type": "display_data" } ], "source": [ "p = figure(width = 800, height=500, title='ポートフォリオのリスクリターン')\n", "p.line(tvols, trets, line_color='blue', line_width=3)\n", "p.cross(rsks, rtns, size = 20)\n", "p.xaxis.axis_label='ポートフォリオのリスク'\n", "p.yaxis.axis_label = 'ポートフォリオのリターン' \n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 資産にちょっと変更を加えてみる\n", "* 投資の基本として資産間の相関の低いものを入れるとポートフォリオのリターンが向上すると言うものがあります。\n", "* 分散投資とよく言われますが、よく似た動きの商品を沢山購入しても実は意味がありません。\n", "* ここではもしもの話として米株、欧日株、新興株の相関が低下したとしてのポートフォリオの見てみます。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "corrs2 = np.array([\n", " [1.0, 0.2, 0.1, -0.1, 0, 0, 0.7],\n", " [0.2, 1.0, 0.1, -0.1, 0.1, 0.1, 0.6],\n", " [0.1, 0.1, 1.0, 0.1, 0.2, 0.3, 0.6],\n", " [-0.1, -0.1, 0.1, 1.0, 0.8, 0.4, 0.4],\n", " [0, 0.1, 0.2, 0.8, 1.0, 0.5, 0.4],\n", " [0, 0.1, 0.3, 0.4, 0.4, 1.0, 0.1],\n", " [0.7, 0.6, 0.6, 0.4, 0.4, 0.1, 1.0]\n", "])\n", "\n", "# 最初の\n", "corrs = np.array([\n", " [1.0, 0.9, 0.8, -0.1, 0, 0, 0.7],\n", " [0.9, 1.0, 0.8, -0.1, 0.1, 0.1, 0.6],\n", " [0.8, 0.8, 1.0, 0.1, 0.2, 0.3, 0.6],\n", " [-0.1, -0.1, 0.1, 1.0, 0.8, 0.4, 0.4],\n", " [0, 0.1, 0.2, 0.8, 1.0, 0.5, 0.4],\n", " [0, 0.1, 0.3, 0.4, 0.4, 1.0, 0.1],\n", " [0.7, 0.6, 0.6, 0.4, 0.4, 0.1, 1.0]\n", "]) # 相関" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "c1 = []\n", "c2 = []\n", "c3 = []\n", "c4 = []\n", "c5 = []\n", "c6 = []\n", "c7 = []\n", "\n", "d1 = testi(r = rsks[0], box = c1)\n", "d2 = testi(r = rsks[1], box = c2)\n", "d3 = testi(r = rsks[2], box = c3)\n", "d4 = testi(r = rsks[3], box = c4)\n", "d5 = testi(r = rsks[4], box = c5)\n", "d6 = testi(r = rsks[5], box = c6)\n", "d7 = testi(r = rsks[6], box = c7)\n", "\n", "SIgma2 = [d1*np.array(corrs2[0]), d2*np.array(corrs2[1]), d3*np.array(corrs2[2]), d4*np.array(corrs2[3]), \\\n", " d5*np.array(corrs2[4]), d6*np.array(corrs2[5]), d7*np.array(corrs2[6])]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([ 0.015376 , 0.0036704, 0.0022816, -0.0003472, 0. ,\n", " 0. , 0.0129332]),\n", " array([ 0.0036704, 0.021904 , 0.0027232, -0.0004144, 0.0007104,\n", " 0.0026492, 0.0132312]),\n", " array([ 0.0022816, 0.0027232, 0.033856 , 0.0005152, 0.0017664,\n", " 0.0098808, 0.0164496]),\n", " array([-0.0003472, -0.0004144, 0.0005152, 0.000784 , 0.0010752,\n", " 0.0020048, 0.0016688]),\n", " array([ 0. , 0.0007104, 0.0017664, 0.0010752, 0.002304 ,\n", " 0.004296 , 0.0028608]),\n", " array([ 0. , 0.0026492, 0.0098808, 0.0020048, 0.0034368,\n", " 0.032041 , 0.0026671]),\n", " array([ 0.0129332, 0.0132312, 0.0164496, 0.0016688, 0.0028608,\n", " 0.0026671, 0.022201 ])]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SIgma2" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def min_func_var2(weights):\n", " return np.dot(weights.T, np.dot(SIgma2, weights))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "tvols2 = []\n", "\n", "for tret in trets:\n", " cons = [{'type': 'eq', 'fun': lambda x: np.sum(x) - 1},\n", " {'type': 'ineq', 'fun': lambda x: np.sum(rtns * x) - tret}]\n", " res = sco.minimize(fun=min_func_var2, x0 = x0, method = 'SLSQP', bounds = bnds, constraints = cons)\n", " tvols2.append((np.sqrt(res['fun'])))\n", "\n", "tvols2 = np.array(tvols2) " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " var docs_json = {\"229ac163-7f42-4bcc-9908-575b1650bace\":{\"roots\":{\"references\":[{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":{\"__ndarray__\":\"ClqGJT6cmj8KWoYlPpyaPwpahiU+nJo/ClqGJT6cmj8KWoYlPpyaPwpahiU+nJo/ClqGJT6cmj8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-6289d5e81c45\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"82830e91-9219-4947-9b1f-7b1e3bbc150e\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"source\":{\"id\":\"f98bff43-72fc-4c56-a73e-1005c36d8dab\",\"type\":\"ColumnDataSource\"}},\"id\":\"6f81d194-f389-443a-aa74-10c932d897d8\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"3a09c2c1-24e6-4a8e-92ca-dcee0b6f37e8\",\"type\":\"BasicTicker\"},{\"attributes\":{\"data_source\":{\"id\":\"f98bff43-72fc-4c56-a73e-1005c36d8dab\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"bbc4f98f-cd8a-4c64-b987-b0996985213b\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"9700a629-9be1-4f6e-b063-580ea6982083\",\"type\":\"Line\"},\"selection_glyph\":null,\"view\":{\"id\":\"6f81d194-f389-443a-aa74-10c932d897d8\",\"type\":\"CDSView\"}},\"id\":\"30ccb357-9303-482e-8f85-0628a0b3ecb1\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"label\":{\"value\":\"\\u9ad8\\u76f8\\u95a2\"},\"renderers\":[{\"id\":\"30ccb357-9303-482e-8f85-0628a0b3ecb1\",\"type\":\"GlyphRenderer\"}]},\"id\":\"6cf882b0-0160-4473-a9a1-626f41a63628\",\"type\":\"LegendItem\"},{\"attributes\":{\"source\":{\"id\":\"db7a672a-4798-46fc-b611-6289d5e81c45\",\"type\":\"ColumnDataSource\"}},\"id\":\"ad79870e-07d1-4e4b-8ba5-727f100844bb\",\"type\":\"CDSView\"},{\"attributes\":{\"items\":[{\"id\":\"6cf882b0-0160-4473-a9a1-626f41a63628\",\"type\":\"LegendItem\"},{\"id\":\"2c04e8a2-dc46-4e10-82c4-54a42d4941a8\",\"type\":\"LegendItem\"}],\"location\":\"top_left\",\"plot\":{\"id\":\"fd4189c9-7dbf-4f32-9d1e-74ffb5bcbf2f\",\"subtype\":\"Figure\",\"type\":\"Plot\"}},\"id\":\"7c60f20f-8beb-4624-a3fd-e9ae6df667a7\",\"type\":\"Legend\"},{\"attributes\":{},\"id\":\"8f6a2305-b05c-4751-990c-55c659e7a4c6\",\"type\":\"BasicTicker\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"fd4189c9-7dbf-4f32-9d1e-74ffb5bcbf2f\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"8f6a2305-b05c-4751-990c-55c659e7a4c6\",\"type\":\"BasicTicker\"}},\"id\":\"3a15de1f-de44-4fd8-a2d8-9be1939d3b0d\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"9f57a46a-0bdc-4439-9325-54fefb45799a\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"axis_label\":\"\\u30dd\\u30fc\\u30c8\\u30d5\\u30a9\\u30ea\\u30aa\\u306e\\u30ea\\u30b9\\u30af\",\"formatter\":{\"id\":\"ccda8428-9381-4509-b651-9ea5e91240cb\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"fd4189c9-7dbf-4f32-9d1e-74ffb5bcbf2f\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"3a09c2c1-24e6-4a8e-92ca-dcee0b6f37e8\",\"type\":\"BasicTicker\"}},\"id\":\"eed79408-f993-40a2-a5c9-d67169890117\",\"type\":\"LinearAxis\"},{\"attributes\":{\"overlay\":{\"id\":\"82830e91-9219-4947-9b1f-7b1e3bbc150e\",\"type\":\"BoxAnnotation\"}},\"id\":\"87b9bc25-842b-4ff6-924e-3eafca8aa6e9\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"2cec2e55-84e2-4a3c-8de8-8a3fe183ced1\",\"type\":\"HelpTool\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"8d03c902-ce7f-43b4-87b8-445c36f5fc2f\",\"type\":\"PanTool\"},{\"id\":\"3fe77a11-45ab-4e47-acfe-fb211ef67e9f\",\"type\":\"WheelZoomTool\"},{\"id\":\"87b9bc25-842b-4ff6-924e-3eafca8aa6e9\",\"type\":\"BoxZoomTool\"},{\"id\":\"edd7ebcd-3986-4d6f-8a47-5599e60e8fb5\",\"type\":\"SaveTool\"},{\"id\":\"5144d418-2f17-439f-89dc-21f09014740f\",\"type\":\"ResetTool\"},{\"id\":\"2cec2e55-84e2-4a3c-8de8-8a3fe183ced1\",\"type\":\"HelpTool\"}]},\"id\":\"264d9bdc-ab39-4c9c-a750-a4ed4b5e5944\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"ccda8428-9381-4509-b651-9ea5e91240cb\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"line_color\":{\"value\":\"blue\"},\"line_width\":{\"value\":3},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"bbc4f98f-cd8a-4c64-b987-b0996985213b\",\"type\":\"Line\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":{\"__ndarray__\":\"WDm0yHa+vz+LbOf7qfHCP1pkO99Pjcc/eekmMQisnD/6fmq8dJOoP7bz/dR46cY/30+Nl24Swz8=\",\"dtype\":\"float64\",\"shape\":[7]},\"y\":{\"__ndarray__\":\"pHA9CtejsD8zMzMzMzOzP8P1KFyPwrU/2/l+arx0kz9aZDvfT42XPyuHFtnO96M/GQRWDi2yrT8=\",\"dtype\":\"float64\",\"shape\":[7]}}},\"id\":\"f79a50e8-db66-4c63-8d93-f73531f95a41\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"edd7ebcd-3986-4d6f-8a47-5599e60e8fb5\",\"type\":\"SaveTool\"},{\"attributes\":{\"plot\":null,\"text\":\"\\u30dd\\u30fc\\u30c8\\u30d5\\u30a9\\u30ea\\u30aa\\u306e\\u30ea\\u30b9\\u30af\\u30ea\\u30bf\\u30fc\\u30f3\"},\"id\":\"11db82c2-f930-458f-b31c-89cc2f9290d0\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"5144d418-2f17-439f-89dc-21f09014740f\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"8d03c902-ce7f-43b4-87b8-445c36f5fc2f\",\"type\":\"PanTool\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":{\"__ndarray__\":\"2Ugu3qZwmj/ZSC7epnCaP9lILt6mcJo/2Ugu3qZwmj/ZSC7epnCaPxdfLt6mcJo/bdxGse1xmj8LcCk3LHGaP9r+PZ5wcZo/5pJL8waBmj8HjW+XcaSaP6qieRhi25o/C4y97mElmz+HgGZJ14GbP0EbGeUK8Js/bFRveC5vnD9AX6M3Y/6cP8/oigvAnJ0/G7GyIOgIoT98/QsvOwOfPx6z0omEbaE/DP84jgmuoT+BwnJaHPehP5pkMfPyR6I/EKSQz8Ofoj9hHZG1jiGiP88W6vpeY6M/2BwkQTYkoz9cRrICSUCkPwhProL4s6Q/HVjK6oovpT+T12cTNKqlP1SEPYBaK6Y/A04ZrZ6upj9oH6hwDDWnPxvxqdatvac/4w7WiMZIqD/jSVo9stWoP8uCr7E3Zak/laQlBTT3qT+fBZchNoqqPxd8foKUHqs/vYnvmBe1qz/vqlMLBk2sP8EmlzpV5qw/Xp+rEvOArT+O/thCtRyuP2INctymua4/gXnqyqpXrz9f7ulirPavP9CvAm9QS7A/KkCU7LibsD/7xraFjOywPz0ccTDKPbE/NcvilGCPsT/BOg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9W+EMnqrP56ofyOU0as/KFyPwvUorD+0D59hV4CsPz7DrgC516w/yHa+nxovrT9UKs4+fIatP97d3d3d3a0/aJHtfD81rj/yRP0boYyuP374DLsC5K4/CKwcWmQ7rz+SXyz5xZKvPx4TPJgn6q8/VOOlm8QgsD8ZvS1rdUywP96WtTomeLA/pHA9CtejsD9pSsXZh8+wPy4kTak4+7A/9P3UeOkmsT+511xImlKxP36x5BdLfrE/Q4ts5/upsT8JZfS2rNWxP84+fIZdAbI/kxgEVg4tsj9Z8oslv1iyPx7ME/VvhLI/46WbxCCwsj+ofyOU0duyP25Zq2OCB7M/MzMzMzMzsz/4DLsC5F6zP77mQtKUirM/g8DKoUW2sz9ImlJx9uGzPw102kCnDbQ/001iEFg5tD+YJ+rfCGW0P10Bcq+5kLQ/I9v5fmq8tD/otIFOG+i0P62OCR7ME7U/c2iR7Xw/tT84Qhm9LWu1P/0boYzelrU/w/UoXI/CtT8=\",\"dtype\":\"float64\",\"shape\":[100]}}},\"id\":\"f98bff43-72fc-4c56-a73e-1005c36d8dab\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"3fe77a11-45ab-4e47-acfe-fb211ef67e9f\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"label\":{\"value\":\"\\u4f4e\\u76f8\\u95a2\"},\"renderers\":[{\"id\":\"a68f5298-a297-432a-b7c6-c2a412e34537\",\"type\":\"GlyphRenderer\"}]},\"id\":\"2c04e8a2-dc46-4e10-82c4-54a42d4941a8\",\"type\":\"LegendItem\"},{\"attributes\":{\"axis_label\":\"\\u30dd\\u30fc\\u30c8\\u30d5\\u30a9\\u30ea\\u30aa\\u306e\\u30ea\\u30bf\\u30fc\\u30f3\",\"formatter\":{\"id\":\"9f57a46a-0bdc-4439-9325-54fefb45799a\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"fd4189c9-7dbf-4f32-9d1e-74ffb5bcbf2f\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"8f6a2305-b05c-4751-990c-55c659e7a4c6\",\"type\":\"BasicTicker\"}},\"id\":\"6d519a2b-0ff6-4a59-b9e1-eb1a2bfc6558\",\"type\":\"LinearAxis\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":20},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"754837f9-996e-4055-86aa-5f770c7c1419\",\"type\":\"Cross\"},{\"attributes\":{\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"line_width\":{\"value\":3},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"9700a629-9be1-4f6e-b063-580ea6982083\",\"type\":\"Line\"},{\"attributes\":{\"callback\":null},\"id\":\"2c5321e9-8e05-43fe-9979-1170a5db0d6a\",\"type\":\"DataRange1d\"},{\"attributes\":{\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"line_width\":{\"value\":3},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"3f6e8a9e-7be8-4e68-81a1-d9ae66a5b385\",\"type\":\"Line\"},{\"attributes\":{\"line_color\":{\"value\":\"red\"},\"line_width\":{\"value\":3},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"fbd99271-346f-4422-87a1-bb71c7f11ab0\",\"type\":\"Line\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":20},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"ad109f51-4e27-4939-a646-16749114da35\",\"type\":\"Cross\"},{\"attributes\":{\"data_source\":{\"id\":\"f79a50e8-db66-4c63-8d93-f73531f95a41\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"754837f9-996e-4055-86aa-5f770c7c1419\",\"type\":\"Cross\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"ad109f51-4e27-4939-a646-16749114da35\",\"type\":\"Cross\"},\"selection_glyph\":null,\"view\":{\"id\":\"98666855-5c5d-432b-9641-7ba78425bd3c\",\"type\":\"CDSView\"}},\"id\":\"9b8021ac-60f5-4bc8-b9f1-4617f2fb1d31\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"data_source\":{\"id\":\"db7a672a-4798-46fc-b611-6289d5e81c45\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"fbd99271-346f-4422-87a1-bb71c7f11ab0\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"3f6e8a9e-7be8-4e68-81a1-d9ae66a5b385\",\"type\":\"Line\"},\"selection_glyph\":null,\"view\":{\"id\":\"ad79870e-07d1-4e4b-8ba5-727f100844bb\",\"type\":\"CDSView\"}},\"id\":\"a68f5298-a297-432a-b7c6-c2a412e34537\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"callback\":null},\"id\":\"3d13f834-27e0-46b4-b692-e62b68edc50d\",\"type\":\"DataRange1d\"},{\"attributes\":{\"plot\":{\"id\":\"fd4189c9-7dbf-4f32-9d1e-74ffb5bcbf2f\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"3a09c2c1-24e6-4a8e-92ca-dcee0b6f37e8\",\"type\":\"BasicTicker\"}},\"id\":\"70e4d330-ac58-44bc-bc26-dba63c89bb7d\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"8f791243-568c-4a69-a672-68d4bd4d4254\",\"type\":\"LinearScale\"},{\"attributes\":{\"below\":[{\"id\":\"eed79408-f993-40a2-a5c9-d67169890117\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"6d519a2b-0ff6-4a59-b9e1-eb1a2bfc6558\",\"type\":\"LinearAxis\"}],\"plot_height\":500,\"plot_width\":800,\"renderers\":[{\"id\":\"eed79408-f993-40a2-a5c9-d67169890117\",\"type\":\"LinearAxis\"},{\"id\":\"70e4d330-ac58-44bc-bc26-dba63c89bb7d\",\"type\":\"Grid\"},{\"id\":\"6d519a2b-0ff6-4a59-b9e1-eb1a2bfc6558\",\"type\":\"LinearAxis\"},{\"id\":\"3a15de1f-de44-4fd8-a2d8-9be1939d3b0d\",\"type\":\"Grid\"},{\"id\":\"82830e91-9219-4947-9b1f-7b1e3bbc150e\",\"type\":\"BoxAnnotation\"},{\"id\":\"7c60f20f-8beb-4624-a3fd-e9ae6df667a7\",\"type\":\"Legend\"},{\"id\":\"30ccb357-9303-482e-8f85-0628a0b3ecb1\",\"type\":\"GlyphRenderer\"},{\"id\":\"a68f5298-a297-432a-b7c6-c2a412e34537\",\"type\":\"GlyphRenderer\"},{\"id\":\"9b8021ac-60f5-4bc8-b9f1-4617f2fb1d31\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"11db82c2-f930-458f-b31c-89cc2f9290d0\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"264d9bdc-ab39-4c9c-a750-a4ed4b5e5944\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"2c5321e9-8e05-43fe-9979-1170a5db0d6a\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"977cc565-78b1-49f4-b83a-565ff88b14cd\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"3d13f834-27e0-46b4-b692-e62b68edc50d\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"8f791243-568c-4a69-a672-68d4bd4d4254\",\"type\":\"LinearScale\"}},\"id\":\"fd4189c9-7dbf-4f32-9d1e-74ffb5bcbf2f\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"977cc565-78b1-49f4-b83a-565ff88b14cd\",\"type\":\"LinearScale\"},{\"attributes\":{\"source\":{\"id\":\"f79a50e8-db66-4c63-8d93-f73531f95a41\",\"type\":\"ColumnDataSource\"}},\"id\":\"98666855-5c5d-432b-9641-7ba78425bd3c\",\"type\":\"CDSView\"}],\"root_ids\":[\"fd4189c9-7dbf-4f32-9d1e-74ffb5bcbf2f\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.10\"}};\n", " var render_items = [{\"docid\":\"229ac163-7f42-4bcc-9908-575b1650bace\",\"elementid\":\"3c70a03e-58dd-4e97-a247-79719ad0228b\",\"modelid\":\"fd4189c9-7dbf-4f32-9d1e-74ffb5bcbf2f\"}];\n", "\n", " root.Bokeh.embed.embed_items(docs_json, render_items);\n", " }\n", "\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " var attempts = 0;\n", " var timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " clearInterval(timer);\n", " }\n", " attempts++;\n", " if (attempts > 100) {\n", " console.log(\"Bokeh: ERROR: Unable to embed document because BokehJS library is missing\")\n", " clearInterval(timer);\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "fd4189c9-7dbf-4f32-9d1e-74ffb5bcbf2f" } }, "output_type": "display_data" } ], "source": [ "p = figure(width=800, height = 500, title='ポートフォリオのリスクリターン')\n", "p.line(tvols, trets, line_color='blue', line_width=3, legend='高相関')\n", "p.line(tvols2, trets, line_color='red', line_width=3, legend='低相関')\n", "p.cross(rsks, rtns, size=20)\n", "\n", "p.xaxis.axis_label ='ポートフォリオのリスク'\n", "p.yaxis.axis_label = 'ポートフォリオのリターン'\n", "p.legend.location = 'top_left'\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }