Difference between revisions of "Sven/AMPsuper.R"

From Organic Design wiki
m
m (Changed protection level for "Sven/AMPsuper.R" ([edit=sysop] (indefinite) [move=sysop] (indefinite) [read=sysop] (indefinite)))
 
(25 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[Category:Sven/AMP]][[Category:Sven/AMP]]
 +
<R>
 
library(chron)
 
library(chron)
 +
 +
# -------------------------------------------------------------- #
 +
# ------------------------- Globals ---------------------------- #
 +
# -------------------------------------------------------------- #
 +
ignoreWithdrawal <- T
  
 
# -------------------------------------------------------------- #
 
# -------------------------------------------------------------- #
 
# -------------------  Create the dataset  --------------------- #
 
# -------------------  Create the dataset  --------------------- #
 
# -------------------------------------------------------------- #
 
# -------------------------------------------------------------- #
 +
 +
  
 
period <- seq.dates("8/12/1998", "2/19/2003", by = "weeks")
 
period <- seq.dates("8/12/1998", "2/19/2003", by = "weeks")
 
period <- period[as.logical(seq(length(period)) %% 2)]                  # Fortnightly
 
period <- period[as.logical(seq(length(period)) %% 2)]                  # Fortnightly
 
period <- c(dates("7/23/98"), period, dates("3/23/2005"))              # All dates
 
period <- c(dates("7/23/98"), period, dates("3/23/2005"))              # All dates
period <- c(period[1:35], dates("11/20/99"),period[36:length(period)])   # Adding missing date
+
period <- c(period[1:35], dates("11/20/99"),period[36:length(period)]) # Adding missing date
 +
# Modifying a few dates
 +
period[c(10,19,21,32,91,94,117)] <- period[c(10,19,21,32,91,94,117)]+c(2,1,6,1,1,1,2)
 +
 
 
transaction <- c(4879.26, rep(101, 2), rep(50.50, 23), rep(51.01, 9),8.26, rep(51.01, 18), rep(51.57,52), rep(52.81, 15), -2591)
 
transaction <- c(4879.26, rep(101, 2), rep(50.50, 23), rep(51.01, 9),8.26, rep(51.01, 18), rep(51.57,52), rep(52.81, 15), -2591)
  
Line 26: Line 38:
 
units <- (transaction - fees) / unitprice
 
units <- (transaction - fees) / unitprice
  
dset <- data.frame(period, transaction, fees, unitprice, round(units,4))
+
dset <- data.frame(Date=period, Amount=transaction, Fees=fees, "Unit price"=unitprice, Units=round(units,4))
 +
write.table(dset, "AMP.txt", row.names=FALSE, sep="\t")
 +
 
 +
sellprice <- data.frame(Date=dates("3/21/2006"), Amount=10467.78, Fees=0, "Unit price"=1.4952, units=7000.9209)
 +
 
 +
# solving for i: A(0,n) = (1 + i)^n
 +
ieffective <- c()
 +
for( i in 1:nrow(dset)) {
 +
  An <- sellprice$"Unit.price"/dset[i,"Unit.price"]            # Accumulation between (0,n)
 +
    n <- as.numeric((sellprice$Date -  dset[i,"Date"])/365.25)  # time period in years                             
 +
    ieffective[i] <- An^(1/n) -1                                # Effective interest calculation
 +
}
 +
 
 +
dset <- cbind(dset, "i(effective)" = ieffective)
 +
 
 +
# Ignoring last observation which was a withdrawal (money reinvested at higher rate of return)
 +
if(ignoreWithdrawal) {
 +
  dset <- dset[-nrow(dset),]
 +
}
 +
# Diagnostic plots (ignoring last observation withdraw)
 +
plot(Unit.price~Date, data=dset, type="s", main="Balanced fund")  # Unit prive versus time
 +
 
 +
# Calculate weighted average for i(effective)
 +
sum(dset$Units/sum(dset$Units) * dset$"i(effective)")
 +
 
 +
# Both plans i(effective)
 +
bal <- c(Units=sum(dset$Units), "i(effective)"= sum(dset$Units/sum(dset$Units) * dset$"i(effective)"))
 +
limBal <- c(Units=2237.9625,
 +
                "i(effective)" = (1.0015/1.0081) ^ (1/as.numeric((dates("3/21/06") - dates("7/21/98"))/365.25)) - 1)
 +
allUnits <- bal["Units"]+limBal["Units"]
 +
# Overall return i(effective)
 +
ioverall <- bal["i(effective)"]*(bal["Units"]/allUnits)+limBal["i(effective)"]*(limBal["Units"]/allUnits)
 +
 
 +
# Results
 +
if(ignoreWithdrawal) {
 +
  print("Excluding withdrawal")
 +
}else{
 +
  print("Including withdrawal")
 +
}
 +
print(paste("Balanced fund annualised return = ",round(bal["i(effective)"]*100,2),"%", sep=""))
 +
print(paste("Limited Access Balanced fund annualised return = ",round(limBal["i(effective)"]*100,2),"%", sep=""))
 +
print(paste("Balanced fund annualised return = ",round(ioverall*100,2),"%", sep=""))
 +
 
 +
# Creating a LaTeX table of dset
 +
setwd("/tmp")
 +
library(Hmisc)
 +
latex(round(dset,4))
 +
 
 +
write.table(round(dset,3), "dump.txt", row.names=F, sep="\t")
 +
</R>

Latest revision as of 00:05, 30 May 2011

<R> library(chron)

  1. -------------------------------------------------------------- #
  2. ------------------------- Globals ---------------------------- #
  3. -------------------------------------------------------------- #

