Time Series Classification Synthetic vs Real Financial Time Series – Part IV

See Part IPart II and Part III in this article for instructions from Matthew Smith on which R packages and data sets you need.

I compute the 10 day rolling mean and standard deviation using the tq_mutate function from the tidyquant package. value corresponds to the returns of the financial time series and is plotted in blue with the 10 day rolling average and standard deviation plotted over the returns. (I use melt again here but look into the pivot_longer function for a more intuitive application)

# Rolling mean and standard deviations
# I only use a random sample of 1 of each class of the grouped observations to save on memory and to make the plot more readable.
# The rollowing window is 10 days
# I use the tq_mutate functionality from the “tidyquant” package to keep things in a “tidy” format as per the “tidyverse” ‘rules’.
# In the plot “value” is the returns, “mean_10” is the 10 day rolling mean and “sd_10” is the 10 day rolling standard deviation.

plot0 <- df %>%
filter(class == 0) %>%
as_tibble() %>%
group_by(row_id) %>%
nest() %>%
ungroup() %>%
sample_n(1) %>%
unnest() %>%
mutate(variable = as.Date(variable)) %>%
tq_mutate(
select = value,
mutate_fun = rollapply,
width = 10,
align = “right”,
FUN = mean,
na.rm = TRUE,
col_rename = “mean_10”
) %>%
tq_mutate(
select = value,
mutate_fun = rollapply,
width = 10,
align = “right”,
FUN = sd,
na.rm = TRUE,
col_rename = “sd_10”
) %>%
melt(measure.vars = 5:7) %>%
setNames(c(“row_id”, “class”, “data set”, “date”, “variable”, “value”)) %>%
ggplot(aes(x = date)) +
geom_line(aes(y = value, colour = variable)) +
ggtitle(“Synthetic Financial Time Series Rolling Mean and Standard Deviation”) +
theme_classic() +
scale_colour_manual(values = c(“#1f77b4”, “red”, “black”)) +
theme(axis.text.x = element_blank(), legend.position = “bottom”, legend.title = element_blank())

plot1 <- df %>%
filter(class == 1) %>%
as_tibble() %>%
group_by(row_id) %>%
nest() %>%
ungroup() %>%
sample_n(1) %>%
unnest() %>%
mutate(variable = as.Date(variable)) %>%
tq_mutate(
select = value,
mutate_fun = rollapply,
width = 10,
align = “right”,
FUN = mean,
na.rm = TRUE,
col_rename = “mean_10”
) %>%
tq_mutate(
select = value,
mutate_fun = rollapply,
width = 10,
align = “right”,
FUN = sd,
na.rm = TRUE,
col_rename = “sd_10”
) %>%
melt(measure.vars = 5:7) %>%
setNames(c(“row_id”, “class”, “data set”, “date”, “variable”, “value”)) %>%
ggplot(aes(x = date)) +
geom_line(aes(y = value, colour = variable)) +
ggtitle(“Real Financial Time Series Rolling Mean and Standard Deviation”) +
theme_classic() +
scale_colour_manual(values = c(“#1f77b4”, “red”, “black”)) +
theme(axis.text.x = element_blank(), legend.position = “bottom”, legend.title = element_blank())

plot_grid(plot0, plot1)

Visit Matthew Smith – R Blog to see the next step in his analysis.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from Matthew Smith - R Blog and is being posted with its permission. The views expressed in this material are solely those of the author and/or Matthew Smith - R Blog and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.