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.

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

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

An important note in the code here is that I randomly sample by group, that is, I do not take a random sample from all observations across all groups. Instead I group_by each time series (each of the 6,000 observations after I filtered by the class == 0, likewise when I filter by the class == 1), I then nest() the data to collapse the daily time series for each asset into a list. From here I will have 6,000 observations, each of which has their time series nested inside a list. Thus, I can sample 1 of the 6,000 observations and then unnest() and obtain a full time series set of one of the random assets selected, – instead of sampling randomly over all assets time series data (which would be completely wrong).

For example the following commented out code group_by() the ID variable and nest() the data, takes a random sample_n() of the grouped data and then unnest() the data to its original form, this time with a random sample of the IDs.

# group_by(row_id) %>%
# nest() %>%
# ungroup() %>%
# sample_n(1) %>%
# unnest() %>%

Next I compute the Dickey Fuller test on both series for a single random observation, hence the sample_n(1) argument (it’s too computationally expensive to compute it on all 12,000 observations).

For the synthetically created series.

# Dickey Fuller test on the 0 class
# I only randomly sample 1 of the assets for the 0 class to save on output space

df %>%
filter(class == 0) %>%
group_by(row_id) %>%
nest() %>%
ungroup() %>%
sample_n(1) %>%
unnest() %>%
nest(-row_id) %>%
mutate(adf_res = map(data, ~ adf.test(.x$value))) %>%
unnest(adf_res)

## Augmented Dickey-Fuller Test
## alternative: stationary
## ## Type 1: no drift no trend
## lag ADF p.value
## [1,] 0 -17.94 0.01
## [2,] 1 -11.75 0.01
## [3,] 2 -8.66 0.01
## [4,] 3 -7.62 0.01
## [5,] 4 -7.13 0.01
## Type 2: with drift no trend
## lag ADF p.value
## [1,] 0 -17.94 0.01
## [2,] 1 -11.76 0.01
## [3,] 2 -8.67 0.01
## [4,] 3 -7.64 0.01
## [5,] 4 -7.15 0.01
## Type 3: with drift and trend
## lag ADF p.value
## [1,] 0 -18.00 0.01
## [2,] 1 -11.83 0.01
## [3,] 2 -8.77 0.01
## [4,] 3 -7.74 0.01
## [5,] 4 -7.26 0.01
## —-
## Note: in fact, p.value = 0.01 means p.value <= 0.01

## # A tibble: 3 x 3
## row_id data adf_res
## >
## 1 7807 [260 x 4]
## 2 7807 [260 x 4]
## 3 7807 [260 x 4]

The same but on the real financial series.

# Dickey Fuller test on the 1 class
# I only randomly sample 1 of the assets for the 1 class to save on output space

df %>%
filter(class == 1) %>%
group_by(row_id) %>%
nest() %>%
ungroup() %>%
sample_n(1) %>%
unnest() %>%
nest(-row_id) %>%
mutate(adf_res = map(data, ~ adf.test(.x$value))) %>%
unnest(adf_res)

## Augmented Dickey-Fuller Test
## alternative: stationary
##
## Type 1: no drift no trend
## lag ADF p.value
## [1,] 0 -15.99 0.01
## [2,] 1 -10.71 0.01
## [3,] 2 -9.12 0.01
## [4,] 3 -8.74 0.01
## [5,] 4 -7.58 0.01
## Type 2: with drift no trend
## lag ADF p.value
## [1,] 0 -16.10 0.01
## [2,] 1 -10.84 0.01
## [3,] 2 -9.27 0.01
## [4,] 3 -8.93 0.01
## [5,] 4 -7.81 0.01
## Type 3: with drift and trend
## lag ADF p.value
## [1,] 0 -16.27 0.01
## [2,] 1 -10.99 0.01
## [3,] 2 -9.46 0.01
## [4,] 3 -9.18 0.01
## [5,] 4 -8.06 0.01
## —-
## Note: in fact, p.value = 0.01 means p.value <= 0.01

## # A tibble: 3 x 3
## row_id data adf_res
## >
## 1 10833 [260 x 4]
## 2 10833 [260 x 4]
## 3 10833 [260 x 4]

Stay tuned for the next installment, in which Matthew will review he Jarque-Bera tests for normality.

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.

trading top