Improving S&P Stock Prediction With Time Series Stock Similarity
Improving S&P Stock Prediction With Time Series Stock Similarity
Lior Sidi
Department of Software and Information System Engineering
Ben-Gurion University of the Negev
Israel
liorsidisc@[Link]
on data collected on a particular stock. In this paper, we enriched that we are the first to evaluate how similar stocks enchantments
the stock data with related stocks just as a professional trader would can improve the prediction of a stock price. We prepared a back-
have done to improve the stock prediction models. We tested five testing framework to train and optimize prediction models with
different similarities functions and found co-integration similarity different time series processing, segmentation, and modeling. In the
to have the best improvement on the prediction model. We evaluate Methods chapter, we describe the back-testing pipeline and explain
the models on seven S&P stocks from various industries over five the different similarity functions we apply for finding the most
years period. The prediction model we trained on similar stocks similar stocks. In the Experiment setup chapter, we explain how
had significantly better results with 0.55 mean accuracy, and 19.782 we optimize and evaluate the back-testing process on S&P stocks.
profit compare to the state of the art model with an accuracy of Finally, in the experiment results chapter, we compare between
0.52 and profit of 6.6. models that trained on similar stocks with models that trained
only on the target stock or random stocks. The results show a
KEYWORDS clear advantage of models trained on similar stocks with a short
horizon period (the next day) with a mean profit of 19.87 and a
ACM proceedings, Time series similarity, Stock predictions
mean accuracy of 0.55.
ACM Reference Format:
Lior Sidi. 2020. Improving S&P stock prediction with time series stock
2 RELATED WORK
similarity. In Proceedings of . ACM, New York, NY, USA, Article 4, 14 pages.
[Link] In this research, we aim to improve stock prediction with a hybrid
approach that combines stock similarity and classification. This
section starts with an overview of the stock data representation
1 INTRODUCTION and evaluation. We review different appliances of stock similarities
Prediction of stock price or any financial equity is well-investigated and clustering techniques.
subject by many researchers [10], traders, and hedge funds. In
an entire algotrading framework, the stock prediction component
collects information from different sources, such as market trading
2.1 Stock time series overview
and news. The components’ goal is to feed the strategy component Cavalcante et al. describes a two parts framework for financial
with feed on the following price values. The strategy component trading forecasting; the first part deals with conventional forecast-
is responsible for digesting the information regarding the current ing aspects such as data preparation, algorithm choosing, model
trader position, risk parameters, losses, and the price prediction to training, and accuracy evaluation. The second part is responsible
set actions for buying or selling particular finance equity. for financial forecasting aspects such as trading strategy and then
Similarity analysis on time series data in the finance domain is profit evaluation.
used widely to cluster different equities into domains for manual
exploration [20] [1] but also to identify correlated stocks for trading 2.1.1 Stock time series data
strategy [2] and for stock recommendation [17]. . A standard financial data usually consist of aggregated data of the
In this research, we investigate if a prediction model improves stock price for a certain period. The aggregations are usually high
by adding similar stock during training and in prediction. Our two price, low price, opening price, closing price, trade volume, trading
main research questions are "is the enhancement of similar stock amount. Many papers also extract known technical indicators to
identify trends and momentum in the stock price [21]. Table 1 cover
Permission to make digital or hard copies of all or part of this work for personal or
the most important technical indicators.
classroom use is granted without fee provided that copies are not made or distributed A critical aspect in the data preparation is the prediction period,
for profit or commercial advantage and that copies bear this notice and the full citation a short horizon period such as one day, one week, and one month
on the first page. Copyrights for components of this work owned by others than ACM
must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, is more suitable for financial prediction with technical indicators
to post on servers or to redistribute to lists, requires prior specific permission and/or a [9].
fee. Request permissions from permissions@[Link].
,
2.1.2 Segmentation
© Association for Computing Machinery.
. Time series representation and segmentation are a major part of
, L. Sidi
many stock time-series tasks. The goal is to reduce the dimensional- clustering to extract subsequences per time series such as sliding
ity and complexity of the data and enable identification of technical window. Aghabozorgi et al. also adds time point clustering to cluster
patterns, clustering, or prediction [6]. the points values in the time series.
The similarity and the clustering are highly affected by the seg-
2.1.3 Prediction
mentation and representation method applied, Keogh and Kasetty
. Forecasting proven to be well-suited for financial data modeling,
boldly claim that the clustering of time-series subsequences with
sophisticated machine learning (ML) models such as artificial neural
time window is meaningless unless only the significant motifs are
networks, SVM and genetic programming showed state of the art
considered.
results in the field.[12] [9] [21].
In the rest of this section, we will explore the recent appliances
Finnie et al. surveyed different techniques for time series fore-
of clustering on financial time series data per type of similarity
cast on financial data; they differentiate between machine learning
distances.
technique, forecasting period, and the input variables. They summa-
rized with the noticed that the Artificial Neural Networks (ANNs) 2.2.1 Numeric distance
is a dominant machine learning technique. Nevertheless, Gerlein . Aghabozorgi and Teh used different clustering methods to cate-
et al. demonstrates that also simple ML models such as decision gorize companies based on their stock data similarity; they used
tree, logistic regression, nearest neighbor, and Naive Bayes showed basic Euclidean distance to find similarity in time points in stock
good results as well. Therefore using simple forecast algorithms can data. Because Euclidean distance is not capable of identifying trends
be a good benchmark to evaluate different representation methods shapeliness they used Dynamic Time Warping (DTW) distance to
and enrichments. find similarity in the stock data shape, DTW first introduced in the
1960s and still show similar results to more advance methods [8].
2.1.4 Evaluation
In general, DTW deals with unequal length and solves the local
. Evaluating a stock time series predictor involves two types of
shift problem in the time series to find similar shapes between time
metrics, the first is a conventional evaluation of the predictor with
series in different time phase axis.
the accuracy measures such as mean absolute error, mean absolute
Wang et al. applied DTW on foreign exchange (FX) market and
percentage error, and root mean square error. The second is money
use minimal spanning tree (MST) and hierarchical tree (HT) to
evaluation, which evaluates the profit for a certain trading strategy
cluster different currencies together. They strongly claimed that the
[6].
usage of the Pearson correlation coefficient (PCC) is not suitable
One of the most popular strategies for evaluation is a Buy &
for FX time series data because it is not robust to outliers and must
Hold strategy [10]. The strategy simply buys a stock that predicted
have homogeneous, synchronous, and equal length samples.
to go up and sell it otherwise. Still, any strategy shall apply a risk
Jeon et al. searched for similar patterns in historical stock data
control mechanism such as stop loss[7].
with DTW and stepwise regression feature selection to improve
predictions. The selected data set is used to train an artificial neu-
2.2 Similarity and clustering on stock time ral network (ANN). They evaluate their predictor with root mean
series square error (RMSE) and new evaluation that represent the target
Clustering in time series stock data serves many goals such as port- value with SAX and apply Jaro-Winkler similarity.
folios balancing, patterns discovery, risk reduction, finding similar Caiado and Crato used generalize autoregressive conditional het-
companies, prediction, and recommendation. Applying a cluster- eroskedasticity (GARCH) models to estimate the distance between
ing model requires three key components: clustering algorithm, stock time series volatilities. They used hierarchical clustering and
similarity definition, and evaluation method [23]. multidimensional scaling technique to differentiate geographical
Keogh and Kasetty address two types of clustering time series, stock markets. GARCH model assumes that the conditional vari-
the first is whole time-series clustering to cluster set of individ- ance is dependent on a past linear volatility model; the GARCH
ual time series by their similarity, and the second is subsequence model is lean with parameters and provides a good representation
Improving S&P stock prediction with time series stock similarity ,
from recent points. Therefore, adding financial features that 3.3 Similar stocks enhancement
capture previous information such as exponential windows The stock enhancement component applies the different functions
might be more beneficial. for measuring the stock similarity. In figure 2, we describe the
• Time Window - Represents each instance as a window necessary configurations, the value for calculating the similarity,
of adjacent time points, this modeling allows the model to the fix the length function, the similarity functions, and lastly, the
find a relation between adjacent points. Still, this modeling k top stocks are chosen.
enlarges the instance and can harm the ability to add more As explained in the previous data modeling section, The combi-
features and stocks. nation of similar stocks depends on the data modeling approach.
In our implementation, we extract windows for each stock
separately and do not join their values as applied in the time 3.3.1 length fixing. Each instance (stock or equity) may miss
point modeling. On the one hand, the similar stocks data different time points due to system error, vacation days, or stock
is not used in prediction, only in training. But on the other splitting.
hand, the training data increases dramatically with similar When computing the distances between time-series, it’s impor-
stocks and improves the models. tant to correlate them to have the same length size, in order to do
this, we implemented and examined the following fixing methods:
3.2.3 Data segmentation. • Time join - a basic correlation between stocks, if one stock
is missing a time point while the other is not the time point
• Principals component analysis (PCA) - creates a smaller is eliminated (equivalent to inner join in SQL) this fixing is
representation of the dataset while maintaining its variance the most popular but may reduce the data substantially.
using eigenvector decomposition on the data covariance. • Delayed time join - stock values are pushed t times points
The PCA produces principal components (PCs), which are a backward (delay), this correlation meant to identify if one
linear combination of different features that acts as a new stock indicated future behavior of the other one.
attribute. PCA is a common tool for data exploration and • Padding - basic padding fixing technique that adds a dupli-
allows good reasoning of the data variance. cate value at the beginning of the shorter series.
In our experiment, we set the PCA to produce 3 PCs from • Perceptually important points (PIP) - select the most
the entire data set to act as new attributes. important points in a series with the following steps: the
• Symbolic ApproXimation Aggregation (SAX) - A dimen- first and the last points are set as PIP’s. Then, the third PIP
sionality reduction technique, allows distance measures to will be the point with the maximum distance to the first two
be defined on the symbolic approach that lower bounds eu- PIP’s. The fourth PIP will be the point with the maximum
clidean distance [15]. distance between two adjacent PIPS, the algorithm finish
SAX involves performing two stages on the data: first, it when achieving a predefined number of points.
transforms the original time-series into the appropriate piece- In our experiment, We use PIP on each stock to find impor-
wise aggregate approximation (PAA) representation. tant time points (10 percent of original length), and then we
The PAA representation divides the series to parts (according combined both PIPs and correlated the stock time points.
to the given output length) and calculates each interval’s
mean value. Later it converts the PAA data into a string after 3.3.2 Similarity functions.
normalization according to the given alphabet size. In our • Euclidean distance - A common indicator that measures
experiment, we use SAX representation while keeping the the dissimilarity between time series comparing the obser-
same word size as the origin. vations at the exact same time. The Euclidean distance is a
Improving S&P stock prediction with time series stock similarity ,
square root of the sum of the squared differences of each 3.4 Stock prediction
pair of corresponding points. The prediction component trains a regressor or a classifier model
The main limitation of this measure is its inability to identify with the relevant algorithm. The model goal is to predict if the stock
shifting and trends in the data. value will increase or decrease to a certain horizon.
• Pearson correlation coefficient - A known measure of Figure 4 explains how the system train and apply the classifier
the linear correlation between two vectors, the coefficient is or the regressor model. For the classifiers, the system train on the
calculated by dividing the two series covariance with theirs two classes in the traditional way. The regressors are trained to
standard deviation product, the correlation value range is predict the next price value, but in prediction, the predicted value
between -1 and 1 for negative and positive correlation. is a binary value for an increase or decrease in the price value.
Pearson has two major limitations regarding stock price In our experiment, we choose to evaluate two ensemble algo-
correlation. The first is that it assumes stationary behavior, rithms that showed good results in the stock prediction domain:
and the second is that it cannot deal with non-linear behavior Random Forest and Gradient Boosting Tree. Both models have a
between series. classification and regression implementation in Scikit-learn (Python
• Dynamic Time Warping (DTW) - A template matching library).
algorithm in pattern recognition, DTW, which can align
• Random Forest - Train t decision trees on different features
sequences that vary in time or speed.
and, in prediction, apply a majority voting on the results from
DTW is an old technique but still very relevant in financial
all trees.
similarity. In our experiment, we used Python’s implementa-
• gradient boosting trees - Train a chain of decision trees
tion of DTW, based on Euclidean distance.
where each tree tries to predict the error of the previous
• MINDIST - A distance computation of SAX representation,
decision tree, the model has a learning rate for summing the
the MINDIST formula defined by [15] and explained in fig-
values from the tree chains.
ure 3. The main limitation of MINDIST in stock price series,
as mentioned by Liu and Shao, is that it does not address
adjacent change in values. To fix this, we tested MINDIST
(and all other similarity functions as well) on the price rate
of change (PROC) to identify a high increment of adjacent
change.
• Co-integration - A statistical feature between multiple non-
stationary time series, co-integration checks if there is a
parameter that it’s multiplication with one of the time series
resolve with a constant spread between the non-stationary
series.
Stock prices are not necessarily stationary because their
mean and standard deviation may change over time. Co- Figure 4: The training and appliance pipeline for classifiers
integration is used widely to compare similarity between and regressors models
stocks and may state that there is some relation between
them [3].
For testing series co-integration, we use the co-integration
Python implementation "stattools" library that test for co- 4 EXPERIMENT SETUP
integration behavior with Engle-Granger two-step co-integration In order to evaluate if stocks similarity improves a baseline model,
test. We used the test P-value as a similarity measurement we conduct two-step experiments (back-testing) to evaluate differ-
between the two series, a low p-value of the test means that ent types of configurations. The first experiment goal is to come
the series are co-integrates. up with a processing pipeline and a baseline model. The second
experiment is to evaluate how different stock similarity functions
influence the baseline model. In figure 5, we mapped the different
configuration parameters the back-testing process will evaluate.
Our dataset contains daily historical data for all the S&P (Stan-
dard & Poor) 500 stock market index companies from 2012 to 2017.
The features given are date, open price, closing price, highest price,
lowest price, volume, and the short name of the stock. The S&P is
an American stock index of the largest companies listed in NYSE or
Figure 3: MINDIST Equation - n is the number of window NASDAQ, maintained by S&P Dow Jones Indices. It covers about
points, w the number of segments, Gi the value number i 80 percent of the American equity market by capitalization.
of series G transformed by the SAX method, and the Hi the We apply the evaluation process on stocks from different indus-
value number i of series H transformed by the SAX method. tries: Consumer (Disney - DIS, Coca Cola KO), Health (Johnson and
Johnson - JNJ), Industrial (General electric - GE , 3M - MMM), In-
formation technology (Google - GOOGL) and Financial (JP Morgan
, L. Sidi
Figure 5: A configuration tree of all the setup to be optimize and evaluate in workflow pipeline
- JPM). The validation folds are set to five and prepared for each length fixing functions, size of k similar stocks (10, 25, 50), similar
stock separately. value to compare (Close price or price rate of change).
The experiment will compare the best-enhanced model with the
best non-enhanced model from the first experiment and with a
4.1 Experiment 1 - processing model model that randomly choose stocks for enhancement. The second
evaluation comparison goal is to evaluate if the model’s improvement is due to
The experiment’s goal is to evaluate the basic processing and pre- similarity enhancement and not due to general stock enrichment.
diction model parameters to set a baseline model and processing
configurations. The baseline settings are set in the next experi- 5 RESULTS
ments to evaluate the similarity enhancement, rather them model
The evaluation metrics are accuracy score and F1 score; we calculate
and processing tuning.
each metric per class (increase/decrease) and average it to one score.
For processing, the experiment evaluates features (univariate or
To evaluate the model profit, we implement a simple Buy & Hold
multivariate), segmentation methods (SAX, PCA, or raw values),
algorithm that applies a long or short position regarding the model
temporal modeling (time points, or windows size 5 or 10). For stock
price prediction. We also measure the risk of the strategy with the
prediction, the experiment evaluates the following configurations:
Sharp ratio.
prediction value (close price or price rate of change), horizon (next
For visualizing the results of the experiment, we use Tableau
day, next three days or next week), and weighing instances per
software to export graphs and tables based on CSV results from the
stock (applied only for the Euclidean similarity models).
pipeline Python implementation.
To identify if stock similarity enhancements improve a baseline
model we do not need to focus on improving the models with
endless parameters tuning, but set recommended parameters to 5.1 Experiment 1 - processing model
reduce the complexity of the experiments, the models recommended evaluation
configuration: Random forest with 100 trees and Gradient boosting The first stage of experiment one is to evaluate which processing
with 0.02 learning rate. The first experiment also applies two basic configuration will resolve with the best accuracy, F1 score, profit,
stock similarity configurations: Euclidean similarity function on and low risk.
the price value with ten similar stock compared to no enrichment
of similar stocks. 5.1.1 Processing configuration evaluation
. In figure 5, we present the different configuration and metrics of
the data processing configuration, and in figure 7, we emphasize the
4.2 Experiment 2 - Enhancement similarities
profit difference per configuration. The best overall performance
evaluation configurations are univariate modeling with SAX transformation.
The second experiment evaluates the improvement each similarity Furthermore, SAX transformation showed the best results with all
function parameters have on the baseline models defined in the other configurations. Each metric in the figure is the mean of 1680
first experiment. examples (7 different stocks, five-folds, three different horizons,
For similarity parameters, the experiment evaluates similarity two predictions values, four types of models and two types of K
functions (co-integration, DTW, Euclidean, Pearson, and SAX), top stocks)
Improving S&P stock prediction with time series stock similarity ,
Figure 11: Experiment 2 folds profit per stock - a profit comparison between top 50 stocks from co-integration similarity
(orange) and 100 random stock selection enhancement (Blue) for each stock (x axis) in different folds (y axis)
[2] Saeed Aghabozorgi and Ying Wah Teh. 2014. Stock market co-movement assess-
ment using a three-phase clustering method. Expert Systems with Applications
41, 4 PART 1 (2014), 1301–1314. [Link]
[3] Carol Alexander and Anca Dimitriu. 2003. Equity Indexing , Cointegration and
Stock Price Dispersion : A Regime Switching Approach to Market Efficiency.
ISMA Centre Discussion Papers in Finance 44, 2 (2003), 29.
[4] Tiago Branco. [n. d.]. Pattern analysis in stock markets optimized by genetic
algorithms using modified SAX. ([n. d.]), 1–10.
[5] Jorge Caiado and Nuno Crato. 2007. A GARCH-based method for clustering
of financial time series: International stock markets evidence. Munich Personal
RePec Archive 2074 (2007).
[6] Rodolfo C. Cavalcante, Rodrigo C. Brasileiro, Victor L.F. Souza, Jarley P. Nobrega,
and Adriano L.I. Oliveira. 2016. Computational Intelligence and Financial Markets:
A Survey and Future Directions. Expert Systems with Applications 55 (2016), 194–
211. [Link]
[7] T Chande. 1997. Beyond Technical Analysis. 143 pages. [Link]
com/scholar?hl=en&btnG=Search&q=intitle:Beyond+Technical+Analysis#2
[8] H Ding, G Trajcevski, P Scheuermann, X Wang, and E J Keogh. 2008. Querying
and mining of time series data: experimental comparison of representations
and distance measures. Proc. of the VLDB Endowment 1, 2 (2008), 1542–1552.
Figure 12: Experiment 2 random selection compare - a profit [Link]
comparison between SAX and co-integration similarities on [9] Cain Evans, Konstantinos Pappas, and Fatos Xhafa. 2013. Utilizing artificial
top 50 stocks and random stock selection neural networks and genetic algorithms to build an algo-trading model for intra-
day foreign exchange speculation. Mathematical and Computer Modelling 58, 5-6
(2013), 1249–1266. [Link]
[10] Gavin Finnie, Bjoern Krollner, Bruce Vanstone, and Gavin Finnie. 2010. Financial
time series forecasting with machine learning techniques : A survey. European
pipeline on other investment instruments like crypt-currency and Symposium on Artificial Neural Networks ESANN2010 April (2010). [Link]
commodities (Gold, Oil, silver, etc.). We believe that the similarity [Link]/bruce_vanstone/17
pipeline on other datasets where most of the items do not correlate [11] Eduardo A. Gerlein, Martin McGinnity, Ammar Belatreche, and Sonya Cole-
man. 2016. Evaluating machine learning classification for financial trading:
will have dramatically better results than in this research. An empirical approach. Expert Systems with Applications 54 (2016), 193–207.
For future work, we suggest some improvements. The most [Link]
straightforward is to apply an ensemble similarity model on the [12] Yong Hu, Kang Liu, Xiangzhou Zhang, Lijun Su, E. W.T. Ngai, and Mei Liu. 2015.
Application of evolutionary computation for rule discovery in stock algorithmic
different similarity measurements to combine the advantage of each trading: A literature review. Applied Soft Computing Journal 36 (2015), 534–551.
method. Another improvement is to use deep learning models; we [Link]
[13] Seungwoo Jeon, Bonghee Hong, and Victor Chang. 2017. Pattern graph tracking-
assume that the similarity enhancement will increase the training based stock price prediction using big data. Future Generation Computer Systems
dataset and will improve deep learning models that require much (2017). [Link]
more data for training. [14] Eamonn Keogh and Shruti Kasetty. 2002. On the need for time series data mining
benchmarks. Proceedings of the eighth ACM SIGKDD international conference on
Knowledge discovery and data mining - KDD ’02 (2002), 102. [Link]
REFERENCES 1145/775047.775062
[1] Saeed Aghabozorgi, Ali Seyed Shirkhorshidi, and Teh Ying Wah. 2015. Time- [15] Jessica Lin, Eamonn Keogh, Stefano Lonardi, and Bill Chiu. 2003. A symbolic
series clustering - A decade review. Information Systems 53 (2015), 16–38. https: representation of time series, with implications for streaming algorithms. Pro-
//[Link]/10.1016/[Link].2015.04.007 ceedings of the 8th ACM SIGMOD workshop on Research issues in data mining and
, L. Sidi
(a) Experiment 2 similarity configurations - a full metrics comparison between similarity configurations
(a) Experiment 2 length fixing functions - a full metrics comparison between different
fixing functions on top similarity configurations
(b) Experiment 2 length fixing functions - a full metrics comparison between different
fixing functions on top similarity configurations
(c) Experiment 2 random selection compare - a full metrics comparison between SAX and co-integration similar-
ities on top 50 stocks and random stock selection
Improving S&P stock prediction with time series stock similarity ,
In Figure 16, we demonstrate a confusion matrix between the target stock and the sector for the top 10 similar stocks found per similarity
values (horizon) and similarity function (vertical). For example, the Pearson and close proc (Price rate of change) on the coca-cola stock
found that all top 10 similar stocks belong to the same sector as coca-cola, consumer staples. The stocks and sectors arranged in a way that
each stock corresponds to its relevant sector, meaning that if the diagonal is all dark green with ten value, the similarity function found the
stocks similar to its sector’s stocks.
In Figure 16, we describe which stocks found most similar to the target stocks, for example, in the coca-cola stocks, the similarities
functions found Pepsi to be the most similar.
, L. Sidi
Figure 16: Target stock and sector confusion matrix for top 10 stocks per similarity function (vertical) and similarity value
(horizontal)