使用 R 计算每月抵押贷款的利率
Compute interest rate of monthly mortgage payments using R
我尝试在给定抵押贷款总额、期限和每月还款额的情况下计算年金的利率。
每月年金计算公式:
i
J = T * --------------
1 - (1 + i)^-n
其中 T 是抵押贷款总额,i 是每月利息,J 是年金
示例:
library(purrr)
library(dplyr)
mortgage <- 300000
#annualRate <- ??
duration <- 360
#annuity
ann <- 1108.86
#Use brute force to find the interest rate
annuity <- function(annualRate, mortgage, duration) {
monthRate <- annualRate / 12
ann <- (monthRate / (1- ((1+monthRate)^(-duration)) )) * mortgage
data.frame(rate = annualRate*100, ann = ann)
}
#Try some rates
checkRates <- seq(1,3,0.5)/100
res <- map(checkRates , annuity, mortgage, duration) %>% bind_rows()
res
# rate ann
#1 1.0 964.9186
#2 1.5 1035.3606
#3 2.0 1108.8584
#4 2.5 1185.3627
#5 3.0 1264.8121
使用这种蛮力技术的兴趣是 2.0%
检查解决方案:
annualRate <- 2/100
monthRate <- annualRate / 12
(monthRate / (1- ((1+monthRate)^(-duration)) )) * mortgage
#[1] 1108.858
但是,我想知道是否可以在不使用蛮力的情况下计算出 R 中的确切利率。
# 1108.86 i
# ------- = --------------
# 300000 1 - (1 + i)^-360
是否可以使用 R 求解此方程式?
任何建议表示赞赏。
我认为您正在寻找 uniroot
。这是您的问题的示例用法:
mortgage <- 300000
duration <- 360
ann <- 1108.86
uniroot(function(x) mortgage * x/12 / (1 - (1+x/12)^(-duration)) - ann,
c(1e-6,1))$root * 100
#[1] 2.000091
我尝试在给定抵押贷款总额、期限和每月还款额的情况下计算年金的利率。
每月年金计算公式:
i
J = T * --------------
1 - (1 + i)^-n
其中 T 是抵押贷款总额,i 是每月利息,J 是年金
示例:
library(purrr)
library(dplyr)
mortgage <- 300000
#annualRate <- ??
duration <- 360
#annuity
ann <- 1108.86
#Use brute force to find the interest rate
annuity <- function(annualRate, mortgage, duration) {
monthRate <- annualRate / 12
ann <- (monthRate / (1- ((1+monthRate)^(-duration)) )) * mortgage
data.frame(rate = annualRate*100, ann = ann)
}
#Try some rates
checkRates <- seq(1,3,0.5)/100
res <- map(checkRates , annuity, mortgage, duration) %>% bind_rows()
res
# rate ann
#1 1.0 964.9186
#2 1.5 1035.3606
#3 2.0 1108.8584
#4 2.5 1185.3627
#5 3.0 1264.8121
使用这种蛮力技术的兴趣是 2.0%
检查解决方案:
annualRate <- 2/100
monthRate <- annualRate / 12
(monthRate / (1- ((1+monthRate)^(-duration)) )) * mortgage
#[1] 1108.858
但是,我想知道是否可以在不使用蛮力的情况下计算出 R 中的确切利率。
# 1108.86 i
# ------- = --------------
# 300000 1 - (1 + i)^-360
是否可以使用 R 求解此方程式? 任何建议表示赞赏。
我认为您正在寻找 uniroot
。这是您的问题的示例用法:
mortgage <- 300000
duration <- 360
ann <- 1108.86
uniroot(function(x) mortgage * x/12 / (1 - (1+x/12)^(-duration)) - ann,
c(1e-6,1))$root * 100
#[1] 2.000091