Skip to content
All library documents

Weekly Simple Returns and Calendar Week Numbering

Article Quant Q&A · Author: Masher

Summary

The document examines how to aggregate daily simple returns into weekly returns in R. Its example compounds returns within each calendar-week group by multiplying the daily gross returns and subtracting one. The unexpected output includes a week numbered zero, prompting a question about how that group is defined and whether it should be included.

Key ideas

  • Compounded simple returns over a period are obtained by multiplying daily gross returns and subtracting one.
  • The example groups observations using a year and a week number formatted with the percent-W convention.
  • That convention numbers weeks from zero and uses Monday as the first day of the week.
  • Other conventions differ: percent-U starts weeks on Sunday, while percent-V follows ISO week rules.
  • A weekly aggregation helper may avoid manual calendar grouping, but the document does not compare its results.

Tags

Full text
# Transforming daily simple returns into weekly


# Transforming daily simple returns into weekly












I am trying to transform daily simple returns into weekly returns. I am using the following R code:

```
library(zoo)
tt <- seq(Sys.Date(), by='day', length=365)
vals <- data.frame(A=runif(365), B=rnorm(365), C=1:365)
z <- zoo(vals, tt)
week <- function(x)format(x, '%Y.%W')
dailytoweekly <- function(x) (prod(x+1)-1)
aggregate(z, by=week, FUN=dailytoweekly)
```

based on this post: https://stackoverflow.com/questions/16442396/convert-daily-to-weekly-monthly-data-with-r . The function `dailytoweekly` is written on the basis of simple return's time-additive properties. What puzzles me is the result:

> `2015.50 19.7687016 -0.88561787 5.039000e+03 2015.51 19.8390703 -1.21981217 1.729728e+07 2015.52 4.9931289 -1.76381705 7.343900e+04 2016.00 2.2251422 2.98725821 7.979000e+03 2016.01 15.9745238 -1.33997280 5.967562e+09 2016.02 15.3588303 -0.91805535 3.389158e+10 `

```
2015.50 19.7687016 -0.88561787 5.039000e+03
2015.51 19.8390703 -1.21981217 1.729728e+07
2015.52  4.9931289 -1.76381705 7.343900e+04
2016.00  2.2251422  2.98725821 7.979000e+03
2016.01 15.9745238 -1.33997280 5.967562e+09
2016.02 15.3588303 -0.91805535 3.389158e+10
```

Why there is a week 0 in the results and what is it supposed to represent? As there are 53 weeks in total, with indices from 0 to 52 inclusive. Any help on this issue would be appreciated :)

## Answer by Joshua Ulrich (score 1, accepted)

https://quant.stackexchange.com/a/22362

> Why there is a week 0 in the results and what is it supposed to represent?

The answer is found in `?strptime`:

> '%W' Week of the year as decimal number (00-53) using Monday as the first day of week (and typically with the first Monday of the year as day 1 of week 1). The UK convention.

Perhaps you were expecting the output from one of the other formats?

> '%U' Week of the year as decimal number (00-53) using Sunday as the first day 1 of the week (and typically with the first Sunday of the year as day 1 of week 1). The US convention. '%V' Week of the year as decimal number (01-53) as defined in ISO 8601. If the week (starting on Monday) containing 1 January has four or more days in the new year, then it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1. (Accepted but ignored on input.)

You might try see if `xts::apply.weekly` solves your problem. Using it will avoid the problem in your question.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.