ignoreWithdrawal <- T

  1. -------------------------------------------------------------- #
  2. ------------------- Create the dataset --------------------- #
  3. -------------------------------------------------------------- #


period <- seq.dates("8/12/1998", "2/19/2003", by = "weeks") period <- period[as.logical(seq(length(period)) %% 2)] # Fortnightly period <- c(dates("7/23/98"), period, dates("3/23/2005")) # All dates period <- c(period[1:35], dates("11/20/99"),period[36:length(period)]) # Adding missing date

  1. Modifying a few dates

period[c(10,19,21,32,91,94,117)] <- period[c(10,19,21,32,91,94,117)]+c(2,1,6,1,1,1,2)

transaction <- c(4879.26, rep(101, 2), rep(50.50, 23), rep(51.01, 9),8.26, rep(51.01, 18), rep(51.57,52), rep(52.81, 15), -2591)

fees <- c(97.59, rep(3.54, 2), rep(1.76, 23), rep(2.55, 9), 0 , rep(1.78, 18), rep(1.8,52), rep(1.84,15), 0) unitprice <- c(1.1621, 1.1426, 1.1488, 1.1231, 1.1192, 1.1171, 1.1338, 1.1590, 1.1585, 1.1637, 1.1712,

                      1.1964, 1.1983, 1.2057, 1.1979, 1.2096, 1.2092, 1.2151, 1.2199, 1.2254, 
                      1.2273, 1.2209, 1.2130, 1.2162, 1.2269, 1.2398, 1.2294, 1.2223, 1.2280, 1.2375, 1.2300,
                      1.2247, 1.2263, 1.2412, 1.2431, 1.2467, 1.2551, 1.2701, 1.2769, 1.2716, 1.2583, 1.2649,
                      1.2627, 1.2764, 1.2840, 1.2907, 1.2636, 1.2822, 1.2821, 1.2803, 1.2975, 1.2953, 1.3155, 1.3115,
                      1.3154, 1.3211, 1.3401, 1.3314, 1.3411, 1.3260, 1.3194, 1.3221, 1.3163, 1.3169, 1.2803,
                      1.2872, 1.3005, 1.3045, 1.2948, 1.2839, 1.2809, 1.2854, 1.2875, 1.3071, 1.2984, 1.2998,
                      1.2968, 1.2931, 1.2916, 1.2868, 1.2855, 1.2663, 1.2569, 1.2260, 1.2366, 1.2464, 1.2538,
                      1.2632, 1.2779, 1.2743, 1.2712, 1.2735, 1.2659, 1.2577, 1.2548, 1.2645, 1.2634, 1.2589,
                      1.2537, 1.2413, 1.2410, 1.2399, 1.2259, 1.2091, 1.2127, 1.1620, 1.1426, 1.1728, 1.1598,
                      1.1423, 1.1174, 1.1178, 1.1406, 1.1269, 1.1465, 1.1285, 1.1275, 1.1325, 1.1185, 1.1094,
                      1.0964, 1.3060)

units <- (transaction - fees) / unitprice

dset <- data.frame(Date=period, Amount=transaction, Fees=fees, "Unit price"=unitprice, Units=round(units,4)) write.table(dset, "AMP.txt", row.names=FALSE, sep="\t")

sellprice <- data.frame(Date=dates("3/21/2006"), Amount=10467.78, Fees=0, "Unit price"=1.4952, units=7000.9209)

  1. solving for i: A(0,n) = (1 + i)^n

ieffective <- c() for( i in 1:nrow(dset)) {

 An <- sellprice$"Unit.price"/dset[i,"Unit.price"]             # Accumulation between (0,n)
   n <- as.numeric((sellprice$Date -  dset[i,"Date"])/365.25)  # time period in years                               
   ieffective[i] <- An^(1/n) -1                                # Effective interest calculation

}

dset <- cbind(dset, "i(effective)" = ieffective)

  1. Ignoring last observation which was a withdrawal (money reinvested at higher rate of return)

if(ignoreWithdrawal) {

 dset <- dset[-nrow(dset),]

}

  1. Diagnostic plots (ignoring last observation withdraw)

plot(Unit.price~Date, data=dset, type="s", main="Balanced fund") # Unit prive versus time

  1. Calculate weighted average for i(effective)

sum(dset$Units/sum(dset$Units) * dset$"i(effective)")

  1. Both plans i(effective)

bal <- c(Units=sum(dset$Units), "i(effective)"= sum(dset$Units/sum(dset$Units) * dset$"i(effective)")) limBal <- c(Units=2237.9625,

               "i(effective)" = (1.0015/1.0081) ^ (1/as.numeric((dates("3/21/06") - dates("7/21/98"))/365.25)) - 1)

allUnits <- bal["Units"]+limBal["Units"]

  1. Overall return i(effective)

ioverall <- bal["i(effective)"]*(bal["Units"]/allUnits)+limBal["i(effective)"]*(limBal["Units"]/allUnits)

  1. Results

if(ignoreWithdrawal) {

 print("Excluding withdrawal")

}else{

 print("Including withdrawal")

} print(paste("Balanced fund annualised return = ",round(bal["i(effective)"]*100,2),"%", sep="")) print(paste("Limited Access Balanced fund annualised return = ",round(limBal["i(effective)"]*100,2),"%", sep="")) print(paste("Balanced fund annualised return = ",round(ioverall*100,2),"%", sep=""))

  1. Creating a LaTeX table of dset

setwd("/tmp") library(Hmisc) latex(round(dset,4))

write.table(round(dset,3), "dump.txt", row.names=F, sep="\t") </R>