|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "metadata": {}, |
| 5 | + "metadata": { |
| 6 | + "collapsed": false |
| 7 | + }, |
6 | 8 | "source": [ |
7 | 9 | "# It gets loopy" |
8 | 10 | ] |
9 | 11 | }, |
10 | 12 | { |
11 | 13 | "cell_type": "markdown", |
12 | | - "metadata": {}, |
| 14 | + "metadata": { |
| 15 | + "collapsed": false |
| 16 | + }, |
13 | 17 | "source": [ |
14 | 18 | "In this notebook, we will work with stock prices. I'll get you started by writing some code that go and fetch the last quotes of a few American stocks. We will then write loops and functions to do two things:\n", |
15 | 19 | "\n", |
|
19 | 23 | }, |
20 | 24 | { |
21 | 25 | "cell_type": "markdown", |
22 | | - "metadata": {}, |
| 26 | + "metadata": { |
| 27 | + "collapsed": false |
| 28 | + }, |
23 | 29 | "source": [ |
24 | 30 | "## Setup" |
25 | 31 | ] |
26 | 32 | }, |
27 | 33 | { |
28 | 34 | "cell_type": "markdown", |
29 | | - "metadata": {}, |
| 35 | + "metadata": { |
| 36 | + "collapsed": false |
| 37 | + }, |
30 | 38 | "source": [ |
31 | 39 | "We start by importing the quotes. It is not crucial to understand the code here below as we will see what a call to an API is during a future session. It works broadly like this: we call a server that sends us back a kind of nested dictionaries and lists that contain the quotes and date of those quotes for the 5 largest tech firms (that are sometimes referred to as GAFAM)." |
32 | 40 | ] |
|
53 | 61 | }, |
54 | 62 | { |
55 | 63 | "cell_type": "markdown", |
56 | | - "metadata": {}, |
| 64 | + "metadata": { |
| 65 | + "collapsed": false |
| 66 | + }, |
57 | 67 | "source": [ |
58 | 68 | "Run the cell above: it will run for some time and then ends without showing anything. In fact, it loads a peculiar data structure in the memory of your computer. The structure consists in prices and dates contained inside a dictionary which is itself contained in a dictionary. It is kind of a lousy structure so far but we will see in next sessions (the pandas one) how to make it easier. For now, we have to rely on this awkward structure. Here are some example of how to use it." |
59 | 69 | ] |
|
120 | 130 | }, |
121 | 131 | { |
122 | 132 | "cell_type": "markdown", |
123 | | - "metadata": {}, |
| 133 | + "metadata": { |
| 134 | + "collapsed": false |
| 135 | + }, |
124 | 136 | "source": [ |
125 | 137 | "## Let's work" |
126 | 138 | ] |
127 | 139 | }, |
128 | 140 | { |
129 | 141 | "cell_type": "markdown", |
130 | | - "metadata": {}, |
| 142 | + "metadata": { |
| 143 | + "collapsed": false |
| 144 | + }, |
131 | 145 | "source": [ |
132 | 146 | "OK, enough playing around! Now is your turn: Let's start by stretching our programming muscles. I'll write a line that will create a new variable called *quotes_amazon* and that is a list that contains the quotes of Amazon (in dollars) for all the data we have.\n", |
133 | 147 | "\n", |
|
149 | 163 | }, |
150 | 164 | { |
151 | 165 | "cell_type": "markdown", |
152 | | - "metadata": {}, |
| 166 | + "metadata": { |
| 167 | + "collapsed": false |
| 168 | + }, |
153 | 169 | "source": [ |
154 | 170 | "Done? Good! Now for a harder problem: We will create a function that will compute the mean of any list that is passed (provided it contains integers or floats, of course). The function will work broadly like this: You initialize a counter at 0, you loop over the list and, at each iteration, you add the value at that position to the counter. Once the loop is over, you divide this by the length of the list to obtain the average.\n", |
155 | 171 | "\n", |
|
186 | 202 | }, |
187 | 203 | { |
188 | 204 | "cell_type": "markdown", |
189 | | - "metadata": {}, |
| 205 | + "metadata": { |
| 206 | + "collapsed": false |
| 207 | + }, |
190 | 208 | "source": [ |
191 | 209 | "Let's now build on top of what we did previously to build a function that computes the mobile average. Let's start by using a fix number of lags to be considered (you can always extend it later): We want to have a function that takes a list and produces a new list containing the average value of the three past value (you can either align the two list by setting the three first value of your result to *None* or not, this will impact the way you will have to solve the next exercises, but both ways will be equally practical). Test it on the Google quotes that are in the variable *google_quotes*." |
192 | 210 | ] |
|
207 | 225 | }, |
208 | 226 | { |
209 | 227 | "cell_type": "markdown", |
210 | | - "metadata": {}, |
| 228 | + "metadata": { |
| 229 | + "collapsed": false |
| 230 | + }, |
211 | 231 | "source": [ |
212 | 232 | "Now that we have done it for a fix number of lags, do it again but now allow for a second parameter to your function: the number of lags you want to consider for your moving average." |
213 | 233 | ] |
|
223 | 243 | }, |
224 | 244 | { |
225 | 245 | "cell_type": "markdown", |
226 | | - "metadata": {}, |
| 246 | + "metadata": { |
| 247 | + "collapsed": false |
| 248 | + }, |
227 | 249 | "source": [ |
228 | 250 | "We are now going to compute the mean squared error. Write a function that sums over the squared differences between the actual value and your moving-average-based forecast. This will help us determining the optimal number of lags for our secret investment strategy at the next step." |
229 | 251 | ] |
|
239 | 261 | }, |
240 | 262 | { |
241 | 263 | "cell_type": "markdown", |
242 | | - "metadata": {}, |
| 264 | + "metadata": { |
| 265 | + "collapsed": false |
| 266 | + }, |
243 | 267 | "source": [ |
244 | 268 | "Our masterplan to get rich is getting together nicely...\n", |
245 | 269 | "\n", |
246 | 270 | "\n", |
247 | 271 | "\n", |
248 | | - "The last step before creating the actual investement strategy: we are going to evaluate which lag produces the smallest mean square error. For that, create a function that loops over the range between 1 (the naive forecast) and 6 and return the optimal lag (the one with the smallest RMSE)." |
| 272 | + "The last step before creating the actual investement strategy: we are going to evaluate which lag produces the smallest mean square error. For that, create a function that loops over the range between 2 and 6 and return the optimal lag (the one with the smallest MSE)." |
249 | 273 | ] |
250 | 274 | }, |
251 | 275 | { |
|
259 | 283 | }, |
260 | 284 | { |
261 | 285 | "cell_type": "markdown", |
262 | | - "metadata": {}, |
| 286 | + "metadata": { |
| 287 | + "collapsed": false |
| 288 | + }, |
263 | 289 | "source": [ |
264 | 290 | "We are now going to devise an incredibly dumb investment strategy: if your moving-average prediction (with the optimal lag - see, we are already [double-dipping](https://en.wikipedia.org/wiki/Circular_analysis) in the data...) is higher than the current price on a day, we invest all our accrued fortune (if we currently had no stock) or hold (if we had already some value invested), if the prediction is below, we sell our whole position (or do nothing if we had no money invested at the time).\n", |
265 | 291 | "\n", |
266 | | - "How much would we have today if we had started using this strategy on February 1st and started out with 100 Euros?\n", |
| 292 | + "How much would we have today if we had started using this strategy on February 3rd and started out with 100 Euros?\n", |
267 | 293 | "\n", |
268 | 294 | "This is a significantly more complex problem than the ones above and you might want to split it into subproblems (for example, a good idea could be to devise a function that takes two values and returns the variation between those). There are many ways to solve it, find the one that makes more sense to you. The result should be the same for everyone, though." |
269 | 295 | ] |
|
279 | 305 | }, |
280 | 306 | { |
281 | 307 | "cell_type": "markdown", |
282 | | - "metadata": {}, |
| 308 | + "metadata": { |
| 309 | + "collapsed": false |
| 310 | + }, |
283 | 311 | "source": [ |
284 | 312 | "## Get rich (fast) or code tryin'" |
285 | 313 | ] |
286 | 314 | }, |
287 | 315 | { |
288 | 316 | "cell_type": "markdown", |
289 | | - "metadata": {}, |
| 317 | + "metadata": { |
| 318 | + "collapsed": false |
| 319 | + }, |
290 | 320 | "source": [ |
291 | 321 | "The last step for today is to assess how much we will make in the near future. For this, we will use a small, non-parametric Monte-Carlo simulation.\n", |
292 | 322 | "\n", |
|
310 | 340 | }, |
311 | 341 | { |
312 | 342 | "cell_type": "markdown", |
313 | | - "metadata": {}, |
| 343 | + "metadata": { |
| 344 | + "collapsed": false |
| 345 | + }, |
314 | 346 | "source": [ |
315 | 347 | "The next cell will represent the distribution of your profits, only execute it once you have your 5000 results in the variable *results*." |
316 | 348 | ] |
|
354 | 386 | ], |
355 | 387 | "metadata": { |
356 | 388 | "kernelspec": { |
| 389 | + "argv": [ |
| 390 | + "python", |
| 391 | + "-m", |
| 392 | + "ipykernel_launcher", |
| 393 | + "-f", |
| 394 | + "{connection_file}" |
| 395 | + ], |
357 | 396 | "display_name": "Python 3", |
| 397 | + "env": null, |
| 398 | + "interrupt_mode": "signal", |
358 | 399 | "language": "python", |
| 400 | + "metadata": null, |
359 | 401 | "name": "python3" |
360 | 402 | }, |
361 | 403 | "language_info": { |
|
0 commit comments