Skip to content

Instantly share code, notes, and snippets.

@i2pi
Created September 30, 2010 20:05
Show Gist options
  • Save i2pi/605227 to your computer and use it in GitHub Desktop.
Save i2pi/605227 to your computer and use it in GitHub Desktop.
interest <- function (balance, r)
{
cash_flow <- c(0,diff(c(0,balance)))
for (i in 2:length(cash_flow))
cash_flow[i] <- (cash_flow[i] + cash_flow[i-1]) * (1 + r)
cash_flow[2:(length(balance)+1)] - balance
}
gen_nsweep <- function (N, days=31)
{
M <- rep(1000, days)
for (i in 1:N)
{
k <- floor(days * (i / (N+1)))
M[k:days] <- M[k:days] - (M[1]/N)
}
return (M)
}
days<-31
r<-0.01
plot(sapply(1:days, function(i) interest(gen_nsweep(i, days), r)[days]), type='s', xlab='N sweeps', ylab='Interest $')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment