# IRR 計算機開發文件
###### tags: `Insurance` `IRR` `開發文件`

---
# 待辦事項
- [x] 1. 壓力測試(小金額)
- [x] 2. 12/14確保 api服務有on
---
# 彙整需修改總表
- [x] customerA 繳費金額範例範圍調整成,較符合邏輯之數值
>修改年繳保費 為1~10^10 ,領回年份改成 1~10^13
- [x] customerB 繳費與領回總淨值為0時,報酬率應該為0% 回報錯誤為6.66e-14%
>
---
# 開發資訊
計算器網址
>192.168.5.75/api
Request 規格:
* URL: `/IRR_calculator`
* Method: `POST`
* Content-Type: `application/json`
Request Body範例:
| Parameter | Example | Description |
| -------- | -------- |---------|
| PaymentYear | "6" | 繳費年期 |
| PaymentPerYear | "20000" | 年繳保費 |
| RetrieveYear | "10" | 第幾年末領回 |
| PaymentRetrieve | "130000" | 領回金額 |
:::info
```
{
"PaymentYear": "6",
"PaymentPerYear":"20000",
"RetrieveYear":"10",
"PaymentRetrieve":"130000"
}
```
:::
Response Body範例:
| Parameter | Example | Description |
| -------- | -------- |---------|
| totalpayment |"120000"| 總繳金額 |
| netvalue |"10000"| 總淨報酬 |
| IRR_result |"1.08%""| IRR內部報酬率 |
:::info
```
{
"totalpayment": totalpayment,
"netvalue" :netvalue,
"IRR_result" :IRR_result
}
```
:::
---
### 事項:
取消彈出視窗
### 文案:
#### 問號:
繳費年期:
>繳費年期為保費總繳費年數,譬如:你買了10年期儲蓄險,只要繳交6年,填寫年份就是為6。
年繳保費:
>年繳保費為您每年繳交之保費總額。
第幾年末領回:
>第幾年末領回為您保單領回年份,譬如:你買了10年期儲蓄險,並於第10年末可領回所有保費及利息,填寫年份就是為10。
領回金額:
>領回金額為保單結束後,領回的所有保費加利息。
#### IRR是什麼?「文章」:
[文章連結](https://hackmd.io/@Arsheng/r1Kvmpo3r)
# 前端
輸入框限制
年份:
1. 正整數(無負數、小數點)
2. 範圍1~200年
金額:
1. 正整數(無負數、小數點)
2. 範圍1~1兆
# 測試
確認各種設備及環境都可以使用
### 壓力測試結果
官網API可以同時處理380個request,記錄如下
```
➜ twse_crawler git:(master) ✗ go run main.go
2019/12/04 14:34:03 expected: 200, failed: 0
2019/12/04 14:34:10 expected: 210, failed: 0
2019/12/04 14:34:18 expected: 220, failed: 0
2019/12/04 14:34:27 expected: 230, failed: 0
2019/12/04 14:34:34 expected: 240, failed: 0
2019/12/04 14:34:41 expected: 250, failed: 0
2019/12/04 14:34:53 expected: 260, failed: 0
2019/12/04 14:35:04 expected: 270, failed: 0
2019/12/04 14:35:15 expected: 280, failed: 0
2019/12/04 14:35:26 expected: 290, failed: 0
2019/12/04 14:35:37 expected: 300, failed: 0
2019/12/04 14:35:48 expected: 310, failed: 0
2019/12/04 14:35:59 expected: 320, failed: 0
2019/12/04 14:36:10 expected: 330, failed: 0
2019/12/04 14:36:22 expected: 340, failed: 0
2019/12/04 14:36:33 expected: 350, failed: 0
2019/12/04 14:36:44 expected: 360, failed: 0
2019/12/04 14:36:56 expected: 370, failed: 0
2019/12/04 14:37:07 expected: 380, failed: 0
2019/12/04 14:37:18 {269 520}
2019/12/04 14:37:18 {317 520}
2019/12/04 14:37:18 {286 520}
2019/12/04 14:37:18 {223 520}
2019/12/04 14:37:19 expected: 390, failed: 4
2019/12/04 14:37:28 {345 520}
2019/12/04 14:37:28 {348 520}
2019/12/04 14:37:28 {349 520}
2019/12/04 14:37:28 {347 520}
2019/12/04 14:37:28 {367 520}
2019/12/04 14:37:28 {341 520}
2019/12/04 14:37:28 {385 520}
2019/12/04 14:37:28 {181 520}
2019/12/04 14:37:28 {346 520}
2019/12/04 14:37:28 {312 520}
2019/12/04 14:37:30 expected: 400, failed: 10
2019/12/04 14:37:40 {340 520}
2019/12/04 14:37:40 {194 520}
2019/12/04 14:37:40 {193 520}
2019/12/04 14:37:40 {327 520}
2019/12/04 14:37:40 {246 520}
2019/12/04 14:37:40 {247 520}
2019/12/04 14:37:40 {219 520}
2019/12/04 14:37:40 {187 520}
2019/12/04 14:37:40 {231 520}
2019/12/04 14:37:40 {209 520}
2019/12/04 14:37:40 {234 520}
2019/12/04 14:37:40 {240 520}
2019/12/04 14:37:40 {210 520}
2019/12/04 14:37:40 {189 520}
2019/12/04 14:37:40 {226 520}
2019/12/04 14:37:40 {331 520}
2019/12/04 14:37:40 {186 520}
2019/12/04 14:37:40 {225 520}
2019/12/04 14:37:40 {215 520}
2019/12/04 14:37:40 {339 520}
2019/12/04 14:37:40 {216 520}
2019/12/04 14:37:40 {330 520}
2019/12/04 14:37:40 {239 520}
2019/12/04 14:37:40 {336 520}
2019/12/04 14:37:40 {233 520}
2019/12/04 14:37:40 {227 520}
2019/12/04 14:37:40 {230 520}
2019/12/04 14:37:40 {212 520}
2019/12/04 14:37:40 {224 520}
2019/12/04 14:37:40 {188 520}
2019/12/04 14:37:42 expected: 410, failed: 30
```
每天測試,持續一陣子
誰測試,多久?
# 2.0 浮動現金流IRR計算機
### v1.0 計算機轉前端運算:
#### Python
載入值
```
{
"PaymentYear": "6",
"PaymentPerYear":"20000",
"RetrieveYear":"10",
"PaymentRetrieve":"130000"
}
```
list = [] # 新增空list
總繳費為支出 = 每年繳費 * 繳費年期
120000 = 6 * 20000
**現金流** :
累加每年20000支出現金流
for i in range(0,繳費年期)
list.append(-每年繳費)
累加繳完支出等待契約到期之0支出現金流
for i in range(0,領回年期 - 繳費年期)
list.append(0)
累加領回總金額
list.append(領回金額)
現金流結果:
[-20000, -20000 , -20000 , -20000 , -20000 , -20000 , 0 , 0, 0 , +130000]
總繳費
totalpayment = str(int(PY) * int(PPY))
淨收入報酬
netvalue = str(int(PR) - int(PY) * int(PPY))
---
### finance IRR from finance.js
計算機轉前端運算相關javascrpit 套件
[連結](http://financejs.org/#IRR)
Github
[連結](https://github.com/ebradyjobory/finance.js)
#### 前端輸入欄位
| Parameter | Example | Description |code|
| -------- | -------- |---------|-|
| PaymentYear | "6" | 繳費年期 | PY|
| PaymentPerYear | "20000" | 年繳保費 | PPY|
| RetrieveYear | "10" | 第幾年末領回 | RY|
| PaymentRetrieve | "130000" | 領回金額 | PR|
#### code
:::info
```
輸入數值以下為例
繳費年期 = 6
年繳保費 = 20000
第幾年末領回 = 10
領回金額 = 130000
需要的結果:
1. 總繳保費:
年繳保費 * 繳費年期
2. 總淨報酬:
領回金額 - 總繳保費
3. IRR:
需要現金流陣列如下:
6年現金流20000支出,3年現金流0支出,最後一年領回130000收入
cfs = [-20000, -20000 , -20000 , -20000 , -20000 , -20000 , 0, 0, 0 , +130000]
// Internal Rate of Return (IRR)
Finance.prototype.IRR = function(cfs) {
var depth = cfs.depth;
var args = cfs.cashFlow;
var numberOfTries = 1;
// Cash flow values must contain at least one positive value and one negative value
var positive, negative;
Array.prototype.slice.call(args).forEach(function (value) {
if (value > 0) positive = true;
if (value < 0) negative = true;
})
if (!positive || !negative) throw new Error('IRR requires at least one positive value and one negative value');
function npv(rate) {
numberOfTries++;
if (numberOfTries > depth) {
throw new Error('IRR can\'t find a result');
}
var rrate = (1 + rate/100);
var npv = args[0];
for (var i = 1; i < args.length; i++) {
npv += (args[i] / Math.pow(rrate, i));
}
return npv;
}
return Math.round(seekZero(npv) * 100) / 100;
};
```
:::
:::info
```
// seekZero seeks the zero point of the function fn(x), accurate to within x \pm 0.01. fn(x) must be decreasing with x.
function seekZero(fn) {
var x = 1;
while (fn(x) > 0) {
x += 1;
}
while (fn(x) < 0) {
x -= 0.01
}
return x + 0.01;
}
```
:::