Created
October 2, 2021 09:25
-
-
Save messefor/762f7447c1f5a90574d14f68f64d032d to your computer and use it in GitHub Desktop.
calc_compound_interest.ipynb
This file contains 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": { | |
"name": "calc_compound_interest.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyP6Vq/bEQXag2tVQu/JgZC6", | |
"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/messefor/762f7447c1f5a90574d14f68f64d032d/calc_compound_interest.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "x5sQMaF6SYxT" | |
}, | |
"source": [ | |
"複利の計算\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "5JhmlwGKST_K" | |
}, | |
"source": [ | |
"def comp_interest_return(b, n, r, a=0):\n", | |
" '''Compound interest calculation\n", | |
" \n", | |
" b: Original principal amount 元本\n", | |
" n: Years 年数\n", | |
" r: Real interest rate(Annual) 実質年利\n", | |
" a: Amount put aside 年間積立額\n", | |
" '''\n", | |
" R = 1 + r\n", | |
" return R * a * (R ** n - 1) / (R - 1) + (b - a) * R ** n" | |
], | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "niKMgdu_SsU1" | |
}, | |
"source": [ | |
"積立額が0の場合、`a=0`で計算" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "DPnp7uGUSoQd", | |
"outputId": "4f28bdd4-9933-41fc-9102-a33bd8576d5c" | |
}, | |
"source": [ | |
"b = 1000000 # 元本 100万円\n", | |
"r = 0.05 # 5%\n", | |
"n = 10 # 10年\n", | |
"\n", | |
"ret = comp_interest_return(b, n, r, a=0)\n", | |
"print(f'元本:{b}円 実質年利:{r * 100:.1f}%, {n}年')\n", | |
"print(f'{ret:.0f}円')" | |
], | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"元本:1000000円 実質年利:5.0%, 10年\n", | |
"1628895円\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Cjy_hEtuUF6V" | |
}, | |
"source": [ | |
"元本と積立額が異なる場合`a!=b`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "-pCN27GvUOpn", | |
"outputId": "e56b7039-270e-4acf-e4e5-15cb6b3cf1f9" | |
}, | |
"source": [ | |
"b = 1000000 # 元本 100万円\n", | |
"a = 100000 # 毎年 10万円積み立て\n", | |
"r = 0.05 # 5%\n", | |
"n = 10 # 10年\n", | |
"\n", | |
"ret = comp_interest_return(b, n, r, a)\n", | |
"print(f'元本:{b}円, 積立額:{a}円/年, 実質年利:{r * 100:.1f}%, {n}年')\n", | |
"print(f'{ret:.0f}円')" | |
], | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"元本:1000000円, 積立額:100000円/年, 実質年利:5.0%, 10年\n", | |
"2786684円\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "WERNeJa7Tmci" | |
}, | |
"source": [ | |
"元本と積立額が同じ場合`a=b`\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "tSr-u-iKTHZ9", | |
"outputId": "2bfc1c5f-f86c-4c5a-861c-61ab82cf93e1" | |
}, | |
"source": [ | |
"b = 100000 # 元本 10万円\n", | |
"a = 100000 # 毎年 10万円積み立て\n", | |
"r = 0.05 # 5%\n", | |
"n = 10 # 10年\n", | |
"\n", | |
"ret = comp_interest_return(b, n, r, a)\n", | |
"print(f'元本:{b}円, 積立額:{a}円/年 実質年利:{r * 100:.1f}%, {n}年')\n", | |
"print(f'{ret:.0f}円')" | |
], | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"元本:100000円, 積立額:100000円/年 実質年利:5.0%, 10年\n", | |
"1320679円\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "zlBrIYpYUCPQ" | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment