Last active
July 20, 2024 21:16
-
-
Save davidhcefx/f2a8df24aed327ea4602fe0ce51907d5 to your computer and use it in GitHub Desktop.
如果每年固定撥出一定比例的薪水,拿來存款投資,考量薪資成長率的情況下總共會有多少資金?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"name": "每年拿薪水固定比例的投資報酬.ipynb", | |
"authorship_tag": "ABX9TyObVopdsDmSmjhyNv5own3i", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/davidhcefx/f2a8df24aed327ea4602fe0ce51907d5/.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# 每年拿薪水固定比例\n", | |
"\n", | |
"如果每年固定撥出一定比例的薪水,拿來存款投資,考量薪資成長率的情況下總共會有多少資金?" | |
], | |
"metadata": { | |
"id": "hf3Z-zqstk2F" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 工作年數\n", | |
"WORK_YEARS = 40\n", | |
"# 第一年年收入\n", | |
"INIT_INCOME = 480000\n", | |
"# 薪資成長率 (年)\n", | |
"GROWTH = '3%'\n", | |
"# 投資佔收入比例\n", | |
"DEP_RATIO = '15%'\n", | |
"# 投資的平均年化報酬率\n", | |
"ROI = '5%'\n", | |
"\n", | |
"\n", | |
"def parse_percentage(s: str) -> float:\n", | |
" if not isinstance(s, str) or not s.endswith('%'):\n", | |
" raise TypeError('Wrong format')\n", | |
" return float(s[:-1]) / 100\n", | |
"\n", | |
"\n", | |
"def calc_return() -> int:\n", | |
" ret = 0\n", | |
" work_years = WORK_YEARS\n", | |
" init_income = INIT_INCOME\n", | |
" growth = parse_percentage(GROWTH)\n", | |
" dep_ratio = parse_percentage(DEP_RATIO)\n", | |
" roi = parse_percentage(ROI)\n", | |
" incomes = [init_income] * work_years # income per year\n", | |
" for i in range(1, work_years):\n", | |
" incomes[i] = incomes[i-1] * (1 + growth)\n", | |
" deposites = [ic * dep_ratio for ic in incomes]\n", | |
" for i in range(work_years):\n", | |
" ret += deposites[work_years - 1 - i] * (1 + roi) ** i\n", | |
"\n", | |
" return int(ret)\n", | |
"\n", | |
"r = calc_return()\n", | |
"print('Total return:', r)\n", | |
"print('總資金: {} 萬'.format(r / 10000))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "L0drMEbTpVhQ", | |
"outputId": "4170227a-d094-4872-c310-da9b9c3617f6" | |
}, | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Total return: 13600623\n", | |
"總資金: 1360.0623 萬\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment