<|start_filename|>index.html<|end_filename|>
<!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="<NAME>">

		<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><NAME></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;"><EMAIL></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>
