Frequency resampling primer
Risk factor models often rely upon data that is of different frequencies. A typical example is when using quarterly macroeconomic data, such as prices, unemployment rates, etc., with financial data, which is often daily (or even intra-daily). To use both data sources in the same model, higher frequency data needs to be resampled to match the lower frequency data.
The DataFrame
and Series
Pandas objects have a built-in .resample()
method that specifies the lower frequency. This method is chained with a method to create the lower-frequency statistic, such as .mean()
for the average of the data within the new frequency period, or .min()
for the minimum of the data.
In this exercise you'll practice converting daily returns
data to weekly and quarterly frequency.
This exercise is part of the course
Quantitative Risk Management in Python
Exercise instructions
- Convert
returns
to quarterly frequency averagereturns_q
using the.resample()
and.mean()
methods. - Examine the header of
returns_q
, noting that the.resample()
method takes care of the date index for you. - Now convert
returns
to weekly frequency minimumreturns_w
, using the.min()
method. - Examine the header of
returns_w
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Convert daily returns to quarterly average returns
returns_q = returns.____('Q').____
# Examine the beginning of the quarterly series
print(returns_q.____)
# Now convert daily returns to weekly minimum returns
returns_w = ____.resample('W').____
# Examine the beginning of the weekly series
print(returns_w.____)