/
index.html
executable file
·422 lines (340 loc) · 12.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Trend Estimation in Time Series Signals</title>
<meta name="description" content="Trend Estimation in Time Series Signals">
<meta name="author" content="Bugra Akyildiz">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.css">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<link href='http://fonts.googleapis.com/css?family=Lato:100,300,400' rel='stylesheet' type='text/css'>
<!-- If the query includes 'print-pdf', include the PDF print sheet
<link rel="stylesheet" href="css/print/p.css">-->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h2 style="line-height:1.5;"><strong>Trend Estimation </strong> <br /> in Time Series Signals</h2>
</section>
<section>
<img src="assets/extrapolating.png" height="700" width="900"/>
</section>
<section>
<h2><strong>Hi!</strong></h2>
<p><strong>Bugra Akyildiz</strong> </p>
<p>Data Scientist at Axial</p>
<p>@bugraa</p><br>
<h4><strong>Machine Learning Newsletter </strong>| <strong>mln.io</strong></h4><br>
<h5 style="color:#57068C;">bugra@nyu.edu</h5>
<br>
<h5>http://bit.ly/pydata-seattle-2015</h5>
</section>
<section data-background="#b9131a">
<h2><strong>Axial</strong></h2>
<p>A network that brings private companies with investors together</p>
<p>Enables business owners access to private capital markets</p>
<h4>We are hiring! | axial.net</h4>
</section>
<section>
<h3>Trend Estimation</h3>
<p>Family of methods to be able to detect and predict tendencies and regularities in time series signals</p>
<br>
<ul>
<li> Depends on problem and domain </li>
<li> Medium to Long Term Trend </li>
<li> Mitigates seasonality(cycles) from data </li>
</ul>
<br><br>
<h4> Why? </h4>
<ul>
<li> Trends are very interpretable </li>
<li> Trends are easy to deal with when original signal is not very useful for processing </li>
</ul>
</section>
<section data-markdown>
## Trend Estimation Methods
- Moving average filtering
- Exponential Weighted Moving Average (EWMA)
- Median filtering
- Bandpass filtering
- Hodrick Prescott Filter
- $l_1$ trend filtering
</section>
<section data-markdown>
## Data
- The S&P 500, or the Standard & Poor's 500, is an American stock market index based on the market capitalizations of 500 large companies having common stock listed on the NYSE or NASDAQ.
- The S&P 500 index components and their weightings are determined by S&P Dow Jones Indices.
- The National Bureau of Economic Research has classified common stocks as a leading indicator of __business cycles__.
- We will come to these _cycles_ later.
```python
import pandas as pd
df = pd.read_csv(_SNP_500_PATH, parse_dates=['Date'])
df = df.sort(['Date'])
```
</section>
<section>
<h4> SNP 500 Data</h4>
<img src="assets/SNP_500_Close_Price_between_2014_01_01___2015_07_11.png" />
</section>
<section data-markdown>
## Moving Average Filtering
- Average the signal over a window
$$ y(t) = \frac{\displaystyle\sum_{i=-\frac{w}{2}}^{\frac{w}{2}} x(t + i)}{w} $$
</section>
<section data-markdown>
### In Python
```python
import pandas as pd
window = 11
averaged_signal = pd.rolling_mean(df.Close, window)
```
</section>
<section data-markdown>
## Good to Know
- Linear
- Not really a trend estimation method, but provides baseline
- If the window size is small, it removes high volatility part in the signal
- If the window size is large, it exposes the long-term trend
- Not robust to outliers and abrupt changes for small and medium window sizes
</section>
<section>
<img src="assets/Mean_Average_signal_with_window_=_11.png" />
</section>
<section>
<img src="assets/Mean_Average_signal_with_window_=_21.png" />
</section>
<section>
<img src="assets/Mean_Average_signal_with_window_=_61.png" />
</section>
<section data-markdown>
## Median Filtering
$$ y(t) = median\\{ x[t-\frac{w}{2}, t+\frac{w}{2}] \\} $$
where $w$ is the window size whose median will replace the original data point
</section>
<section data-markdown>
### In Python
```python
from scipy import signal as sp_signal
window = 11
median_filtered_signal = sp_signal.medfilt(df.Close, window)
```
</section>
<section data-markdown>
## Good to Know
- Nonlinear
- Very robust to noise
- If the window size is very large, it could _shadow_ mid-term change
- Trend signal may not be smooth(actually rarely is in practice)
</section>
<section>
<img src="assets/Median_Filtered_signal_with_window_=_11.png" />
</section>
<section>
<img src="assets/Median_Filtered_signal_with_window_=_21.png" />
</section>
<section>
<img src="assets/Median_Filtered_signal_with_window_=_61.png" />
</section>
<section>
<h2> EWMA </h2>
<img src="assets/ewma-formulas.png" />
</section>
<section data-markdown>
### In Python
```python
import pandas as pd
span = 20
ewma_signal = pd.stats.moments.ewma(df.Close, span=span)
```
</section>
<section data-markdown>
## Good to Know
- Linear
- Could provide a better estimate than a simple moving average because the weights
are better distributed
- Not robust to outliers and abrupt changes
- Very flexible in terms of weights and puts more emphasis on the spatial window
in the signal
</section>
<section>
<img src="assets/Exponential_Smoothed_signal_with_span_=_11.png" />
</section>
<section>
<img src="assets/Exponential_Smoothed_signal_with_span_=_21.png" />
</section>
<section>
<img src="assets/Exponential_Smoothed_signal_with_span_=_61.png" />
</section>
<section data-markdown>
## Bandpass Filtering
It filters based on __frequency response__ of the signal. It attenuates very low range
(long term) and very high frequency(short-term, volatility) and exposes mid-term
trend in the signal.
</section>
<section data-markdown>
## In Python
```python
## Filter Construction
filter_order = 2
low_cutoff_frequency = 0.001
high_cutoff_frequency = 0.15
b, a = sp_signal.butter(filter_order, [low_cutoff_frequency, high_cutoff_frequency],
btype='bandpass', output='ba')
bandpass_filtered = sp_signal.filtfilt(b, a, df.Close.values)
```
</section>
<section data-markdown>
## Good to Know
- Allow certain frequencies of the signal(between `low cutoff frequency` and `high cutoff frequency`) and attenuates the other frequencies.
- This provides a flexible way to remove/attenuate low frequency(very long term) and high frequency(short-term) in the signal.
- Could prepare different filters to stop a particular band as well(called band-stop filter).
- Similar to Hodrick-Prescott Filter, it extracts mid-term trend by removing very small changes(bias) and extracting short-term changes(cycle).
</section>
<section>
<img src="assets/Band_pass_Filtered_Signal_with_order:_2,_cutoff_frequency:_0.1.png" />
</section>
<section>
<img src="assets/Band_pass_Filtered_Signal_with_order:_2,_cutoff_frequency:_0.01.png" />
</section>
<section>
<img src="assets/Band_pass_Filtered_Signal_with_order:_8,_cutoff_frequency:_0.01.png" />
</section>
<section data-markdown>
## Hodrick-Prescott(HP) Filter
- Decomposes the time-series signal into a trend $x_t$ (mid-term growth) and a
cyclical component(recurring and seasonal signal) $c_t$.
$$y_t = x_t + c_t$$
</section>
<section>
<h2> HP Minimization Function </h2>
<img src="assets/hodrick-prescott-filter-minimization.png" height="200" width="500"/>
</section>
<section data-markdown>
## Good to Know
- Linear
- Decomposes the signal into two distinct components(trend and cycle)
- Cycle part => short term, season
- Trend part => medium to long term
- With changing regularizer, smoothing can be adjusted in the signal
- Bandpass filter is at its heart
- Perfect for signals that show seasonality
- Yields good results when noise is normally distributed
</section>
<section data-markdown>
### In Python
```python
import statsmodels.api as sm
lamb = 10 # Regularizer, lambda
snp_cycle, snp_trend = sm.tsa.filters.hpfilter(df.Close, lamb=lamb)
```
</section>
<section>
<img src="assets/Trend_and_Cycle_Signal_with_smoothing_parameter:_10.png" />
</section>
<section>
<img src="assets/Trend_and_Cycle_Signal_with_smoothing_parameter:_1000.png" />
</section>
<section>
<img src="assets/Trend_and_Cycle_Signal_with_smoothing_parameter:_1000000.png" />
</section>
<section data-markdown>
## $l_1$ Trend Filtering
Explanation: Instead of minimizing the mean squared error in HP minimization
function, what if we minimize by $l_1$ error? We could get a very robust way
to measure trend in the signal.
- Optimization function:
$$ \frac{1}{2} \lVert x - y \rVert_2^2 + \lambda \lVert Dx \rVert_1$$
where $x,y \in \mathbf{R}^n$ and $D$ is the second order difference matrix
</section>
<section data-markdown>
## Good to Know
- Nonlinear
- Trend is piecewise linear, generally very smooth
- The kinks, or changes in slope of the estimated trend show abrupt events
- Changes in trend could be used for outlier detection
- Computationally a little bit expensive.
- Yields good results when noise is exponentially distributed
</section>
<section data-markdown>
### Get the library
```bash
# See the source code: https://github.com/bugra/l1
# PRs are more than welcome!
git clone https://github.com/bugra/l1
cd l1
python setup.py install
```
### In Python
```python
from l1 import l1 # Get the library from: https://github.com/bugra/l1
regularizer = 1
l1_trend = l1(df.Close.values, regularizer)
```
</section>
<section>
<img src="assets/l_1_Trend_Signal_with_regularizer:_1.png" />
</section>
<section>
<img src="assets/l_1_Trend_Signal_with_regularizer:_10.png" />
</section>
<section>
<img src="assets/l_1_Trend_Signal_with_regularizer:_100.png" />
</section>
<section>
<h3> Questions? </h3>
<br>
<img src="assets/qa_cartoon.jpg" height="500" width="800"/>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: false,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/fade/none
math: {
mathjax: 'http://cdn.mathjax.org/mathjax/latest/MathJax.js',
config: 'TeX-AMS_HTML-full' // See http://docs.mathjax.org/en/latest/config-files.html
},
// Parallax scrolling
// parallaxBackgroundImage: 'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg',
// parallaxBackgroundSize: '2100px 900px',
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/math/math.js', async: true }, // Necessary for mathjax plugin
]
});
</script>
</body>
</html>