{"id":1202,"date":"2016-12-28T10:13:15","date_gmt":"2016-12-28T10:13:15","guid":{"rendered":"http:\/\/kourentzes.com\/forecasting\/?p=1202"},"modified":"2016-12-31T00:53:49","modified_gmt":"2016-12-31T00:53:49","slug":"can-neural-networks-predict-trended-time-series","status":"publish","type":"post","link":"https:\/\/kourentzes.com\/forecasting\/2016\/12\/28\/can-neural-networks-predict-trended-time-series\/","title":{"rendered":"Can neural networks predict trended time series?"},"content":{"rendered":"<p style=\"text-align: justify;\">Yes and&#8230; no! First, I should say that I am thinking of the common types of neural networks that are comprised by neurons that use some type of sigmoid transfer function, although the arguments discussed here are applicable to other types of neural networks. Before answering the question, let us first quickly summarise how typical neural networks function. Note that the discussion is done in a time series forecasting context, so some of the arguments here are specific to that and are not relevant to classification tasks!<\/p>\n<p><strong>1. Multilayer Perceptron (MLP) neural networks<\/strong><\/p>\n<p style=\"text-align: justify;\">MLPs are a basic form of neural networks. Having a good understanding of these can help one understand most types of neural networks, as typically other types are constructed by adding more connections (such as feedbacks or skip-layer\/direct connections). Let us assume that we have three different inputs, (X<sub>1<\/sub>, X<sub>2<\/sub>, X<sub>3<\/sub>), which could be different variables or lags of the target variables. A MLP with a single hidden layer, with 5 hidden nodes and a single output layer can be visualised as in Fig. 1.<\/p>\n<div id=\"attachment_1203\" style=\"width: 360px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/mlpplot.png\"><img aria-describedby=\"caption-attachment-1203\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-1203\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/mlpplot.png\" width=\"350\" height=\"262\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/mlpplot.png 561w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/mlpplot-150x112.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/mlpplot-300x225.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><p id=\"caption-attachment-1203\" class=\"wp-caption-text\">Fig. 1. MLP with 3 inputs, 5 hidden nodes arranged in a single layer and a single output node.<\/p><\/div>\n<p style=\"text-align: justify;\">An input (for example <em>X<sub>1<\/sub><\/em>) is passed and processed through all 5 hidden nodes (<em>H<sub>i<\/sub><\/em>), the results of which are combined in the output (<em>O<sub>1<\/sub><\/em>). If you prefer, the formula is:<br \/>\n<img src='https:\/\/s0.wp.com\/latex.php?latex=Y_1+%3D+b_0+%2B+%5Csum_%7Bi%3D1%7D%5E5+%7B+%5Cleft%28+b_i+f%28a_%7B0%2Ci%7D+%2B+%5Csum_%7Bj%3D1%7D%5E3%7Ba_%7Bj%2Ci%7DX_%7Bj%7D%7D%29+%5Cright%29+%7D+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='Y_1 = b_0 + \\sum_{i=1}^5 { \\left( b_i f(a_{0,i} + \\sum_{j=1}^3{a_{j,i}X_{j}}) \\right) } ' title='Y_1 = b_0 + \\sum_{i=1}^5 { \\left( b_i f(a_{0,i} + \\sum_{j=1}^3{a_{j,i}X_{j}}) \\right) } ' class='latex' \/>, \u00a0\u00a0\u00a0 (1)<br \/>\nwhere <em>b<sub>0<\/sub><\/em> and <em>a<sub>0,i<\/sub><\/em> are constants and <em>b<sub>i<\/sub><\/em> and <em>a<sub>j,i<\/sub><\/em> are weights for each input <em>X<sub>j<\/sub><\/em> and hidden node <em>H<sub>i<\/sub><\/em>. Looking carefully at either Eq. 1 or Fig. 1 we can observe that each neuron is a conventional regression that passes through a transfer function <em>f()<\/em> to become nonlinear. The neural network arranges several such neurons in a network, effectively passing the inputs through multiple (typically) nonlinear regressions and combining the results in the output node. This combination of several nonlinear regressions is what gives a neural network each approximation capabilities. With a sufficient number of nodes it is possible to approximate any function to an arbitrary degree of accuracy. Another interesting effect of this is that neural networks can encode multiple events using single binary dummy variables, as <a href=\"http:\/\/kourentzes.com\/forecasting\/2016\/07\/09\/the-impact-of-special-days-in-call-arrivals-forecasting-a-neural-network-approach-to-modelling-special-days\/\">this paper<\/a> demonstrates. We could add several hidden layers, resulting in a precursor to deep-learning. In principle we could make direct connections from the inputs to layers deeper in the network or the output directly (resulting in nonlinear-linear models) or feedback loops (resulting in recurrent networks).<\/p>\n<p style=\"text-align: justify;\">The transfer function <em>f()<\/em> is typically either the logistic sigmoid or the hyperbolic tangent for regression problems. The output node typically uses a linear transfer function, acting as a conventional linear regression. To really understand how the input values are transformed to the network output, we need to understand how a single neuron functions.<\/p>\n<p><strong>2. Neurons<\/strong><\/p>\n<p style=\"text-align: justify;\">Consider a neuron as a nonlinear regression of the form (for the example with 3 inputs):<\/p>\n<p><img src='https:\/\/s0.wp.com\/latex.php?latex=H_i+%3D+f%28a_%7B0%2Ci%7D+%2B+%5Csum_%7Bj%3D1%7D%5E3%7Ba_%7Bj%2Ci%7DX_%7Bj%7D%7D%29+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='H_i = f(a_{0,i} + \\sum_{j=1}^3{a_{j,i}X_{j}}) ' title='H_i = f(a_{0,i} + \\sum_{j=1}^3{a_{j,i}X_{j}}) ' class='latex' \/>. \u00a0\u00a0\u00a0 (2)<\/p>\n<p style=\"text-align: justify;\">If <em>f()<\/em> is the identity function, then (2) becomes a conventional linear regression. If <em>f()<\/em> is nonlinear then the magic starts! Depending on the values of the weights <em>a<sub>j,i<\/sub><\/em> and the constant <em>a<sub>0,i<\/sub><\/em> the behaviour of neuron changes substantially. To better understand this let us take the example of a single input neuron and visualise the different behaviours. In the following interactive example you can choose:<\/p>\n<ul>\n<li>the type of transfer function;<\/li>\n<li>the values of the input, weight and constant.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">The first plot shows the input-output values, the plot of the transfer function and with cyan background the area of values that can be considered by the neuron given selected weight and constant. The second plot provides a view of the neuron function, given the transfer function, weight and constant. Observe that the weight controls the width of the neuron and the constant the location, along the transfer function.<br \/>\n<iframe loading=\"lazy\" id=\"Neuron\" style=\"border: none; width: 100%; height: 560px;\" src=\"https:\/\/kourentzes.shinyapps.io\/shinyNeuron\/\" width=\"300\" height=\"150\" frameborder=\"0\"><\/iframe><br \/>\nWhat is quite important to note here is that both logistic sigmoid and hyperbolic tangent squash the input between two values and the output cannot increase or decrease indefinitely, as with the linear. Also the combination of weight and constant can result in different forms of nonlinearities or approximate linear behaviours. As a side note, although I do not see MLP as anything to do with simulating biological networks, the sigmoid-type transfer functions are partly inspired by the stimulated or not states of biological neurons.<\/p>\n<p style=\"text-align: justify;\">By now two things should become evident:<\/p>\n<ul>\n<li style=\"text-align: justify;\">The scale of the inputs is very important for neural networks, as very large or small values result in the same constant output, essentially acting at the bounds of the neuron plots above. Although in theory it is possible to achieve the desired scaling using only appropriate weights and constants, training of networks is aided tremendously by scaling the inputs to a reasonable range, often close to [-1,1].<\/li>\n<li style=\"text-align: justify;\">With sigmoid type transfer functions it is impossible to achieve an ever increasing\/decreasing range of outputs. So for example if we were to use as an input a vector (1, 2, 3, 4, 5, 6, &#8230;, n) the output would be squashed between [0, 1] or [-1, 1] depending on the transfer function, irrespectively of how large <em>n<\/em> is.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Of course, as Eq. (1) suggests, in a neural network the output of a neuron is multiplied by a weight and shifted by a constant, so it is relatively easy to achieve output values much greater than the bounds of a single neuron. Nonetheless, a network will still &#8220;saturate&#8221; and reach a minimum\/maximum value and cannot decrease\/increase perpetually, unless non-squashing neurons are used as well (this is for example a case where direct connections to a linear output become useful). An example of this follows.<\/p>\n<p style=\"text-align: justify;\">Suppose we want to predict the future values of a deterministic upward trend with no noise, of the form: y<sub>t<\/sub> = x<sub>t<\/sub> and x<sub>t<\/sub> = (1, 2, 3, 4, &#8230;). We scale the observed values between [-1, 1] to facilitate the training of the neural network. We use only 80% of the values for training the network and the remaining 20% to test the performance of the forecasts (test set A). We train a network with 5 logistic hidden nodes and a single linear output. Fig. 2 provides the resulting network with the trained weights and constants.<\/p>\n<div id=\"attachment_1240\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.1.png\"><img aria-describedby=\"caption-attachment-1240\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-1240\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.1.png\" width=\"450\" height=\"334\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.1.png 672w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.1-150x111.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.1-300x223.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.1-660x490.png 660w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><p id=\"caption-attachment-1240\" class=\"wp-caption-text\">Fig. 2. Trained network for predicting deterministic trend.<\/p><\/div>\n<p style=\"text-align: justify;\">The single input (scaled x<sub>t<\/sub>) is fed to all five nodes. Observe that it is multiplied with different weights (black numbers) and shifted by different constants (blue numbers) at each node. When additional inputs are used, the inherent difficulty in interpreting all these weights together, makes neural networks to be considered as black boxes. Fig. 3 provides the observed y<sub>t<\/sub> and predicted neural network values. The network is able to provide a very good fit in the training set and for most of test set A, but as the values increase (test set B) we can see that the networks starts to saturate (the individual nodes reach the upper bounds of the values they can output and eventually the whole network) and the predicted trend tapers off. As we saw earlier, each sigmoid-type node has a maximum value it can output.<\/p>\n<div id=\"attachment_1241\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.2.png\"><img aria-describedby=\"caption-attachment-1241\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-1241\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.2.png\" width=\"450\" height=\"334\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.2.png 672w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.2-150x111.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.2-300x223.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.2-660x490.png 660w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><p id=\"caption-attachment-1241\" class=\"wp-caption-text\">Fig. 3. Actuals and neural network values for the case of deterministic trend with no noise.<\/p><\/div>\n<p style=\"text-align: justify;\">This raises a significant doubt whether neural networks can forecast trended time series, if they are unable to model such an easy case. One would argue that with careful scaling of data (see good fit in test set A) it is possible to predict trends, but that implies that one knows the range that the future values would be in, to accommodate them with appropriate scaling. This information is typically unknown, especially when the trend is stochastic in nature.<\/p>\n<p><strong>3. Forecasting trends<\/strong><\/p>\n<p style=\"text-align: justify;\">Although forecasting trends is problematic when using raw data, we can pre-process the time series to enable successful modelling. We can remove any trend through differencing. Much like with ARIMA modelling, we overcome the problem of requiring the network to provide ever increasing\/decreasing values and therefore we can model such series. For example, considering one of the yearly M3 competition series we can produce the following forecast:<\/p>\n<div id=\"attachment_1247\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.3-1.png\"><img aria-describedby=\"caption-attachment-1247\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-1247\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.3-1.png\" width=\"450\" height=\"334\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.3-1.png 672w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.3-1-150x111.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.3-1-300x223.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2016\/12\/nn.trend_.3-1-660x490.png 660w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><p id=\"caption-attachment-1247\" class=\"wp-caption-text\">Fig. 4. Neural network forecast for a trending yearly M3 competition series.<\/p><\/div>\n<p style=\"text-align: justify;\">Fig. 4 provides the actuals and forecasts after differencing and scaling is applied, the forecast is produced and subsequently differencing and scaling are reversed. However there are some limitations to consider:<\/p>\n<ul>\n<li style=\"text-align: justify;\">This approach implies a two stage model, where first <em>z<sub>t <\/sub>= y<sub>t <\/sub>&#8211; y<sub>t-1 <\/sub><\/em>is constructed and then\u00a0<em>z<sub>t<\/sub><\/em> is modelled using neural networks. This imposes a set of modelling constraints that may be inappropriate.<\/li>\n<li style=\"text-align: justify;\">The neural network is capable of capturing nonlinearities. However if such nonlinearities are connected to the level, for example multiplicative seasonality, then by using differences we are making it very difficult for the network to approximate the underlying functional form.<\/li>\n<li style=\"text-align: justify;\">Differencing implies stochastic trend, which in principle is inappropriate when dealing with deterministic trend.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Therefore, it is fair to say that differencing is useful, but is by no means the only way to deal with trends and surely not always the best option. However, it is useful to understand how sigmoid-type neurons and networks are bound to fail in modelling raw trended time series. There have been several innovations in neural networks for forecasting, but most are bound by this limitation due to the transfer functions used.<\/p>\n<p style=\"text-align: justify;\">So, can neural networks forecast trended time series? Fig. 4 suggests yes, but how to best do it is still an open question. Past research that I have been part of has shown that using differences is reliable and effective (for example see the specifications of neural networks <a href=\"http:\/\/kourentzes.com\/forecasting\/2011\/04\/19\/validation-in-models-of-climate-change-and-forecasting-accuracy\/\">here<\/a> and <a href=\"http:\/\/kourentzes.com\/forecasting\/2014\/04\/19\/neural-network-ensemble-operators-for-time-series-forecasting\/\">here<\/a>), even though there are unresolved problems with differencing. Surely just expecting the network to &#8220;learn&#8221; to forecasts trends is not enough.<\/p>\n<div class=\"SPOSTARBUST-Related-Posts\"><H3>Related Posts<\/H3><ul class=\"entry-meta\"><li class=\"SPOSTARBUST-Related-Post\"><a title=\"Discussion panel on &#8216;AI in research&#8217; at Sk\u00f6vde\" href=\"https:\/\/kourentzes.com\/forecasting\/2020\/11\/06\/discussion-panel-on-ai-in-research-at-skovde\/\" rel=\"bookmark\">Discussion panel on &#8216;AI in research&#8217; at Sk\u00f6vde<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"OR62 -The quest for greater forecasting accuracy: Perspectives from Statistics &#038; Machine Learning\" href=\"https:\/\/kourentzes.com\/forecasting\/2020\/10\/20\/or62-forecasting-stream\/\" rel=\"bookmark\">OR62 -The quest for greater forecasting accuracy: Perspectives from Statistics &#038; Machine Learning<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"Forecasting Forum Scandinavia &#8211; first workshop!\" href=\"https:\/\/kourentzes.com\/forecasting\/2020\/09\/20\/forecasting-forum-scandinavia-first-workshop\/\" rel=\"bookmark\">Forecasting Forum Scandinavia &#8211; first workshop!<\/a><\/li>\n<\/ul><\/div><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Yes and&#8230; no! First, I should say that I am thinking of the common types of neural networks that are comprised by neurons that use some type of sigmoid transfer function, although the arguments discussed here are applicable to other types of neural networks. Before answering the question, let us first quickly summarise how typical\u2026 <span class=\"read-more\"><a href=\"https:\/\/kourentzes.com\/forecasting\/2016\/12\/28\/can-neural-networks-predict-trended-time-series\/\">Read More &raquo;<\/a><\/span><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[41],"tags":[14,76,12,53],"_links":{"self":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1202"}],"collection":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/comments?post=1202"}],"version-history":[{"count":1,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1202\/revisions"}],"predecessor-version":[{"id":1256,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1202\/revisions\/1256"}],"wp:attachment":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/media?parent=1202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/categories?post=1202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/tags?post=1202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- WP Super Cache is installed but broken. The constant WPCACHEHOME must be set in the file wp-config.php and point at the WP Super Cache plugin directory. -->