ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
lab_104.ipynb
(4585B)
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Building a Module\n",
8 "\n",
9 "A module in Python is conceptually not much more than a file with python code in it that you can access and run by importing the module.\n",
10 "\n",
11 "When you `import` a module, you are reading the file and making the code contained in it accessible through its alias.\n",
12 "\n",
13 "Start by creating a new file called `hello.py` and add the following message in there:\n",
14 "\n",
15 "```python\n",
16 "message = 'Hello Jim'\n",
17 "```\n",
18 "\n",
19 "and then try executing this code:\n",
20 "\n",
21 "```python\n",
22 "import hello as h\n",
23 "h.message\n",
24 "```\n",
25 "\n",
26 "Great!\n",
27 "\n",
28 "Now, let's change the message. Edit the file to now say:\n",
29 "\n",
30 "```python\n",
31 "message = 'Hello Jane'\n",
32 "```\n",
33 "\n",
34 "and rexecute the code:\n",
35 "\n",
36 "```python\n",
37 "import hello as h\n",
38 "h.message\n",
39 "```\n",
40 "\n",
41 "What went wrong?\n",
42 "\n",
43 "Since the contents of modules dont usually change very much (except during module development) Python is smart about importing modules and once a module is imported, it doesnt bother re-importing the module. Normally, this is what you want, but in our case when we are developing the module we want to force the module to geet automatically reloaded everytime it changes.\n",
44 "\n",
45 "Fortunately, there is a _magic_ command sequence that does exactly that. First, we have to load an extension called `autoreload` by executing the _magic command_ `%load_ext autoreload`. Once you have loaded that extension, you now have access to a new _magic command_ called `%autoreload` which supports different modes of autoreload. The mode we want for now is to automatically reload anything that changes, which is mode 2. So, you need to execute:\n",
46 "\n",
47 "```python\n",
48 "%load_ext autoreload\n",
49 "%autoreload 2\n",
50 "```\n",
51 "\n",
52 "Now try executing:\n",
53 "\n",
54 "```python\n",
55 "h.message\n",
56 "```\n",
57 "\n",
58 "and you will see that future edits to the hello file will immediately be reloaded.\n",
59 "\n",
60 "Try editing the `hello.py` file to:\n",
61 "\n",
62 "```python\n",
63 "message = 'Hello John and Jane'\n",
64 "```\n",
65 "\n",
66 "and then execute\n",
67 "\n",
68 "```python\n",
69 "h.message\n",
70 "```\n",
71 "\n",
72 "Now, let's create our new module, which we'll build on through the course, and put the `drawdown` function we created in that module. Create a file called `edhec_risk_kit.py` and copy the follwing code into it.\n",
73 "\n",
74 "```python\n",
75 "\n",
76 "import pandas as pd\n",
77 "\n",
78 "def drawdown(return_series: pd.Series):\n",
79 " \"\"\"Takes a time series of asset returns.\n",
80 " returns a DataFrame with columns for\n",
81 " the wealth index, \n",
82 " the previous peaks, and \n",
83 " the percentage drawdown\n",
84 " \"\"\"\n",
85 " wealth_index = 1000*(1+return_series).cumprod()\n",
86 " previous_peaks = wealth_index.cummax()\n",
87 " drawdowns = (wealth_index - previous_peaks)/previous_peaks\n",
88 " return pd.DataFrame({\"Wealth\": wealth_index, \n",
89 " \"Previous Peak\": previous_peaks, \n",
90 " \"Drawdown\": drawdowns})\n",
91 "\n",
92 "def get_ffme_returns():\n",
93 " \"\"\"\n",
94 " Load the Fama-French Dataset for the returns of the Top and Bottom Deciles by MarketCap\n",
95 " \"\"\"\n",
96 " me_m = pd.read_csv(\"data/Portfolios_Formed_on_ME_monthly_EW.csv\",\n",
97 " header=0, index_col=0, na_values=-99.99)\n",
98 " rets = me_m[['Lo 10', 'Hi 10']]\n",
99 " rets.columns = ['SmallCap', 'LargeCap']\n",
100 " rets = rets/100\n",
101 " rets.index = pd.to_datetime(rets.index, format=\"%Y%m\").to_period('M')\n",
102 " return rets\n",
103 "\n",
104 "```\n"
105 ]
106 },
107 {
108 "cell_type": "code",
109 "execution_count": null,
110 "metadata": {},
111 "outputs": [],
112 "source": []
113 }
114 ],
115 "metadata": {
116 "kernelspec": {
117 "display_name": "Python 3",
118 "language": "python",
119 "name": "python3"
120 },
121 "language_info": {
122 "codemirror_mode": {
123 "name": "ipython",
124 "version": 3
125 },
126 "file_extension": ".py",
127 "mimetype": "text/x-python",
128 "name": "python",
129 "nbconvert_exporter": "python",
130 "pygments_lexer": "ipython3",
131 "version": "3.8.8"
132 }
133 },
134 "nbformat": 4,
135 "nbformat_minor": 2
136 }