# 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 Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 and is being posted with permission from Matthew Smith. The views expressed in this material are solely those of the author and/or Matthew Smith and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.