ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
notebook.ipynb
(12663B)
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {
6 "deletable": true,
7 "editable": true
8 },
9 "source": [
10 "#Exercises: Statistical Moments and Normality Testing\n",
11 "\n",
12 "## Lecture Link : \n",
13 "https://www.quantopian.com/lectures/statistical-moments\n",
14 "\n",
15 "###IMPORTANT NOTE: \n",
16 "This lecture corresponds to the Statistical Moments and Normality Testing 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",
17 "\n",
18 "Part of the Quantopian Lecture Series:\n",
19 "\n",
20 "* [www.quantopian.com/lectures](https://www.quantopian.com/lectures)\n",
21 "* [github.com/quantopian/research_public](https://github.com/quantopian/research_public)\n",
22 "\n",
23 "----"
24 ]
25 },
26 {
27 "cell_type": "code",
28 "execution_count": null,
29 "metadata": {
30 "collapsed": false,
31 "deletable": true,
32 "editable": true
33 },
34 "outputs": [],
35 "source": [
36 "# Useful Libraries\n",
37 "import matplotlib.pyplot as plt\n",
38 "import numpy as np\n",
39 "import pandas as pd\n",
40 "import scipy.stats as stats\n",
41 "from statsmodels.stats.stattools import jarque_bera"
42 ]
43 },
44 {
45 "cell_type": "markdown",
46 "metadata": {
47 "deletable": true,
48 "editable": true
49 },
50 "source": [
51 "---"
52 ]
53 },
54 {
55 "cell_type": "markdown",
56 "metadata": {
57 "deletable": true,
58 "editable": true
59 },
60 "source": [
61 "#Exercise 1: Testing for Skew\n",
62 "\n",
63 "##a. Artificial Example\n",
64 "\n",
65 "Use the results from the `stats.skew` function to determine the skew of the artificial distribution named X. "
66 ]
67 },
68 {
69 "cell_type": "code",
70 "execution_count": null,
71 "metadata": {
72 "collapsed": false,
73 "deletable": true,
74 "editable": true,
75 "scrolled": false
76 },
77 "outputs": [],
78 "source": [
79 "xs2 = np.linspace(stats.gamma.ppf(0.01, 0.7, loc=-1), stats.gamma.ppf(0.99, 0.7, loc=-1), 150) + 1\n",
80 "\n",
81 "X = stats.gamma.pdf(xs2, 1.5)\n",
82 "\n",
83 "#Your code goes here"
84 ]
85 },
86 {
87 "cell_type": "markdown",
88 "metadata": {
89 "deletable": true,
90 "editable": true
91 },
92 "source": [
93 "##b. Real Example\n",
94 "\n",
95 "Use the results from the `stats.skew` function to determine the skew of the returns of NFLX and use it to make a conclusion about the symmetry of the stock's returns. "
96 ]
97 },
98 {
99 "cell_type": "code",
100 "execution_count": null,
101 "metadata": {
102 "collapsed": false,
103 "deletable": true,
104 "editable": true,
105 "scrolled": false
106 },
107 "outputs": [],
108 "source": [
109 "start = '2015-01-01'\n",
110 "end = '2016-01-01'\n",
111 "pricing = get_pricing('NFLX', fields='price', start_date=start, end_date=end)\n",
112 "returns = pricing.pct_change()[1:]\n",
113 "\n",
114 "#Your code goes here"
115 ]
116 },
117 {
118 "cell_type": "markdown",
119 "metadata": {
120 "deletable": true,
121 "editable": true
122 },
123 "source": [
124 "---"
125 ]
126 },
127 {
128 "cell_type": "markdown",
129 "metadata": {
130 "deletable": true,
131 "editable": true
132 },
133 "source": [
134 "#Exercise 2: Testing for Kurtosis\n",
135 "\n",
136 "##a. Artificial Example\n",
137 "\n",
138 "Use the results from the `stats.kurtosis` function to determine the excess kurtosis of the artificial distribution named Y. "
139 ]
140 },
141 {
142 "cell_type": "code",
143 "execution_count": null,
144 "metadata": {
145 "collapsed": false,
146 "deletable": true,
147 "editable": true
148 },
149 "outputs": [],
150 "source": [
151 "xs = np.linspace(-6,6, 300) + 2 \n",
152 "\n",
153 "Y = stats.cosine.pdf(xs)\n",
154 "\n",
155 "#Your code goes here"
156 ]
157 },
158 {
159 "cell_type": "markdown",
160 "metadata": {
161 "deletable": true,
162 "editable": true
163 },
164 "source": [
165 "##b. Real Example\n",
166 "\n",
167 "Use the results from the `stats.kurtosis` function to determine the kurtosis of the returns of NFLX and use it to make a conclusion about the volatility of the stock's price."
168 ]
169 },
170 {
171 "cell_type": "code",
172 "execution_count": null,
173 "metadata": {
174 "collapsed": false,
175 "deletable": true,
176 "editable": true
177 },
178 "outputs": [],
179 "source": [
180 "start = '2015-01-01'\n",
181 "end = '2016-01-01'\n",
182 "pricing = get_pricing('NFLX', fields='price', start_date=start, end_date=end)\n",
183 "returns = pricing.pct_change()[1:]\n",
184 "\n",
185 "#Your code goes here"
186 ]
187 },
188 {
189 "cell_type": "markdown",
190 "metadata": {
191 "deletable": true,
192 "editable": true
193 },
194 "source": [
195 "---"
196 ]
197 },
198 {
199 "cell_type": "markdown",
200 "metadata": {
201 "deletable": true,
202 "editable": true
203 },
204 "source": [
205 "# Exercise 3: Skew and Normality\n",
206 "\n",
207 "##a. Artificial Example II\n",
208 "\n",
209 "Use the results from the `stats.skew` function to determine the skew of the artificial distribution named Z. "
210 ]
211 },
212 {
213 "cell_type": "code",
214 "execution_count": null,
215 "metadata": {
216 "collapsed": false,
217 "deletable": true,
218 "editable": true
219 },
220 "outputs": [],
221 "source": [
222 "xs2 = np.linspace(stats.lognorm.ppf(0.01, 0.7, loc=-.1), stats.lognorm.ppf(0.99, 0.7, loc=-.1), 150)\n",
223 "\n",
224 "lognorm = stats.lognorm.pdf(xs2, 0.4)\n",
225 "\n",
226 "Z = lognorm/2 + lognorm[::-1]\n",
227 "\n",
228 "#Your code goes here"
229 ]
230 },
231 {
232 "cell_type": "markdown",
233 "metadata": {
234 "deletable": true,
235 "editable": true
236 },
237 "source": [
238 "## b. Jarque-Bera Calibration\n",
239 "\n",
240 "Ensure that the `jarque-bera` function is calibrated by running it on many trials of simulated data and ensuring that the sample probability that the test returns a result under the p-value is equal to the p-value."
241 ]
242 },
243 {
244 "cell_type": "code",
245 "execution_count": null,
246 "metadata": {
247 "collapsed": false,
248 "deletable": true,
249 "editable": true
250 },
251 "outputs": [],
252 "source": [
253 "N = 1000\n",
254 "M = 1000\n",
255 "\n",
256 "pvalues = np.ndarray((N))\n",
257 "\n",
258 "for i in range(N):\n",
259 " # Draw M samples from a normal distribution \n",
260 " X = np.random.normal(0, 1, M);\n",
261 " _, pvalue, _, _ = jarque_bera(X)\n",
262 " pvalues[i] = pvalue\n",
263 "\n",
264 "num_significant = len(pvalues[pvalues < 0.05])\n",
265 "\n",
266 "#Your code goes here"
267 ]
268 },
269 {
270 "cell_type": "markdown",
271 "metadata": {
272 "deletable": true,
273 "editable": true
274 },
275 "source": [
276 "## c. Jarque-Bera Test\n",
277 "\n",
278 "Use the `Jarque-Bera` function to determine the normality of Z."
279 ]
280 },
281 {
282 "cell_type": "code",
283 "execution_count": null,
284 "metadata": {
285 "collapsed": false,
286 "deletable": true,
287 "editable": true
288 },
289 "outputs": [],
290 "source": [
291 "#Your code goes here"
292 ]
293 },
294 {
295 "cell_type": "markdown",
296 "metadata": {
297 "deletable": true,
298 "editable": true
299 },
300 "source": [
301 "## d. Skewness and Normality\n",
302 "\n",
303 "Plot Z and observe that skewness is not informative unless the underlying distribution is somewhat normal."
304 ]
305 },
306 {
307 "cell_type": "code",
308 "execution_count": null,
309 "metadata": {
310 "collapsed": false,
311 "deletable": true,
312 "editable": true
313 },
314 "outputs": [],
315 "source": [
316 "#Your code goes here"
317 ]
318 },
319 {
320 "cell_type": "markdown",
321 "metadata": {
322 "deletable": true,
323 "editable": true
324 },
325 "source": [
326 "---"
327 ]
328 },
329 {
330 "cell_type": "markdown",
331 "metadata": {
332 "deletable": true,
333 "editable": true
334 },
335 "source": [
336 "# Exercise 4: Out of Sample Test\n",
337 "\n",
338 "##a. Testing for Normality\n",
339 "\n",
340 "Plot a histogram of the historical returns of AMC to ensure it is unimodal and vaguely normal before testing it for skewness in part b."
341 ]
342 },
343 {
344 "cell_type": "code",
345 "execution_count": null,
346 "metadata": {
347 "collapsed": false,
348 "deletable": true,
349 "editable": true,
350 "scrolled": false
351 },
352 "outputs": [],
353 "source": [
354 "start = '2014-01-01'\n",
355 "end = '2016-01-01'\n",
356 "pricing = get_pricing('AMC', fields='price', start_date=start, end_date=end)\n",
357 "returns = pricing.pct_change()[1:]\n",
358 "\n",
359 "#Your code goes here"
360 ]
361 },
362 {
363 "cell_type": "markdown",
364 "metadata": {
365 "deletable": true,
366 "editable": true
367 },
368 "source": [
369 "##b. Test for Skew\n",
370 "\n",
371 "Find the skew of the historical returns of AMC between 2014 to 2016. "
372 ]
373 },
374 {
375 "cell_type": "code",
376 "execution_count": null,
377 "metadata": {
378 "collapsed": false,
379 "deletable": true,
380 "editable": true
381 },
382 "outputs": [],
383 "source": [
384 "start = '2014-01-01'\n",
385 "end = '2016-01-01'\n",
386 "pricing = get_pricing('AMC', fields='price', start_date=start, end_date=end)\n",
387 "returns = pricing.pct_change()[1:]\n",
388 "\n",
389 "#Your code goes here"
390 ]
391 },
392 {
393 "cell_type": "markdown",
394 "metadata": {
395 "deletable": true,
396 "editable": true
397 },
398 "source": [
399 "## c. Out of Sample Test\n",
400 "\n",
401 "Find the skew of the historical retunrs of AMC from the first half of 2016 to determine if the skew from part b holds outside of the original sample."
402 ]
403 },
404 {
405 "cell_type": "code",
406 "execution_count": null,
407 "metadata": {
408 "collapsed": false,
409 "deletable": true,
410 "editable": true
411 },
412 "outputs": [],
413 "source": [
414 "start = '2016-01-01'\n",
415 "end = '2016-07-01'\n",
416 "out_pricing = get_pricing('AMC', fields='price', start_date=start, end_date=end)\n",
417 "out_returns = out_pricing.pct_change()[1:]\n",
418 "\n",
419 "#Your code goes here"
420 ]
421 },
422 {
423 "cell_type": "markdown",
424 "metadata": {
425 "deletable": true,
426 "editable": true
427 },
428 "source": [
429 "## d. Rolling Skew\n",
430 "\n",
431 "Plot the rolling skew of AMC using the `pd.rolling_skew` function. "
432 ]
433 },
434 {
435 "cell_type": "code",
436 "execution_count": null,
437 "metadata": {
438 "collapsed": false,
439 "deletable": true,
440 "editable": true
441 },
442 "outputs": [],
443 "source": [
444 "AMC = get_pricing('AMC', fields='price', start_date='2015-01-01', end_date='2017-01-01')\n",
445 "\n",
446 "#Your code goes here"
447 ]
448 },
449 {
450 "cell_type": "markdown",
451 "metadata": {
452 "deletable": true,
453 "editable": true
454 },
455 "source": [
456 "---\n",
457 "\n",
458 "Congratulations on completing the Statistical Moments and Normality Testing exercises!\n",
459 "\n",
460 "As you learn more about writing trading algorithms and the Quantopian platform, be sure to check out the daily [Quantopian Contest](https://www.quantopian.com/contest), in which you can compete for a cash prize every day.\n",
461 "\n",
462 "Start by going through the [Writing a Contest Algorithm](https://www.quantopian.com/tutorials/contest) tutorial."
463 ]
464 },
465 {
466 "cell_type": "markdown",
467 "metadata": {
468 "deletable": true,
469 "editable": true
470 },
471 "source": [
472 "*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.*"
473 ]
474 }
475 ],
476 "metadata": {
477 "kernelspec": {
478 "display_name": "Python 2",
479 "language": "python",
480 "name": "python2"
481 },
482 "language_info": {
483 "codemirror_mode": {
484 "name": "ipython",
485 "version": 2
486 },
487 "file_extension": ".py",
488 "mimetype": "text/x-python",
489 "name": "python",
490 "nbconvert_exporter": "python",
491 "pygments_lexer": "ipython2",
492 "version": "2.7.12"
493 }
494 },
495 "nbformat": 4,
496 "nbformat_minor": 0
497 }