Investing in financial products involves risk to your capital.

Asset Classes

Free investment financial education

More Campus Resources

Useful Tools and Information

Language

Multilingual content from IBKR

# Time Series Classification Synthetic vs Real Financial Time Series – Part VII

###### Posted February 3, 2021 at 12:22 pm
Matthew Smith
Matthew Smith - R Blog

See Part IPart II ,Part III, Part IV, Part V and Part VI in this series for instructions from Matthew Smith on which R packages and data sets you need.

## Autocorrelation plots:

I plot the Autocorrelation Function for a “random” sample of observations time series. I selected 4 observations and filtered the data by them.

######################################################################
################# ACF plots ##########################################

# I only use 4 observations for these plots, 2 from the “synthetic” class and 2 from the “real” class.

df %>%
filter(row_id == 6422 | row_id == 8967 | row_id == 6080 | row_id == 5734) %>%
mutate(date = as.Date(variable)) %>%
ggplot(aes(x = date)) +
geom_line(aes(y = value), color = “red”, alpha = 0.4) +
geom_hline(yintercept = 0) +
facet_wrap(~ row_id + class) +
theme_tq()

acf_data <- df %>%
dplyr::filter(row_id == 6422 | row_id == 8967 | row_id == 6080 | row_id == 5734) %>%
mutate(date = as.Date(variable))

df_acf <- acf_data %>%
group_by(row_id) %>%
summarise(list_acf = list(acf(value, plot=FALSE))) %>%
mutate(acf_vals = purrr::map(list_acf, ~as.numeric(.x\$acf))) %>%
select(-list_acf) %>%
unnest() %>%
group_by(row_id) %>%
mutate(lag = row_number() – 1)

df_ci <- acf_data %>%
group_by(row_id) %>%
summarise(ci = qnorm((1 + 0.95)/2)/sqrt(n()))

ggplot(df_acf, aes(x = lag, y = acf_vals)) +
geom_bar(stat=”identity”, width=.05) +
geom_hline(yintercept = 0) +
geom_hline(data = df_ci, aes(yintercept = -ci), color=”blue”, linetype=”dotted”) +
geom_hline(data = df_ci, aes(yintercept = ci), color=”blue”, linetype=”dotted”) +
labs(x=”Lag”, y=”ACF”) +
facet_wrap(~ row_id) +
theme_tq()

Stay tuned for the next installment with a focus on how to generate the financial time series features using the `tsfeatures` package.

Visit Matthew Smith – R Blog to download the complete R code and see additional details featured in this tutorial: https://lf0.com/post/synth-real-time-series/financial-time-series/

###### 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.

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.