ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
notebook.ipynb
(9539B)
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {
6 "deletable": true,
7 "editable": true
8 },
9 "source": [
10 "#Exercises: Integration, Cointegration, and Stationarity \n",
11 "by Delaney Granizo-Mackenzie and Maxwell Margenot\n",
12 "\n",
13 "## Lecture Link :\n",
14 "https://www.quantopian.com/lectures/integration-cointegration-and-stationarity\n",
15 "\n",
16 "###IMPORTANT NOTE: \n",
17 "This lecture corresponds to the Integration, Cointegration, and Stationarity lecture, which is part of the Quantopian lecture series. This homework expects you to rely heavily on the code presented in the corresponding lecture. Please copy and paste regularly from that lecture when starting to work on the problems, as trying to do them from scratch will likely be too difficult.\n",
18 "\n",
19 "Part of the Quantopian Lecture Series:\n",
20 "\n",
21 "* [www.quantopian.com/lectures](https://www.quantopian.com/lectures)\n",
22 "* [github.com/quantopian/research_public](https://github.com/quantopian/research_public)\n",
23 "----"
24 ]
25 },
26 {
27 "cell_type": "markdown",
28 "metadata": {
29 "deletable": true,
30 "editable": true
31 },
32 "source": [
33 "## Helper Functions"
34 ]
35 },
36 {
37 "cell_type": "code",
38 "execution_count": null,
39 "metadata": {
40 "collapsed": true,
41 "deletable": true,
42 "editable": true
43 },
44 "outputs": [],
45 "source": [
46 "# Useful Functions\n",
47 "def check_for_stationarity(X, cutoff=0.01):\n",
48 " # H_0 in adfuller is unit root exists (non-stationary)\n",
49 " # We must observe significant p-value to convince ourselves that the series is stationary\n",
50 " pvalue = adfuller(X)[1]\n",
51 " if pvalue < cutoff:\n",
52 " print 'p-value = ' + str(pvalue) + ' The series is likely stationary.'\n",
53 " return True\n",
54 " else:\n",
55 " print 'p-value = ' + str(pvalue) + ' The series is likely non-stationary.'\n",
56 " return False\n",
57 " \n",
58 "def generate_datapoint(params):\n",
59 " mu = params[0]\n",
60 " sigma = params[1]\n",
61 " return np.random.normal(mu, sigma)"
62 ]
63 },
64 {
65 "cell_type": "code",
66 "execution_count": null,
67 "metadata": {
68 "collapsed": true,
69 "deletable": true,
70 "editable": true
71 },
72 "outputs": [],
73 "source": [
74 "# Useful Libraries\n",
75 "import numpy as np\n",
76 "import pandas as pd\n",
77 "\n",
78 "import statsmodels\n",
79 "import statsmodels.api as sm\n",
80 "from statsmodels.tsa.stattools import coint, adfuller\n",
81 "\n",
82 "import matplotlib.pyplot as plt"
83 ]
84 },
85 {
86 "cell_type": "markdown",
87 "metadata": {
88 "deletable": true,
89 "editable": true
90 },
91 "source": [
92 "----"
93 ]
94 },
95 {
96 "cell_type": "markdown",
97 "metadata": {
98 "deletable": true,
99 "editable": true
100 },
101 "source": [
102 "#Exercise 1: Stationarity Testing\n",
103 "\n",
104 "##a. Checking For Stationarity\n",
105 "\n",
106 "Check whether the following series is stationary using the tests from the lecture."
107 ]
108 },
109 {
110 "cell_type": "code",
111 "execution_count": null,
112 "metadata": {
113 "collapsed": false,
114 "deletable": true,
115 "editable": true
116 },
117 "outputs": [],
118 "source": [
119 "QQQ = get_pricing(\"QQQ\", start_date='2014-1-1', end_date='2015-1-1', fields='price')\n",
120 "QQQ.name = QQQ.name.symbol\n",
121 "\n",
122 "# Your code goes here"
123 ]
124 },
125 {
126 "cell_type": "markdown",
127 "metadata": {
128 "deletable": true,
129 "editable": true
130 },
131 "source": [
132 "## b. Checking for Normality\n",
133 "\n",
134 "As an extra all-purpose check, and one that is often done on series, check whether the above series is normally distributed using the Jarque-Bera test."
135 ]
136 },
137 {
138 "cell_type": "code",
139 "execution_count": null,
140 "metadata": {
141 "collapsed": false,
142 "deletable": true,
143 "editable": true
144 },
145 "outputs": [],
146 "source": [
147 "from statsmodels.stats.stattools import jarque_bera\n",
148 "\n",
149 "# Your code goes here"
150 ]
151 },
152 {
153 "cell_type": "markdown",
154 "metadata": {
155 "deletable": true,
156 "editable": true
157 },
158 "source": [
159 "##c. Constructing Examples I\n",
160 "\n",
161 "Create/provide a series that is stationary and different from any covered so far in the exercise or the lecture."
162 ]
163 },
164 {
165 "cell_type": "code",
166 "execution_count": null,
167 "metadata": {
168 "collapsed": false,
169 "deletable": true,
170 "editable": true
171 },
172 "outputs": [],
173 "source": [
174 "# Your code goes here"
175 ]
176 },
177 {
178 "cell_type": "markdown",
179 "metadata": {
180 "deletable": true,
181 "editable": true
182 },
183 "source": [
184 "##d. Constructing Examples II\n",
185 "\n",
186 "Create/provide a series that is non-stationary and different from any covered so far in the exercise or the lecture."
187 ]
188 },
189 {
190 "cell_type": "code",
191 "execution_count": null,
192 "metadata": {
193 "collapsed": true,
194 "deletable": true,
195 "editable": true
196 },
197 "outputs": [],
198 "source": [
199 "# Your code goes here"
200 ]
201 },
202 {
203 "cell_type": "markdown",
204 "metadata": {
205 "deletable": true,
206 "editable": true
207 },
208 "source": [
209 "----"
210 ]
211 },
212 {
213 "cell_type": "markdown",
214 "metadata": {
215 "deletable": true,
216 "editable": true
217 },
218 "source": [
219 "# Exercise 2: Estimate Order of Integration\n",
220 "\n",
221 "Use the techniques laid out in the lecture notebook to estimate the order of integration for the following timeseries."
222 ]
223 },
224 {
225 "cell_type": "code",
226 "execution_count": null,
227 "metadata": {
228 "collapsed": false,
229 "deletable": true,
230 "editable": true
231 },
232 "outputs": [],
233 "source": [
234 "QQQ = get_pricing(\"QQQ\", start_date='2014-1-1', end_date='2015-1-1', fields='price')\n",
235 "QQQ.name = QQQ.name.symbol\n",
236 "\n",
237 "# Write code to estimate the order of integration of QQQ.\n",
238 "# Feel free to sample from the code provided in the lecture."
239 ]
240 },
241 {
242 "cell_type": "markdown",
243 "metadata": {
244 "deletable": true,
245 "editable": true
246 },
247 "source": [
248 "----"
249 ]
250 },
251 {
252 "cell_type": "markdown",
253 "metadata": {
254 "deletable": true,
255 "editable": true
256 },
257 "source": [
258 "#Exercise 3: Find a Stationary Linear (Cointegrated) Combination\n",
259 "\n",
260 "Use the techniques laid out in the lecture notebook to find a linear combination of the following timeseries that is stationary."
261 ]
262 },
263 {
264 "cell_type": "code",
265 "execution_count": null,
266 "metadata": {
267 "collapsed": false,
268 "deletable": true,
269 "editable": true
270 },
271 "outputs": [],
272 "source": [
273 "T = 500\n",
274 "\n",
275 "X1 = pd.Series(index=range(T))\n",
276 "X1.name = 'X1'\n",
277 "\n",
278 "for t in range(T):\n",
279 " # Now the parameters are dependent on time\n",
280 " # Specifically, the mean of the series changes over time\n",
281 " params = (t * 0.1, 1)\n",
282 " X1[t] = generate_datapoint(params)\n",
283 "\n",
284 "X2 = np.power(X1, 2) + X1\n",
285 "X3 = np.power(X1, 3) + X1\n",
286 "X4 = np.sin(X1) + X1\n",
287 "\n",
288 "# We now have 4 time series, X1, X2, X3, X4\n",
289 "# Determine a linear combination of the 4 that is stationary over the \n",
290 "# time period shown using the techniques in the lecture."
291 ]
292 },
293 {
294 "cell_type": "markdown",
295 "metadata": {},
296 "source": [
297 "---\n",
298 "\n",
299 "Congratulations on completing the Integration, Cointegration, and Stationarity exercises!\n",
300 "\n",
301 "As you learn more about writing trading models and the Quantopian platform, enter the daily [Quantopian Contest](https://www.quantopian.com/contest). Your strategy will be evaluated for a cash prize every day.\n",
302 "\n",
303 "Start by going through the [Writing a Contest Algorithm](https://www.quantopian.com/tutorials/contest) tutorial."
304 ]
305 },
306 {
307 "cell_type": "markdown",
308 "metadata": {
309 "deletable": true,
310 "editable": true
311 },
312 "source": [
313 "*This presentation is for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation for any security; nor does it constitute an offer to provide investment advisory or other services by Quantopian, Inc. (\"Quantopian\"). Nothing contained herein constitutes investment advice or offers any opinion with respect to the suitability of any security, and any views expressed herein should not be taken as advice to buy, sell, or hold any security or as an endorsement of any security or company. In preparing the information contained herein, Quantopian, Inc. has not taken into account the investment needs, objectives, and financial circumstances of any particular investor. Any views expressed and data illustrated herein were prepared based upon information, believed to be reliable, available to Quantopian, Inc. at the time of publication. Quantopian makes no guarantees as to their accuracy or completeness. All information is subject to change and may quickly become unreliable for various reasons, including changes in market conditions or economic circumstances.*"
314 ]
315 }
316 ],
317 "metadata": {
318 "kernelspec": {
319 "display_name": "Python 2",
320 "language": "python",
321 "name": "python2"
322 },
323 "language_info": {
324 "codemirror_mode": {
325 "name": "ipython",
326 "version": 2
327 },
328 "file_extension": ".py",
329 "mimetype": "text/x-python",
330 "name": "python",
331 "nbconvert_exporter": "python",
332 "pygments_lexer": "ipython2",
333 "version": "2.7.12"
334 }
335 },
336 "nbformat": 4,
337 "nbformat_minor": 0
338 }