R/loanInvest.R

#' Main code for computation of evolution of loan.
#' 
#' Two kinfs of functions are called.
#' - loanStep and investStep: compute evolution of capital, either on the repayment or the investment
#' - invFunc, insFunc: compute the amount of money paid as investment or insurance
#'
#' @param maxMonth maximum number of months to run the simulation
#' @param loanRate interests paid on remaining capital
#' @param loanCapital principal amount of money borrowed
#' @param investRate interests gained on invested capital
#' @param investCapital initialy invested capital
#' @param invFunc investment function, compute the amount invested
#' @param insFunc insurance function, param: (iniCapital, remCapital, rate) on initial or remaining capital for example, compute the amount paid to insurance
#' @param termFunc termination function, param: (remMonth, capLoan), can either terminate on month or capital, returns a boolean
#' @return evolution of various quantities of interrest
loanInvest <- function(maxMonth,
                       loanRate,
                       loanTransfer,
                       loanCapital,
                       investRate,
                       investCapital,
                       invFunc,
                       insFunc,
                       termFunc)
{
  interest <- 0. # total amount of interests paid to the bank
  insurance <- 0. # total amount of money paid to insure the loan
  remMonth <- maxMonth # remaining number of month
  remCapLoan <- loanCapital # remaining capital
  
  capInvest <- investCapital # capital invested
  
  nbMonth <- 0 # number of months
  
  logCapPaid <- vector("numeric", maxMonth)
  logCapRem <- vector("numeric", maxMonth)
  logIntPaid <- vector("numeric", maxMonth)
  logCumInt <- vector("numeric", maxMonth)
  
  logInvestCap <- vector("numeric", maxMonth)
  logCapInterest <- vector("numeric", maxMonth)
  
  logInsMonth <- vector("numeric", maxMonth)
  logInsTot <- vector("numeric", maxMonth)
  
  while (termFunc(remMonth, remCapLoan) != T)
    # loop over months until termination condition is reached
  {
    nbMonth <- nbMonth + 1 # without termination, algorithm goes one for another month
    
    monthTransfer <- loanTransfer # total amount of money transfered this month, initialized with the fixed amount
    
    insAmount <- 0.
    if ((nbMonth - 1) %% 12 == 0)
      # annual actions, for example paying the insurance
    {
      insAmount <- insFunc(remCapLoan) # amount of money paid to the insurance
      insurance <- insurance + insAmount # increase the total amount of money paid to the insurance
      monthTransfer <- monthTransfer - insAmount # decrease the amount available this month for loan repay and investment
    }
    
    resLoan <- loanStep(loanRate, monthTransfer, remCapLoan) # update the remaining capital and various quantities
    
    monthTransfer <- monthTransfer - resLoan$interest - resLoan$capPaid # decrease the amount of money available for investment
    interest <- interest + resLoan$interest # sum of all interest paid is increased by interrest paid this month
    remCapLoan <- remCapLoan - resLoan$capPaid # decrease the amount of loan capital to repay
    
    totInvest <- invFunc(monthTransfer) # computation of the amount invested this month
    resInvest <- investStep(
      investRate, # evolution of invested capital, and computation of investment interrests
      totInvest,
      capInvest
    )
    capInvest <- capInvest + resInvest$capIncrease # increase the capital invested, considering new transfer and interrests
    
    logCapPaid[nbMonth] <- resLoan$capPaid
    logCapRem[nbMonth] <- remCapLoan
    logIntPaid[nbMonth] <- resLoan$interest
    logCumInt[nbMonth] <- interest
    
    logCapInterest[nbMonth] <- resInvest$interest
    logInvestCap[nbMonth] <- capInvest
    
    logInsMonth[nbMonth] <- insAmount
    logInsTot[nbMonth] <- insurance
    
    remMonth <- remMonth - 1 # number of remaining months decreased
  }
  
  payPlan <- list(
    logCapPaid = logCapPaid[1:nbMonth],
    logCapRem = logCapRem[1:nbMonth],
    logIntPaid = logIntPaid[1:nbMonth],
    logCumInt = logCumInt[1:nbMonth]
  )
  
  investPlan <- list(logCapInterest = logCapInterest[1:nbMonth],
                     logInvestCap = logInvestCap[1:nbMonth])
  
  insurancePlan <- list(logInsMonth = logInsMonth[1:nbMonth],
                        logInsTot = logInsTot[1:nbMonth])
  
  return (
    list(
      insurance = insurance,
      interest = interest,
      nbMonth = nbMonth,
      nbYear = nbMonth / 12,
      investPlan = investPlan,
      insurancePlan = insurancePlan,
      payPlan = payPlan
    )
  )
}
vkubicki/rfinance documentation built on May 14, 2019, 7:22 p.m.