[R-SIG-Finance] Fiscal year version of table.CalendarReturns from PerformanceAnalytics
matt at considine.net
matt at considine.net
Wed Aug 8 15:45:20 CEST 2012
I needed to corroborate some fiscal year return numbers and turned to
PerformanceAnalytics to see what was available. table.CalendarReturns
had the output I needed, but is set up for - predictably - calendar
year returns.
Below is what I hacked up to output fiscal year returns. The idea was
to try to change the base code as little as possible and found I could
accomplish that by playing with the date indexes and month labels.
Obviously, this is a kludge. If anyone has more elegant ways or ideas
of dealing with non-December fiscal years, I'd be interested in
hearing about them. In the meantime, I hope what's below helps
someone (my changes are identified by "MattC"). If there are errors
anyone sees, I'd appreciate hearing about them.
Rgds,
Matt
#e.g. table.FiscalReturns(total.ret,digits=2,FYendmonth=6)
# to show table of returns to two digits for FYrs ending June using a
# dataset named "total.ret"
table.FiscalReturns <- function(R, digits=1,
as.perc=TRUE,
geometric=TRUE,
FYendmonth=6) ##MattC
{
ri = checkData(R, method = "zoo")
columns = ncol(ri)
columnnames = colnames(ri)
rownames = rownames(ri)
firstyear =
as.numeric(format(strptime(as.POSIXct(time(ri)[1]),"%Y-%m-%d"), "%Y"))
lastyear =
as.numeric(format(strptime(as.POSIXct(time(ri)[length(ri[,1])]),
"%Y-%m-%d"), "%Y"))
#start MattC
if (FYendmonth != 12) {
firstyrmo =
as.numeric(format(strptime(as.POSIXct(time(ri)[1]),"%Y-%m-%d"), "%m"))
lastyrmo =
as.numeric(format(strptime(as.POSIXct(time(ri)[length(ri[,1])]),
"%Y-%m-%d"), "%m"))
if (firstyrmo>FYendmonth) firstyear <- firstyear+1
if (lastyrmo>FYendmonth) lastyear <- lastyear+1
newdates<-as.Date(as.yearmon(index(ri))+(firstyrmo-FYendmonth)/12,frac=1)
index(ri)<-newdates
}
#end MattC
year = format(strptime(as.POSIXct(time(ri)), "%Y-%m-%d"),"%Y")
month = format(strptime(as.POSIXct(time(ri)), "%Y-%m-%d"),"%b")
monthlabels =
strftime(seq.Date(as.Date(paste("2000-01-01",sep="")), length.out = 12,
by = "months"), format = "%b")
rowlabels = (firstyear:lastyear)
for (column in 1:columns) {
target.df = as.data.frame(matrix(data = as.numeric(NA),
length(rowlabels), length(monthlabels), dimnames =
list(rowlabels, monthlabels)))
for (i in 1:length(ri[, 1])) {
if (!is.na(ri[i, column])) {
target.df[year[i], month[i]] = ri[i, column]
}
}
yearcol = as.data.frame(matrix(data = as.numeric(NA),
length(rowlabels), 1, dimnames = list(rowlabels,
columnnames[column])))
for (i in 1:length(yearcol[, 1])) {
if (geometric)
yearcol[i, columnnames[column]] = prod(1 +
na.omit(as.numeric(target.df[i,]))) - 1
else yearcol[i, columnnames[column]] =
sum(as.numeric(target.df[i,]), na.rm = TRUE)
if (yearcol[i, columnnames[column]] == 0)
yearcol[i, columnnames[column]] = NA
}
target.df = cbind(target.df, yearcol)
if (as.perc)
multiplier = 100
else multiplier = 1
target.df = target.df * multiplier
target.df = base::round(target.df, digits)
if (column == 1)
result.df = target.df
else {
result.df = cbind(result.df, target.df[, 13])
}
}
#start MattC
if (FYendmonth != 12) {
monthlabels =
strftime(seq.Date(as.Date(paste("2000-",FYendmonth+1,"-01",sep="")),
length.out = 12, by = "months"), format = "%b")
}
#end MattC
colnames(result.df) = c(monthlabels, columnnames)
result.df
}
More information about the R-SIG-Finance
mailing list