Skip to content

Commit d0a8634

Browse files
authored
Merge pull request #30 from CharlesHoffreumon/master
Fixed small errors with exercises
2 parents 235fc25 + f65a174 commit d0a8634

File tree

1 file changed

+60
-18
lines changed

1 file changed

+60
-18
lines changed

Session_4/exercises_4.ipynb

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"metadata": {},
5+
"metadata": {
6+
"collapsed": false
7+
},
68
"source": [
79
"# It gets loopy"
810
]
911
},
1012
{
1113
"cell_type": "markdown",
12-
"metadata": {},
14+
"metadata": {
15+
"collapsed": false
16+
},
1317
"source": [
1418
"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",
1519
"\n",
@@ -19,14 +23,18 @@
1923
},
2024
{
2125
"cell_type": "markdown",
22-
"metadata": {},
26+
"metadata": {
27+
"collapsed": false
28+
},
2329
"source": [
2430
"## Setup"
2531
]
2632
},
2733
{
2834
"cell_type": "markdown",
29-
"metadata": {},
35+
"metadata": {
36+
"collapsed": false
37+
},
3038
"source": [
3139
"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)."
3240
]
@@ -53,7 +61,9 @@
5361
},
5462
{
5563
"cell_type": "markdown",
56-
"metadata": {},
64+
"metadata": {
65+
"collapsed": false
66+
},
5767
"source": [
5868
"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."
5969
]
@@ -120,14 +130,18 @@
120130
},
121131
{
122132
"cell_type": "markdown",
123-
"metadata": {},
133+
"metadata": {
134+
"collapsed": false
135+
},
124136
"source": [
125137
"## Let's work"
126138
]
127139
},
128140
{
129141
"cell_type": "markdown",
130-
"metadata": {},
142+
"metadata": {
143+
"collapsed": false
144+
},
131145
"source": [
132146
"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",
133147
"\n",
@@ -149,7 +163,9 @@
149163
},
150164
{
151165
"cell_type": "markdown",
152-
"metadata": {},
166+
"metadata": {
167+
"collapsed": false
168+
},
153169
"source": [
154170
"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",
155171
"\n",
@@ -186,7 +202,9 @@
186202
},
187203
{
188204
"cell_type": "markdown",
189-
"metadata": {},
205+
"metadata": {
206+
"collapsed": false
207+
},
190208
"source": [
191209
"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*."
192210
]
@@ -207,7 +225,9 @@
207225
},
208226
{
209227
"cell_type": "markdown",
210-
"metadata": {},
228+
"metadata": {
229+
"collapsed": false
230+
},
211231
"source": [
212232
"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."
213233
]
@@ -223,7 +243,9 @@
223243
},
224244
{
225245
"cell_type": "markdown",
226-
"metadata": {},
246+
"metadata": {
247+
"collapsed": false
248+
},
227249
"source": [
228250
"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."
229251
]
@@ -239,13 +261,15 @@
239261
},
240262
{
241263
"cell_type": "markdown",
242-
"metadata": {},
264+
"metadata": {
265+
"collapsed": false
266+
},
243267
"source": [
244268
"Our masterplan to get rich is getting together nicely...\n",
245269
"\n",
246270
"![coming_together](https://i1.wp.com/media1.tenor.com/images/a71c94c3aa7ad66a5051f81f48d14dd2/tenor.gif?w=688&ssl=1)\n",
247271
"\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)."
249273
]
250274
},
251275
{
@@ -259,11 +283,13 @@
259283
},
260284
{
261285
"cell_type": "markdown",
262-
"metadata": {},
286+
"metadata": {
287+
"collapsed": false
288+
},
263289
"source": [
264290
"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",
265291
"\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",
267293
"\n",
268294
"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."
269295
]
@@ -279,14 +305,18 @@
279305
},
280306
{
281307
"cell_type": "markdown",
282-
"metadata": {},
308+
"metadata": {
309+
"collapsed": false
310+
},
283311
"source": [
284312
"## Get rich (fast) or code tryin'"
285313
]
286314
},
287315
{
288316
"cell_type": "markdown",
289-
"metadata": {},
317+
"metadata": {
318+
"collapsed": false
319+
},
290320
"source": [
291321
"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",
292322
"\n",
@@ -310,7 +340,9 @@
310340
},
311341
{
312342
"cell_type": "markdown",
313-
"metadata": {},
343+
"metadata": {
344+
"collapsed": false
345+
},
314346
"source": [
315347
"The next cell will represent the distribution of your profits, only execute it once you have your 5000 results in the variable *results*."
316348
]
@@ -354,8 +386,18 @@
354386
],
355387
"metadata": {
356388
"kernelspec": {
389+
"argv": [
390+
"python",
391+
"-m",
392+
"ipykernel_launcher",
393+
"-f",
394+
"{connection_file}"
395+
],
357396
"display_name": "Python 3",
397+
"env": null,
398+
"interrupt_mode": "signal",
358399
"language": "python",
400+
"metadata": null,
359401
"name": "python3"
360402
},
361403
"language_info": {

0 commit comments

Comments
 (0)