{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import plotly.graph_objs as graph_obj\n", "import dash\n", "import dash_core_components as dcc\n", "import dash_html_components as dhc" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "r_inv = 3\n", "r_inc = 30\n", "r_cap = 20.315\n", "age = 30" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def calc_rate(r_inv, r_inc, r_cap, amt, n):\n", " r_0 = np.power(1.0 + r_inv, n)\n", " r_1 = np.power(1.0 + r_inv, n - 1)\n", " r_s = r_0 - np.max([r_0 - 1.0, 0.0]) * r_cap\n", " r_i = r_0 + (r_1 - np.max([r_1 - 1.0, 0.0]) * r_cap) * r_inc\n", " ret = np.power(r_i / r_s, 1.0 / n) - 1.0\n", " delta = amt * (r_i - r_s)\n", " return ret, delta" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def gen_seq(r_inv, r_inc, r_cap, amt, N):\n", " x = []\n", " r = []\n", " a = []\n", " for n in range(N, 60):\n", " res = calc_rate(r_inv, r_inc, r_cap, amt, 60 - n)\n", " x.append(n)\n", " r.append(res[0] * 100.0)\n", " a.append(int(res[1]))\n", " return x, r, a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "app = dash.Dash()\n", "app.layout = dhc.Div([\n", " dhc.Div([\n", " dhc.Div('投資収益率 (%)'),\n", " dcc.Input(\n", " id='r_inv',\n", " value=r_inv,\n", " type='number', inputmode='numeric'\n", " ),\n", " ]),\n", " dhc.Div([\n", " dhc.Div('所得税率 + 住民税率 (%)'),\n", " dcc.Input(\n", " id='r_inc',\n", " value=r_inc,\n", " type='number', inputmode='numeric'\n", " ),\n", " ]),\n", " dhc.Div([\n", " dhc.Div('譲渡税率 (%)'),\n", " dcc.Input(\n", " id='r_cap',\n", " value=r_cap,\n", " type='number', inputmode='numeric'\n", " ),\n", " ]),\n", " dhc.Div([\n", " dhc.Div('拠出開始時の年齢'),\n", " dcc.Input(\n", " id='age',\n", " value=age,\n", " type='number', inputmode='numeric'),\n", " ]),\n", " dhc.Div([\n", " dhc.Div('毎月の掛け金'),\n", " dcc.Input(\n", " id='amt',\n", " value=23000,\n", " type='number', inputmode='numeric'),\n", " ]),\n", " dcc.Graph(\n", " id='graph1',\n", " figure={},\n", " style={}\n", " )\n", "])\n", "@app.callback(\n", " dash.dependencies.Output(component_id='graph1', component_property='figure'),\n", " [\n", " dash.dependencies.Input(component_id='r_inv', component_property='value'),\n", " dash.dependencies.Input(component_id='r_inc', component_property='value'),\n", " dash.dependencies.Input(component_id='r_cap', component_property='value'),\n", " dash.dependencies.Input(component_id='age', component_property='value'),\n", " dash.dependencies.Input(component_id='amt', component_property='value'),\n", " ],\n", ")\n", "def update_output(input_r_inv, input_r_inc, input_r_cap, input_age, input_amt):\n", " data = []\n", " try:\n", " r_inv = float(input_r_inv) / 100.0\n", " r_inc = float(input_r_inc) / 100.0\n", " r_cap = float(input_r_cap) / 100.0\n", " age = int(input_age)\n", " amt = 12 * int(input_amt)\n", " except ValueError:\n", " age = 60\n", " a = 0\n", " if age < 60:\n", " x, r, a = gen_seq(r_inv, r_inc, r_cap, amt, age)\n", " trace_a = graph_obj.Bar(\n", " x=x, y=a,\n", " name='将来価値'\n", " )\n", " data.append(trace_a)\n", " trace_r = graph_obj.Scatter(\n", " x=x, y=r,\n", " name='収益率',\n", " yaxis='y2'\n", " )\n", " data.append(trace_r)\n", " total = int(np.sum(a) / 10000)\n", " layout = {\n", " 'title': f\"iDeCo利用によるお得分の評価 - 将来価値総額: {total}万円\",\n", " 'xaxis': {'title': '拠出時年齢'},\n", " 'yaxis': {\n", " 'title': '将来価値',\n", " 'rangemode': 'tozero',\n", " 'showgrid': False,\n", " },\n", " 'yaxis2': {\n", " 'title': '収益率',\n", " 'rangemode': 'tozero',\n", " 'showgrid': False,\n", " 'overlaying': 'y',\n", " 'side': 'right',\n", " },\n", " }\n", " return {\n", " 'data': data,\n", " 'layout': layout,\n", " }" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)\n" ] } ], "source": [ "app.run_server()" ] }, { "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 }