├── .github
└── workflows
│ └── r.yml
├── Bank_Data
├── Bank Data.ipynb
└── README.md
├── Boston Housing Data
├── Housing_Data.ipynb
└── README.md
├── Forecasting Risk Analytics
├── Forecasting Risk ES_VAR.R
├── Project_Forecasting Risk.pdf
└── README.md
├── Option_Pricing
├── Binomial-Tree-Call.ipynb
├── Finite Difference, Binomial Trees.ipynb
├── Monte Carlo.ipynb
└── README.md
├── R
└── Copula Modeling
│ ├── ACF.pdf
│ ├── Actual Plot.png
│ ├── Copula Project.R
│ ├── README.md
│ └── Simulated Plot,Frank Copula.png
├── README.md
├── Risk Management
├── Default_Estimation.ipynb
├── EuroDollar Futures.ipynb
├── Forecasting Sales using ARIMA.ipynb
├── README.md
└── Var Exercise-Monte Carlo.ipynb
└── SECURITY.md
/.github/workflows/r.yml:
--------------------------------------------------------------------------------
1 | # This workflow uses actions that are not certified by GitHub.
2 | # They are provided by a third-party and are governed by
3 | # separate terms of service, privacy policy, and support
4 | # documentation.
5 | #
6 | # See https://github.com/r-lib/actions/tree/master/examples#readme for
7 | # additional example workflows available for the R community.
8 |
9 | name: R
10 |
11 | on:
12 | push:
13 | branches: [ "main" ]
14 | pull_request:
15 | branches: [ "main" ]
16 |
17 | permissions:
18 | contents: read
19 |
20 | jobs:
21 | build:
22 | runs-on: macos-latest
23 | strategy:
24 | matrix:
25 | r-version: ['3.6.3', '4.1.1']
26 |
27 | steps:
28 | - uses: actions/checkout@v3
29 | - name: Set up R ${{ matrix.r-version }}
30 | uses: r-lib/actions/setup-r@f57f1301a053485946083d7a45022b278929a78a
31 | with:
32 | r-version: ${{ matrix.r-version }}
33 | - name: Install dependencies
34 | run: |
35 | install.packages(c("remotes", "rcmdcheck"))
36 | remotes::install_deps(dependencies = TRUE)
37 | shell: Rscript {0}
38 | - name: Check
39 | run: rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "error")
40 | shell: Rscript {0}
41 |
--------------------------------------------------------------------------------
/Bank_Data/Bank Data.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {
7 | "id": "e6sxfiNJljkT"
8 | },
9 | "outputs": [],
10 | "source": [
11 | "url=\"C://Users//adib-//OneDrive//Desktop//Assignment_1//bank-additional-full.csv\""
12 | ]
13 | },
14 | {
15 | "cell_type": "code",
16 | "execution_count": 2,
17 | "metadata": {
18 | "id": "bg4GB1NFeN6w"
19 | },
20 | "outputs": [],
21 | "source": [
22 | "import pandas as pd\n",
23 | "from math import log2\n",
24 | "from sklearn.model_selection import train_test_split"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 3,
30 | "metadata": {
31 | "colab": {
32 | "base_uri": "https://localhost:8080/",
33 | "height": 487
34 | },
35 | "id": "FIelj9yAeN6x",
36 | "outputId": "452157b0-699e-4e00-cff8-ef6902b2d0e8"
37 | },
38 | "outputs": [
39 | {
40 | "data": {
41 | "text/html": [
42 | "
\n",
43 | "\n",
56 | "
\n",
57 | " \n",
58 | " \n",
59 | " | \n",
60 | " age | \n",
61 | " job | \n",
62 | " marital | \n",
63 | " education | \n",
64 | " default | \n",
65 | " housing | \n",
66 | " loan | \n",
67 | " contact | \n",
68 | " month | \n",
69 | " day_of_week | \n",
70 | " ... | \n",
71 | " campaign | \n",
72 | " pdays | \n",
73 | " previous | \n",
74 | " poutcome | \n",
75 | " emp.var.rate | \n",
76 | " cons.price.idx | \n",
77 | " cons.conf.idx | \n",
78 | " euribor3m | \n",
79 | " nr.employed | \n",
80 | " y | \n",
81 | "
\n",
82 | " \n",
83 | " \n",
84 | " \n",
85 | " 0 | \n",
86 | " 56 | \n",
87 | " housemaid | \n",
88 | " married | \n",
89 | " basic.4y | \n",
90 | " no | \n",
91 | " no | \n",
92 | " no | \n",
93 | " telephone | \n",
94 | " may | \n",
95 | " mon | \n",
96 | " ... | \n",
97 | " 1 | \n",
98 | " 999 | \n",
99 | " 0 | \n",
100 | " nonexistent | \n",
101 | " 1.1 | \n",
102 | " 93.994 | \n",
103 | " -36.4 | \n",
104 | " 4.857 | \n",
105 | " 5191.0 | \n",
106 | " no | \n",
107 | "
\n",
108 | " \n",
109 | " 1 | \n",
110 | " 57 | \n",
111 | " services | \n",
112 | " married | \n",
113 | " high.school | \n",
114 | " unknown | \n",
115 | " no | \n",
116 | " no | \n",
117 | " telephone | \n",
118 | " may | \n",
119 | " mon | \n",
120 | " ... | \n",
121 | " 1 | \n",
122 | " 999 | \n",
123 | " 0 | \n",
124 | " nonexistent | \n",
125 | " 1.1 | \n",
126 | " 93.994 | \n",
127 | " -36.4 | \n",
128 | " 4.857 | \n",
129 | " 5191.0 | \n",
130 | " no | \n",
131 | "
\n",
132 | " \n",
133 | " 2 | \n",
134 | " 37 | \n",
135 | " services | \n",
136 | " married | \n",
137 | " high.school | \n",
138 | " no | \n",
139 | " yes | \n",
140 | " no | \n",
141 | " telephone | \n",
142 | " may | \n",
143 | " mon | \n",
144 | " ... | \n",
145 | " 1 | \n",
146 | " 999 | \n",
147 | " 0 | \n",
148 | " nonexistent | \n",
149 | " 1.1 | \n",
150 | " 93.994 | \n",
151 | " -36.4 | \n",
152 | " 4.857 | \n",
153 | " 5191.0 | \n",
154 | " no | \n",
155 | "
\n",
156 | " \n",
157 | " 3 | \n",
158 | " 40 | \n",
159 | " admin. | \n",
160 | " married | \n",
161 | " basic.6y | \n",
162 | " no | \n",
163 | " no | \n",
164 | " no | \n",
165 | " telephone | \n",
166 | " may | \n",
167 | " mon | \n",
168 | " ... | \n",
169 | " 1 | \n",
170 | " 999 | \n",
171 | " 0 | \n",
172 | " nonexistent | \n",
173 | " 1.1 | \n",
174 | " 93.994 | \n",
175 | " -36.4 | \n",
176 | " 4.857 | \n",
177 | " 5191.0 | \n",
178 | " no | \n",
179 | "
\n",
180 | " \n",
181 | " 4 | \n",
182 | " 56 | \n",
183 | " services | \n",
184 | " married | \n",
185 | " high.school | \n",
186 | " no | \n",
187 | " no | \n",
188 | " yes | \n",
189 | " telephone | \n",
190 | " may | \n",
191 | " mon | \n",
192 | " ... | \n",
193 | " 1 | \n",
194 | " 999 | \n",
195 | " 0 | \n",
196 | " nonexistent | \n",
197 | " 1.1 | \n",
198 | " 93.994 | \n",
199 | " -36.4 | \n",
200 | " 4.857 | \n",
201 | " 5191.0 | \n",
202 | " no | \n",
203 | "
\n",
204 | " \n",
205 | " ... | \n",
206 | " ... | \n",
207 | " ... | \n",
208 | " ... | \n",
209 | " ... | \n",
210 | " ... | \n",
211 | " ... | \n",
212 | " ... | \n",
213 | " ... | \n",
214 | " ... | \n",
215 | " ... | \n",
216 | " ... | \n",
217 | " ... | \n",
218 | " ... | \n",
219 | " ... | \n",
220 | " ... | \n",
221 | " ... | \n",
222 | " ... | \n",
223 | " ... | \n",
224 | " ... | \n",
225 | " ... | \n",
226 | " ... | \n",
227 | "
\n",
228 | " \n",
229 | " 41183 | \n",
230 | " 73 | \n",
231 | " retired | \n",
232 | " married | \n",
233 | " professional.course | \n",
234 | " no | \n",
235 | " yes | \n",
236 | " no | \n",
237 | " cellular | \n",
238 | " nov | \n",
239 | " fri | \n",
240 | " ... | \n",
241 | " 1 | \n",
242 | " 999 | \n",
243 | " 0 | \n",
244 | " nonexistent | \n",
245 | " -1.1 | \n",
246 | " 94.767 | \n",
247 | " -50.8 | \n",
248 | " 1.028 | \n",
249 | " 4963.6 | \n",
250 | " yes | \n",
251 | "
\n",
252 | " \n",
253 | " 41184 | \n",
254 | " 46 | \n",
255 | " blue-collar | \n",
256 | " married | \n",
257 | " professional.course | \n",
258 | " no | \n",
259 | " no | \n",
260 | " no | \n",
261 | " cellular | \n",
262 | " nov | \n",
263 | " fri | \n",
264 | " ... | \n",
265 | " 1 | \n",
266 | " 999 | \n",
267 | " 0 | \n",
268 | " nonexistent | \n",
269 | " -1.1 | \n",
270 | " 94.767 | \n",
271 | " -50.8 | \n",
272 | " 1.028 | \n",
273 | " 4963.6 | \n",
274 | " no | \n",
275 | "
\n",
276 | " \n",
277 | " 41185 | \n",
278 | " 56 | \n",
279 | " retired | \n",
280 | " married | \n",
281 | " university.degree | \n",
282 | " no | \n",
283 | " yes | \n",
284 | " no | \n",
285 | " cellular | \n",
286 | " nov | \n",
287 | " fri | \n",
288 | " ... | \n",
289 | " 2 | \n",
290 | " 999 | \n",
291 | " 0 | \n",
292 | " nonexistent | \n",
293 | " -1.1 | \n",
294 | " 94.767 | \n",
295 | " -50.8 | \n",
296 | " 1.028 | \n",
297 | " 4963.6 | \n",
298 | " no | \n",
299 | "
\n",
300 | " \n",
301 | " 41186 | \n",
302 | " 44 | \n",
303 | " technician | \n",
304 | " married | \n",
305 | " professional.course | \n",
306 | " no | \n",
307 | " no | \n",
308 | " no | \n",
309 | " cellular | \n",
310 | " nov | \n",
311 | " fri | \n",
312 | " ... | \n",
313 | " 1 | \n",
314 | " 999 | \n",
315 | " 0 | \n",
316 | " nonexistent | \n",
317 | " -1.1 | \n",
318 | " 94.767 | \n",
319 | " -50.8 | \n",
320 | " 1.028 | \n",
321 | " 4963.6 | \n",
322 | " yes | \n",
323 | "
\n",
324 | " \n",
325 | " 41187 | \n",
326 | " 74 | \n",
327 | " retired | \n",
328 | " married | \n",
329 | " professional.course | \n",
330 | " no | \n",
331 | " yes | \n",
332 | " no | \n",
333 | " cellular | \n",
334 | " nov | \n",
335 | " fri | \n",
336 | " ... | \n",
337 | " 3 | \n",
338 | " 999 | \n",
339 | " 1 | \n",
340 | " failure | \n",
341 | " -1.1 | \n",
342 | " 94.767 | \n",
343 | " -50.8 | \n",
344 | " 1.028 | \n",
345 | " 4963.6 | \n",
346 | " no | \n",
347 | "
\n",
348 | " \n",
349 | "
\n",
350 | "
41188 rows × 21 columns
\n",
351 | "
"
352 | ],
353 | "text/plain": [
354 | " age job marital education default housing loan \\\n",
355 | "0 56 housemaid married basic.4y no no no \n",
356 | "1 57 services married high.school unknown no no \n",
357 | "2 37 services married high.school no yes no \n",
358 | "3 40 admin. married basic.6y no no no \n",
359 | "4 56 services married high.school no no yes \n",
360 | "... ... ... ... ... ... ... ... \n",
361 | "41183 73 retired married professional.course no yes no \n",
362 | "41184 46 blue-collar married professional.course no no no \n",
363 | "41185 56 retired married university.degree no yes no \n",
364 | "41186 44 technician married professional.course no no no \n",
365 | "41187 74 retired married professional.course no yes no \n",
366 | "\n",
367 | " contact month day_of_week ... campaign pdays previous \\\n",
368 | "0 telephone may mon ... 1 999 0 \n",
369 | "1 telephone may mon ... 1 999 0 \n",
370 | "2 telephone may mon ... 1 999 0 \n",
371 | "3 telephone may mon ... 1 999 0 \n",
372 | "4 telephone may mon ... 1 999 0 \n",
373 | "... ... ... ... ... ... ... ... \n",
374 | "41183 cellular nov fri ... 1 999 0 \n",
375 | "41184 cellular nov fri ... 1 999 0 \n",
376 | "41185 cellular nov fri ... 2 999 0 \n",
377 | "41186 cellular nov fri ... 1 999 0 \n",
378 | "41187 cellular nov fri ... 3 999 1 \n",
379 | "\n",
380 | " poutcome emp.var.rate cons.price.idx cons.conf.idx euribor3m \\\n",
381 | "0 nonexistent 1.1 93.994 -36.4 4.857 \n",
382 | "1 nonexistent 1.1 93.994 -36.4 4.857 \n",
383 | "2 nonexistent 1.1 93.994 -36.4 4.857 \n",
384 | "3 nonexistent 1.1 93.994 -36.4 4.857 \n",
385 | "4 nonexistent 1.1 93.994 -36.4 4.857 \n",
386 | "... ... ... ... ... ... \n",
387 | "41183 nonexistent -1.1 94.767 -50.8 1.028 \n",
388 | "41184 nonexistent -1.1 94.767 -50.8 1.028 \n",
389 | "41185 nonexistent -1.1 94.767 -50.8 1.028 \n",
390 | "41186 nonexistent -1.1 94.767 -50.8 1.028 \n",
391 | "41187 failure -1.1 94.767 -50.8 1.028 \n",
392 | "\n",
393 | " nr.employed y \n",
394 | "0 5191.0 no \n",
395 | "1 5191.0 no \n",
396 | "2 5191.0 no \n",
397 | "3 5191.0 no \n",
398 | "4 5191.0 no \n",
399 | "... ... ... \n",
400 | "41183 4963.6 yes \n",
401 | "41184 4963.6 no \n",
402 | "41185 4963.6 no \n",
403 | "41186 4963.6 yes \n",
404 | "41187 4963.6 no \n",
405 | "\n",
406 | "[41188 rows x 21 columns]"
407 | ]
408 | },
409 | "execution_count": 3,
410 | "metadata": {},
411 | "output_type": "execute_result"
412 | }
413 | ],
414 | "source": [
415 | "data=pd.read_csv(url)\n",
416 | "data"
417 | ]
418 | },
419 | {
420 | "cell_type": "code",
421 | "execution_count": 4,
422 | "metadata": {
423 | "colab": {
424 | "base_uri": "https://localhost:8080/"
425 | },
426 | "id": "rR7Jn8U2dTdt",
427 | "outputId": "0f525032-a7b7-44d6-9a71-d2f14b125fa1"
428 | },
429 | "outputs": [
430 | {
431 | "data": {
432 | "text/plain": [
433 | "999 39673\n",
434 | "3 439\n",
435 | "6 412\n",
436 | "4 118\n",
437 | "9 64\n",
438 | "2 61\n",
439 | "7 60\n",
440 | "12 58\n",
441 | "10 52\n",
442 | "5 46\n",
443 | "13 36\n",
444 | "11 28\n",
445 | "1 26\n",
446 | "15 24\n",
447 | "14 20\n",
448 | "8 18\n",
449 | "0 15\n",
450 | "16 11\n",
451 | "17 8\n",
452 | "18 7\n",
453 | "19 3\n",
454 | "22 3\n",
455 | "21 2\n",
456 | "26 1\n",
457 | "20 1\n",
458 | "25 1\n",
459 | "27 1\n",
460 | "Name: pdays, dtype: int64"
461 | ]
462 | },
463 | "execution_count": 4,
464 | "metadata": {},
465 | "output_type": "execute_result"
466 | }
467 | ],
468 | "source": [
469 | "# For most of the data points, pdays feature remains constant as 999. So we remove this feature\n",
470 | "data['pdays'].value_counts()"
471 | ]
472 | },
473 | {
474 | "cell_type": "code",
475 | "execution_count": 5,
476 | "metadata": {
477 | "colab": {
478 | "base_uri": "https://localhost:8080/"
479 | },
480 | "id": "KcoAOPSFdfs1",
481 | "outputId": "dae2c872-9e8a-4cf2-9776-d13afbed1a8b"
482 | },
483 | "outputs": [
484 | {
485 | "data": {
486 | "text/plain": [
487 | "5228.1 16234\n",
488 | "5099.1 8534\n",
489 | "5191.0 7763\n",
490 | "5195.8 3683\n",
491 | "5076.2 1663\n",
492 | "5017.5 1071\n",
493 | "4991.6 773\n",
494 | "5008.7 650\n",
495 | "4963.6 635\n",
496 | "5023.5 172\n",
497 | "5176.3 10\n",
498 | "Name: nr.employed, dtype: int64"
499 | ]
500 | },
501 | "execution_count": 5,
502 | "metadata": {},
503 | "output_type": "execute_result"
504 | }
505 | ],
506 | "source": [
507 | "data['nr.employed'].value_counts() #Index Feature"
508 | ]
509 | },
510 | {
511 | "cell_type": "code",
512 | "execution_count": 6,
513 | "metadata": {
514 | "colab": {
515 | "base_uri": "https://localhost:8080/"
516 | },
517 | "id": "xNCZxGSJeN6y",
518 | "outputId": "cfdfee3f-e459-438f-fcd8-e8e093c004cf"
519 | },
520 | "outputs": [
521 | {
522 | "data": {
523 | "text/plain": [
524 | "False"
525 | ]
526 | },
527 | "execution_count": 6,
528 | "metadata": {},
529 | "output_type": "execute_result"
530 | }
531 | ],
532 | "source": [
533 | "# There are no missing numeric values in the whole dataframe\n",
534 | "data.isnull().values.any()"
535 | ]
536 | },
537 | {
538 | "cell_type": "code",
539 | "execution_count": 7,
540 | "metadata": {},
541 | "outputs": [
542 | {
543 | "name": "stdout",
544 | "output_type": "stream",
545 | "text": [
546 | "yes 21576\n",
547 | "no 18622\n",
548 | "unknown 990\n",
549 | "Name: housing, dtype: int64\n",
550 | "no 33950\n",
551 | "yes 6248\n",
552 | "unknown 990\n",
553 | "Name: loan, dtype: int64\n",
554 | "no 32588\n",
555 | "unknown 8597\n",
556 | "yes 3\n",
557 | "Name: default, dtype: int64\n",
558 | "university.degree 12168\n",
559 | "high.school 9515\n",
560 | "basic.9y 6045\n",
561 | "professional.course 5243\n",
562 | "basic.4y 4176\n",
563 | "basic.6y 2292\n",
564 | "unknown 1731\n",
565 | "illiterate 18\n",
566 | "Name: education, dtype: int64\n",
567 | "admin. 10422\n",
568 | "blue-collar 9254\n",
569 | "technician 6743\n",
570 | "services 3969\n",
571 | "management 2924\n",
572 | "retired 1720\n",
573 | "entrepreneur 1456\n",
574 | "self-employed 1421\n",
575 | "housemaid 1060\n",
576 | "unemployed 1014\n",
577 | "student 875\n",
578 | "unknown 330\n",
579 | "Name: job, dtype: int64\n",
580 | "nonexistent 35563\n",
581 | "failure 4252\n",
582 | "success 1373\n",
583 | "Name: poutcome, dtype: int64\n",
584 | "married 24928\n",
585 | "single 11568\n",
586 | "divorced 4612\n",
587 | "unknown 80\n",
588 | "Name: marital, dtype: int64\n"
589 | ]
590 | }
591 | ],
592 | "source": [
593 | "print(data['housing'].value_counts())\n",
594 | "print(data['loan'].value_counts())\n",
595 | "print(data['default'].value_counts())\n",
596 | "print(data['education'].value_counts())\n",
597 | "print(data['job'].value_counts())\n",
598 | "print(data['poutcome'].value_counts())\n",
599 | "print(data['marital'].value_counts())\n",
600 | "\n",
601 | "\n",
602 | "\n"
603 | ]
604 | },
605 | {
606 | "cell_type": "code",
607 | "execution_count": 8,
608 | "metadata": {
609 | "colab": {
610 | "base_uri": "https://localhost:8080/",
611 | "height": 423
612 | },
613 | "id": "tni4X_EfupUV",
614 | "outputId": "f89f7cda-9be7-4388-bdf0-56895698d225"
615 | },
616 | "outputs": [
617 | {
618 | "data": {
619 | "text/html": [
620 | "\n",
621 | "\n",
634 | "
\n",
635 | " \n",
636 | " \n",
637 | " | \n",
638 | " age | \n",
639 | " job | \n",
640 | " marital | \n",
641 | " education | \n",
642 | " default | \n",
643 | " housing | \n",
644 | " loan | \n",
645 | " contact | \n",
646 | " month | \n",
647 | " day_of_week | \n",
648 | " duration | \n",
649 | " campaign | \n",
650 | " previous | \n",
651 | " poutcome | \n",
652 | " y | \n",
653 | "
\n",
654 | " \n",
655 | " \n",
656 | " \n",
657 | " 0 | \n",
658 | " 56 | \n",
659 | " housemaid | \n",
660 | " married | \n",
661 | " basic.4y | \n",
662 | " no | \n",
663 | " no | \n",
664 | " no | \n",
665 | " telephone | \n",
666 | " may | \n",
667 | " mon | \n",
668 | " 261 | \n",
669 | " 1 | \n",
670 | " 0 | \n",
671 | " nonexistent | \n",
672 | " no | \n",
673 | "
\n",
674 | " \n",
675 | " 1 | \n",
676 | " 57 | \n",
677 | " services | \n",
678 | " married | \n",
679 | " high.school | \n",
680 | " unknown | \n",
681 | " no | \n",
682 | " no | \n",
683 | " telephone | \n",
684 | " may | \n",
685 | " mon | \n",
686 | " 149 | \n",
687 | " 1 | \n",
688 | " 0 | \n",
689 | " nonexistent | \n",
690 | " no | \n",
691 | "
\n",
692 | " \n",
693 | " 2 | \n",
694 | " 37 | \n",
695 | " services | \n",
696 | " married | \n",
697 | " high.school | \n",
698 | " no | \n",
699 | " yes | \n",
700 | " no | \n",
701 | " telephone | \n",
702 | " may | \n",
703 | " mon | \n",
704 | " 226 | \n",
705 | " 1 | \n",
706 | " 0 | \n",
707 | " nonexistent | \n",
708 | " no | \n",
709 | "
\n",
710 | " \n",
711 | " 3 | \n",
712 | " 40 | \n",
713 | " admin. | \n",
714 | " married | \n",
715 | " basic.6y | \n",
716 | " no | \n",
717 | " no | \n",
718 | " no | \n",
719 | " telephone | \n",
720 | " may | \n",
721 | " mon | \n",
722 | " 151 | \n",
723 | " 1 | \n",
724 | " 0 | \n",
725 | " nonexistent | \n",
726 | " no | \n",
727 | "
\n",
728 | " \n",
729 | " 4 | \n",
730 | " 56 | \n",
731 | " services | \n",
732 | " married | \n",
733 | " high.school | \n",
734 | " no | \n",
735 | " no | \n",
736 | " yes | \n",
737 | " telephone | \n",
738 | " may | \n",
739 | " mon | \n",
740 | " 307 | \n",
741 | " 1 | \n",
742 | " 0 | \n",
743 | " nonexistent | \n",
744 | " no | \n",
745 | "
\n",
746 | " \n",
747 | " ... | \n",
748 | " ... | \n",
749 | " ... | \n",
750 | " ... | \n",
751 | " ... | \n",
752 | " ... | \n",
753 | " ... | \n",
754 | " ... | \n",
755 | " ... | \n",
756 | " ... | \n",
757 | " ... | \n",
758 | " ... | \n",
759 | " ... | \n",
760 | " ... | \n",
761 | " ... | \n",
762 | " ... | \n",
763 | "
\n",
764 | " \n",
765 | " 41183 | \n",
766 | " 73 | \n",
767 | " retired | \n",
768 | " married | \n",
769 | " professional.course | \n",
770 | " no | \n",
771 | " yes | \n",
772 | " no | \n",
773 | " cellular | \n",
774 | " nov | \n",
775 | " fri | \n",
776 | " 334 | \n",
777 | " 1 | \n",
778 | " 0 | \n",
779 | " nonexistent | \n",
780 | " yes | \n",
781 | "
\n",
782 | " \n",
783 | " 41184 | \n",
784 | " 46 | \n",
785 | " blue-collar | \n",
786 | " married | \n",
787 | " professional.course | \n",
788 | " no | \n",
789 | " no | \n",
790 | " no | \n",
791 | " cellular | \n",
792 | " nov | \n",
793 | " fri | \n",
794 | " 383 | \n",
795 | " 1 | \n",
796 | " 0 | \n",
797 | " nonexistent | \n",
798 | " no | \n",
799 | "
\n",
800 | " \n",
801 | " 41185 | \n",
802 | " 56 | \n",
803 | " retired | \n",
804 | " married | \n",
805 | " university.degree | \n",
806 | " no | \n",
807 | " yes | \n",
808 | " no | \n",
809 | " cellular | \n",
810 | " nov | \n",
811 | " fri | \n",
812 | " 189 | \n",
813 | " 2 | \n",
814 | " 0 | \n",
815 | " nonexistent | \n",
816 | " no | \n",
817 | "
\n",
818 | " \n",
819 | " 41186 | \n",
820 | " 44 | \n",
821 | " technician | \n",
822 | " married | \n",
823 | " professional.course | \n",
824 | " no | \n",
825 | " no | \n",
826 | " no | \n",
827 | " cellular | \n",
828 | " nov | \n",
829 | " fri | \n",
830 | " 442 | \n",
831 | " 1 | \n",
832 | " 0 | \n",
833 | " nonexistent | \n",
834 | " yes | \n",
835 | "
\n",
836 | " \n",
837 | " 41187 | \n",
838 | " 74 | \n",
839 | " retired | \n",
840 | " married | \n",
841 | " professional.course | \n",
842 | " no | \n",
843 | " yes | \n",
844 | " no | \n",
845 | " cellular | \n",
846 | " nov | \n",
847 | " fri | \n",
848 | " 239 | \n",
849 | " 3 | \n",
850 | " 1 | \n",
851 | " failure | \n",
852 | " no | \n",
853 | "
\n",
854 | " \n",
855 | "
\n",
856 | "
41188 rows × 15 columns
\n",
857 | "
"
858 | ],
859 | "text/plain": [
860 | " age job marital education default housing loan \\\n",
861 | "0 56 housemaid married basic.4y no no no \n",
862 | "1 57 services married high.school unknown no no \n",
863 | "2 37 services married high.school no yes no \n",
864 | "3 40 admin. married basic.6y no no no \n",
865 | "4 56 services married high.school no no yes \n",
866 | "... ... ... ... ... ... ... ... \n",
867 | "41183 73 retired married professional.course no yes no \n",
868 | "41184 46 blue-collar married professional.course no no no \n",
869 | "41185 56 retired married university.degree no yes no \n",
870 | "41186 44 technician married professional.course no no no \n",
871 | "41187 74 retired married professional.course no yes no \n",
872 | "\n",
873 | " contact month day_of_week duration campaign previous poutcome \\\n",
874 | "0 telephone may mon 261 1 0 nonexistent \n",
875 | "1 telephone may mon 149 1 0 nonexistent \n",
876 | "2 telephone may mon 226 1 0 nonexistent \n",
877 | "3 telephone may mon 151 1 0 nonexistent \n",
878 | "4 telephone may mon 307 1 0 nonexistent \n",
879 | "... ... ... ... ... ... ... ... \n",
880 | "41183 cellular nov fri 334 1 0 nonexistent \n",
881 | "41184 cellular nov fri 383 1 0 nonexistent \n",
882 | "41185 cellular nov fri 189 2 0 nonexistent \n",
883 | "41186 cellular nov fri 442 1 0 nonexistent \n",
884 | "41187 cellular nov fri 239 3 1 failure \n",
885 | "\n",
886 | " y \n",
887 | "0 no \n",
888 | "1 no \n",
889 | "2 no \n",
890 | "3 no \n",
891 | "4 no \n",
892 | "... ... \n",
893 | "41183 yes \n",
894 | "41184 no \n",
895 | "41185 no \n",
896 | "41186 yes \n",
897 | "41187 no \n",
898 | "\n",
899 | "[41188 rows x 15 columns]"
900 | ]
901 | },
902 | "execution_count": 8,
903 | "metadata": {},
904 | "output_type": "execute_result"
905 | }
906 | ],
907 | "source": [
908 | "# Dropping constant features(mostly) or index features as they provide no valuable information to the model\n",
909 | "data.drop(['cons.price.idx','cons.conf.idx','pdays','euribor3m','emp.var.rate','nr.employed'],axis=1,inplace=True)\n",
910 | "data"
911 | ]
912 | },
913 | {
914 | "cell_type": "code",
915 | "execution_count": 9,
916 | "metadata": {
917 | "id": "D5TCIyqIVFPY"
918 | },
919 | "outputs": [],
920 | "source": [
921 | "# Replacing the categorical features'unknown values with their most common values\n",
922 | "data['housing'].replace('unknown','yes',inplace=True)\n",
923 | "data['default'].replace('unknown','yes',inplace=True)\n",
924 | "data['loan'].replace('unknown','yes',inplace=True)\n",
925 | "data['education'].replace('unknown','university.degree',inplace=True)\n",
926 | "data['job'].replace('unknown','admin.',inplace=True)\n",
927 | "data['poutcome'].replace('nonexistent','failure',inplace=True)\n",
928 | "data['marital'].replace('unknown','married',inplace=True)\n",
929 | "\n"
930 | ]
931 | },
932 | {
933 | "cell_type": "code",
934 | "execution_count": 10,
935 | "metadata": {
936 | "id": "cl3xlbG4eN63"
937 | },
938 | "outputs": [],
939 | "source": [
940 | "import numpy as np"
941 | ]
942 | },
943 | {
944 | "cell_type": "code",
945 | "execution_count": 11,
946 | "metadata": {
947 | "colab": {
948 | "base_uri": "https://localhost:8080/",
949 | "height": 423
950 | },
951 | "id": "HiVmj-z5eN64",
952 | "outputId": "f5f34e29-f648-42d7-aec2-507992c3ebfb"
953 | },
954 | "outputs": [
955 | {
956 | "data": {
957 | "text/html": [
958 | "\n",
959 | "\n",
972 | "
\n",
973 | " \n",
974 | " \n",
975 | " | \n",
976 | " age | \n",
977 | " job | \n",
978 | " marital | \n",
979 | " education | \n",
980 | " default | \n",
981 | " housing | \n",
982 | " loan | \n",
983 | " contact | \n",
984 | " month | \n",
985 | " day_of_week | \n",
986 | " duration | \n",
987 | " campaign | \n",
988 | " previous | \n",
989 | " poutcome | \n",
990 | " y | \n",
991 | "
\n",
992 | " \n",
993 | " \n",
994 | " \n",
995 | " 0 | \n",
996 | " 56 | \n",
997 | " housemaid | \n",
998 | " married | \n",
999 | " basic.4y | \n",
1000 | " no | \n",
1001 | " no | \n",
1002 | " no | \n",
1003 | " telephone | \n",
1004 | " may | \n",
1005 | " mon | \n",
1006 | " 261 | \n",
1007 | " 1 | \n",
1008 | " 0 | \n",
1009 | " failure | \n",
1010 | " no | \n",
1011 | "
\n",
1012 | " \n",
1013 | " 1 | \n",
1014 | " 57 | \n",
1015 | " services | \n",
1016 | " married | \n",
1017 | " high.school | \n",
1018 | " yes | \n",
1019 | " no | \n",
1020 | " no | \n",
1021 | " telephone | \n",
1022 | " may | \n",
1023 | " mon | \n",
1024 | " 149 | \n",
1025 | " 1 | \n",
1026 | " 0 | \n",
1027 | " failure | \n",
1028 | " no | \n",
1029 | "
\n",
1030 | " \n",
1031 | " 2 | \n",
1032 | " 37 | \n",
1033 | " services | \n",
1034 | " married | \n",
1035 | " high.school | \n",
1036 | " no | \n",
1037 | " yes | \n",
1038 | " no | \n",
1039 | " telephone | \n",
1040 | " may | \n",
1041 | " mon | \n",
1042 | " 226 | \n",
1043 | " 1 | \n",
1044 | " 0 | \n",
1045 | " failure | \n",
1046 | " no | \n",
1047 | "
\n",
1048 | " \n",
1049 | " 3 | \n",
1050 | " 40 | \n",
1051 | " admin. | \n",
1052 | " married | \n",
1053 | " basic.6y | \n",
1054 | " no | \n",
1055 | " no | \n",
1056 | " no | \n",
1057 | " telephone | \n",
1058 | " may | \n",
1059 | " mon | \n",
1060 | " 151 | \n",
1061 | " 1 | \n",
1062 | " 0 | \n",
1063 | " failure | \n",
1064 | " no | \n",
1065 | "
\n",
1066 | " \n",
1067 | " 4 | \n",
1068 | " 56 | \n",
1069 | " services | \n",
1070 | " married | \n",
1071 | " high.school | \n",
1072 | " no | \n",
1073 | " no | \n",
1074 | " yes | \n",
1075 | " telephone | \n",
1076 | " may | \n",
1077 | " mon | \n",
1078 | " 307 | \n",
1079 | " 1 | \n",
1080 | " 0 | \n",
1081 | " failure | \n",
1082 | " no | \n",
1083 | "
\n",
1084 | " \n",
1085 | " ... | \n",
1086 | " ... | \n",
1087 | " ... | \n",
1088 | " ... | \n",
1089 | " ... | \n",
1090 | " ... | \n",
1091 | " ... | \n",
1092 | " ... | \n",
1093 | " ... | \n",
1094 | " ... | \n",
1095 | " ... | \n",
1096 | " ... | \n",
1097 | " ... | \n",
1098 | " ... | \n",
1099 | " ... | \n",
1100 | " ... | \n",
1101 | "
\n",
1102 | " \n",
1103 | " 41183 | \n",
1104 | " 73 | \n",
1105 | " retired | \n",
1106 | " married | \n",
1107 | " professional.course | \n",
1108 | " no | \n",
1109 | " yes | \n",
1110 | " no | \n",
1111 | " cellular | \n",
1112 | " nov | \n",
1113 | " fri | \n",
1114 | " 334 | \n",
1115 | " 1 | \n",
1116 | " 0 | \n",
1117 | " failure | \n",
1118 | " yes | \n",
1119 | "
\n",
1120 | " \n",
1121 | " 41184 | \n",
1122 | " 46 | \n",
1123 | " blue-collar | \n",
1124 | " married | \n",
1125 | " professional.course | \n",
1126 | " no | \n",
1127 | " no | \n",
1128 | " no | \n",
1129 | " cellular | \n",
1130 | " nov | \n",
1131 | " fri | \n",
1132 | " 383 | \n",
1133 | " 1 | \n",
1134 | " 0 | \n",
1135 | " failure | \n",
1136 | " no | \n",
1137 | "
\n",
1138 | " \n",
1139 | " 41185 | \n",
1140 | " 56 | \n",
1141 | " retired | \n",
1142 | " married | \n",
1143 | " university.degree | \n",
1144 | " no | \n",
1145 | " yes | \n",
1146 | " no | \n",
1147 | " cellular | \n",
1148 | " nov | \n",
1149 | " fri | \n",
1150 | " 189 | \n",
1151 | " 2 | \n",
1152 | " 0 | \n",
1153 | " failure | \n",
1154 | " no | \n",
1155 | "
\n",
1156 | " \n",
1157 | " 41186 | \n",
1158 | " 44 | \n",
1159 | " technician | \n",
1160 | " married | \n",
1161 | " professional.course | \n",
1162 | " no | \n",
1163 | " no | \n",
1164 | " no | \n",
1165 | " cellular | \n",
1166 | " nov | \n",
1167 | " fri | \n",
1168 | " 442 | \n",
1169 | " 1 | \n",
1170 | " 0 | \n",
1171 | " failure | \n",
1172 | " yes | \n",
1173 | "
\n",
1174 | " \n",
1175 | " 41187 | \n",
1176 | " 74 | \n",
1177 | " retired | \n",
1178 | " married | \n",
1179 | " professional.course | \n",
1180 | " no | \n",
1181 | " yes | \n",
1182 | " no | \n",
1183 | " cellular | \n",
1184 | " nov | \n",
1185 | " fri | \n",
1186 | " 239 | \n",
1187 | " 3 | \n",
1188 | " 1 | \n",
1189 | " failure | \n",
1190 | " no | \n",
1191 | "
\n",
1192 | " \n",
1193 | "
\n",
1194 | "
41188 rows × 15 columns
\n",
1195 | "
"
1196 | ],
1197 | "text/plain": [
1198 | " age job marital education default housing loan \\\n",
1199 | "0 56 housemaid married basic.4y no no no \n",
1200 | "1 57 services married high.school yes no no \n",
1201 | "2 37 services married high.school no yes no \n",
1202 | "3 40 admin. married basic.6y no no no \n",
1203 | "4 56 services married high.school no no yes \n",
1204 | "... ... ... ... ... ... ... ... \n",
1205 | "41183 73 retired married professional.course no yes no \n",
1206 | "41184 46 blue-collar married professional.course no no no \n",
1207 | "41185 56 retired married university.degree no yes no \n",
1208 | "41186 44 technician married professional.course no no no \n",
1209 | "41187 74 retired married professional.course no yes no \n",
1210 | "\n",
1211 | " contact month day_of_week duration campaign previous poutcome y \n",
1212 | "0 telephone may mon 261 1 0 failure no \n",
1213 | "1 telephone may mon 149 1 0 failure no \n",
1214 | "2 telephone may mon 226 1 0 failure no \n",
1215 | "3 telephone may mon 151 1 0 failure no \n",
1216 | "4 telephone may mon 307 1 0 failure no \n",
1217 | "... ... ... ... ... ... ... ... ... \n",
1218 | "41183 cellular nov fri 334 1 0 failure yes \n",
1219 | "41184 cellular nov fri 383 1 0 failure no \n",
1220 | "41185 cellular nov fri 189 2 0 failure no \n",
1221 | "41186 cellular nov fri 442 1 0 failure yes \n",
1222 | "41187 cellular nov fri 239 3 1 failure no \n",
1223 | "\n",
1224 | "[41188 rows x 15 columns]"
1225 | ]
1226 | },
1227 | "execution_count": 11,
1228 | "metadata": {},
1229 | "output_type": "execute_result"
1230 | }
1231 | ],
1232 | "source": [
1233 | "data"
1234 | ]
1235 | },
1236 | {
1237 | "cell_type": "code",
1238 | "execution_count": 12,
1239 | "metadata": {
1240 | "id": "CfATYB2lf5CH"
1241 | },
1242 | "outputs": [],
1243 | "source": [
1244 | "\n",
1245 | "\n",
1246 | "from math import log2"
1247 | ]
1248 | },
1249 | {
1250 | "cell_type": "code",
1251 | "execution_count": 13,
1252 | "metadata": {
1253 | "id": "LLguWJmTeN65"
1254 | },
1255 | "outputs": [],
1256 | "source": [
1257 | " def entropy(S,target_attribute):\n",
1258 | "\n",
1259 | " d=dict(S[target_attribute].value_counts())\n",
1260 | " result=0\n",
1261 | " for i in d.values():\n",
1262 | " result-= ((i)/(len(S[target_attribute])))*log2((i)/(len(S[target_attribute])))\n",
1263 | " return result\n",
1264 | "\n"
1265 | ]
1266 | },
1267 | {
1268 | "cell_type": "code",
1269 | "execution_count": 14,
1270 | "metadata": {
1271 | "colab": {
1272 | "base_uri": "https://localhost:8080/"
1273 | },
1274 | "id": "5mAhaDfeeN65",
1275 | "outputId": "3706e768-cefd-432e-bddf-409aa455c916"
1276 | },
1277 | "outputs": [
1278 | {
1279 | "data": {
1280 | "text/plain": [
1281 | "0.507870194516452"
1282 | ]
1283 | },
1284 | "execution_count": 14,
1285 | "metadata": {},
1286 | "output_type": "execute_result"
1287 | }
1288 | ],
1289 | "source": [
1290 | "entropy(data,'y')"
1291 | ]
1292 | },
1293 | {
1294 | "cell_type": "code",
1295 | "execution_count": 15,
1296 | "metadata": {
1297 | "id": "3HSJS4WNpFxP"
1298 | },
1299 | "outputs": [],
1300 | "source": [
1301 | "def Information_Gain(S,attribute):\n",
1302 | "\n",
1303 | " total_entropy = entropy(S,'y') \n",
1304 | " \n",
1305 | " values,counts= np.unique(data[attribute],return_counts=True) \n",
1306 | " \n",
1307 | " Weighted_Entropy = np.sum([(counts[i]/np.sum(counts))*entropy(data.where(data[attribute]==values[i]).dropna(),'y') for i in range(len(values))]) \n",
1308 | " \n",
1309 | " Information_Gain = total_entropy - Weighted_Entropy \n",
1310 | " \n",
1311 | " return Information_Gain\n",
1312 | " "
1313 | ]
1314 | },
1315 | {
1316 | "cell_type": "code",
1317 | "execution_count": 16,
1318 | "metadata": {},
1319 | "outputs": [
1320 | {
1321 | "name": "stdout",
1322 | "output_type": "stream",
1323 | "text": [
1324 | "40.02406040594348\n"
1325 | ]
1326 | }
1327 | ],
1328 | "source": [
1329 | "# Converting the numeric feature into string, but first we sort dataset according to the ascending order of age\n",
1330 | "data=data.sort_values(by=['age'])\n",
1331 | "print(np.mean(data.age))\n"
1332 | ]
1333 | },
1334 | {
1335 | "cell_type": "code",
1336 | "execution_count": 17,
1337 | "metadata": {},
1338 | "outputs": [],
1339 | "source": [
1340 | "# Although we can start by choosing by 40 as the threshold value, we start by choosing a bit higher threshold such as 56 \n",
1341 | "# and then +-5 intervals and see what happens to the Information Gain"
1342 | ]
1343 | },
1344 | {
1345 | "cell_type": "code",
1346 | "execution_count": 18,
1347 | "metadata": {
1348 | "id": "PSa-dNZecyNU"
1349 | },
1350 | "outputs": [],
1351 | "source": [
1352 | "# This output list represent the trial & error process for 56-62 threshold values for age and their information gain.Age 60 gives the highest information gain and afterwards it decreases\n",
1353 | "# 56,50,51,52,53,54,55,57,58,59,60,61,62=[0.006041613935893975,0.0020598988482228853,0.0024383397975635823,0.0028021424452681076,0.003408171214054123,0.004124783828806988,0.0051480816768750826,\n",
1354 | "# 0.007731777401046247,0.009965340775132947,0.011579407576361389,0.012205424195230852,0.011276350586099493,0.010641970374872078]"
1355 | ]
1356 | },
1357 | {
1358 | "cell_type": "code",
1359 | "execution_count": 19,
1360 | "metadata": {
1361 | "id": "eFSCPnv8cyKb"
1362 | },
1363 | "outputs": [],
1364 | "source": [
1365 | "# Choosing 60 as the threshold value that gives the highest information by trial & error\n",
1366 | "threshold=60\n",
1367 | "\n",
1368 | "idx=data[data['age']<=threshold].index\n",
1369 | "data['l_age']= '>'+str(threshold)\n",
1370 | "data.loc[idx,'l_age']= '<='+str(threshold)"
1371 | ]
1372 | },
1373 | {
1374 | "cell_type": "code",
1375 | "execution_count": 20,
1376 | "metadata": {
1377 | "colab": {
1378 | "base_uri": "https://localhost:8080/"
1379 | },
1380 | "id": "6MfdW4tsbY4f",
1381 | "outputId": "2cb965c0-abbb-4ea8-875e-19e4f7b1196b"
1382 | },
1383 | "outputs": [
1384 | {
1385 | "data": {
1386 | "text/plain": [
1387 | "0.012205424195230852"
1388 | ]
1389 | },
1390 | "execution_count": 20,
1391 | "metadata": {},
1392 | "output_type": "execute_result"
1393 | }
1394 | ],
1395 | "source": [
1396 | "Information_Gain(data,'l_age')"
1397 | ]
1398 | },
1399 | {
1400 | "cell_type": "code",
1401 | "execution_count": 21,
1402 | "metadata": {
1403 | "colab": {
1404 | "base_uri": "https://localhost:8080/",
1405 | "height": 423
1406 | },
1407 | "id": "Yzc_BDKh6MZO",
1408 | "outputId": "8e4afde9-54bd-4a8c-a3b0-c13b679d4b17"
1409 | },
1410 | "outputs": [
1411 | {
1412 | "data": {
1413 | "text/html": [
1414 | "\n",
1415 | "\n",
1428 | "
\n",
1429 | " \n",
1430 | " \n",
1431 | " | \n",
1432 | " age | \n",
1433 | " job | \n",
1434 | " marital | \n",
1435 | " education | \n",
1436 | " default | \n",
1437 | " housing | \n",
1438 | " loan | \n",
1439 | " contact | \n",
1440 | " month | \n",
1441 | " day_of_week | \n",
1442 | " duration | \n",
1443 | " campaign | \n",
1444 | " previous | \n",
1445 | " poutcome | \n",
1446 | " y | \n",
1447 | " l_age | \n",
1448 | "
\n",
1449 | " \n",
1450 | " \n",
1451 | " \n",
1452 | " 38274 | \n",
1453 | " 17 | \n",
1454 | " student | \n",
1455 | " single | \n",
1456 | " university.degree | \n",
1457 | " no | \n",
1458 | " no | \n",
1459 | " yes | \n",
1460 | " cellular | \n",
1461 | " oct | \n",
1462 | " tue | \n",
1463 | " 896 | \n",
1464 | " 1 | \n",
1465 | " 2 | \n",
1466 | " success | \n",
1467 | " yes | \n",
1468 | " <=60 | \n",
1469 | "
\n",
1470 | " \n",
1471 | " 37579 | \n",
1472 | " 17 | \n",
1473 | " student | \n",
1474 | " single | \n",
1475 | " basic.9y | \n",
1476 | " no | \n",
1477 | " yes | \n",
1478 | " yes | \n",
1479 | " cellular | \n",
1480 | " aug | \n",
1481 | " fri | \n",
1482 | " 498 | \n",
1483 | " 2 | \n",
1484 | " 1 | \n",
1485 | " failure | \n",
1486 | " yes | \n",
1487 | " <=60 | \n",
1488 | "
\n",
1489 | " \n",
1490 | " 37539 | \n",
1491 | " 17 | \n",
1492 | " student | \n",
1493 | " single | \n",
1494 | " basic.9y | \n",
1495 | " no | \n",
1496 | " yes | \n",
1497 | " no | \n",
1498 | " cellular | \n",
1499 | " aug | \n",
1500 | " fri | \n",
1501 | " 182 | \n",
1502 | " 2 | \n",
1503 | " 2 | \n",
1504 | " failure | \n",
1505 | " no | \n",
1506 | " <=60 | \n",
1507 | "
\n",
1508 | " \n",
1509 | " 37140 | \n",
1510 | " 17 | \n",
1511 | " student | \n",
1512 | " single | \n",
1513 | " university.degree | \n",
1514 | " no | \n",
1515 | " yes | \n",
1516 | " no | \n",
1517 | " cellular | \n",
1518 | " aug | \n",
1519 | " wed | \n",
1520 | " 432 | \n",
1521 | " 3 | \n",
1522 | " 2 | \n",
1523 | " success | \n",
1524 | " no | \n",
1525 | " <=60 | \n",
1526 | "
\n",
1527 | " \n",
1528 | " 37558 | \n",
1529 | " 17 | \n",
1530 | " student | \n",
1531 | " single | \n",
1532 | " basic.9y | \n",
1533 | " no | \n",
1534 | " yes | \n",
1535 | " no | \n",
1536 | " cellular | \n",
1537 | " aug | \n",
1538 | " fri | \n",
1539 | " 92 | \n",
1540 | " 3 | \n",
1541 | " 2 | \n",
1542 | " success | \n",
1543 | " no | \n",
1544 | " <=60 | \n",
1545 | "
\n",
1546 | " \n",
1547 | " ... | \n",
1548 | " ... | \n",
1549 | " ... | \n",
1550 | " ... | \n",
1551 | " ... | \n",
1552 | " ... | \n",
1553 | " ... | \n",
1554 | " ... | \n",
1555 | " ... | \n",
1556 | " ... | \n",
1557 | " ... | \n",
1558 | " ... | \n",
1559 | " ... | \n",
1560 | " ... | \n",
1561 | " ... | \n",
1562 | " ... | \n",
1563 | " ... | \n",
1564 | "
\n",
1565 | " \n",
1566 | " 40450 | \n",
1567 | " 92 | \n",
1568 | " retired | \n",
1569 | " married | \n",
1570 | " university.degree | \n",
1571 | " no | \n",
1572 | " no | \n",
1573 | " yes | \n",
1574 | " cellular | \n",
1575 | " aug | \n",
1576 | " tue | \n",
1577 | " 1064 | \n",
1578 | " 1 | \n",
1579 | " 1 | \n",
1580 | " success | \n",
1581 | " yes | \n",
1582 | " >60 | \n",
1583 | "
\n",
1584 | " \n",
1585 | " 38921 | \n",
1586 | " 94 | \n",
1587 | " retired | \n",
1588 | " married | \n",
1589 | " basic.9y | \n",
1590 | " no | \n",
1591 | " no | \n",
1592 | " no | \n",
1593 | " cellular | \n",
1594 | " nov | \n",
1595 | " wed | \n",
1596 | " 134 | \n",
1597 | " 1 | \n",
1598 | " 1 | \n",
1599 | " failure | \n",
1600 | " no | \n",
1601 | " >60 | \n",
1602 | "
\n",
1603 | " \n",
1604 | " 27826 | \n",
1605 | " 95 | \n",
1606 | " retired | \n",
1607 | " divorced | \n",
1608 | " basic.6y | \n",
1609 | " no | \n",
1610 | " no | \n",
1611 | " no | \n",
1612 | " cellular | \n",
1613 | " mar | \n",
1614 | " thu | \n",
1615 | " 85 | \n",
1616 | " 1 | \n",
1617 | " 0 | \n",
1618 | " failure | \n",
1619 | " no | \n",
1620 | " >60 | \n",
1621 | "
\n",
1622 | " \n",
1623 | " 38455 | \n",
1624 | " 98 | \n",
1625 | " retired | \n",
1626 | " married | \n",
1627 | " basic.4y | \n",
1628 | " yes | \n",
1629 | " yes | \n",
1630 | " no | \n",
1631 | " cellular | \n",
1632 | " oct | \n",
1633 | " fri | \n",
1634 | " 272 | \n",
1635 | " 2 | \n",
1636 | " 0 | \n",
1637 | " failure | \n",
1638 | " yes | \n",
1639 | " >60 | \n",
1640 | "
\n",
1641 | " \n",
1642 | " 38452 | \n",
1643 | " 98 | \n",
1644 | " retired | \n",
1645 | " married | \n",
1646 | " basic.4y | \n",
1647 | " yes | \n",
1648 | " yes | \n",
1649 | " no | \n",
1650 | " cellular | \n",
1651 | " oct | \n",
1652 | " fri | \n",
1653 | " 476 | \n",
1654 | " 1 | \n",
1655 | " 2 | \n",
1656 | " success | \n",
1657 | " yes | \n",
1658 | " >60 | \n",
1659 | "
\n",
1660 | " \n",
1661 | "
\n",
1662 | "
41188 rows × 16 columns
\n",
1663 | "
"
1664 | ],
1665 | "text/plain": [
1666 | " age job marital education default housing loan \\\n",
1667 | "38274 17 student single university.degree no no yes \n",
1668 | "37579 17 student single basic.9y no yes yes \n",
1669 | "37539 17 student single basic.9y no yes no \n",
1670 | "37140 17 student single university.degree no yes no \n",
1671 | "37558 17 student single basic.9y no yes no \n",
1672 | "... ... ... ... ... ... ... ... \n",
1673 | "40450 92 retired married university.degree no no yes \n",
1674 | "38921 94 retired married basic.9y no no no \n",
1675 | "27826 95 retired divorced basic.6y no no no \n",
1676 | "38455 98 retired married basic.4y yes yes no \n",
1677 | "38452 98 retired married basic.4y yes yes no \n",
1678 | "\n",
1679 | " contact month day_of_week duration campaign previous poutcome y \\\n",
1680 | "38274 cellular oct tue 896 1 2 success yes \n",
1681 | "37579 cellular aug fri 498 2 1 failure yes \n",
1682 | "37539 cellular aug fri 182 2 2 failure no \n",
1683 | "37140 cellular aug wed 432 3 2 success no \n",
1684 | "37558 cellular aug fri 92 3 2 success no \n",
1685 | "... ... ... ... ... ... ... ... ... \n",
1686 | "40450 cellular aug tue 1064 1 1 success yes \n",
1687 | "38921 cellular nov wed 134 1 1 failure no \n",
1688 | "27826 cellular mar thu 85 1 0 failure no \n",
1689 | "38455 cellular oct fri 272 2 0 failure yes \n",
1690 | "38452 cellular oct fri 476 1 2 success yes \n",
1691 | "\n",
1692 | " l_age \n",
1693 | "38274 <=60 \n",
1694 | "37579 <=60 \n",
1695 | "37539 <=60 \n",
1696 | "37140 <=60 \n",
1697 | "37558 <=60 \n",
1698 | "... ... \n",
1699 | "40450 >60 \n",
1700 | "38921 >60 \n",
1701 | "27826 >60 \n",
1702 | "38455 >60 \n",
1703 | "38452 >60 \n",
1704 | "\n",
1705 | "[41188 rows x 16 columns]"
1706 | ]
1707 | },
1708 | "execution_count": 21,
1709 | "metadata": {},
1710 | "output_type": "execute_result"
1711 | }
1712 | ],
1713 | "source": [
1714 | "data"
1715 | ]
1716 | },
1717 | {
1718 | "cell_type": "code",
1719 | "execution_count": 22,
1720 | "metadata": {
1721 | "id": "YIyw1pBL6M5p"
1722 | },
1723 | "outputs": [],
1724 | "source": [
1725 | "# We do the similar tactic with the duration feature as it's continuous\n",
1726 | "data=data.sort_values(by=['duration'])"
1727 | ]
1728 | },
1729 | {
1730 | "cell_type": "code",
1731 | "execution_count": 23,
1732 | "metadata": {
1733 | "colab": {
1734 | "base_uri": "https://localhost:8080/"
1735 | },
1736 | "id": "hc1BM0G16U3c",
1737 | "outputId": "cff38374-f078-4171-a47a-4936b22c084a"
1738 | },
1739 | "outputs": [
1740 | {
1741 | "data": {
1742 | "text/plain": [
1743 | "258.2850101971448"
1744 | ]
1745 | },
1746 | "execution_count": 23,
1747 | "metadata": {},
1748 | "output_type": "execute_result"
1749 | }
1750 | ],
1751 | "source": [
1752 | "np.mean(data['duration'])"
1753 | ]
1754 | },
1755 | {
1756 | "cell_type": "code",
1757 | "execution_count": 24,
1758 | "metadata": {
1759 | "id": "juU4Vvz06are"
1760 | },
1761 | "outputs": [],
1762 | "source": [
1763 | "# # Starting with 258 as threshold value for duration. Here we take some jumps in threshold values. We have the following Information Gain by trial & error. Hence, choosing 450 as as threshold value\n",
1764 | "# 258,289.350,375,450,500,600=[0.0650935234793425,0.06529231501228083,0.06719093723384761,0.06834116976822469,0.06875318837819422,0.06891361639584348,0.06764866059681529,0.060781066259440075]"
1765 | ]
1766 | },
1767 | {
1768 | "cell_type": "code",
1769 | "execution_count": 25,
1770 | "metadata": {
1771 | "id": "NhpTyqWj6dq6"
1772 | },
1773 | "outputs": [],
1774 | "source": [
1775 | "# Choosing 450 as the threshold value that gives the highest information for the duration feature\n",
1776 | "th=450\n",
1777 | "\n",
1778 | "idx=data[data['duration']<=th].index\n",
1779 | "data['l_duration']= '>'+str(th)\n",
1780 | "data.loc[idx,'l_duration']= '<='+str(th)"
1781 | ]
1782 | },
1783 | {
1784 | "cell_type": "code",
1785 | "execution_count": 26,
1786 | "metadata": {
1787 | "colab": {
1788 | "base_uri": "https://localhost:8080/",
1789 | "height": 467
1790 | },
1791 | "id": "TDzt2N_e7ACu",
1792 | "outputId": "81a6632d-7dcb-4196-ab42-02070a66dd31"
1793 | },
1794 | "outputs": [
1795 | {
1796 | "data": {
1797 | "text/html": [
1798 | "\n",
1799 | "\n",
1812 | "
\n",
1813 | " \n",
1814 | " \n",
1815 | " | \n",
1816 | " age | \n",
1817 | " job | \n",
1818 | " marital | \n",
1819 | " education | \n",
1820 | " default | \n",
1821 | " housing | \n",
1822 | " loan | \n",
1823 | " contact | \n",
1824 | " month | \n",
1825 | " day_of_week | \n",
1826 | " duration | \n",
1827 | " campaign | \n",
1828 | " previous | \n",
1829 | " poutcome | \n",
1830 | " y | \n",
1831 | " l_age | \n",
1832 | " l_duration | \n",
1833 | "
\n",
1834 | " \n",
1835 | " \n",
1836 | " \n",
1837 | " 6251 | \n",
1838 | " 39 | \n",
1839 | " admin. | \n",
1840 | " married | \n",
1841 | " high.school | \n",
1842 | " no | \n",
1843 | " yes | \n",
1844 | " no | \n",
1845 | " telephone | \n",
1846 | " may | \n",
1847 | " tue | \n",
1848 | " 0 | \n",
1849 | " 4 | \n",
1850 | " 0 | \n",
1851 | " failure | \n",
1852 | " no | \n",
1853 | " <=60 | \n",
1854 | " <=450 | \n",
1855 | "
\n",
1856 | " \n",
1857 | " 33015 | \n",
1858 | " 31 | \n",
1859 | " blue-collar | \n",
1860 | " married | \n",
1861 | " basic.9y | \n",
1862 | " no | \n",
1863 | " no | \n",
1864 | " no | \n",
1865 | " cellular | \n",
1866 | " may | \n",
1867 | " mon | \n",
1868 | " 0 | \n",
1869 | " 2 | \n",
1870 | " 0 | \n",
1871 | " failure | \n",
1872 | " no | \n",
1873 | " <=60 | \n",
1874 | " <=450 | \n",
1875 | "
\n",
1876 | " \n",
1877 | " 28063 | \n",
1878 | " 53 | \n",
1879 | " blue-collar | \n",
1880 | " divorced | \n",
1881 | " high.school | \n",
1882 | " no | \n",
1883 | " yes | \n",
1884 | " no | \n",
1885 | " cellular | \n",
1886 | " apr | \n",
1887 | " fri | \n",
1888 | " 0 | \n",
1889 | " 3 | \n",
1890 | " 0 | \n",
1891 | " failure | \n",
1892 | " no | \n",
1893 | " <=60 | \n",
1894 | " <=450 | \n",
1895 | "
\n",
1896 | " \n",
1897 | " 23031 | \n",
1898 | " 59 | \n",
1899 | " management | \n",
1900 | " married | \n",
1901 | " university.degree | \n",
1902 | " no | \n",
1903 | " yes | \n",
1904 | " no | \n",
1905 | " cellular | \n",
1906 | " aug | \n",
1907 | " tue | \n",
1908 | " 0 | \n",
1909 | " 10 | \n",
1910 | " 0 | \n",
1911 | " failure | \n",
1912 | " no | \n",
1913 | " <=60 | \n",
1914 | " <=450 | \n",
1915 | "
\n",
1916 | " \n",
1917 | " 28239 | \n",
1918 | " 44 | \n",
1919 | " blue-collar | \n",
1920 | " married | \n",
1921 | " basic.9y | \n",
1922 | " no | \n",
1923 | " no | \n",
1924 | " no | \n",
1925 | " cellular | \n",
1926 | " apr | \n",
1927 | " tue | \n",
1928 | " 1 | \n",
1929 | " 1 | \n",
1930 | " 0 | \n",
1931 | " failure | \n",
1932 | " no | \n",
1933 | " <=60 | \n",
1934 | " <=450 | \n",
1935 | "
\n",
1936 | " \n",
1937 | " ... | \n",
1938 | " ... | \n",
1939 | " ... | \n",
1940 | " ... | \n",
1941 | " ... | \n",
1942 | " ... | \n",
1943 | " ... | \n",
1944 | " ... | \n",
1945 | " ... | \n",
1946 | " ... | \n",
1947 | " ... | \n",
1948 | " ... | \n",
1949 | " ... | \n",
1950 | " ... | \n",
1951 | " ... | \n",
1952 | " ... | \n",
1953 | " ... | \n",
1954 | " ... | \n",
1955 | "
\n",
1956 | " \n",
1957 | " 7727 | \n",
1958 | " 37 | \n",
1959 | " unemployed | \n",
1960 | " married | \n",
1961 | " professional.course | \n",
1962 | " no | \n",
1963 | " yes | \n",
1964 | " no | \n",
1965 | " telephone | \n",
1966 | " may | \n",
1967 | " fri | \n",
1968 | " 3631 | \n",
1969 | " 2 | \n",
1970 | " 0 | \n",
1971 | " failure | \n",
1972 | " yes | \n",
1973 | " <=60 | \n",
1974 | " >450 | \n",
1975 | "
\n",
1976 | " \n",
1977 | " 13820 | \n",
1978 | " 31 | \n",
1979 | " technician | \n",
1980 | " married | \n",
1981 | " professional.course | \n",
1982 | " no | \n",
1983 | " no | \n",
1984 | " no | \n",
1985 | " cellular | \n",
1986 | " jul | \n",
1987 | " thu | \n",
1988 | " 3643 | \n",
1989 | " 1 | \n",
1990 | " 0 | \n",
1991 | " failure | \n",
1992 | " yes | \n",
1993 | " <=60 | \n",
1994 | " >450 | \n",
1995 | "
\n",
1996 | " \n",
1997 | " 40537 | \n",
1998 | " 27 | \n",
1999 | " admin. | \n",
2000 | " single | \n",
2001 | " high.school | \n",
2002 | " no | \n",
2003 | " no | \n",
2004 | " no | \n",
2005 | " telephone | \n",
2006 | " aug | \n",
2007 | " fri | \n",
2008 | " 3785 | \n",
2009 | " 1 | \n",
2010 | " 0 | \n",
2011 | " failure | \n",
2012 | " no | \n",
2013 | " <=60 | \n",
2014 | " >450 | \n",
2015 | "
\n",
2016 | " \n",
2017 | " 22192 | \n",
2018 | " 52 | \n",
2019 | " blue-collar | \n",
2020 | " married | \n",
2021 | " basic.4y | \n",
2022 | " no | \n",
2023 | " no | \n",
2024 | " no | \n",
2025 | " telephone | \n",
2026 | " aug | \n",
2027 | " thu | \n",
2028 | " 4199 | \n",
2029 | " 3 | \n",
2030 | " 0 | \n",
2031 | " failure | \n",
2032 | " yes | \n",
2033 | " <=60 | \n",
2034 | " >450 | \n",
2035 | "
\n",
2036 | " \n",
2037 | " 24091 | \n",
2038 | " 33 | \n",
2039 | " technician | \n",
2040 | " single | \n",
2041 | " professional.course | \n",
2042 | " no | \n",
2043 | " yes | \n",
2044 | " no | \n",
2045 | " telephone | \n",
2046 | " nov | \n",
2047 | " mon | \n",
2048 | " 4918 | \n",
2049 | " 1 | \n",
2050 | " 0 | \n",
2051 | " failure | \n",
2052 | " no | \n",
2053 | " <=60 | \n",
2054 | " >450 | \n",
2055 | "
\n",
2056 | " \n",
2057 | "
\n",
2058 | "
41188 rows × 17 columns
\n",
2059 | "
"
2060 | ],
2061 | "text/plain": [
2062 | " age job marital education default housing loan \\\n",
2063 | "6251 39 admin. married high.school no yes no \n",
2064 | "33015 31 blue-collar married basic.9y no no no \n",
2065 | "28063 53 blue-collar divorced high.school no yes no \n",
2066 | "23031 59 management married university.degree no yes no \n",
2067 | "28239 44 blue-collar married basic.9y no no no \n",
2068 | "... ... ... ... ... ... ... ... \n",
2069 | "7727 37 unemployed married professional.course no yes no \n",
2070 | "13820 31 technician married professional.course no no no \n",
2071 | "40537 27 admin. single high.school no no no \n",
2072 | "22192 52 blue-collar married basic.4y no no no \n",
2073 | "24091 33 technician single professional.course no yes no \n",
2074 | "\n",
2075 | " contact month day_of_week duration campaign previous poutcome \\\n",
2076 | "6251 telephone may tue 0 4 0 failure \n",
2077 | "33015 cellular may mon 0 2 0 failure \n",
2078 | "28063 cellular apr fri 0 3 0 failure \n",
2079 | "23031 cellular aug tue 0 10 0 failure \n",
2080 | "28239 cellular apr tue 1 1 0 failure \n",
2081 | "... ... ... ... ... ... ... ... \n",
2082 | "7727 telephone may fri 3631 2 0 failure \n",
2083 | "13820 cellular jul thu 3643 1 0 failure \n",
2084 | "40537 telephone aug fri 3785 1 0 failure \n",
2085 | "22192 telephone aug thu 4199 3 0 failure \n",
2086 | "24091 telephone nov mon 4918 1 0 failure \n",
2087 | "\n",
2088 | " y l_age l_duration \n",
2089 | "6251 no <=60 <=450 \n",
2090 | "33015 no <=60 <=450 \n",
2091 | "28063 no <=60 <=450 \n",
2092 | "23031 no <=60 <=450 \n",
2093 | "28239 no <=60 <=450 \n",
2094 | "... ... ... ... \n",
2095 | "7727 yes <=60 >450 \n",
2096 | "13820 yes <=60 >450 \n",
2097 | "40537 no <=60 >450 \n",
2098 | "22192 yes <=60 >450 \n",
2099 | "24091 no <=60 >450 \n",
2100 | "\n",
2101 | "[41188 rows x 17 columns]"
2102 | ]
2103 | },
2104 | "execution_count": 26,
2105 | "metadata": {},
2106 | "output_type": "execute_result"
2107 | }
2108 | ],
2109 | "source": [
2110 | "data"
2111 | ]
2112 | },
2113 | {
2114 | "cell_type": "code",
2115 | "execution_count": 27,
2116 | "metadata": {
2117 | "colab": {
2118 | "base_uri": "https://localhost:8080/"
2119 | },
2120 | "id": "xMqbHXz77BAc",
2121 | "outputId": "2f7bc22c-496a-48c2-fc6d-828170f040ae"
2122 | },
2123 | "outputs": [
2124 | {
2125 | "data": {
2126 | "text/plain": [
2127 | "0.06891361639584348"
2128 | ]
2129 | },
2130 | "execution_count": 27,
2131 | "metadata": {},
2132 | "output_type": "execute_result"
2133 | }
2134 | ],
2135 | "source": [
2136 | "Information_Gain(data,'l_duration')"
2137 | ]
2138 | },
2139 | {
2140 | "cell_type": "code",
2141 | "execution_count": 28,
2142 | "metadata": {
2143 | "id": "ESVSnEwF7FS3"
2144 | },
2145 | "outputs": [],
2146 | "source": [
2147 | "# Now we drop the initial age and duration columns\n",
2148 | "data.drop(['age','duration'],axis=1,inplace=True)"
2149 | ]
2150 | },
2151 | {
2152 | "cell_type": "code",
2153 | "execution_count": 29,
2154 | "metadata": {},
2155 | "outputs": [
2156 | {
2157 | "data": {
2158 | "text/html": [
2159 | "\n",
2160 | "\n",
2173 | "
\n",
2174 | " \n",
2175 | " \n",
2176 | " | \n",
2177 | " job | \n",
2178 | " marital | \n",
2179 | " education | \n",
2180 | " default | \n",
2181 | " housing | \n",
2182 | " loan | \n",
2183 | " contact | \n",
2184 | " month | \n",
2185 | " day_of_week | \n",
2186 | " campaign | \n",
2187 | " previous | \n",
2188 | " poutcome | \n",
2189 | " y | \n",
2190 | " l_age | \n",
2191 | " l_duration | \n",
2192 | "
\n",
2193 | " \n",
2194 | " \n",
2195 | " \n",
2196 | " 6251 | \n",
2197 | " admin. | \n",
2198 | " married | \n",
2199 | " high.school | \n",
2200 | " no | \n",
2201 | " yes | \n",
2202 | " no | \n",
2203 | " telephone | \n",
2204 | " may | \n",
2205 | " tue | \n",
2206 | " 4 | \n",
2207 | " 0 | \n",
2208 | " failure | \n",
2209 | " no | \n",
2210 | " <=60 | \n",
2211 | " <=450 | \n",
2212 | "
\n",
2213 | " \n",
2214 | " 33015 | \n",
2215 | " blue-collar | \n",
2216 | " married | \n",
2217 | " basic.9y | \n",
2218 | " no | \n",
2219 | " no | \n",
2220 | " no | \n",
2221 | " cellular | \n",
2222 | " may | \n",
2223 | " mon | \n",
2224 | " 2 | \n",
2225 | " 0 | \n",
2226 | " failure | \n",
2227 | " no | \n",
2228 | " <=60 | \n",
2229 | " <=450 | \n",
2230 | "
\n",
2231 | " \n",
2232 | " 28063 | \n",
2233 | " blue-collar | \n",
2234 | " divorced | \n",
2235 | " high.school | \n",
2236 | " no | \n",
2237 | " yes | \n",
2238 | " no | \n",
2239 | " cellular | \n",
2240 | " apr | \n",
2241 | " fri | \n",
2242 | " 3 | \n",
2243 | " 0 | \n",
2244 | " failure | \n",
2245 | " no | \n",
2246 | " <=60 | \n",
2247 | " <=450 | \n",
2248 | "
\n",
2249 | " \n",
2250 | " 23031 | \n",
2251 | " management | \n",
2252 | " married | \n",
2253 | " university.degree | \n",
2254 | " no | \n",
2255 | " yes | \n",
2256 | " no | \n",
2257 | " cellular | \n",
2258 | " aug | \n",
2259 | " tue | \n",
2260 | " 10 | \n",
2261 | " 0 | \n",
2262 | " failure | \n",
2263 | " no | \n",
2264 | " <=60 | \n",
2265 | " <=450 | \n",
2266 | "
\n",
2267 | " \n",
2268 | " 28239 | \n",
2269 | " blue-collar | \n",
2270 | " married | \n",
2271 | " basic.9y | \n",
2272 | " no | \n",
2273 | " no | \n",
2274 | " no | \n",
2275 | " cellular | \n",
2276 | " apr | \n",
2277 | " tue | \n",
2278 | " 1 | \n",
2279 | " 0 | \n",
2280 | " failure | \n",
2281 | " no | \n",
2282 | " <=60 | \n",
2283 | " <=450 | \n",
2284 | "
\n",
2285 | " \n",
2286 | " ... | \n",
2287 | " ... | \n",
2288 | " ... | \n",
2289 | " ... | \n",
2290 | " ... | \n",
2291 | " ... | \n",
2292 | " ... | \n",
2293 | " ... | \n",
2294 | " ... | \n",
2295 | " ... | \n",
2296 | " ... | \n",
2297 | " ... | \n",
2298 | " ... | \n",
2299 | " ... | \n",
2300 | " ... | \n",
2301 | " ... | \n",
2302 | "
\n",
2303 | " \n",
2304 | " 7727 | \n",
2305 | " unemployed | \n",
2306 | " married | \n",
2307 | " professional.course | \n",
2308 | " no | \n",
2309 | " yes | \n",
2310 | " no | \n",
2311 | " telephone | \n",
2312 | " may | \n",
2313 | " fri | \n",
2314 | " 2 | \n",
2315 | " 0 | \n",
2316 | " failure | \n",
2317 | " yes | \n",
2318 | " <=60 | \n",
2319 | " >450 | \n",
2320 | "
\n",
2321 | " \n",
2322 | " 13820 | \n",
2323 | " technician | \n",
2324 | " married | \n",
2325 | " professional.course | \n",
2326 | " no | \n",
2327 | " no | \n",
2328 | " no | \n",
2329 | " cellular | \n",
2330 | " jul | \n",
2331 | " thu | \n",
2332 | " 1 | \n",
2333 | " 0 | \n",
2334 | " failure | \n",
2335 | " yes | \n",
2336 | " <=60 | \n",
2337 | " >450 | \n",
2338 | "
\n",
2339 | " \n",
2340 | " 40537 | \n",
2341 | " admin. | \n",
2342 | " single | \n",
2343 | " high.school | \n",
2344 | " no | \n",
2345 | " no | \n",
2346 | " no | \n",
2347 | " telephone | \n",
2348 | " aug | \n",
2349 | " fri | \n",
2350 | " 1 | \n",
2351 | " 0 | \n",
2352 | " failure | \n",
2353 | " no | \n",
2354 | " <=60 | \n",
2355 | " >450 | \n",
2356 | "
\n",
2357 | " \n",
2358 | " 22192 | \n",
2359 | " blue-collar | \n",
2360 | " married | \n",
2361 | " basic.4y | \n",
2362 | " no | \n",
2363 | " no | \n",
2364 | " no | \n",
2365 | " telephone | \n",
2366 | " aug | \n",
2367 | " thu | \n",
2368 | " 3 | \n",
2369 | " 0 | \n",
2370 | " failure | \n",
2371 | " yes | \n",
2372 | " <=60 | \n",
2373 | " >450 | \n",
2374 | "
\n",
2375 | " \n",
2376 | " 24091 | \n",
2377 | " technician | \n",
2378 | " single | \n",
2379 | " professional.course | \n",
2380 | " no | \n",
2381 | " yes | \n",
2382 | " no | \n",
2383 | " telephone | \n",
2384 | " nov | \n",
2385 | " mon | \n",
2386 | " 1 | \n",
2387 | " 0 | \n",
2388 | " failure | \n",
2389 | " no | \n",
2390 | " <=60 | \n",
2391 | " >450 | \n",
2392 | "
\n",
2393 | " \n",
2394 | "
\n",
2395 | "
41188 rows × 15 columns
\n",
2396 | "
"
2397 | ],
2398 | "text/plain": [
2399 | " job marital education default housing loan \\\n",
2400 | "6251 admin. married high.school no yes no \n",
2401 | "33015 blue-collar married basic.9y no no no \n",
2402 | "28063 blue-collar divorced high.school no yes no \n",
2403 | "23031 management married university.degree no yes no \n",
2404 | "28239 blue-collar married basic.9y no no no \n",
2405 | "... ... ... ... ... ... ... \n",
2406 | "7727 unemployed married professional.course no yes no \n",
2407 | "13820 technician married professional.course no no no \n",
2408 | "40537 admin. single high.school no no no \n",
2409 | "22192 blue-collar married basic.4y no no no \n",
2410 | "24091 technician single professional.course no yes no \n",
2411 | "\n",
2412 | " contact month day_of_week campaign previous poutcome y l_age \\\n",
2413 | "6251 telephone may tue 4 0 failure no <=60 \n",
2414 | "33015 cellular may mon 2 0 failure no <=60 \n",
2415 | "28063 cellular apr fri 3 0 failure no <=60 \n",
2416 | "23031 cellular aug tue 10 0 failure no <=60 \n",
2417 | "28239 cellular apr tue 1 0 failure no <=60 \n",
2418 | "... ... ... ... ... ... ... ... ... \n",
2419 | "7727 telephone may fri 2 0 failure yes <=60 \n",
2420 | "13820 cellular jul thu 1 0 failure yes <=60 \n",
2421 | "40537 telephone aug fri 1 0 failure no <=60 \n",
2422 | "22192 telephone aug thu 3 0 failure yes <=60 \n",
2423 | "24091 telephone nov mon 1 0 failure no <=60 \n",
2424 | "\n",
2425 | " l_duration \n",
2426 | "6251 <=450 \n",
2427 | "33015 <=450 \n",
2428 | "28063 <=450 \n",
2429 | "23031 <=450 \n",
2430 | "28239 <=450 \n",
2431 | "... ... \n",
2432 | "7727 >450 \n",
2433 | "13820 >450 \n",
2434 | "40537 >450 \n",
2435 | "22192 >450 \n",
2436 | "24091 >450 \n",
2437 | "\n",
2438 | "[41188 rows x 15 columns]"
2439 | ]
2440 | },
2441 | "execution_count": 29,
2442 | "metadata": {},
2443 | "output_type": "execute_result"
2444 | }
2445 | ],
2446 | "source": [
2447 | "data"
2448 | ]
2449 | },
2450 | {
2451 | "cell_type": "code",
2452 | "execution_count": 30,
2453 | "metadata": {},
2454 | "outputs": [
2455 | {
2456 | "data": {
2457 | "text/plain": [
2458 | "6251 no\n",
2459 | "33015 no\n",
2460 | "28063 no\n",
2461 | "23031 no\n",
2462 | "28239 no\n",
2463 | " ... \n",
2464 | "7727 yes\n",
2465 | "13820 yes\n",
2466 | "40537 no\n",
2467 | "22192 yes\n",
2468 | "24091 no\n",
2469 | "Name: y, Length: 41188, dtype: object"
2470 | ]
2471 | },
2472 | "execution_count": 30,
2473 | "metadata": {},
2474 | "output_type": "execute_result"
2475 | }
2476 | ],
2477 | "source": [
2478 | "o=data['y']\n",
2479 | "o"
2480 | ]
2481 | },
2482 | {
2483 | "cell_type": "code",
2484 | "execution_count": 31,
2485 | "metadata": {},
2486 | "outputs": [],
2487 | "source": [
2488 | "data.drop(columns='y',axis=1,inplace=True)"
2489 | ]
2490 | },
2491 | {
2492 | "cell_type": "code",
2493 | "execution_count": 32,
2494 | "metadata": {},
2495 | "outputs": [],
2496 | "source": [
2497 | "data=pd.concat([data,o],axis=1)"
2498 | ]
2499 | },
2500 | {
2501 | "cell_type": "code",
2502 | "execution_count": 33,
2503 | "metadata": {},
2504 | "outputs": [
2505 | {
2506 | "data": {
2507 | "text/html": [
2508 | "\n",
2509 | "\n",
2522 | "
\n",
2523 | " \n",
2524 | " \n",
2525 | " | \n",
2526 | " job | \n",
2527 | " marital | \n",
2528 | " education | \n",
2529 | " default | \n",
2530 | " housing | \n",
2531 | " loan | \n",
2532 | " contact | \n",
2533 | " month | \n",
2534 | " day_of_week | \n",
2535 | " campaign | \n",
2536 | " previous | \n",
2537 | " poutcome | \n",
2538 | " l_age | \n",
2539 | " l_duration | \n",
2540 | " y | \n",
2541 | "
\n",
2542 | " \n",
2543 | " \n",
2544 | " \n",
2545 | " 6251 | \n",
2546 | " admin. | \n",
2547 | " married | \n",
2548 | " high.school | \n",
2549 | " no | \n",
2550 | " yes | \n",
2551 | " no | \n",
2552 | " telephone | \n",
2553 | " may | \n",
2554 | " tue | \n",
2555 | " 4 | \n",
2556 | " 0 | \n",
2557 | " failure | \n",
2558 | " <=60 | \n",
2559 | " <=450 | \n",
2560 | " no | \n",
2561 | "
\n",
2562 | " \n",
2563 | " 33015 | \n",
2564 | " blue-collar | \n",
2565 | " married | \n",
2566 | " basic.9y | \n",
2567 | " no | \n",
2568 | " no | \n",
2569 | " no | \n",
2570 | " cellular | \n",
2571 | " may | \n",
2572 | " mon | \n",
2573 | " 2 | \n",
2574 | " 0 | \n",
2575 | " failure | \n",
2576 | " <=60 | \n",
2577 | " <=450 | \n",
2578 | " no | \n",
2579 | "
\n",
2580 | " \n",
2581 | " 28063 | \n",
2582 | " blue-collar | \n",
2583 | " divorced | \n",
2584 | " high.school | \n",
2585 | " no | \n",
2586 | " yes | \n",
2587 | " no | \n",
2588 | " cellular | \n",
2589 | " apr | \n",
2590 | " fri | \n",
2591 | " 3 | \n",
2592 | " 0 | \n",
2593 | " failure | \n",
2594 | " <=60 | \n",
2595 | " <=450 | \n",
2596 | " no | \n",
2597 | "
\n",
2598 | " \n",
2599 | " 23031 | \n",
2600 | " management | \n",
2601 | " married | \n",
2602 | " university.degree | \n",
2603 | " no | \n",
2604 | " yes | \n",
2605 | " no | \n",
2606 | " cellular | \n",
2607 | " aug | \n",
2608 | " tue | \n",
2609 | " 10 | \n",
2610 | " 0 | \n",
2611 | " failure | \n",
2612 | " <=60 | \n",
2613 | " <=450 | \n",
2614 | " no | \n",
2615 | "
\n",
2616 | " \n",
2617 | " 28239 | \n",
2618 | " blue-collar | \n",
2619 | " married | \n",
2620 | " basic.9y | \n",
2621 | " no | \n",
2622 | " no | \n",
2623 | " no | \n",
2624 | " cellular | \n",
2625 | " apr | \n",
2626 | " tue | \n",
2627 | " 1 | \n",
2628 | " 0 | \n",
2629 | " failure | \n",
2630 | " <=60 | \n",
2631 | " <=450 | \n",
2632 | " no | \n",
2633 | "
\n",
2634 | " \n",
2635 | " ... | \n",
2636 | " ... | \n",
2637 | " ... | \n",
2638 | " ... | \n",
2639 | " ... | \n",
2640 | " ... | \n",
2641 | " ... | \n",
2642 | " ... | \n",
2643 | " ... | \n",
2644 | " ... | \n",
2645 | " ... | \n",
2646 | " ... | \n",
2647 | " ... | \n",
2648 | " ... | \n",
2649 | " ... | \n",
2650 | " ... | \n",
2651 | "
\n",
2652 | " \n",
2653 | " 7727 | \n",
2654 | " unemployed | \n",
2655 | " married | \n",
2656 | " professional.course | \n",
2657 | " no | \n",
2658 | " yes | \n",
2659 | " no | \n",
2660 | " telephone | \n",
2661 | " may | \n",
2662 | " fri | \n",
2663 | " 2 | \n",
2664 | " 0 | \n",
2665 | " failure | \n",
2666 | " <=60 | \n",
2667 | " >450 | \n",
2668 | " yes | \n",
2669 | "
\n",
2670 | " \n",
2671 | " 13820 | \n",
2672 | " technician | \n",
2673 | " married | \n",
2674 | " professional.course | \n",
2675 | " no | \n",
2676 | " no | \n",
2677 | " no | \n",
2678 | " cellular | \n",
2679 | " jul | \n",
2680 | " thu | \n",
2681 | " 1 | \n",
2682 | " 0 | \n",
2683 | " failure | \n",
2684 | " <=60 | \n",
2685 | " >450 | \n",
2686 | " yes | \n",
2687 | "
\n",
2688 | " \n",
2689 | " 40537 | \n",
2690 | " admin. | \n",
2691 | " single | \n",
2692 | " high.school | \n",
2693 | " no | \n",
2694 | " no | \n",
2695 | " no | \n",
2696 | " telephone | \n",
2697 | " aug | \n",
2698 | " fri | \n",
2699 | " 1 | \n",
2700 | " 0 | \n",
2701 | " failure | \n",
2702 | " <=60 | \n",
2703 | " >450 | \n",
2704 | " no | \n",
2705 | "
\n",
2706 | " \n",
2707 | " 22192 | \n",
2708 | " blue-collar | \n",
2709 | " married | \n",
2710 | " basic.4y | \n",
2711 | " no | \n",
2712 | " no | \n",
2713 | " no | \n",
2714 | " telephone | \n",
2715 | " aug | \n",
2716 | " thu | \n",
2717 | " 3 | \n",
2718 | " 0 | \n",
2719 | " failure | \n",
2720 | " <=60 | \n",
2721 | " >450 | \n",
2722 | " yes | \n",
2723 | "
\n",
2724 | " \n",
2725 | " 24091 | \n",
2726 | " technician | \n",
2727 | " single | \n",
2728 | " professional.course | \n",
2729 | " no | \n",
2730 | " yes | \n",
2731 | " no | \n",
2732 | " telephone | \n",
2733 | " nov | \n",
2734 | " mon | \n",
2735 | " 1 | \n",
2736 | " 0 | \n",
2737 | " failure | \n",
2738 | " <=60 | \n",
2739 | " >450 | \n",
2740 | " no | \n",
2741 | "
\n",
2742 | " \n",
2743 | "
\n",
2744 | "
41188 rows × 15 columns
\n",
2745 | "
"
2746 | ],
2747 | "text/plain": [
2748 | " job marital education default housing loan \\\n",
2749 | "6251 admin. married high.school no yes no \n",
2750 | "33015 blue-collar married basic.9y no no no \n",
2751 | "28063 blue-collar divorced high.school no yes no \n",
2752 | "23031 management married university.degree no yes no \n",
2753 | "28239 blue-collar married basic.9y no no no \n",
2754 | "... ... ... ... ... ... ... \n",
2755 | "7727 unemployed married professional.course no yes no \n",
2756 | "13820 technician married professional.course no no no \n",
2757 | "40537 admin. single high.school no no no \n",
2758 | "22192 blue-collar married basic.4y no no no \n",
2759 | "24091 technician single professional.course no yes no \n",
2760 | "\n",
2761 | " contact month day_of_week campaign previous poutcome l_age \\\n",
2762 | "6251 telephone may tue 4 0 failure <=60 \n",
2763 | "33015 cellular may mon 2 0 failure <=60 \n",
2764 | "28063 cellular apr fri 3 0 failure <=60 \n",
2765 | "23031 cellular aug tue 10 0 failure <=60 \n",
2766 | "28239 cellular apr tue 1 0 failure <=60 \n",
2767 | "... ... ... ... ... ... ... ... \n",
2768 | "7727 telephone may fri 2 0 failure <=60 \n",
2769 | "13820 cellular jul thu 1 0 failure <=60 \n",
2770 | "40537 telephone aug fri 1 0 failure <=60 \n",
2771 | "22192 telephone aug thu 3 0 failure <=60 \n",
2772 | "24091 telephone nov mon 1 0 failure <=60 \n",
2773 | "\n",
2774 | " l_duration y \n",
2775 | "6251 <=450 no \n",
2776 | "33015 <=450 no \n",
2777 | "28063 <=450 no \n",
2778 | "23031 <=450 no \n",
2779 | "28239 <=450 no \n",
2780 | "... ... ... \n",
2781 | "7727 >450 yes \n",
2782 | "13820 >450 yes \n",
2783 | "40537 >450 no \n",
2784 | "22192 >450 yes \n",
2785 | "24091 >450 no \n",
2786 | "\n",
2787 | "[41188 rows x 15 columns]"
2788 | ]
2789 | },
2790 | "execution_count": 33,
2791 | "metadata": {},
2792 | "output_type": "execute_result"
2793 | }
2794 | ],
2795 | "source": [
2796 | "data"
2797 | ]
2798 | },
2799 | {
2800 | "cell_type": "code",
2801 | "execution_count": 34,
2802 | "metadata": {
2803 | "id": "J5Lh8_FU_fFT"
2804 | },
2805 | "outputs": [],
2806 | "source": [
2807 | "def ID3(data,origin_data,features,target_attribute_name='y',parent_node_class = None):\n",
2808 | " if len(np.unique(data[target_attribute_name]))<=1:\n",
2809 | " return np.unique(data[target_attribute_name])[0]\n",
2810 | "\n",
2811 | " elif len(data)==0:\n",
2812 | " return np.unique(origin_data[target_attribute_name])[np.argmax(np.unique(origin_data[target_attribute_name],return_counts=True)[1])]\n",
2813 | "\n",
2814 | " elif len(features)==0: #Here we can adjust the depth of the tree by tampering with the len(feature)==X.\n",
2815 | " return parent_node_class \n",
2816 | " \n",
2817 | " #If none of the above holds true, grow the tree! \n",
2818 | " \n",
2819 | " else:\n",
2820 | " parent_node_class = np.unique(data[target_attribute_name])[np.argmax(np.unique(data[target_attribute_name],return_counts=True)[1])] \n",
2821 | " \n",
2822 | " #Select the feature which best splits the dataset via List Comprehension\n",
2823 | " info_values = [Information_Gain(data,feature) for feature in features] \n",
2824 | " best_feature_index = np.argmax(info_values) \n",
2825 | " best_feature = features[best_feature_index] \n",
2826 | " \n",
2827 | " #Create the tree structure.\n",
2828 | " tree = {best_feature:{}} \n",
2829 | " \n",
2830 | " \n",
2831 | " #Remove the feature with the best inforamtion gain from the feature space \n",
2832 | " features = [i for i in features if i != best_feature] \n",
2833 | " \n",
2834 | " \n",
2835 | " \n",
2836 | " for value in np.unique(data[best_feature]): \n",
2837 | " #Split the dataset along the value of the feature with the largest information gain \n",
2838 | " sub_data = data.where(data[best_feature] == value).dropna() \n",
2839 | " \n",
2840 | " #Call the ID3 algorithm for each of those sub_datasets with recursion.\n",
2841 | " subtree = ID3(sub_data,origin_data,features,target_attribute_name,parent_node_class) \n",
2842 | " \n",
2843 | " tree[best_feature][value] = subtree\n",
2844 | " return(tree) "
2845 | ]
2846 | }
2847 | ],
2848 | "metadata": {
2849 | "colab": {
2850 | "collapsed_sections": [],
2851 | "name": "Assignment_BUET.ipynb",
2852 | "provenance": []
2853 | },
2854 | "interpreter": {
2855 | "hash": "12d56e101fb626aa06cc0610ba999bd152779df661ca54f10a56d5b4a60f6a64"
2856 | },
2857 | "kernelspec": {
2858 | "display_name": "Python 3",
2859 | "language": "python",
2860 | "name": "python3"
2861 | },
2862 | "language_info": {
2863 | "codemirror_mode": {
2864 | "name": "ipython",
2865 | "version": 3
2866 | },
2867 | "file_extension": ".py",
2868 | "mimetype": "text/x-python",
2869 | "name": "python",
2870 | "nbconvert_exporter": "python",
2871 | "pygments_lexer": "ipython3",
2872 | "version": "3.8.5"
2873 | }
2874 | },
2875 | "nbformat": 4,
2876 | "nbformat_minor": 1
2877 | }
2878 |
--------------------------------------------------------------------------------
/Bank_Data/README.md:
--------------------------------------------------------------------------------
1 | ## Decision Tree-Mortgage Data Classification Problem
2 | This is a small project where I use a bank data with various features determining mortgage approval. This is a classificiation problem where I use ID3 algorithm to build a decision tree. And for that different types of feature engineering are tested.
3 |
--------------------------------------------------------------------------------
/Boston Housing Data/README.md:
--------------------------------------------------------------------------------
1 | ## Testing three different ML models via cross validation
2 | Boston Dataset Contains information about different houses in Boston. There are 506 samples and 13 feature variables in this dataset. https://archive.ics.uci.edu/ml/machine-learning-databases/housing/.
3 | Three types of model are used in this notebook: Linear Regression, KNN & Decision Tree. Elbow test is also done for KNN to find the optimum K-value. And lastly through cross validation, accuracy scores are represented
4 |
--------------------------------------------------------------------------------
/Forecasting Risk Analytics/Forecasting Risk ES_VAR.R:
--------------------------------------------------------------------------------
1 | rm(list=ls())
2 | library(copula)
3 | library(tseries)
4 | library(sn)
5 | library("Ecdat")
6 | library("fGarch")
7 | library("evir")
8 | library("forecast")
9 | library(ks)
10 | library(quantmod)
11 | library(MASS)
12 | library(fitdistrplus)
13 | getSymbols("SPXL",src='yahoo',auto.assign = TRUE,from='2013-09-29',to='2023-09-29')
14 | getSymbols("AMD",src='yahoo',auto.assign = TRUE,from='2013-09-29',to='2023-09-29')
15 | spxl_r=dailyReturn(SPXL$SPXL.Adjusted,type='log')
16 | amd_r=dailyReturn(AMD$AMD.Adjusted,type='log')
17 | spxl_r=coredata(spxl_r)
18 | amd_r=coredata(amd_r)
19 | plot(amd_r)
20 | points(spxl_r)
21 | ## Preliminary Stats
22 | plot(spxl_r)
23 | plot(amd_r)
24 | par(mfrow=c(1,1))
25 | acf(spxl_r,main='SPXL Daily Returns')
26 | acf(amd_r, main='AMD Daily Returns')
27 | summary(amd_r)
28 | summary(spxl_r)
29 | ##Let's Try seeing the residuals with AR(1) and validate the justification of time dependence & volatility clustering
30 | fitAR1_s=arima(spxl_r,order=c(0,0,2),include.mean=TRUE)#fit ARIMA model
31 | fitAR1_s
32 | res_s=residuals(fitAR1_s)
33 | acf(res_s,main='Residual MA(2)') ## Not white noise. Therefore, it is more important to capture the time dependencies
34 | acf(res_s^2,main='Residual Sq MA(2)') ## Numerous spikes at variety of lags
35 | ##AR(1)+GARCH(1,1)
36 | fit_s=garchFit(formula=~arma(1,0)+garch(1,1),data=spxl_r,cond.dist="norm",trace=FALSE)
37 | fit_a=garchFit(formula=~arma(1,0)+garch(1,1),data=amd_r,cond.dist="norm",trace=FALSE)
38 | fit_a
39 | res_a=residuals(fit_a,standardize=TRUE) ##White noise
40 | res_s=residuals(fit_s,standardize=TRUE)
41 | fit_ta=fitdistr(res_a,'t',upper=10)
42 | fit_ts=fitdistr(res_s,'t',upper=10)
43 | fit_ts
44 | par(mfrow=c(1,1))
45 | acf(res_s,main='SPXL Residual AR(1)-GARCH (1,1)')
46 | acf(res_s^2, main='SPXL Residual Sq AR(1)-GARCH(1,1)')
47 | acf(res_a,main='AMD Residual AR(1)-GARCH(1,1)')
48 | acf(res_a^2,main='AMD Residual Sq AR(1)-GARCH(1,1)')
49 | ## Copula modeling
50 |
51 | est1=fit_ta$estimate
52 | est2=fit_ts$estimate
53 |
54 | u1=pt(res_a,df=est1['df'])
55 | u2=pt(res_s,df=est2['df'])
56 |
57 | ##Combining uniform data
58 | uhat=cbind(u1,u2)
59 |
60 | ##Copula
61 | Ct=fitCopula(copula=tCopula(dim=2),data=uhat,method="ml")## T Copula
62 | Ct@estimate
63 | Ct_logL=loglikCopula(param=Ct@estimate,u=uhat,copula=tCopula(dim=2));#compute loglikelihood function
64 | Ct_logL
65 | -2*Ct_logL+2*length(Ct@estimate)#AIC
66 | Cg=fitCopula(copula=normalCopula(dim=2),data=uhat,method="ml")#fit Gaussian copula
67 | Cg@estimate;
68 | Cg_logL=loglikCopula(param=Cg@estimate,u=uhat,copula=normalCopula(dim=2));
69 | Cg_logL
70 | -2*Cg_logL+2*length(Cg@estimate);
71 | Ccl=fitCopula(copula=claytonCopula(dim=2),data=uhat,method="ml");#fit clayton copula
72 | Ccl@estimate
73 | Ccl_logL=loglikCopula(param=Ccl@estimate,u=uhat,copula=claytonCopula(dim=2));
74 | Ccl_logL
75 | -2*Ccl_logL+2*length(Ccl@estimate);#compute AIC
76 |
77 | Cgmbl=fitCopula(copula=gumbelCopula(dim=2),data=uhat,method="ml");#fit gumbel copula
78 | Cgmbl@estimate
79 | Cgmbl
80 | Cgmbl_logL=loglikCopula(param=Cgmbl@estimate,u=uhat,copula=gumbelCopula(dim=2))
81 | Cgmbl_logL
82 | -2*Cgmbl_logL+2*length(Cgmbl@estimate);
83 |
84 | ##So T Copula is the best for joint distribution
85 | Ct@estimate
86 | rho=Ct@estimate[1]
87 | correlation_matrix=matrix(c(1,rho,rho,1),nrow=2)
88 | correlation_matrix
89 | confidence_level <- 0.99
90 | n=length(amd_r)
91 | Simu_U=rCopula(n,tCopula(dim=2,Ct@estimate[1],df=Ct@estimate[2]))
92 | Simu_X1=qt(Simu_U[,1],df=est1['df'])
93 | Simu_X2=qt(Simu_U[,2],df=est2['df'])
94 | plot(Simu_X1,Simu_X2,xlab='AMD',ylab='SPXL',main='T-Copula
95 | Scatter Plot-Simulated AMD & SPXL Residuals ',col='red')
96 | points(res_a,res_s,col='blue',pch=4)
97 | summary(Simu_U)
98 |
99 | forecasted_s=predict(fit_s,n.ahead=1)
100 | forecasted_a=predict(fit_a,n.ahead=1)
101 |
102 | forecasted_volatility_a=forecasted_a$standardDeviation
103 | forecasted_volatility_s=forecasted_s$standardDeviation
104 |
105 | forecasted_s$meanForecast
106 | forecasted_a$meanForecast## Scaling
107 | scaled_residuals_x1=Simu_X1*forecasted_volatility_a
108 | scaled_residuals_x2=Simu_X2*forecasted_volatility_s
109 |
110 | plot(scaled_residuals_x1,scaled_residuals_x2)
111 |
112 | plot(spxl_r)
113 | plot(amd_r)
114 | weight_values=seq(0.1,0.9,by=0.1)
115 | n_simulations=10000
116 | VaR_estimates=numeric(length(weight_values))
117 | VaR_estimates
118 |
119 | for (i in seq_along(weight_values)){
120 | weight=weight_values[i]
121 | portfolio_returns=weight*(forecasted_a$meanForecast+scaled_residuals_x1)+(1-weight)*(forecasted_s$meanForecast+scaled_residuals_x2)
122 | VaR_estimates[i]=quantile(portfolio_returns,1-confidence_level)
123 | }
124 | VaR_estimates
125 | ##Expected Shortfall
126 | confidence_level=0.975
127 | ES_estimates=numeric(length(weight_values))
128 | VaR_estimates=numeric(length(weight_values))
129 | for (i in seq_along(weight_values)){
130 | weight=weight_values[i]
131 | portfolio_returns=weight*(forecasted_a$meanForecast+scaled_residuals_x1)+(1-weight)*(forecasted_s$meanForecast+scaled_residuals_x2)
132 | VaR_estimates[i]=quantile(portfolio_returns,1-confidence_level)
133 | ES_estimates[i]=mean(portfolio_returns[portfolio_returns"
70 | ]
71 | },
72 | "metadata": {},
73 | "output_type": "display_data"
74 | }
75 | ],
76 | "source": [
77 | "stock_prices[0,0]=S0\n",
78 | "\n",
79 | "for i in range (1,N):\n",
80 | " M=i+1\n",
81 | " stock_prices[i,0]=d*stock_prices[i-1,0]\n",
82 | " for j in range (1,M):\n",
83 | " stock_prices[i,j]=u*stock_prices[i-1,j-1]\n",
84 | "\n",
85 | "expiration=stock_prices[-1,:].toarray()-K\n",
86 | "expiration=np.where(expiration>=0,expiration,0)\n",
87 | "\n",
88 | "\n",
89 | "option_prices[-1,:]=expiration\n",
90 | "\n",
91 | "\n",
92 | "for i in range(N - 2,-1,-1):\n",
93 | " for j in range(i+1):\n",
94 | " option_prices[i,j]=np.exp(-r * t) * ((1-p) * option_prices[i+1,j] + p * option_prices[i+1,j+1])\n",
95 | " if stock_prices[i,j]-K>option_prices[i,j]:\n",
96 | " option_prices[i,j]=stock_prices[i,j]-K\n",
97 | " \n",
98 | "\n",
99 | "\n",
100 | "\n",
101 | "\n",
102 | "plt.spy(option_prices)\n",
103 | "print(option_prices)"
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "execution_count": null,
109 | "metadata": {},
110 | "outputs": [],
111 | "source": []
112 | }
113 | ],
114 | "metadata": {
115 | "kernelspec": {
116 | "display_name": "base",
117 | "language": "python",
118 | "name": "python3"
119 | },
120 | "language_info": {
121 | "codemirror_mode": {
122 | "name": "ipython",
123 | "version": 3
124 | },
125 | "file_extension": ".py",
126 | "mimetype": "text/x-python",
127 | "name": "python",
128 | "nbconvert_exporter": "python",
129 | "pygments_lexer": "ipython3",
130 | "version": "3.9.13"
131 | },
132 | "orig_nbformat": 4,
133 | "vscode": {
134 | "interpreter": {
135 | "hash": "4f77cdfaf4507c9d7563b1dafbbefd1211a5ec1bc40dd9ff43d298279ac97956"
136 | }
137 | }
138 | },
139 | "nbformat": 4,
140 | "nbformat_minor": 2
141 | }
142 |
--------------------------------------------------------------------------------
/Option_Pricing/Finite Difference, Binomial Trees.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [
8 | {
9 | "name": "stdout",
10 | "output_type": "stream",
11 | "text": [
12 | "6.918695403001518\n"
13 | ]
14 | }
15 | ],
16 | "source": [
17 | "##Binomial Tree- Call\n",
18 | "\n",
19 | "import numpy as np\n",
20 | "N=12\n",
21 | "T=0.25\n",
22 | "dt=T/N\n",
23 | "S0=100\n",
24 | "K=95\n",
25 | "r=0.03\n",
26 | "div=0.02\n",
27 | "sigma=0.2\n",
28 | "u = np.exp(sigma * np.sqrt(dt))\n",
29 | "d = 1/u\n",
30 | "p = (np.exp((r-div)*dt)-d)/ (u - d)\n",
31 | "\n",
32 | "\n",
33 | "stock_prices = np.zeros((N, N))\n",
34 | "call_prices = np.zeros((N, N))\n",
35 | "\n",
36 | "stock_prices[0,0] = S0\n",
37 | "\n",
38 | "\n",
39 | "for i in range(1, N):\n",
40 | " M = i + 1\n",
41 | " stock_prices[i, 0] = d * stock_prices[i-1, 0]\n",
42 | " for j in range(1, M ):\n",
43 | " stock_prices[i, j] = u * stock_prices[i - 1, j - 1]\n",
44 | "\n",
45 | "expiration = stock_prices[-1,:] - K\n",
46 | "expiration = np.where(expiration >= 0, expiration, 0)\n",
47 | "\n",
48 | "\n",
49 | "call_prices[-1,:] = expiration\n",
50 | "\n",
51 | "\n",
52 | "for i in range(N - 2,-1,-1):\n",
53 | " for j in range(i + 1):\n",
54 | " call_prices[i,j] = np.exp(-r*dt) * ((1-p) * call_prices[i+1,j] + p * call_prices[i+1,j+1])\n",
55 | "\n",
56 | "\n",
57 | "print(call_prices[0,0])"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 2,
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "from scipy.stats import norm"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 3,
72 | "metadata": {},
73 | "outputs": [],
74 | "source": [
75 | "##Black Scholes -Gaussian\n",
76 | "\n",
77 | "def BlackScholes(r,S0,K,T,sigma,div):\n",
78 | " d1=(np.log(S0/K) + (r+sigma**2/2)*T)/(sigma*np.sqrt(T))\n",
79 | " d2=d1-sigma*np.sqrt(T)\n",
80 | "\n",
81 | " price=(S0*np.exp(-div*T))*norm.cdf(d1,0,1) - K*np.exp(-r*T)*norm.cdf(d2,0,1)\n",
82 | "\n",
83 | " return price\n",
84 | " "
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": 4,
90 | "metadata": {},
91 | "outputs": [
92 | {
93 | "data": {
94 | "text/plain": [
95 | "7.0107551333798455"
96 | ]
97 | },
98 | "execution_count": 4,
99 | "metadata": {},
100 | "output_type": "execute_result"
101 | }
102 | ],
103 | "source": [
104 | "BlackScholes(0.03,100,95,0.25,0.2,0.02)"
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "execution_count": 5,
110 | "metadata": {},
111 | "outputs": [
112 | {
113 | "name": "stdout",
114 | "output_type": "stream",
115 | "text": [
116 | "6.918695403001518\n"
117 | ]
118 | }
119 | ],
120 | "source": [
121 | "##American Call Binomial\n",
122 | "\n",
123 | "import numpy as np\n",
124 | "import matplotlib.pyplot as plt\n",
125 | "\n",
126 | "\n",
127 | "N=12\n",
128 | "t=0.25\n",
129 | "dt=t/N\n",
130 | "S0=100\n",
131 | "K=95\n",
132 | "r=0.03\n",
133 | "div=0.02\n",
134 | "sigma=0.2\n",
135 | "u=np.exp(sigma*np.sqrt(dt))\n",
136 | "d=1/u\n",
137 | "\n",
138 | "p=(np.exp((r-div)*dt)-d)/(u-d)\n",
139 | "stock_prices=np.zeros((N,N))\n",
140 | "option_prices=np.zeros((N,N))\n",
141 | "\n",
142 | "stock_prices[0,0]=S0\n",
143 | "\n",
144 | "for i in range (1,N):\n",
145 | " M=i+1\n",
146 | " stock_prices[i,0]=d*stock_prices[i-1,0]\n",
147 | " for j in range (1,M):\n",
148 | " stock_prices[i,j]=u*stock_prices[i-1,j-1]\n",
149 | "\n",
150 | "expiration=stock_prices[-1,:]-K\n",
151 | "expiration=np.where(expiration>=0,expiration,0)\n",
152 | "\n",
153 | "\n",
154 | "option_prices[-1,:]=expiration\n",
155 | "\n",
156 | "\n",
157 | "for i in range(N - 2,-1,-1):\n",
158 | " for j in range(i+1):\n",
159 | " option_prices[i,j]=np.exp(-r*dt) * ((1-p) * option_prices[i+1,j] + p * option_prices[i+1,j+1])\n",
160 | " if stock_prices[i,j]-K>option_prices[i,j]:\n",
161 | " option_prices[i,j]=stock_prices[i,j]-K\n",
162 | " \n",
163 | "\n",
164 | "print(option_prices[0,0])"
165 | ]
166 | },
167 | {
168 | "cell_type": "code",
169 | "execution_count": 6,
170 | "metadata": {},
171 | "outputs": [],
172 | "source": [
173 | "##Finite Difference\n",
174 | "\n",
175 | "def fin_diff(S,K,T,sigma,r,q,N,k):\n",
176 | " dt = T/N; \n",
177 | " dx = sigma*np.sqrt(3*dt)\n",
178 | " nu = r - q - 0.5*sigma**2\n",
179 | " pu = 0.5*dt*((sigma/dx)**2 + nu/dx)\n",
180 | " pm = 1.0 - dt*(sigma/dx)**2 - r*dt \n",
181 | " pd = 0.5*dt*((sigma/dx)**2 - nu/dx)\n",
182 | " grid = np.zeros((N+1,2*k+1))\n",
183 | " \n",
184 | "\n",
185 | " St = [S*np.exp(-k*dx)]\n",
186 | " for j in range(1, 2*k+1):\n",
187 | " St.append(St[j-1]*np.exp(dx))\n",
188 | " \n",
189 | " \n",
190 | " for j in range(2*k+1):\n",
191 | " grid[N,j] = max(0, St[j] - K)\n",
192 | " \n",
193 | " \n",
194 | " \n",
195 | " for i in range(N-1, -1, -1):\n",
196 | " for j in range(1, 2*k):\n",
197 | " grid[i,j] =pu*grid[i+1,j+1] + pm*grid[i+1,j] + pd*grid[i+1,j-1]\n",
198 | " \n",
199 | " \n",
200 | " grid[i,0] = grid[i,1]\n",
201 | " grid[i,2*k] = grid[i,2*k-1] + (St[2*k]-St[2*k-1])\n",
202 | " \n",
203 | " return grid[0,k]"
204 | ]
205 | },
206 | {
207 | "cell_type": "code",
208 | "execution_count": 7,
209 | "metadata": {},
210 | "outputs": [
211 | {
212 | "data": {
213 | "text/plain": [
214 | "7.018645027227497"
215 | ]
216 | },
217 | "execution_count": 7,
218 | "metadata": {},
219 | "output_type": "execute_result"
220 | }
221 | ],
222 | "source": [
223 | "fin_diff(100,95,0.25,0.2,0.03,0.02,8,3)"
224 | ]
225 | },
226 | {
227 | "cell_type": "code",
228 | "execution_count": null,
229 | "metadata": {},
230 | "outputs": [],
231 | "source": []
232 | }
233 | ],
234 | "metadata": {
235 | "kernelspec": {
236 | "display_name": "base",
237 | "language": "python",
238 | "name": "python3"
239 | },
240 | "language_info": {
241 | "codemirror_mode": {
242 | "name": "ipython",
243 | "version": 3
244 | },
245 | "file_extension": ".py",
246 | "mimetype": "text/x-python",
247 | "name": "python",
248 | "nbconvert_exporter": "python",
249 | "pygments_lexer": "ipython3",
250 | "version": "3.9.13"
251 | },
252 | "orig_nbformat": 4,
253 | "vscode": {
254 | "interpreter": {
255 | "hash": "4f77cdfaf4507c9d7563b1dafbbefd1211a5ec1bc40dd9ff43d298279ac97956"
256 | }
257 | }
258 | },
259 | "nbformat": 4,
260 | "nbformat_minor": 2
261 | }
262 |
--------------------------------------------------------------------------------
/Option_Pricing/Monte Carlo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [
8 | {
9 | "name": "stdout",
10 | "output_type": "stream",
11 | "text": [
12 | "7.01766531898455\n"
13 | ]
14 | }
15 | ],
16 | "source": [
17 | "##Monte Carlo\n",
18 | "T=0.25\n",
19 | "S0=100\n",
20 | "K=95\n",
21 | "r=0.03\n",
22 | "div=0.02\n",
23 | "sigma=0.2\n",
24 | "\n",
25 | "\n",
26 | "import numpy as np\n",
27 | "np.random.seed(1)\n",
28 | "\n",
29 | "iterations=1000000 # 1 million iterations\n",
30 | "\n",
31 | "z=np.random.standard_normal(iterations)\n",
32 | "ST=S0*np.exp((r-0.5 * sigma**2-div)*T + sigma * np.sqrt(T)*z) #Euler Equation modified with dividend yield\n",
33 | "payoff=np.maximum(ST-K,0) ##Payoff at expiration\n",
34 | "Call_price=np.exp (-r*T) * np.sum(payoff)/iterations\n",
35 | "\n",
36 | "print(Call_price)"
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": null,
42 | "metadata": {},
43 | "outputs": [],
44 | "source": []
45 | }
46 | ],
47 | "metadata": {
48 | "kernelspec": {
49 | "display_name": "base",
50 | "language": "python",
51 | "name": "python3"
52 | },
53 | "language_info": {
54 | "codemirror_mode": {
55 | "name": "ipython",
56 | "version": 3
57 | },
58 | "file_extension": ".py",
59 | "mimetype": "text/x-python",
60 | "name": "python",
61 | "nbconvert_exporter": "python",
62 | "pygments_lexer": "ipython3",
63 | "version": "3.9.13"
64 | },
65 | "orig_nbformat": 4,
66 | "vscode": {
67 | "interpreter": {
68 | "hash": "4f77cdfaf4507c9d7563b1dafbbefd1211a5ec1bc40dd9ff43d298279ac97956"
69 | }
70 | }
71 | },
72 | "nbformat": 4,
73 | "nbformat_minor": 2
74 | }
75 |
--------------------------------------------------------------------------------
/Option_Pricing/README.md:
--------------------------------------------------------------------------------
1 | ## Binomial Tree Option Pricing. This is a four period model with an american call setting as well as european call setting. Finite Difference and Monte Carlo methods are used to verify the option prices approximately
2 |
--------------------------------------------------------------------------------
/R/Copula Modeling/ACF.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adibyaser/Side_Projects/0aea18fbe6f40cdfef1544274253b166bbbc02c3/R/Copula Modeling/ACF.pdf
--------------------------------------------------------------------------------
/R/Copula Modeling/Actual Plot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adibyaser/Side_Projects/0aea18fbe6f40cdfef1544274253b166bbbc02c3/R/Copula Modeling/Actual Plot.png
--------------------------------------------------------------------------------
/R/Copula Modeling/Copula Project.R:
--------------------------------------------------------------------------------
1 | library(copula)
2 | library(sn)
3 | library(ks)
4 | library(quantmod)
5 | library(MASS)
6 | library(fitdistrplus)
7 | rm(list=ls())
8 | getSymbols("NVDA",src='yahoo',auto.assign = TRUE,from='2013-09-29',to='2023-09-29')
9 | getSymbols("AMD",src='yahoo',auto.assign = TRUE,from='2013-09-29',to='2023-09-29')
10 | nvd_r=monthlyReturn(NVDA$NVDA.Adjusted,type='log')
11 | amd_r=monthlyReturn(AMD$AMD.Adjusted,type='log')
12 | nvd_r=coredata(nvd_r)
13 | amd_r=coredata(amd_r)
14 | n=length(nvd_r)
15 | par(mfrow=c(2,1))
16 | acf(amd_r,main='AMD Monthly Log Returns')
17 | acf(nvd_r,main='NVIDIA Monthly Log Returns ')
18 | ##Fit The following Distributions: Normal, Skewed normal,T,skewed T
19 |
20 | fit_normaln=fitdistr(nvd_r,densfun='normal')
21 | fit_normala=fitdistr(amd_r,densfun='normal')
22 |
23 |
24 | fit_tn=fitdistr(nvd_r,densfun='t',upper=30)
25 | fit_ta=fitdistr(amd_r,densfun='t',upper=50)
26 |
27 | fit_skt_n=st.mple(y=nvd_r)
28 | fit_skt_a=st.mple(y=amd_r)
29 |
30 |
31 | fit_skn_n=sn.mple(y=nvd_r)
32 | fit_skn_a=sn.mple(y=amd_r)
33 | fit_ta
34 |
35 | ##Check AIC For Each of them
36 |
37 | -2*fit_normaln$loglik+2*(2)
38 | -2*fit_normala$loglik+2*(2)
39 |
40 | -2*fit_tn$loglik+2*(3)
41 | -2*fit_ta$loglik+2*(3)
42 |
43 | -2*fit_skn_n$logL+2*(3)
44 | -2*fit_skn_a$logL+2*(3)
45 |
46 | -2*fit_skt_n$logL+2*(4)
47 | -2*fit_skt_a$logL+2*(4)
48 |
49 | ## Skewed Normal & Normal
50 | est1=fit_normala$estimate
51 | est2=fit_skn_n$cp
52 |
53 | u1=pnorm(amd_r,est1)
54 | u2=psn(nvd_r,xi=est2['mean'],omega=est2['s.d.'],alpha=est2['gamma1'])
55 | ##U hat Combinations
56 | uhat=cbind(u1,u2)
57 |
58 | ###Copula
59 | Ct=fitCopula(copula=tCopula(dim=2),data=uhat,method="ml")## T Copula
60 | Ct@estimate
61 | Ct_logL=loglikCopula(param=Ct@estimate,u=uhat,copula=tCopula(dim=2));#compute loglikelihood function
62 | -2*Ct_logL+2*length(Ct@estimate)#AIC
63 | Cfr=fitCopula(copula=frankCopula(dim=2),data=uhat,method="ml")#fit frank copula
64 | Cfr@estimate;
65 | Cfr_logL=loglikCopula(param=Cfr@estimate,u=uhat,copula=frankCopula(dim=2));
66 | -2*Cfr_logL+2*length(Cfr@estimate);
67 | Ccl=fitCopula(copula=claytonCopula(dim=2),data=uhat,method="ml");#fit clayton copula
68 | Ccl@estimate
69 | Ccl_logL=loglikCopula(param=Ccl@estimate,u=uhat,copula=claytonCopula(dim=2));
70 | -2*Ccl_logL+2*length(Ccl@estimate);#compute AIC
71 | Cg=fitCopula(copula=normalCopula(dim=2),data=uhat,method="ml");#fit Gaussian copula
72 | Cg_logL=loglikCopula(param=Cg@estimate,u=uhat,copula=normalCopula(dim=2))
73 | -2*Cg_logL+2*length(Cg@estimate);#compute AIC
74 | ##Simulation For Frank Copula
75 | n=length(amd_r)
76 | Simu_U=rCopula(n,frankCopula(dim=2,Cfr@estimate))
77 | summary(Simu_U)
78 | plot(Simu_U[,1],Simu_U[,2],main='Simulated Data-Uniform Distribution',xlab='Simulated AMD',ylab='Simulated NVIDIA')
79 | Simu_X1=qnorm(Simu_U[,1],est1)
80 | Simu_X2=qsn(Simu_U[,2],dp=est2)
81 | par(mfrow=c(1,1))
82 | plot(Simu_X1,Simu_X2,main='Simulated Plot of AMD & NVIDIA',xlab='Simulated_AMD',ylab='Simulated_NVIDIA')
83 | plot(coredata(amd_r),coredata(nvd_r),main='Actual Plot of AMD & NVIDIA',xlab="AMD",ylab='NVIDIA')
84 |
85 |
--------------------------------------------------------------------------------
/R/Copula Modeling/README.md:
--------------------------------------------------------------------------------
1 | ## This project relates to separating dependency structures from univariate marginal distributions for two stocks namely: NVIDIA and AMD. Secondly, we plot a simulated transformed data to see if our model resembles the actual plot of the data. Sometimes correlated stocks are held in a portfolio. Investors and analysts want to figure out the correlations between the assets. However, correlations only measure linear relationships, excluding non linear ones.That is why it's important to consider the non linear relationships, tail dependencies and estimated distributions to fit around the actual distributions. Copula models thus come into play.Copula models estimate their joint behavior, considering non-linearities as well as provide us with insightful estimates, which is more sophisticated than standard regressions. In this R script, we test out 4 individual distribution pair analysis: Normal, T, Skewed Normal and Skewed T. Then we try several Copula models for fitting and best estimates.
2 |
--------------------------------------------------------------------------------
/R/Copula Modeling/Simulated Plot,Frank Copula.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/adibyaser/Side_Projects/0aea18fbe6f40cdfef1544274253b166bbbc02c3/R/Copula Modeling/Simulated Plot,Frank Copula.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Hi,this is my own codespace to work around and showcase my projects. Feel free to look around.
2 |
--------------------------------------------------------------------------------
/Risk Management/EuroDollar Futures.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Here we are given a 90 days EuroDollar Futures contract. Assume the investor goes long, he/she will accrue daily profit/loss as the future contract is Marked to Market. \n",
8 | "## Assume first Initial Margin is $ 1,485 (USD)\n",
9 | "## Maintenance Margin is $ 1,110 (USD)\n",
10 | "## We know that investor buys 100 contracts\n",
11 | "## 1) Calculate the Daily Profit & Loss, assuming the investor does not take any profit for himself. Calculate daily cash flow to the contract.\n",
12 | "## 2) Calculate the Daily Profit & Loss, assuming the investor does take profit for himself. Calculate daily cash flow to the contract."
13 | ]
14 | },
15 | {
16 | "cell_type": "code",
17 | "execution_count": 35,
18 | "metadata": {},
19 | "outputs": [
20 | {
21 | "data": {
22 | "text/html": [
23 | "\n",
24 | "\n",
37 | "
\n",
38 | " \n",
39 | " \n",
40 | " | \n",
41 | " Date_1 | \n",
42 | " Prices_1 | \n",
43 | "
\n",
44 | " \n",
45 | " \n",
46 | " \n",
47 | " 0 | \n",
48 | " 2007-03-21 | \n",
49 | " 93.635 | \n",
50 | "
\n",
51 | " \n",
52 | " 1 | \n",
53 | " 2007-03-22 | \n",
54 | " 93.685 | \n",
55 | "
\n",
56 | " \n",
57 | " 2 | \n",
58 | " 2007-03-23 | \n",
59 | " 93.570 | \n",
60 | "
\n",
61 | " \n",
62 | " 3 | \n",
63 | " 2007-03-26 | \n",
64 | " 93.500 | \n",
65 | "
\n",
66 | " \n",
67 | " 4 | \n",
68 | " 2007-03-27 | \n",
69 | " 93.320 | \n",
70 | "
\n",
71 | " \n",
72 | " ... | \n",
73 | " ... | \n",
74 | " ... | \n",
75 | "
\n",
76 | " \n",
77 | " 60 | \n",
78 | " 2007-06-15 | \n",
79 | " 93.020 | \n",
80 | "
\n",
81 | " \n",
82 | " 61 | \n",
83 | " 2007-06-18 | \n",
84 | " 92.995 | \n",
85 | "
\n",
86 | " \n",
87 | " 62 | \n",
88 | " 2007-06-19 | \n",
89 | " 93.010 | \n",
90 | "
\n",
91 | " \n",
92 | " 63 | \n",
93 | " 2007-06-20 | \n",
94 | " 93.060 | \n",
95 | "
\n",
96 | " \n",
97 | " 64 | \n",
98 | " 2007-06-21 | \n",
99 | " 93.095 | \n",
100 | "
\n",
101 | " \n",
102 | "
\n",
103 | "
65 rows × 2 columns
\n",
104 | "
"
105 | ],
106 | "text/plain": [
107 | " Date_1 Prices_1\n",
108 | "0 2007-03-21 93.635\n",
109 | "1 2007-03-22 93.685\n",
110 | "2 2007-03-23 93.570\n",
111 | "3 2007-03-26 93.500\n",
112 | "4 2007-03-27 93.320\n",
113 | ".. ... ...\n",
114 | "60 2007-06-15 93.020\n",
115 | "61 2007-06-18 92.995\n",
116 | "62 2007-06-19 93.010\n",
117 | "63 2007-06-20 93.060\n",
118 | "64 2007-06-21 93.095\n",
119 | "\n",
120 | "[65 rows x 2 columns]"
121 | ]
122 | },
123 | "execution_count": 35,
124 | "metadata": {},
125 | "output_type": "execute_result"
126 | }
127 | ],
128 | "source": [
129 | "## We are given the Quotation Prices of the Contracts\n",
130 | "df=pd.read_excel('C:/Users/adib-/Desktop/AdibHW2.xlsx')\n",
131 | "df"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": 36,
137 | "metadata": {},
138 | "outputs": [],
139 | "source": [
140 | "df['difference']=df['Prices_1'].diff()"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": 37,
146 | "metadata": {},
147 | "outputs": [
148 | {
149 | "data": {
150 | "text/html": [
151 | "\n",
152 | "\n",
165 | "
\n",
166 | " \n",
167 | " \n",
168 | " | \n",
169 | " Date_1 | \n",
170 | " Prices_1 | \n",
171 | " difference | \n",
172 | "
\n",
173 | " \n",
174 | " \n",
175 | " \n",
176 | " 0 | \n",
177 | " 2007-03-21 | \n",
178 | " 93.635 | \n",
179 | " NaN | \n",
180 | "
\n",
181 | " \n",
182 | " 1 | \n",
183 | " 2007-03-22 | \n",
184 | " 93.685 | \n",
185 | " 0.050 | \n",
186 | "
\n",
187 | " \n",
188 | " 2 | \n",
189 | " 2007-03-23 | \n",
190 | " 93.570 | \n",
191 | " -0.115 | \n",
192 | "
\n",
193 | " \n",
194 | " 3 | \n",
195 | " 2007-03-26 | \n",
196 | " 93.500 | \n",
197 | " -0.070 | \n",
198 | "
\n",
199 | " \n",
200 | " 4 | \n",
201 | " 2007-03-27 | \n",
202 | " 93.320 | \n",
203 | " -0.180 | \n",
204 | "
\n",
205 | " \n",
206 | " ... | \n",
207 | " ... | \n",
208 | " ... | \n",
209 | " ... | \n",
210 | "
\n",
211 | " \n",
212 | " 60 | \n",
213 | " 2007-06-15 | \n",
214 | " 93.020 | \n",
215 | " -0.125 | \n",
216 | "
\n",
217 | " \n",
218 | " 61 | \n",
219 | " 2007-06-18 | \n",
220 | " 92.995 | \n",
221 | " -0.025 | \n",
222 | "
\n",
223 | " \n",
224 | " 62 | \n",
225 | " 2007-06-19 | \n",
226 | " 93.010 | \n",
227 | " 0.015 | \n",
228 | "
\n",
229 | " \n",
230 | " 63 | \n",
231 | " 2007-06-20 | \n",
232 | " 93.060 | \n",
233 | " 0.050 | \n",
234 | "
\n",
235 | " \n",
236 | " 64 | \n",
237 | " 2007-06-21 | \n",
238 | " 93.095 | \n",
239 | " 0.035 | \n",
240 | "
\n",
241 | " \n",
242 | "
\n",
243 | "
65 rows × 3 columns
\n",
244 | "
"
245 | ],
246 | "text/plain": [
247 | " Date_1 Prices_1 difference\n",
248 | "0 2007-03-21 93.635 NaN\n",
249 | "1 2007-03-22 93.685 0.050\n",
250 | "2 2007-03-23 93.570 -0.115\n",
251 | "3 2007-03-26 93.500 -0.070\n",
252 | "4 2007-03-27 93.320 -0.180\n",
253 | ".. ... ... ...\n",
254 | "60 2007-06-15 93.020 -0.125\n",
255 | "61 2007-06-18 92.995 -0.025\n",
256 | "62 2007-06-19 93.010 0.015\n",
257 | "63 2007-06-20 93.060 0.050\n",
258 | "64 2007-06-21 93.095 0.035\n",
259 | "\n",
260 | "[65 rows x 3 columns]"
261 | ]
262 | },
263 | "execution_count": 37,
264 | "metadata": {},
265 | "output_type": "execute_result"
266 | }
267 | ],
268 | "source": [
269 | "df"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": 38,
275 | "metadata": {},
276 | "outputs": [
277 | {
278 | "data": {
279 | "text/html": [
280 | "\n",
281 | "\n",
294 | "
\n",
295 | " \n",
296 | " \n",
297 | " | \n",
298 | " Date_1 | \n",
299 | " Prices_1 | \n",
300 | " difference | \n",
301 | "
\n",
302 | " \n",
303 | " \n",
304 | " \n",
305 | " 0 | \n",
306 | " 2007-03-21 | \n",
307 | " 93.635 | \n",
308 | " NaN | \n",
309 | "
\n",
310 | " \n",
311 | " 1 | \n",
312 | " 2007-03-22 | \n",
313 | " 93.685 | \n",
314 | " 0.050 | \n",
315 | "
\n",
316 | " \n",
317 | " 2 | \n",
318 | " 2007-03-23 | \n",
319 | " 93.570 | \n",
320 | " -0.115 | \n",
321 | "
\n",
322 | " \n",
323 | " 3 | \n",
324 | " 2007-03-26 | \n",
325 | " 93.500 | \n",
326 | " -0.070 | \n",
327 | "
\n",
328 | " \n",
329 | " 4 | \n",
330 | " 2007-03-27 | \n",
331 | " 93.320 | \n",
332 | " -0.180 | \n",
333 | "
\n",
334 | " \n",
335 | " ... | \n",
336 | " ... | \n",
337 | " ... | \n",
338 | " ... | \n",
339 | "
\n",
340 | " \n",
341 | " 60 | \n",
342 | " 2007-06-15 | \n",
343 | " 93.020 | \n",
344 | " -0.125 | \n",
345 | "
\n",
346 | " \n",
347 | " 61 | \n",
348 | " 2007-06-18 | \n",
349 | " 92.995 | \n",
350 | " -0.025 | \n",
351 | "
\n",
352 | " \n",
353 | " 62 | \n",
354 | " 2007-06-19 | \n",
355 | " 93.010 | \n",
356 | " 0.015 | \n",
357 | "
\n",
358 | " \n",
359 | " 63 | \n",
360 | " 2007-06-20 | \n",
361 | " 93.060 | \n",
362 | " 0.050 | \n",
363 | "
\n",
364 | " \n",
365 | " 64 | \n",
366 | " 2007-06-21 | \n",
367 | " 93.095 | \n",
368 | " 0.035 | \n",
369 | "
\n",
370 | " \n",
371 | "
\n",
372 | "
65 rows × 3 columns
\n",
373 | "
"
374 | ],
375 | "text/plain": [
376 | " Date_1 Prices_1 difference\n",
377 | "0 2007-03-21 93.635 NaN\n",
378 | "1 2007-03-22 93.685 0.050\n",
379 | "2 2007-03-23 93.570 -0.115\n",
380 | "3 2007-03-26 93.500 -0.070\n",
381 | "4 2007-03-27 93.320 -0.180\n",
382 | ".. ... ... ...\n",
383 | "60 2007-06-15 93.020 -0.125\n",
384 | "61 2007-06-18 92.995 -0.025\n",
385 | "62 2007-06-19 93.010 0.015\n",
386 | "63 2007-06-20 93.060 0.050\n",
387 | "64 2007-06-21 93.095 0.035\n",
388 | "\n",
389 | "[65 rows x 3 columns]"
390 | ]
391 | },
392 | "execution_count": 38,
393 | "metadata": {},
394 | "output_type": "execute_result"
395 | }
396 | ],
397 | "source": [
398 | "df"
399 | ]
400 | },
401 | {
402 | "cell_type": "code",
403 | "execution_count": 39,
404 | "metadata": {},
405 | "outputs": [
406 | {
407 | "data": {
408 | "text/html": [
409 | "\n",
410 | "\n",
423 | "
\n",
424 | " \n",
425 | " \n",
426 | " | \n",
427 | " Date_1 | \n",
428 | " Prices_1 | \n",
429 | " difference | \n",
430 | " Daily Gain/Loss | \n",
431 | "
\n",
432 | " \n",
433 | " \n",
434 | " \n",
435 | " 0 | \n",
436 | " 2007-03-21 | \n",
437 | " 93.635 | \n",
438 | " NaN | \n",
439 | " NaN | \n",
440 | "
\n",
441 | " \n",
442 | " 1 | \n",
443 | " 2007-03-22 | \n",
444 | " 93.685 | \n",
445 | " 0.050 | \n",
446 | " 12500.0 | \n",
447 | "
\n",
448 | " \n",
449 | " 2 | \n",
450 | " 2007-03-23 | \n",
451 | " 93.570 | \n",
452 | " -0.115 | \n",
453 | " -28750.0 | \n",
454 | "
\n",
455 | " \n",
456 | " 3 | \n",
457 | " 2007-03-26 | \n",
458 | " 93.500 | \n",
459 | " -0.070 | \n",
460 | " -17500.0 | \n",
461 | "
\n",
462 | " \n",
463 | " 4 | \n",
464 | " 2007-03-27 | \n",
465 | " 93.320 | \n",
466 | " -0.180 | \n",
467 | " -45000.0 | \n",
468 | "
\n",
469 | " \n",
470 | " ... | \n",
471 | " ... | \n",
472 | " ... | \n",
473 | " ... | \n",
474 | " ... | \n",
475 | "
\n",
476 | " \n",
477 | " 60 | \n",
478 | " 2007-06-15 | \n",
479 | " 93.020 | \n",
480 | " -0.125 | \n",
481 | " -31250.0 | \n",
482 | "
\n",
483 | " \n",
484 | " 61 | \n",
485 | " 2007-06-18 | \n",
486 | " 92.995 | \n",
487 | " -0.025 | \n",
488 | " -6250.0 | \n",
489 | "
\n",
490 | " \n",
491 | " 62 | \n",
492 | " 2007-06-19 | \n",
493 | " 93.010 | \n",
494 | " 0.015 | \n",
495 | " 3750.0 | \n",
496 | "
\n",
497 | " \n",
498 | " 63 | \n",
499 | " 2007-06-20 | \n",
500 | " 93.060 | \n",
501 | " 0.050 | \n",
502 | " 12500.0 | \n",
503 | "
\n",
504 | " \n",
505 | " 64 | \n",
506 | " 2007-06-21 | \n",
507 | " 93.095 | \n",
508 | " 0.035 | \n",
509 | " 8750.0 | \n",
510 | "
\n",
511 | " \n",
512 | "
\n",
513 | "
65 rows × 4 columns
\n",
514 | "
"
515 | ],
516 | "text/plain": [
517 | " Date_1 Prices_1 difference Daily Gain/Loss\n",
518 | "0 2007-03-21 93.635 NaN NaN\n",
519 | "1 2007-03-22 93.685 0.050 12500.0\n",
520 | "2 2007-03-23 93.570 -0.115 -28750.0\n",
521 | "3 2007-03-26 93.500 -0.070 -17500.0\n",
522 | "4 2007-03-27 93.320 -0.180 -45000.0\n",
523 | ".. ... ... ... ...\n",
524 | "60 2007-06-15 93.020 -0.125 -31250.0\n",
525 | "61 2007-06-18 92.995 -0.025 -6250.0\n",
526 | "62 2007-06-19 93.010 0.015 3750.0\n",
527 | "63 2007-06-20 93.060 0.050 12500.0\n",
528 | "64 2007-06-21 93.095 0.035 8750.0\n",
529 | "\n",
530 | "[65 rows x 4 columns]"
531 | ]
532 | },
533 | "execution_count": 39,
534 | "metadata": {},
535 | "output_type": "execute_result"
536 | }
537 | ],
538 | "source": [
539 | "df['Daily Gain/Loss']=df['difference']/100 *100*0.25*1000000 ## 3 month LIBOR so that's why 0.25\n",
540 | "df"
541 | ]
542 | },
543 | {
544 | "cell_type": "code",
545 | "execution_count": 40,
546 | "metadata": {},
547 | "outputs": [
548 | {
549 | "data": {
550 | "text/plain": [
551 | "-135000.00000000157"
552 | ]
553 | },
554 | "execution_count": 40,
555 | "metadata": {},
556 | "output_type": "execute_result"
557 | }
558 | ],
559 | "source": [
560 | "df['Daily Gain/Loss'].sum()"
561 | ]
562 | },
563 | {
564 | "cell_type": "code",
565 | "execution_count": 41,
566 | "metadata": {},
567 | "outputs": [],
568 | "source": [
569 | "df['Margin Balance']=None"
570 | ]
571 | },
572 | {
573 | "cell_type": "code",
574 | "execution_count": 42,
575 | "metadata": {},
576 | "outputs": [],
577 | "source": [
578 | "df.iloc[0,4]=148500"
579 | ]
580 | },
581 | {
582 | "cell_type": "code",
583 | "execution_count": 43,
584 | "metadata": {},
585 | "outputs": [
586 | {
587 | "data": {
588 | "text/html": [
589 | "\n",
590 | "\n",
603 | "
\n",
604 | " \n",
605 | " \n",
606 | " | \n",
607 | " Date_1 | \n",
608 | " Prices_1 | \n",
609 | " difference | \n",
610 | " Daily Gain/Loss | \n",
611 | " Margin Balance | \n",
612 | "
\n",
613 | " \n",
614 | " \n",
615 | " \n",
616 | " 0 | \n",
617 | " 2007-03-21 | \n",
618 | " 93.635 | \n",
619 | " NaN | \n",
620 | " NaN | \n",
621 | " 148500 | \n",
622 | "
\n",
623 | " \n",
624 | " 1 | \n",
625 | " 2007-03-22 | \n",
626 | " 93.685 | \n",
627 | " 0.050 | \n",
628 | " 12500.0 | \n",
629 | " None | \n",
630 | "
\n",
631 | " \n",
632 | " 2 | \n",
633 | " 2007-03-23 | \n",
634 | " 93.570 | \n",
635 | " -0.115 | \n",
636 | " -28750.0 | \n",
637 | " None | \n",
638 | "
\n",
639 | " \n",
640 | " 3 | \n",
641 | " 2007-03-26 | \n",
642 | " 93.500 | \n",
643 | " -0.070 | \n",
644 | " -17500.0 | \n",
645 | " None | \n",
646 | "
\n",
647 | " \n",
648 | " 4 | \n",
649 | " 2007-03-27 | \n",
650 | " 93.320 | \n",
651 | " -0.180 | \n",
652 | " -45000.0 | \n",
653 | " None | \n",
654 | "
\n",
655 | " \n",
656 | " ... | \n",
657 | " ... | \n",
658 | " ... | \n",
659 | " ... | \n",
660 | " ... | \n",
661 | " ... | \n",
662 | "
\n",
663 | " \n",
664 | " 60 | \n",
665 | " 2007-06-15 | \n",
666 | " 93.020 | \n",
667 | " -0.125 | \n",
668 | " -31250.0 | \n",
669 | " None | \n",
670 | "
\n",
671 | " \n",
672 | " 61 | \n",
673 | " 2007-06-18 | \n",
674 | " 92.995 | \n",
675 | " -0.025 | \n",
676 | " -6250.0 | \n",
677 | " None | \n",
678 | "
\n",
679 | " \n",
680 | " 62 | \n",
681 | " 2007-06-19 | \n",
682 | " 93.010 | \n",
683 | " 0.015 | \n",
684 | " 3750.0 | \n",
685 | " None | \n",
686 | "
\n",
687 | " \n",
688 | " 63 | \n",
689 | " 2007-06-20 | \n",
690 | " 93.060 | \n",
691 | " 0.050 | \n",
692 | " 12500.0 | \n",
693 | " None | \n",
694 | "
\n",
695 | " \n",
696 | " 64 | \n",
697 | " 2007-06-21 | \n",
698 | " 93.095 | \n",
699 | " 0.035 | \n",
700 | " 8750.0 | \n",
701 | " None | \n",
702 | "
\n",
703 | " \n",
704 | "
\n",
705 | "
65 rows × 5 columns
\n",
706 | "
"
707 | ],
708 | "text/plain": [
709 | " Date_1 Prices_1 difference Daily Gain/Loss Margin Balance\n",
710 | "0 2007-03-21 93.635 NaN NaN 148500\n",
711 | "1 2007-03-22 93.685 0.050 12500.0 None\n",
712 | "2 2007-03-23 93.570 -0.115 -28750.0 None\n",
713 | "3 2007-03-26 93.500 -0.070 -17500.0 None\n",
714 | "4 2007-03-27 93.320 -0.180 -45000.0 None\n",
715 | ".. ... ... ... ... ...\n",
716 | "60 2007-06-15 93.020 -0.125 -31250.0 None\n",
717 | "61 2007-06-18 92.995 -0.025 -6250.0 None\n",
718 | "62 2007-06-19 93.010 0.015 3750.0 None\n",
719 | "63 2007-06-20 93.060 0.050 12500.0 None\n",
720 | "64 2007-06-21 93.095 0.035 8750.0 None\n",
721 | "\n",
722 | "[65 rows x 5 columns]"
723 | ]
724 | },
725 | "execution_count": 43,
726 | "metadata": {},
727 | "output_type": "execute_result"
728 | }
729 | ],
730 | "source": [
731 | "df"
732 | ]
733 | },
734 | {
735 | "cell_type": "code",
736 | "execution_count": 44,
737 | "metadata": {},
738 | "outputs": [],
739 | "source": [
740 | "df1=df.copy()\n",
741 | "df2=df.copy()"
742 | ]
743 | },
744 | {
745 | "cell_type": "code",
746 | "execution_count": 45,
747 | "metadata": {},
748 | "outputs": [
749 | {
750 | "name": "stderr",
751 | "output_type": "stream",
752 | "text": [
753 | "C:\\Users\\adib-\\AppData\\Local\\Temp\\ipykernel_15484\\1135165224.py:2: SettingWithCopyWarning: \n",
754 | "A value is trying to be set on a copy of a slice from a DataFrame\n",
755 | "\n",
756 | "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
757 | " df1['Margin Balance'][i]=df1['Margin Balance'][i-1]+df1['Daily Gain/Loss'][i]\n",
758 | "C:\\Users\\adib-\\AppData\\Local\\Temp\\ipykernel_15484\\1135165224.py:4: SettingWithCopyWarning: \n",
759 | "A value is trying to be set on a copy of a slice from a DataFrame\n",
760 | "\n",
761 | "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
762 | " df1['Margin Balance'][i]=df1['Margin Balance'][i]+(148500-df1['Margin Balance'][i])\n"
763 | ]
764 | }
765 | ],
766 | "source": [
767 | "for i in range(1,len(df1['Margin Balance'])):\n",
768 | " df1['Margin Balance'][i]=df1['Margin Balance'][i-1]+df1['Daily Gain/Loss'][i]\n",
769 | " if df1['Margin Balance'][i]<111000:\n",
770 | " df1['Margin Balance'][i]=df1['Margin Balance'][i]+(148500-df1['Margin Balance'][i])\n",
771 | " \n",
772 | "\n",
773 | "\n"
774 | ]
775 | },
776 | {
777 | "cell_type": "code",
778 | "execution_count": 46,
779 | "metadata": {},
780 | "outputs": [
781 | {
782 | "data": {
783 | "text/html": [
784 | "\n",
785 | "\n",
798 | "
\n",
799 | " \n",
800 | " \n",
801 | " | \n",
802 | " Date_1 | \n",
803 | " Prices_1 | \n",
804 | " difference | \n",
805 | " Daily Gain/Loss | \n",
806 | " Margin Balance | \n",
807 | "
\n",
808 | " \n",
809 | " \n",
810 | " \n",
811 | " 0 | \n",
812 | " 2007-03-21 | \n",
813 | " 93.635 | \n",
814 | " NaN | \n",
815 | " NaN | \n",
816 | " 148500 | \n",
817 | "
\n",
818 | " \n",
819 | " 1 | \n",
820 | " 2007-03-22 | \n",
821 | " 93.685 | \n",
822 | " 0.050 | \n",
823 | " 12500.0 | \n",
824 | " 161000.0 | \n",
825 | "
\n",
826 | " \n",
827 | " 2 | \n",
828 | " 2007-03-23 | \n",
829 | " 93.570 | \n",
830 | " -0.115 | \n",
831 | " -28750.0 | \n",
832 | " 132250.0 | \n",
833 | "
\n",
834 | " \n",
835 | " 3 | \n",
836 | " 2007-03-26 | \n",
837 | " 93.500 | \n",
838 | " -0.070 | \n",
839 | " -17500.0 | \n",
840 | " 114750.0 | \n",
841 | "
\n",
842 | " \n",
843 | " 4 | \n",
844 | " 2007-03-27 | \n",
845 | " 93.320 | \n",
846 | " -0.180 | \n",
847 | " -45000.0 | \n",
848 | " 148500.0 | \n",
849 | "
\n",
850 | " \n",
851 | " ... | \n",
852 | " ... | \n",
853 | " ... | \n",
854 | " ... | \n",
855 | " ... | \n",
856 | " ... | \n",
857 | "
\n",
858 | " \n",
859 | " 60 | \n",
860 | " 2007-06-15 | \n",
861 | " 93.020 | \n",
862 | " -0.125 | \n",
863 | " -31250.0 | \n",
864 | " 197250.0 | \n",
865 | "
\n",
866 | " \n",
867 | " 61 | \n",
868 | " 2007-06-18 | \n",
869 | " 92.995 | \n",
870 | " -0.025 | \n",
871 | " -6250.0 | \n",
872 | " 191000.0 | \n",
873 | "
\n",
874 | " \n",
875 | " 62 | \n",
876 | " 2007-06-19 | \n",
877 | " 93.010 | \n",
878 | " 0.015 | \n",
879 | " 3750.0 | \n",
880 | " 194750.0 | \n",
881 | "
\n",
882 | " \n",
883 | " 63 | \n",
884 | " 2007-06-20 | \n",
885 | " 93.060 | \n",
886 | " 0.050 | \n",
887 | " 12500.0 | \n",
888 | " 207250.0 | \n",
889 | "
\n",
890 | " \n",
891 | " 64 | \n",
892 | " 2007-06-21 | \n",
893 | " 93.095 | \n",
894 | " 0.035 | \n",
895 | " 8750.0 | \n",
896 | " 216000.0 | \n",
897 | "
\n",
898 | " \n",
899 | "
\n",
900 | "
65 rows × 5 columns
\n",
901 | "
"
902 | ],
903 | "text/plain": [
904 | " Date_1 Prices_1 difference Daily Gain/Loss Margin Balance\n",
905 | "0 2007-03-21 93.635 NaN NaN 148500\n",
906 | "1 2007-03-22 93.685 0.050 12500.0 161000.0\n",
907 | "2 2007-03-23 93.570 -0.115 -28750.0 132250.0\n",
908 | "3 2007-03-26 93.500 -0.070 -17500.0 114750.0\n",
909 | "4 2007-03-27 93.320 -0.180 -45000.0 148500.0\n",
910 | ".. ... ... ... ... ...\n",
911 | "60 2007-06-15 93.020 -0.125 -31250.0 197250.0\n",
912 | "61 2007-06-18 92.995 -0.025 -6250.0 191000.0\n",
913 | "62 2007-06-19 93.010 0.015 3750.0 194750.0\n",
914 | "63 2007-06-20 93.060 0.050 12500.0 207250.0\n",
915 | "64 2007-06-21 93.095 0.035 8750.0 216000.0\n",
916 | "\n",
917 | "[65 rows x 5 columns]"
918 | ]
919 | },
920 | "execution_count": 46,
921 | "metadata": {},
922 | "output_type": "execute_result"
923 | }
924 | ],
925 | "source": [
926 | "df1"
927 | ]
928 | },
929 | {
930 | "cell_type": "code",
931 | "execution_count": 47,
932 | "metadata": {},
933 | "outputs": [
934 | {
935 | "data": {
936 | "text/html": [
937 | "\n",
938 | "\n",
951 | "
\n",
952 | " \n",
953 | " \n",
954 | " | \n",
955 | " Date_1 | \n",
956 | " Prices_1 | \n",
957 | " difference | \n",
958 | " Daily Gain/Loss | \n",
959 | " Margin Balance | \n",
960 | " Cash Flow | \n",
961 | "
\n",
962 | " \n",
963 | " \n",
964 | " \n",
965 | " 0 | \n",
966 | " 2007-03-21 | \n",
967 | " 93.635 | \n",
968 | " NaN | \n",
969 | " NaN | \n",
970 | " 148500 | \n",
971 | " NaN | \n",
972 | "
\n",
973 | " \n",
974 | " 1 | \n",
975 | " 2007-03-22 | \n",
976 | " 93.685 | \n",
977 | " 0.050 | \n",
978 | " 12500.0 | \n",
979 | " 161000.0 | \n",
980 | " 12500.0 | \n",
981 | "
\n",
982 | " \n",
983 | " 2 | \n",
984 | " 2007-03-23 | \n",
985 | " 93.570 | \n",
986 | " -0.115 | \n",
987 | " -28750.0 | \n",
988 | " 132250.0 | \n",
989 | " -28750.0 | \n",
990 | "
\n",
991 | " \n",
992 | " 3 | \n",
993 | " 2007-03-26 | \n",
994 | " 93.500 | \n",
995 | " -0.070 | \n",
996 | " -17500.0 | \n",
997 | " 114750.0 | \n",
998 | " -17500.0 | \n",
999 | "
\n",
1000 | " \n",
1001 | " 4 | \n",
1002 | " 2007-03-27 | \n",
1003 | " 93.320 | \n",
1004 | " -0.180 | \n",
1005 | " -45000.0 | \n",
1006 | " 148500.0 | \n",
1007 | " 33750.0 | \n",
1008 | "
\n",
1009 | " \n",
1010 | " ... | \n",
1011 | " ... | \n",
1012 | " ... | \n",
1013 | " ... | \n",
1014 | " ... | \n",
1015 | " ... | \n",
1016 | " ... | \n",
1017 | "
\n",
1018 | " \n",
1019 | " 60 | \n",
1020 | " 2007-06-15 | \n",
1021 | " 93.020 | \n",
1022 | " -0.125 | \n",
1023 | " -31250.0 | \n",
1024 | " 197250.0 | \n",
1025 | " -31250.0 | \n",
1026 | "
\n",
1027 | " \n",
1028 | " 61 | \n",
1029 | " 2007-06-18 | \n",
1030 | " 92.995 | \n",
1031 | " -0.025 | \n",
1032 | " -6250.0 | \n",
1033 | " 191000.0 | \n",
1034 | " -6250.0 | \n",
1035 | "
\n",
1036 | " \n",
1037 | " 62 | \n",
1038 | " 2007-06-19 | \n",
1039 | " 93.010 | \n",
1040 | " 0.015 | \n",
1041 | " 3750.0 | \n",
1042 | " 194750.0 | \n",
1043 | " 3750.0 | \n",
1044 | "
\n",
1045 | " \n",
1046 | " 63 | \n",
1047 | " 2007-06-20 | \n",
1048 | " 93.060 | \n",
1049 | " 0.050 | \n",
1050 | " 12500.0 | \n",
1051 | " 207250.0 | \n",
1052 | " 12500.0 | \n",
1053 | "
\n",
1054 | " \n",
1055 | " 64 | \n",
1056 | " 2007-06-21 | \n",
1057 | " 93.095 | \n",
1058 | " 0.035 | \n",
1059 | " 8750.0 | \n",
1060 | " 216000.0 | \n",
1061 | " 8750.0 | \n",
1062 | "
\n",
1063 | " \n",
1064 | "
\n",
1065 | "
65 rows × 6 columns
\n",
1066 | "
"
1067 | ],
1068 | "text/plain": [
1069 | " Date_1 Prices_1 difference Daily Gain/Loss Margin Balance Cash Flow\n",
1070 | "0 2007-03-21 93.635 NaN NaN 148500 NaN\n",
1071 | "1 2007-03-22 93.685 0.050 12500.0 161000.0 12500.0\n",
1072 | "2 2007-03-23 93.570 -0.115 -28750.0 132250.0 -28750.0\n",
1073 | "3 2007-03-26 93.500 -0.070 -17500.0 114750.0 -17500.0\n",
1074 | "4 2007-03-27 93.320 -0.180 -45000.0 148500.0 33750.0\n",
1075 | ".. ... ... ... ... ... ...\n",
1076 | "60 2007-06-15 93.020 -0.125 -31250.0 197250.0 -31250.0\n",
1077 | "61 2007-06-18 92.995 -0.025 -6250.0 191000.0 -6250.0\n",
1078 | "62 2007-06-19 93.010 0.015 3750.0 194750.0 3750.0\n",
1079 | "63 2007-06-20 93.060 0.050 12500.0 207250.0 12500.0\n",
1080 | "64 2007-06-21 93.095 0.035 8750.0 216000.0 8750.0\n",
1081 | "\n",
1082 | "[65 rows x 6 columns]"
1083 | ]
1084 | },
1085 | "execution_count": 47,
1086 | "metadata": {},
1087 | "output_type": "execute_result"
1088 | }
1089 | ],
1090 | "source": [
1091 | "df1['Cash Flow']=df1['Margin Balance'].diff()\n",
1092 | "df1"
1093 | ]
1094 | },
1095 | {
1096 | "cell_type": "code",
1097 | "execution_count": 48,
1098 | "metadata": {},
1099 | "outputs": [
1100 | {
1101 | "data": {
1102 | "text/html": [
1103 | "\n",
1104 | "\n",
1117 | "
\n",
1118 | " \n",
1119 | " \n",
1120 | " | \n",
1121 | " Date_1 | \n",
1122 | " Prices_1 | \n",
1123 | " difference | \n",
1124 | " Daily Gain/Loss | \n",
1125 | " Margin Balance | \n",
1126 | "
\n",
1127 | " \n",
1128 | " \n",
1129 | " \n",
1130 | " 0 | \n",
1131 | " 2007-03-21 | \n",
1132 | " 93.635 | \n",
1133 | " NaN | \n",
1134 | " NaN | \n",
1135 | " 148500 | \n",
1136 | "
\n",
1137 | " \n",
1138 | " 1 | \n",
1139 | " 2007-03-22 | \n",
1140 | " 93.685 | \n",
1141 | " 0.050 | \n",
1142 | " 12500.0 | \n",
1143 | " None | \n",
1144 | "
\n",
1145 | " \n",
1146 | " 2 | \n",
1147 | " 2007-03-23 | \n",
1148 | " 93.570 | \n",
1149 | " -0.115 | \n",
1150 | " -28750.0 | \n",
1151 | " None | \n",
1152 | "
\n",
1153 | " \n",
1154 | " 3 | \n",
1155 | " 2007-03-26 | \n",
1156 | " 93.500 | \n",
1157 | " -0.070 | \n",
1158 | " -17500.0 | \n",
1159 | " None | \n",
1160 | "
\n",
1161 | " \n",
1162 | " 4 | \n",
1163 | " 2007-03-27 | \n",
1164 | " 93.320 | \n",
1165 | " -0.180 | \n",
1166 | " -45000.0 | \n",
1167 | " None | \n",
1168 | "
\n",
1169 | " \n",
1170 | " ... | \n",
1171 | " ... | \n",
1172 | " ... | \n",
1173 | " ... | \n",
1174 | " ... | \n",
1175 | " ... | \n",
1176 | "
\n",
1177 | " \n",
1178 | " 60 | \n",
1179 | " 2007-06-15 | \n",
1180 | " 93.020 | \n",
1181 | " -0.125 | \n",
1182 | " -31250.0 | \n",
1183 | " None | \n",
1184 | "
\n",
1185 | " \n",
1186 | " 61 | \n",
1187 | " 2007-06-18 | \n",
1188 | " 92.995 | \n",
1189 | " -0.025 | \n",
1190 | " -6250.0 | \n",
1191 | " None | \n",
1192 | "
\n",
1193 | " \n",
1194 | " 62 | \n",
1195 | " 2007-06-19 | \n",
1196 | " 93.010 | \n",
1197 | " 0.015 | \n",
1198 | " 3750.0 | \n",
1199 | " None | \n",
1200 | "
\n",
1201 | " \n",
1202 | " 63 | \n",
1203 | " 2007-06-20 | \n",
1204 | " 93.060 | \n",
1205 | " 0.050 | \n",
1206 | " 12500.0 | \n",
1207 | " None | \n",
1208 | "
\n",
1209 | " \n",
1210 | " 64 | \n",
1211 | " 2007-06-21 | \n",
1212 | " 93.095 | \n",
1213 | " 0.035 | \n",
1214 | " 8750.0 | \n",
1215 | " None | \n",
1216 | "
\n",
1217 | " \n",
1218 | "
\n",
1219 | "
65 rows × 5 columns
\n",
1220 | "
"
1221 | ],
1222 | "text/plain": [
1223 | " Date_1 Prices_1 difference Daily Gain/Loss Margin Balance\n",
1224 | "0 2007-03-21 93.635 NaN NaN 148500\n",
1225 | "1 2007-03-22 93.685 0.050 12500.0 None\n",
1226 | "2 2007-03-23 93.570 -0.115 -28750.0 None\n",
1227 | "3 2007-03-26 93.500 -0.070 -17500.0 None\n",
1228 | "4 2007-03-27 93.320 -0.180 -45000.0 None\n",
1229 | ".. ... ... ... ... ...\n",
1230 | "60 2007-06-15 93.020 -0.125 -31250.0 None\n",
1231 | "61 2007-06-18 92.995 -0.025 -6250.0 None\n",
1232 | "62 2007-06-19 93.010 0.015 3750.0 None\n",
1233 | "63 2007-06-20 93.060 0.050 12500.0 None\n",
1234 | "64 2007-06-21 93.095 0.035 8750.0 None\n",
1235 | "\n",
1236 | "[65 rows x 5 columns]"
1237 | ]
1238 | },
1239 | "execution_count": 48,
1240 | "metadata": {},
1241 | "output_type": "execute_result"
1242 | }
1243 | ],
1244 | "source": [
1245 | "## Starting Second Question\n",
1246 | "df2"
1247 | ]
1248 | },
1249 | {
1250 | "cell_type": "code",
1251 | "execution_count": 49,
1252 | "metadata": {},
1253 | "outputs": [
1254 | {
1255 | "name": "stderr",
1256 | "output_type": "stream",
1257 | "text": [
1258 | "C:\\Users\\adib-\\AppData\\Local\\Temp\\ipykernel_15484\\674684878.py:9: SettingWithCopyWarning: \n",
1259 | "A value is trying to be set on a copy of a slice from a DataFrame\n",
1260 | "\n",
1261 | "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
1262 | " df2['Margin Balance'][i]=df2['Margin Balance'][i-1]\n",
1263 | "C:\\Users\\adib-\\AppData\\Local\\Temp\\ipykernel_15484\\674684878.py:7: SettingWithCopyWarning: \n",
1264 | "A value is trying to be set on a copy of a slice from a DataFrame\n",
1265 | "\n",
1266 | "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
1267 | " df2['Margin Balance'][i]=df2['Margin Balance'][i-1]+df2['Daily Gain/Loss'][i]\n",
1268 | "C:\\Users\\adib-\\AppData\\Local\\Temp\\ipykernel_15484\\674684878.py:11: SettingWithCopyWarning: \n",
1269 | "A value is trying to be set on a copy of a slice from a DataFrame\n",
1270 | "\n",
1271 | "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
1272 | " df2['Margin Balance'][i]=df2['Margin Balance'][i]+(148500-df2['Margin Balance'][i])\n"
1273 | ]
1274 | }
1275 | ],
1276 | "source": [
1277 | "# Same loop different condition\n",
1278 | "## If he decides to take every profit out then the margin balance will experience margin calls more frequently-----\n",
1279 | "\n",
1280 | "\n",
1281 | "for i in range(1,len(df2['Margin Balance'])):\n",
1282 | " if df2['Daily Gain/Loss'][i]<0:\n",
1283 | " df2['Margin Balance'][i]=df2['Margin Balance'][i-1]+df2['Daily Gain/Loss'][i]\n",
1284 | " else:\n",
1285 | " df2['Margin Balance'][i]=df2['Margin Balance'][i-1]\n",
1286 | " if df2['Margin Balance'][i]<111000:\n",
1287 | " df2['Margin Balance'][i]=df2['Margin Balance'][i]+(148500-df2['Margin Balance'][i])\n",
1288 | " "
1289 | ]
1290 | },
1291 | {
1292 | "cell_type": "code",
1293 | "execution_count": 51,
1294 | "metadata": {},
1295 | "outputs": [
1296 | {
1297 | "data": {
1298 | "text/html": [
1299 | "\n",
1300 | "\n",
1313 | "
\n",
1314 | " \n",
1315 | " \n",
1316 | " | \n",
1317 | " Date_1 | \n",
1318 | " Prices_1 | \n",
1319 | " difference | \n",
1320 | " Daily Gain/Loss | \n",
1321 | " Margin Balance | \n",
1322 | "
\n",
1323 | " \n",
1324 | " \n",
1325 | " \n",
1326 | " 0 | \n",
1327 | " 2007-03-21 | \n",
1328 | " 93.635 | \n",
1329 | " NaN | \n",
1330 | " NaN | \n",
1331 | " 148500 | \n",
1332 | "
\n",
1333 | " \n",
1334 | " 1 | \n",
1335 | " 2007-03-22 | \n",
1336 | " 93.685 | \n",
1337 | " 0.050 | \n",
1338 | " 12500.0 | \n",
1339 | " 148500 | \n",
1340 | "
\n",
1341 | " \n",
1342 | " 2 | \n",
1343 | " 2007-03-23 | \n",
1344 | " 93.570 | \n",
1345 | " -0.115 | \n",
1346 | " -28750.0 | \n",
1347 | " 119750.0 | \n",
1348 | "
\n",
1349 | " \n",
1350 | " 3 | \n",
1351 | " 2007-03-26 | \n",
1352 | " 93.500 | \n",
1353 | " -0.070 | \n",
1354 | " -17500.0 | \n",
1355 | " 148500.0 | \n",
1356 | "
\n",
1357 | " \n",
1358 | " 4 | \n",
1359 | " 2007-03-27 | \n",
1360 | " 93.320 | \n",
1361 | " -0.180 | \n",
1362 | " -45000.0 | \n",
1363 | " 148500.0 | \n",
1364 | "
\n",
1365 | " \n",
1366 | " ... | \n",
1367 | " ... | \n",
1368 | " ... | \n",
1369 | " ... | \n",
1370 | " ... | \n",
1371 | " ... | \n",
1372 | "
\n",
1373 | " \n",
1374 | " 60 | \n",
1375 | " 2007-06-15 | \n",
1376 | " 93.020 | \n",
1377 | " -0.125 | \n",
1378 | " -31250.0 | \n",
1379 | " 148500.0 | \n",
1380 | "
\n",
1381 | " \n",
1382 | " 61 | \n",
1383 | " 2007-06-18 | \n",
1384 | " 92.995 | \n",
1385 | " -0.025 | \n",
1386 | " -6250.0 | \n",
1387 | " 142250.0 | \n",
1388 | "
\n",
1389 | " \n",
1390 | " 62 | \n",
1391 | " 2007-06-19 | \n",
1392 | " 93.010 | \n",
1393 | " 0.015 | \n",
1394 | " 3750.0 | \n",
1395 | " 142250.0 | \n",
1396 | "
\n",
1397 | " \n",
1398 | " 63 | \n",
1399 | " 2007-06-20 | \n",
1400 | " 93.060 | \n",
1401 | " 0.050 | \n",
1402 | " 12500.0 | \n",
1403 | " 142250.0 | \n",
1404 | "
\n",
1405 | " \n",
1406 | " 64 | \n",
1407 | " 2007-06-21 | \n",
1408 | " 93.095 | \n",
1409 | " 0.035 | \n",
1410 | " 8750.0 | \n",
1411 | " 142250.0 | \n",
1412 | "
\n",
1413 | " \n",
1414 | "
\n",
1415 | "
65 rows × 5 columns
\n",
1416 | "
"
1417 | ],
1418 | "text/plain": [
1419 | " Date_1 Prices_1 difference Daily Gain/Loss Margin Balance\n",
1420 | "0 2007-03-21 93.635 NaN NaN 148500\n",
1421 | "1 2007-03-22 93.685 0.050 12500.0 148500\n",
1422 | "2 2007-03-23 93.570 -0.115 -28750.0 119750.0\n",
1423 | "3 2007-03-26 93.500 -0.070 -17500.0 148500.0\n",
1424 | "4 2007-03-27 93.320 -0.180 -45000.0 148500.0\n",
1425 | ".. ... ... ... ... ...\n",
1426 | "60 2007-06-15 93.020 -0.125 -31250.0 148500.0\n",
1427 | "61 2007-06-18 92.995 -0.025 -6250.0 142250.0\n",
1428 | "62 2007-06-19 93.010 0.015 3750.0 142250.0\n",
1429 | "63 2007-06-20 93.060 0.050 12500.0 142250.0\n",
1430 | "64 2007-06-21 93.095 0.035 8750.0 142250.0\n",
1431 | "\n",
1432 | "[65 rows x 5 columns]"
1433 | ]
1434 | },
1435 | "execution_count": 51,
1436 | "metadata": {},
1437 | "output_type": "execute_result"
1438 | }
1439 | ],
1440 | "source": [
1441 | "df2"
1442 | ]
1443 | },
1444 | {
1445 | "cell_type": "code",
1446 | "execution_count": 58,
1447 | "metadata": {},
1448 | "outputs": [
1449 | {
1450 | "data": {
1451 | "text/html": [
1452 | "\n",
1453 | "\n",
1466 | "
\n",
1467 | " \n",
1468 | " \n",
1469 | " | \n",
1470 | " Date_1 | \n",
1471 | " Prices_1 | \n",
1472 | " difference | \n",
1473 | " Daily Gain/Loss | \n",
1474 | " Margin Balance | \n",
1475 | " Cash Flow | \n",
1476 | "
\n",
1477 | " \n",
1478 | " \n",
1479 | " \n",
1480 | " 0 | \n",
1481 | " 2007-03-21 | \n",
1482 | " 93.635 | \n",
1483 | " NaN | \n",
1484 | " NaN | \n",
1485 | " 148500 | \n",
1486 | " NaN | \n",
1487 | "
\n",
1488 | " \n",
1489 | " 1 | \n",
1490 | " 2007-03-22 | \n",
1491 | " 93.685 | \n",
1492 | " 0.050 | \n",
1493 | " 12500.0 | \n",
1494 | " 148500 | \n",
1495 | " 0 | \n",
1496 | "
\n",
1497 | " \n",
1498 | " 2 | \n",
1499 | " 2007-03-23 | \n",
1500 | " 93.570 | \n",
1501 | " -0.115 | \n",
1502 | " -28750.0 | \n",
1503 | " 119750.0 | \n",
1504 | " -28750.0 | \n",
1505 | "
\n",
1506 | " \n",
1507 | " 3 | \n",
1508 | " 2007-03-26 | \n",
1509 | " 93.500 | \n",
1510 | " -0.070 | \n",
1511 | " -17500.0 | \n",
1512 | " 148500.0 | \n",
1513 | " 28750.0 | \n",
1514 | "
\n",
1515 | " \n",
1516 | " 4 | \n",
1517 | " 2007-03-27 | \n",
1518 | " 93.320 | \n",
1519 | " -0.180 | \n",
1520 | " -45000.0 | \n",
1521 | " 148500.0 | \n",
1522 | " 0.0 | \n",
1523 | "
\n",
1524 | " \n",
1525 | " ... | \n",
1526 | " ... | \n",
1527 | " ... | \n",
1528 | " ... | \n",
1529 | " ... | \n",
1530 | " ... | \n",
1531 | " ... | \n",
1532 | "
\n",
1533 | " \n",
1534 | " 60 | \n",
1535 | " 2007-06-15 | \n",
1536 | " 93.020 | \n",
1537 | " -0.125 | \n",
1538 | " -31250.0 | \n",
1539 | " 148500.0 | \n",
1540 | " 10000.0 | \n",
1541 | "
\n",
1542 | " \n",
1543 | " 61 | \n",
1544 | " 2007-06-18 | \n",
1545 | " 92.995 | \n",
1546 | " -0.025 | \n",
1547 | " -6250.0 | \n",
1548 | " 142250.0 | \n",
1549 | " -6250.0 | \n",
1550 | "
\n",
1551 | " \n",
1552 | " 62 | \n",
1553 | " 2007-06-19 | \n",
1554 | " 93.010 | \n",
1555 | " 0.015 | \n",
1556 | " 3750.0 | \n",
1557 | " 142250.0 | \n",
1558 | " 0.0 | \n",
1559 | "
\n",
1560 | " \n",
1561 | " 63 | \n",
1562 | " 2007-06-20 | \n",
1563 | " 93.060 | \n",
1564 | " 0.050 | \n",
1565 | " 12500.0 | \n",
1566 | " 142250.0 | \n",
1567 | " 0.0 | \n",
1568 | "
\n",
1569 | " \n",
1570 | " 64 | \n",
1571 | " 2007-06-21 | \n",
1572 | " 93.095 | \n",
1573 | " 0.035 | \n",
1574 | " 8750.0 | \n",
1575 | " 142250.0 | \n",
1576 | " 0.0 | \n",
1577 | "
\n",
1578 | " \n",
1579 | "
\n",
1580 | "
65 rows × 6 columns
\n",
1581 | "
"
1582 | ],
1583 | "text/plain": [
1584 | " Date_1 Prices_1 difference Daily Gain/Loss Margin Balance Cash Flow\n",
1585 | "0 2007-03-21 93.635 NaN NaN 148500 NaN\n",
1586 | "1 2007-03-22 93.685 0.050 12500.0 148500 0\n",
1587 | "2 2007-03-23 93.570 -0.115 -28750.0 119750.0 -28750.0\n",
1588 | "3 2007-03-26 93.500 -0.070 -17500.0 148500.0 28750.0\n",
1589 | "4 2007-03-27 93.320 -0.180 -45000.0 148500.0 0.0\n",
1590 | ".. ... ... ... ... ... ...\n",
1591 | "60 2007-06-15 93.020 -0.125 -31250.0 148500.0 10000.0\n",
1592 | "61 2007-06-18 92.995 -0.025 -6250.0 142250.0 -6250.0\n",
1593 | "62 2007-06-19 93.010 0.015 3750.0 142250.0 0.0\n",
1594 | "63 2007-06-20 93.060 0.050 12500.0 142250.0 0.0\n",
1595 | "64 2007-06-21 93.095 0.035 8750.0 142250.0 0.0\n",
1596 | "\n",
1597 | "[65 rows x 6 columns]"
1598 | ]
1599 | },
1600 | "execution_count": 58,
1601 | "metadata": {},
1602 | "output_type": "execute_result"
1603 | }
1604 | ],
1605 | "source": [
1606 | "df2['Cash Flow']=df2['Margin Balance'].diff()\n",
1607 | "df2"
1608 | ]
1609 | },
1610 | {
1611 | "cell_type": "code",
1612 | "execution_count": 54,
1613 | "metadata": {},
1614 | "outputs": [
1615 | {
1616 | "data": {
1617 | "text/html": [
1618 | "\n",
1619 | "\n",
1632 | "
\n",
1633 | " \n",
1634 | " \n",
1635 | " | \n",
1636 | " Date_1 | \n",
1637 | " Prices_1 | \n",
1638 | " difference | \n",
1639 | " Daily Gain/Loss | \n",
1640 | " Margin Balance | \n",
1641 | "
\n",
1642 | " \n",
1643 | " \n",
1644 | " \n",
1645 | " 0 | \n",
1646 | " 2007-03-21 | \n",
1647 | " 93.635 | \n",
1648 | " NaN | \n",
1649 | " NaN | \n",
1650 | " 148500 | \n",
1651 | "
\n",
1652 | " \n",
1653 | " 1 | \n",
1654 | " 2007-03-22 | \n",
1655 | " 93.685 | \n",
1656 | " 0.050 | \n",
1657 | " 12500.0 | \n",
1658 | " 148500 | \n",
1659 | "
\n",
1660 | " \n",
1661 | " 2 | \n",
1662 | " 2007-03-23 | \n",
1663 | " 93.570 | \n",
1664 | " -0.115 | \n",
1665 | " -28750.0 | \n",
1666 | " 119750.0 | \n",
1667 | "
\n",
1668 | " \n",
1669 | " 3 | \n",
1670 | " 2007-03-26 | \n",
1671 | " 93.500 | \n",
1672 | " -0.070 | \n",
1673 | " -17500.0 | \n",
1674 | " 148500.0 | \n",
1675 | "
\n",
1676 | " \n",
1677 | " 4 | \n",
1678 | " 2007-03-27 | \n",
1679 | " 93.320 | \n",
1680 | " -0.180 | \n",
1681 | " -45000.0 | \n",
1682 | " 148500.0 | \n",
1683 | "
\n",
1684 | " \n",
1685 | " ... | \n",
1686 | " ... | \n",
1687 | " ... | \n",
1688 | " ... | \n",
1689 | " ... | \n",
1690 | " ... | \n",
1691 | "
\n",
1692 | " \n",
1693 | " 60 | \n",
1694 | " 2007-06-15 | \n",
1695 | " 93.020 | \n",
1696 | " -0.125 | \n",
1697 | " -31250.0 | \n",
1698 | " 148500.0 | \n",
1699 | "
\n",
1700 | " \n",
1701 | " 61 | \n",
1702 | " 2007-06-18 | \n",
1703 | " 92.995 | \n",
1704 | " -0.025 | \n",
1705 | " -6250.0 | \n",
1706 | " 142250.0 | \n",
1707 | "
\n",
1708 | " \n",
1709 | " 62 | \n",
1710 | " 2007-06-19 | \n",
1711 | " 93.010 | \n",
1712 | " 0.015 | \n",
1713 | " 3750.0 | \n",
1714 | " 142250.0 | \n",
1715 | "
\n",
1716 | " \n",
1717 | " 63 | \n",
1718 | " 2007-06-20 | \n",
1719 | " 93.060 | \n",
1720 | " 0.050 | \n",
1721 | " 12500.0 | \n",
1722 | " 142250.0 | \n",
1723 | "
\n",
1724 | " \n",
1725 | " 64 | \n",
1726 | " 2007-06-21 | \n",
1727 | " 93.095 | \n",
1728 | " 0.035 | \n",
1729 | " 8750.0 | \n",
1730 | " 142250.0 | \n",
1731 | "
\n",
1732 | " \n",
1733 | "
\n",
1734 | "
65 rows × 5 columns
\n",
1735 | "
"
1736 | ],
1737 | "text/plain": [
1738 | " Date_1 Prices_1 difference Daily Gain/Loss Margin Balance\n",
1739 | "0 2007-03-21 93.635 NaN NaN 148500\n",
1740 | "1 2007-03-22 93.685 0.050 12500.0 148500\n",
1741 | "2 2007-03-23 93.570 -0.115 -28750.0 119750.0\n",
1742 | "3 2007-03-26 93.500 -0.070 -17500.0 148500.0\n",
1743 | "4 2007-03-27 93.320 -0.180 -45000.0 148500.0\n",
1744 | ".. ... ... ... ... ...\n",
1745 | "60 2007-06-15 93.020 -0.125 -31250.0 148500.0\n",
1746 | "61 2007-06-18 92.995 -0.025 -6250.0 142250.0\n",
1747 | "62 2007-06-19 93.010 0.015 3750.0 142250.0\n",
1748 | "63 2007-06-20 93.060 0.050 12500.0 142250.0\n",
1749 | "64 2007-06-21 93.095 0.035 8750.0 142250.0\n",
1750 | "\n",
1751 | "[65 rows x 5 columns]"
1752 | ]
1753 | },
1754 | "execution_count": 54,
1755 | "metadata": {},
1756 | "output_type": "execute_result"
1757 | }
1758 | ],
1759 | "source": [
1760 | "df2"
1761 | ]
1762 | },
1763 | {
1764 | "cell_type": "code",
1765 | "execution_count": null,
1766 | "metadata": {},
1767 | "outputs": [],
1768 | "source": []
1769 | }
1770 | ],
1771 | "metadata": {
1772 | "kernelspec": {
1773 | "display_name": "base",
1774 | "language": "python",
1775 | "name": "python3"
1776 | },
1777 | "language_info": {
1778 | "codemirror_mode": {
1779 | "name": "ipython",
1780 | "version": 3
1781 | },
1782 | "file_extension": ".py",
1783 | "mimetype": "text/x-python",
1784 | "name": "python",
1785 | "nbconvert_exporter": "python",
1786 | "pygments_lexer": "ipython3",
1787 | "version": "3.9.15"
1788 | },
1789 | "orig_nbformat": 4
1790 | },
1791 | "nbformat": 4,
1792 | "nbformat_minor": 2
1793 | }
1794 |
--------------------------------------------------------------------------------
/Risk Management/README.md:
--------------------------------------------------------------------------------
1 | ## In this folder, I will be putting all short miscellaneous projects in Risk Management.
2 |
--------------------------------------------------------------------------------
/Risk Management/Var Exercise-Monte Carlo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## VaR Monte Carlo with as many Tickers symbols as you want."
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 111,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import pandas as pd\n",
17 | "import numpy as np\n",
18 | "import datetime as dt\n",
19 | "import yfinance as yf\n",
20 | "import matplotlib.pyplot as plt\n",
21 | "from scipy.stats import norm"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": 112,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "years=15\n",
31 | "\n",
32 | "endDate=dt.datetime.now()\n",
33 | "startDate=endDate-dt.timedelta(days=365*years)"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": 113,
39 | "metadata": {},
40 | "outputs": [],
41 | "source": [
42 | "tickers=['TSLA','GOOGL','QQQ','VTI','AMD','AMZN'] ## Put as many as tickers as you want"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": 114,
48 | "metadata": {},
49 | "outputs": [],
50 | "source": [
51 | "## Next get adjusted closing price\n",
52 | "\n",
53 | "adj_close_df=pd.DataFrame()"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": 115,
59 | "metadata": {},
60 | "outputs": [
61 | {
62 | "name": "stdout",
63 | "output_type": "stream",
64 | "text": [
65 | "[*********************100%***********************] 1 of 1 completed\n",
66 | "[*********************100%***********************] 1 of 1 completed\n",
67 | "[*********************100%***********************] 1 of 1 completed\n",
68 | "[*********************100%***********************] 1 of 1 completed\n",
69 | "[*********************100%***********************] 1 of 1 completed\n",
70 | "[*********************100%***********************] 1 of 1 completed\n"
71 | ]
72 | }
73 | ],
74 | "source": [
75 | "for i in tickers:\n",
76 | " data=yf.download(i,start=startDate,end=endDate)\n",
77 | " adj_close_df[i]=data['Adj Close']"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 116,
83 | "metadata": {},
84 | "outputs": [],
85 | "source": [
86 | "## We will get the log returns because those are consistent in many aspect than just returns. Secondly, take the first difference as they are not stationary.\n",
87 | "log_returns=np.log(adj_close_df/adj_close_df.shift(1))\n"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": 117,
93 | "metadata": {},
94 | "outputs": [],
95 | "source": [
96 | "## Removing the first row because after the difference one gets NaN\n",
97 | "log_returns=log_returns.dropna()"
98 | ]
99 | },
100 | {
101 | "cell_type": "code",
102 | "execution_count": 118,
103 | "metadata": {},
104 | "outputs": [
105 | {
106 | "data": {
107 | "text/html": [
108 | "\n",
109 | "\n",
122 | "
\n",
123 | " \n",
124 | " \n",
125 | " | \n",
126 | " TSLA | \n",
127 | " GOOGL | \n",
128 | " QQQ | \n",
129 | " VTI | \n",
130 | " AMD | \n",
131 | " AMZN | \n",
132 | "
\n",
133 | " \n",
134 | " Date | \n",
135 | " | \n",
136 | " | \n",
137 | " | \n",
138 | " | \n",
139 | " | \n",
140 | " | \n",
141 | "
\n",
142 | " \n",
143 | " \n",
144 | " \n",
145 | " 2010-06-30 | \n",
146 | " -0.002515 | \n",
147 | " -0.020708 | \n",
148 | " -0.015335 | \n",
149 | " -0.010974 | \n",
150 | " -0.021622 | \n",
151 | " 0.005967 | \n",
152 | "
\n",
153 | " \n",
154 | " 2010-07-01 | \n",
155 | " -0.081723 | \n",
156 | " -0.012347 | \n",
157 | " -0.002813 | \n",
158 | " -0.004386 | \n",
159 | " 0.009517 | \n",
160 | " 0.015439 | \n",
161 | "
\n",
162 | " \n",
163 | " 2010-07-02 | \n",
164 | " -0.134312 | \n",
165 | " -0.006712 | \n",
166 | " -0.002822 | \n",
167 | " -0.004789 | \n",
168 | " -0.030222 | \n",
169 | " -0.016538 | \n",
170 | "
\n",
171 | " \n",
172 | " 2010-07-06 | \n",
173 | " -0.175470 | \n",
174 | " -0.001100 | \n",
175 | " 0.003056 | \n",
176 | " 0.003067 | \n",
177 | " -0.018298 | \n",
178 | " 0.008394 | \n",
179 | "
\n",
180 | " \n",
181 | " 2010-07-07 | \n",
182 | " -0.019431 | \n",
183 | " 0.031889 | \n",
184 | " 0.031426 | \n",
185 | " 0.032024 | \n",
186 | " 0.048520 | \n",
187 | " 0.030160 | \n",
188 | "
\n",
189 | " \n",
190 | " ... | \n",
191 | " ... | \n",
192 | " ... | \n",
193 | " ... | \n",
194 | " ... | \n",
195 | " ... | \n",
196 | " ... | \n",
197 | "
\n",
198 | " \n",
199 | " 2023-09-25 | \n",
200 | " 0.008580 | \n",
201 | " 0.006581 | \n",
202 | " 0.004739 | \n",
203 | " 0.004017 | \n",
204 | " 0.012191 | \n",
205 | " 0.016514 | \n",
206 | "
\n",
207 | " \n",
208 | " 2023-09-26 | \n",
209 | " -0.011688 | \n",
210 | " -0.019563 | \n",
211 | " -0.015130 | \n",
212 | " -0.014510 | \n",
213 | " -0.014689 | \n",
214 | " -0.041133 | \n",
215 | "
\n",
216 | " \n",
217 | " 2023-09-27 | \n",
218 | " -0.014940 | \n",
219 | " 0.015206 | \n",
220 | " 0.002341 | \n",
221 | " 0.001040 | \n",
222 | " 0.021750 | \n",
223 | " 0.000000 | \n",
224 | "
\n",
225 | " \n",
226 | " 2023-09-28 | \n",
227 | " 0.024155 | \n",
228 | " 0.013468 | \n",
229 | " 0.008330 | \n",
230 | " 0.006594 | \n",
231 | " 0.046715 | \n",
232 | " 0.000000 | \n",
233 | "
\n",
234 | " \n",
235 | " 2023-09-29 | \n",
236 | " 0.015465 | \n",
237 | " -0.011020 | \n",
238 | " 0.000726 | \n",
239 | " -0.002915 | \n",
240 | " 0.000584 | \n",
241 | " 0.009008 | \n",
242 | "
\n",
243 | " \n",
244 | "
\n",
245 | "
3336 rows × 6 columns
\n",
246 | "
"
247 | ],
248 | "text/plain": [
249 | " TSLA GOOGL QQQ VTI AMD AMZN\n",
250 | "Date \n",
251 | "2010-06-30 -0.002515 -0.020708 -0.015335 -0.010974 -0.021622 0.005967\n",
252 | "2010-07-01 -0.081723 -0.012347 -0.002813 -0.004386 0.009517 0.015439\n",
253 | "2010-07-02 -0.134312 -0.006712 -0.002822 -0.004789 -0.030222 -0.016538\n",
254 | "2010-07-06 -0.175470 -0.001100 0.003056 0.003067 -0.018298 0.008394\n",
255 | "2010-07-07 -0.019431 0.031889 0.031426 0.032024 0.048520 0.030160\n",
256 | "... ... ... ... ... ... ...\n",
257 | "2023-09-25 0.008580 0.006581 0.004739 0.004017 0.012191 0.016514\n",
258 | "2023-09-26 -0.011688 -0.019563 -0.015130 -0.014510 -0.014689 -0.041133\n",
259 | "2023-09-27 -0.014940 0.015206 0.002341 0.001040 0.021750 0.000000\n",
260 | "2023-09-28 0.024155 0.013468 0.008330 0.006594 0.046715 0.000000\n",
261 | "2023-09-29 0.015465 -0.011020 0.000726 -0.002915 0.000584 0.009008\n",
262 | "\n",
263 | "[3336 rows x 6 columns]"
264 | ]
265 | },
266 | "execution_count": 118,
267 | "metadata": {},
268 | "output_type": "execute_result"
269 | }
270 | ],
271 | "source": [
272 | "log_returns"
273 | ]
274 | },
275 | {
276 | "cell_type": "code",
277 | "execution_count": 119,
278 | "metadata": {},
279 | "outputs": [],
280 | "source": [
281 | "def expected_return(weights,log_returns):\n",
282 | " return np.sum(log_returns.mean()*weights)"
283 | ]
284 | },
285 | {
286 | "cell_type": "code",
287 | "execution_count": 120,
288 | "metadata": {},
289 | "outputs": [],
290 | "source": [
291 | "def standard_deviation(weights,cov_matrix):\n",
292 | " variance=weights.T@cov_matrix@weights\n",
293 | " return np.sqrt(variance)\n"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": 121,
299 | "metadata": {},
300 | "outputs": [
301 | {
302 | "data": {
303 | "text/html": [
304 | "\n",
305 | "\n",
318 | "
\n",
319 | " \n",
320 | " \n",
321 | " | \n",
322 | " TSLA | \n",
323 | " GOOGL | \n",
324 | " QQQ | \n",
325 | " VTI | \n",
326 | " AMD | \n",
327 | " AMZN | \n",
328 | "
\n",
329 | " \n",
330 | " \n",
331 | " \n",
332 | " TSLA | \n",
333 | " 0.001285 | \n",
334 | " 0.000209 | \n",
335 | " 0.000235 | \n",
336 | " 0.000180 | \n",
337 | " 0.000399 | \n",
338 | " 0.000266 | \n",
339 | "
\n",
340 | " \n",
341 | " GOOGL | \n",
342 | " 0.000209 | \n",
343 | " 0.000292 | \n",
344 | " 0.000174 | \n",
345 | " 0.000134 | \n",
346 | " 0.000219 | \n",
347 | " 0.000214 | \n",
348 | "
\n",
349 | " \n",
350 | " QQQ | \n",
351 | " 0.000235 | \n",
352 | " 0.000174 | \n",
353 | " 0.000170 | \n",
354 | " 0.000135 | \n",
355 | " 0.000251 | \n",
356 | " 0.000193 | \n",
357 | "
\n",
358 | " \n",
359 | " VTI | \n",
360 | " 0.000180 | \n",
361 | " 0.000134 | \n",
362 | " 0.000135 | \n",
363 | " 0.000125 | \n",
364 | " 0.000202 | \n",
365 | " 0.000138 | \n",
366 | "
\n",
367 | " \n",
368 | " AMD | \n",
369 | " 0.000399 | \n",
370 | " 0.000219 | \n",
371 | " 0.000251 | \n",
372 | " 0.000202 | \n",
373 | " 0.001247 | \n",
374 | " 0.000274 | \n",
375 | "
\n",
376 | " \n",
377 | " AMZN | \n",
378 | " 0.000266 | \n",
379 | " 0.000214 | \n",
380 | " 0.000193 | \n",
381 | " 0.000138 | \n",
382 | " 0.000274 | \n",
383 | " 0.000430 | \n",
384 | "
\n",
385 | " \n",
386 | "
\n",
387 | "
"
388 | ],
389 | "text/plain": [
390 | " TSLA GOOGL QQQ VTI AMD AMZN\n",
391 | "TSLA 0.001285 0.000209 0.000235 0.000180 0.000399 0.000266\n",
392 | "GOOGL 0.000209 0.000292 0.000174 0.000134 0.000219 0.000214\n",
393 | "QQQ 0.000235 0.000174 0.000170 0.000135 0.000251 0.000193\n",
394 | "VTI 0.000180 0.000134 0.000135 0.000125 0.000202 0.000138\n",
395 | "AMD 0.000399 0.000219 0.000251 0.000202 0.001247 0.000274\n",
396 | "AMZN 0.000266 0.000214 0.000193 0.000138 0.000274 0.000430"
397 | ]
398 | },
399 | "execution_count": 121,
400 | "metadata": {},
401 | "output_type": "execute_result"
402 | }
403 | ],
404 | "source": [
405 | "cov_matrix=log_returns.cov()\n",
406 | "cov_matrix"
407 | ]
408 | },
409 | {
410 | "cell_type": "code",
411 | "execution_count": 122,
412 | "metadata": {},
413 | "outputs": [
414 | {
415 | "data": {
416 | "text/plain": [
417 | "array([0.16666667, 0.16666667, 0.16666667, 0.16666667, 0.16666667,\n",
418 | " 0.16666667])"
419 | ]
420 | },
421 | "execution_count": 122,
422 | "metadata": {},
423 | "output_type": "execute_result"
424 | }
425 | ],
426 | "source": [
427 | "## Create Equally weighted portfolio\n",
428 | "\n",
429 | "portfolio_value=1000000\n",
430 | "weights=np.array([1/len(tickers)]*len(tickers))\n",
431 | "weights"
432 | ]
433 | },
434 | {
435 | "cell_type": "code",
436 | "execution_count": 123,
437 | "metadata": {},
438 | "outputs": [],
439 | "source": [
440 | "portfolio_expected_return=expected_return(weights,log_returns)\n",
441 | "portfolio_standard_deviation=standard_deviation(weights,cov_matrix)"
442 | ]
443 | },
444 | {
445 | "cell_type": "code",
446 | "execution_count": 124,
447 | "metadata": {},
448 | "outputs": [
449 | {
450 | "data": {
451 | "text/plain": [
452 | "0.0166627391905645"
453 | ]
454 | },
455 | "execution_count": 124,
456 | "metadata": {},
457 | "output_type": "execute_result"
458 | }
459 | ],
460 | "source": [
461 | "portfolio_standard_deviation"
462 | ]
463 | },
464 | {
465 | "cell_type": "code",
466 | "execution_count": 125,
467 | "metadata": {},
468 | "outputs": [],
469 | "source": [
470 | "def rand_z_score():\n",
471 | " return np.random.normal(0,1)"
472 | ]
473 | },
474 | {
475 | "cell_type": "code",
476 | "execution_count": 126,
477 | "metadata": {},
478 | "outputs": [],
479 | "source": [
480 | "days=20\n",
481 | "\n",
482 | "def scenario_loss(portfolio_value,portfolio_standard_deviation,z_score,days):\n",
483 | " return portfolio_value*portfolio_expected_return*days+portfolio_value*portfolio_standard_deviation*z_score*np.sqrt(days)"
484 | ]
485 | },
486 | {
487 | "cell_type": "code",
488 | "execution_count": 127,
489 | "metadata": {},
490 | "outputs": [],
491 | "source": [
492 | "##Simulation\n",
493 | "\n",
494 | "n_iter=10000\n",
495 | "scenario_return=[]\n",
496 | "\n",
497 | "for i in range(n_iter):\n",
498 | " z_score=rand_z_score()\n",
499 | " scenario_return.append(scenario_loss(portfolio_value,portfolio_standard_deviation,z_score,days))"
500 | ]
501 | },
502 | {
503 | "cell_type": "code",
504 | "execution_count": 128,
505 | "metadata": {},
506 | "outputs": [],
507 | "source": [
508 | "conf_intv=0.95\n",
509 | "VaR=np.percentile(scenario_return,5)"
510 | ]
511 | },
512 | {
513 | "cell_type": "code",
514 | "execution_count": 129,
515 | "metadata": {},
516 | "outputs": [
517 | {
518 | "data": {
519 | "text/plain": [
520 | "-106272.09525042716"
521 | ]
522 | },
523 | "execution_count": 129,
524 | "metadata": {},
525 | "output_type": "execute_result"
526 | }
527 | ],
528 | "source": [
529 | "VaR"
530 | ]
531 | },
532 | {
533 | "cell_type": "code",
534 | "execution_count": 130,
535 | "metadata": {},
536 | "outputs": [
537 | {
538 | "data": {
539 | "text/plain": [
540 | "0.9999999999999999"
541 | ]
542 | },
543 | "execution_count": 130,
544 | "metadata": {},
545 | "output_type": "execute_result"
546 | }
547 | ],
548 | "source": [
549 | "weights=np.random.rand(len(tickers))\n",
550 | "weights=weights/np.sum(weights)\n",
551 | "sum(weights)"
552 | ]
553 | },
554 | {
555 | "cell_type": "code",
556 | "execution_count": 131,
557 | "metadata": {},
558 | "outputs": [],
559 | "source": [
560 | "portfolio_expected_return=expected_return(weights,log_returns)\n",
561 | "portfolio_standard_deviation=standard_deviation(weights,cov_matrix)\n",
562 | "days=20\n",
563 | "\n",
564 | "def scenario_loss(portfolio_value,portfolio_standard_deviation,z_score,days):\n",
565 | " return portfolio_value*portfolio_expected_return*days+portfolio_value*portfolio_standard_deviation*z_score*np.sqrt(days)"
566 | ]
567 | },
568 | {
569 | "cell_type": "code",
570 | "execution_count": 132,
571 | "metadata": {},
572 | "outputs": [
573 | {
574 | "data": {
575 | "text/plain": [
576 | "(6,)"
577 | ]
578 | },
579 | "execution_count": 132,
580 | "metadata": {},
581 | "output_type": "execute_result"
582 | }
583 | ],
584 | "source": [
585 | "weights.shape"
586 | ]
587 | },
588 | {
589 | "cell_type": "code",
590 | "execution_count": 133,
591 | "metadata": {},
592 | "outputs": [],
593 | "source": [
594 | "##Simulation\n",
595 | "\n",
596 | "n_iter=10000\n",
597 | "scenario_return=[]\n",
598 | "\n",
599 | "for i in range(n_iter):\n",
600 | " z_score=rand_z_score()\n",
601 | " scenario_return.append(scenario_loss(portfolio_value,portfolio_standard_deviation,z_score,days))"
602 | ]
603 | },
604 | {
605 | "cell_type": "code",
606 | "execution_count": 134,
607 | "metadata": {},
608 | "outputs": [
609 | {
610 | "data": {
611 | "text/plain": [
612 | "-130322.34366072073"
613 | ]
614 | },
615 | "execution_count": 134,
616 | "metadata": {},
617 | "output_type": "execute_result"
618 | }
619 | ],
620 | "source": [
621 | "conf_intv=0.95\n",
622 | "VaR=np.percentile(scenario_return,5)\n",
623 | "VaR"
624 | ]
625 | },
626 | {
627 | "cell_type": "code",
628 | "execution_count": null,
629 | "metadata": {},
630 | "outputs": [],
631 | "source": []
632 | }
633 | ],
634 | "metadata": {
635 | "kernelspec": {
636 | "display_name": "base",
637 | "language": "python",
638 | "name": "python3"
639 | },
640 | "language_info": {
641 | "codemirror_mode": {
642 | "name": "ipython",
643 | "version": 3
644 | },
645 | "file_extension": ".py",
646 | "mimetype": "text/x-python",
647 | "name": "python",
648 | "nbconvert_exporter": "python",
649 | "pygments_lexer": "ipython3",
650 | "version": "3.9.15"
651 | },
652 | "orig_nbformat": 4
653 | },
654 | "nbformat": 4,
655 | "nbformat_minor": 2
656 | }
657 |
--------------------------------------------------------------------------------
/SECURITY.md:
--------------------------------------------------------------------------------
1 | # Security Policy
2 |
3 | ## Supported Versions
4 |
5 | Use this section to tell people about which versions of your project are
6 | currently being supported with security updates.
7 |
8 | | Version | Supported |
9 | | ------- | ------------------ |
10 | | 5.1.x | :white_check_mark: |
11 | | 5.0.x | :x: |
12 | | 4.0.x | :white_check_mark: |
13 | | < 4.0 | :x: |
14 |
15 | ## Reporting a Vulnerability
16 |
17 | Use this section to tell people how to report a vulnerability.
18 |
19 | Tell them where to go, how often they can expect to get an update on a
20 | reported vulnerability, what to expect if the vulnerability is accepted or
21 | declined, etc.
22 |
--------------------------------------------------------------------------------