R: using apply.fromstart to calculate returns and standard deviation

515 views Asked by At

I would like to find the total return someone has made over a period as well as the standard deviation of his returns. I have found a function, apply.fromstart in the PerformanceAnalytics package, which looks very promising. I am, however, having difficulty implementing it.

Here is what I have: A dataframe containing various data, including the return per period:
HourlyData

        Time            Position  
2014-08-01 01:00:00      1.01    
2014-08-01 02:00:00      0.99  
2014-08-01 03:00:00      1.01  
2014-08-01 04:00:00      1.02  

I would like to find the total return in each period, as follows:

Period                TotalReturn
2014-08-01 01:00:00      1.01    
2014-08-01 02:00:00      1.01*0.99
2014-08-01 03:00:00      1.01*.099*1.01
2014-08-01 04:00:00      1.01*.099*1.01*1.02

My code currently reads:
apply.fromstart(hourlyData[,2,drop = FALSE],FUN="*",width=1)

I would also like to find the standard deviation of his returns. My code for this part reads as follows:
apply.fromstart(hourlyData[,2,drop = FALSE],FUN="sd",width=1)

The data type of hourlyData$Position is "zoo"

I am getting the following error: In zoo(NA, order.by = as.Date(time(R))) : some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique

I have checked, and there are no duplicate in my row names

Here is the result from running dput(hourlyData):

> structure(list(Period = structure(c(1406844000, 1406847600,
> 1406851200,  1406854800, 1406858400, 1406862000), class = c("POSIXct",
> "POSIXt" )), Login = c(173908L, 173908L, 173908L, 173908L, 173908L,
> 173908L ), NetExposureUSD = c(2188640, 2188730, 2189230, 2189000,
> 2188310,  2187710), EquityUSD = c(9303.51, 9237.82, 8582.18, 9074.76,
> 9929.96, 
> 10743.57), UnrealizedProfitUSD = c(-31.64, -97.33, -752.97, -260.39, 
> 594.81, 1408.42), DepositWithdrawal = c(0, 0, 0, 0, 0, 0), LaggedEquity = structure(c(0, 
> 9303.51, 9237.82, 8582.18, 9074.76, 9929.96), index = 1:6, class = "zoo"), 
>     Return = structure(c(0, -0.00706077598669755, -0.0709734547761268, 
>     0.0573956733603816, 0.0942394068823857, 0.0819348718423841
>     ), index = 1:6, class = "zoo"), Position = structure(c(1, 
>     0.992939224013302, 0.929026545223873, 1.05739567336038, 1.09423940688239, 
>     1.08193487184238), index = 1:6, class = "zoo"), SD = c(NA, 
>     NA, 0.0390979887599392, 0.0641847560185966, 0.0867288719859795, 
>     0.0187573743033249)), .Names = c("Period", "Login", "NetExposureUSD",  "EquityUSD", "UnrealizedProfitUSD",
> "DepositWithdrawal", "LaggedEquity",  "Return", "Position", "SD"),
> row.names = c("2014-08-01 01:00:00",  "2014-08-01 02:00:00",
> "2014-08-01 03:00:00", "2014-08-01 04:00:00",  "2014-08-01 05:00:00",
> "2014-08-01 06:00:00"), class = "data.frame")
>                                  Period  Login NetExposureUSD EquityUSD UnrealizedProfitUSD DepositWithdrawal LaggedEquity      
> Return  Position         SD 2014-08-01 01:00:00 2014-08-01 01:00:00
> 173908        2188640   9303.51              -31.64                 0 
> 0.00  0.000000000 1.0000000         NA 2014-08-01 02:00:00 2014-08-01 02:00:00 173908        2188730   9237.82              -97.33          
> 0      9303.51 -0.007060776 0.9929392         NA 2014-08-01 03:00:00
> 2014-08-01 03:00:00 173908        2189230   8582.18            
> -752.97                 0      9237.82 -0.070973455 0.9290265 0.03909799 2014-08-01 04:00:00 2014-08-01 04:00:00 173908        2189000   9074.76             -260.39                 0      8582.18 
> 0.057395673 1.0573957 0.06418476 2014-08-01 05:00:00 2014-08-01 05:00:00 173908        2188310   9929.96              594.81          
> 0      9074.76  0.094239407 1.0942394 0.08672887 2014-08-01 06:00:00
> 2014-08-01 06:00:00 173908        2187710  10743.57            
> 1408.42                 0      9929.96  0.081934872 1.0819349 0.01875737
1

There are 1 answers

1
David Arenburg On BEST ANSWER

Use the very efficient vectorized base R function cumprod for your first desired result. While the second result could be achieved (less efficiently) using a simple *apply loop

If you want to keep zoo class, do

cumprod(hourlyData$Position)
#         1         2         3         4         5         6 
# 1.0000000 0.9929392 0.9224669 0.9754125 1.0673348 1.1547867

Otherwise

cumprod(as.numeric(hourlyData$Position))
## [1] 1.0000000 0.9929392 0.9224669 0.9754125 1.0673348 1.1547867

For sd (as proposed by @akrun) (used vapply instead of sapply in order to "squeeze" maximum performance out of it)

vapply(seq_len(nrow(hourlyData)), function(i) sd(hourlyData$Position[1:i]), FUN.VALUE = double(1))
# [1] NA 0.004992723 0.039097989 0.052519398 0.063598345 0.063156702