#!/usr/bin/Rscript # Copyright © 2017 Martin Ueding ############################################################################### # Parameters of the Problem # ############################################################################### # Cashback that the insurance company offers. cashback <- 0.04 # Interest that can be gained with long-term investments. interest.long <- 0.06 # Short-term interest rate. interest.short <- 0.01 # Yearly rate without cashback. monthly.rate <- 33.78 annual.payments <- 1 ############################################################################### # Implementation # ############################################################################### # Say that the insurance start on January 1st. Month 0 is the December of the # preceding year, month 1 is the first January. This is counted at the # beginning of the month after all transactions have taken place. month <- c(0:60) year <- floor(month / 12) december <- which(month %% 12 == 0) # Initially we have 11 monthly rates extra money. In January, we just got new # salary which will be used in part to pay the insurance. savings.initial <- 11 * monthly.rate ############################################################################### # Model A: No cashbash # ############################################################################### A <- data.frame(month=month) # All extra money is put into the portfolio. Assuming that the interest is # stable, we can compute the amount of money in the portfolio at each beginning # of the months. Since the money is put in it in January, there is no interest # in January. A$portfolio.value <- savings.initial * (1 + interest.long)^((month-1)/12) # The total amount of money that we have payed to the insurance company is just # the accumulation of the monthly rates. In January we will have payed the # first rate. A$payed <- month * monthly.rate A$cost <- A$portfolio.value - savings.initial ############################################################################### # Model B: Cashback # ############################################################################### B <- data.frame(month=month) B$payed <- vector(length=length(month)) B$savings <- vector(length=length(month)) # In December, we have not payed anything and have everything in the savings # account. B$payed[1] <- 0 B$savings[1] <- savings.initial # For the next months, we need to compute what happens. Note that `i == 2` # corresponds to January because arrays in R are 1-indexed. The loop therefore # starts with January. for (i in c(2:61)) { # First the balances of the previous month are copied to this month. B$payed[i] <- B$payed[i-1] B$savings[i] <- B$savings[i-1] # On the savings account we have gotten interest for the preceding month. if (month[i] >= 2) { B$savings[i] <- B$savings[i] * (1 + interest.short)^(1/12) } # Then we get salary this month which gets added to our savings account. B$savings[i] <- B$savings[i] + monthly.rate B$payed[i] <- B$payed[i] + monthly.rate # If it is a January, we need to pay the insurance. if (month[i] %% 12 == 1 || (annual.payments == 2 && month[i] %% 12 == 7)) { # We have to pay the whole year in advance, but we get a cashback. # Transfer the money from the savings account to the insurance company. transfer <- 12 / annual.payments * monthly.rate * (1 - cashback / annual.payments) B$savings[i] <- B$savings[i] - transfer } } B$cost <- B$savings - savings.initial ############################################################################### # Plots # ############################################################################### # Useful plots. svg('payed.svg') plot(month, A$payed, type='s', main='Money payed to insurer', xlab='Month', ylab='EUR', col='blue', ylim=range(A$payed, B$payed)) lines(month, B$payed, type='s', col='red') dev.off() svg('savings.svg') x <- month y1 <- A$portfolio.value y2 <- B$savings plot(x, y1, type='s', main='Money in portfolio/savings', xlab='Month', ylab='EUR', col='blue', ylim=range(y1, y2)) lines(x, y2, type='s', col='red') dev.off() svg('cost.svg') x <- year[december][2:length(year[december])] y1 <- diff(A$cost[december]) y2 <- diff(B$cost[december]) plot(x, y1, type='p', main='Financial advantage each year', xlab='Year', ylab='EUR', col='blue', ylim=range(y1, y2)) points(x, y2, col='red') dev.off() print(diff(A$cost[december])) print(diff(B$cost[december])) plot(year[december], (A$cost[december] - B$cost[december]) / year[december]) # Elementary plots. #plot(month, A$payed) #plot(month, A$portfolio.value) #plot(year[december], A$cost[december]) #plot(month, B$payed) #plot(month, B$savings) #plot(year[december], B$cost[december]) cost.A <- A$cost[length(A$cost)] cost.B <- B$cost[length(B$cost)] print(cost.A) print(cost.B) print(cost.A - cost.B) print(1 - cost.B / cost.A)