{ "cells": [ { "cell_type": "markdown", "id": "fa243fab-53f5-4695-a635-36d48494cf75", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\"The
" ] }, { "cell_type": "markdown", "id": "31868c69-bcb8-4fe6-9695-b8eb5c842a7e", "metadata": {}, "source": [ "# Python for Finance\n", "\n", "**Online Bootcamp**" ] }, { "cell_type": "markdown", "id": "f9defc78-897b-47d3-9b65-89eb13b0b64f", "metadata": { "tags": [] }, "source": [ "© Dr. Yves J. Hilpisch | The Python Quants GmbH\n", "\n", "http://tpq.io | [training@tpq.io](mailto:trainin@tpq.io) | [@dyjh](http://twitter.com/dyjh)" ] }, { "cell_type": "markdown", "id": "ba1f01c0-fca0-4a5e-9c73-b08f696b746f", "metadata": {}, "source": [ "## `numpy` Package " ] }, { "cell_type": "code", "execution_count": null, "id": "6702ad7e-bf7a-44ea-837d-54e8ecd4bff2", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "id": "3faf2a46-b10d-4005-9eae-9e88afa2845d", "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3, 4, 5]" ] }, { "cell_type": "code", "execution_count": null, "id": "82827ac3-23fc-437e-a859-e215572ea4eb", "metadata": {}, "outputs": [], "source": [ "l" ] }, { "cell_type": "code", "execution_count": null, "id": "195a697a-68de-4f2e-b738-ac37c1604d36", "metadata": {}, "outputs": [], "source": [ "type(l)" ] }, { "cell_type": "markdown", "id": "a15438be-eb69-4bc0-b5e3-bde589f7ab4a", "metadata": {}, "source": [ "## 1d Array Objects" ] }, { "cell_type": "code", "execution_count": null, "id": "044e9c83-a4c3-442f-97f0-c612b35debeb", "metadata": {}, "outputs": [], "source": [ "a = np.array(l)" ] }, { "cell_type": "code", "execution_count": null, "id": "36de3dd2-1580-47ca-b7f9-8990190c05b3", "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "id": "e797950e-b58f-4b55-a133-dff013ee9373", "metadata": {}, "outputs": [], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": null, "id": "d96de6a0-2377-4d12-a746-fe06895a1c18", "metadata": {}, "outputs": [], "source": [ "2 * l" ] }, { "cell_type": "code", "execution_count": null, "id": "aa1daeea-46d9-4b2c-9a55-903df9aec59d", "metadata": {}, "outputs": [], "source": [ "2 * a # vectorized operation (\"scalar multiplication\")" ] }, { "cell_type": "code", "execution_count": null, "id": "54c18705-c12a-4881-a521-e6b5c59110a8", "metadata": {}, "outputs": [], "source": [ "l + l" ] }, { "cell_type": "code", "execution_count": null, "id": "c9bd7bec-a524-4d2c-87b8-5e1d79512cc9", "metadata": {}, "outputs": [], "source": [ "a + a # vectorized operation (\"element-wise addition\")" ] }, { "cell_type": "code", "execution_count": null, "id": "ff9fb22b-7a29-46ea-9a47-41ba32c42482", "metadata": {}, "outputs": [], "source": [ "a / 2" ] }, { "cell_type": "code", "execution_count": null, "id": "cb689da4-433f-4845-b2a4-bde301e332f0", "metadata": {}, "outputs": [], "source": [ "a ** 2" ] }, { "cell_type": "code", "execution_count": null, "id": "f006e0d3-ceaa-442d-a26d-5edcf4e11064", "metadata": {}, "outputs": [], "source": [ "2 ** a" ] }, { "cell_type": "code", "execution_count": null, "id": "88e06567-2bfb-4f2f-83a7-d9242fc33caf", "metadata": {}, "outputs": [], "source": [ "a ** a" ] }, { "cell_type": "code", "execution_count": null, "id": "60043490-6b67-4f5d-915b-a133ff961063", "metadata": {}, "outputs": [], "source": [ "a.sum()" ] }, { "cell_type": "code", "execution_count": null, "id": "085b188c-1954-43c7-8aee-871faecb29b7", "metadata": {}, "outputs": [], "source": [ "sum(a)" ] }, { "cell_type": "code", "execution_count": null, "id": "9c460b87-f3ef-4157-85d6-dc9569648aa2", "metadata": {}, "outputs": [], "source": [ "sum(l)" ] }, { "cell_type": "code", "execution_count": null, "id": "5e74d7e4-a2d4-470c-94db-a4715c6bc017", "metadata": {}, "outputs": [], "source": [ "a.mean()" ] }, { "cell_type": "code", "execution_count": null, "id": "c48b6b7e-50ab-463d-b300-8d9972f3e0f5", "metadata": {}, "outputs": [], "source": [ "a.std()" ] }, { "cell_type": "code", "execution_count": null, "id": "2eafaf65-b614-4ceb-986d-81dbd5579388", "metadata": {}, "outputs": [], "source": [ "np.sqrt(a)" ] }, { "cell_type": "code", "execution_count": null, "id": "9fd1cbfd-21b4-479e-abe1-d7590b73b9a4", "metadata": {}, "outputs": [], "source": [ "np.exp(a)" ] }, { "cell_type": "code", "execution_count": null, "id": "6948fdce-1cdf-4402-b585-07c937ae9a74", "metadata": {}, "outputs": [], "source": [ "np.max(a)" ] }, { "cell_type": "code", "execution_count": null, "id": "2c3c29f8-e4ca-4083-942e-0410dd81c8cf", "metadata": {}, "outputs": [], "source": [ "a.dtype" ] }, { "cell_type": "code", "execution_count": null, "id": "5bdb171a-4d5a-4e57-bdbd-1801fdd9e04c", "metadata": {}, "outputs": [], "source": [ "a.size" ] }, { "cell_type": "code", "execution_count": null, "id": "d4d293cd-399d-4018-bd66-d18f409fada7", "metadata": {}, "outputs": [], "source": [ "b = np.zeros(10)\n", "b" ] }, { "cell_type": "code", "execution_count": null, "id": "caf1184b-6b66-46a2-902e-0a9760fd5dd1", "metadata": {}, "outputs": [], "source": [ "b = np.ones(10)\n", "b" ] }, { "cell_type": "code", "execution_count": null, "id": "97743e40-c54a-4443-9e4a-02363a846aac", "metadata": {}, "outputs": [], "source": [ "b" ] }, { "cell_type": "code", "execution_count": null, "id": "38344d18-aa80-4b05-8467-7368fb972c4f", "metadata": {}, "outputs": [], "source": [ "b.dtype" ] }, { "cell_type": "code", "execution_count": null, "id": "6f65fbcb-2243-4533-8bf8-5828454092b0", "metadata": {}, "outputs": [], "source": [ "b.size" ] }, { "cell_type": "code", "execution_count": null, "id": "03a416ef-5ce0-4a1c-b35d-b64e2a71de91", "metadata": {}, "outputs": [], "source": [ "b.nbytes" ] }, { "cell_type": "code", "execution_count": null, "id": "f7bf8216-6e9a-4896-8e20-b9f97c7a5cfe", "metadata": {}, "outputs": [], "source": [ "b[0]" ] }, { "cell_type": "code", "execution_count": null, "id": "a09eb066-3fbc-41ac-9e1a-8bbab3785e87", "metadata": {}, "outputs": [], "source": [ "b[0] = 10" ] }, { "cell_type": "code", "execution_count": null, "id": "c4573d49-f073-4a41-a734-5f3db46944fc", "metadata": {}, "outputs": [], "source": [ "b" ] }, { "cell_type": "code", "execution_count": null, "id": "bf9a7938-66e5-49e6-bde3-3183aa31cfcf", "metadata": {}, "outputs": [], "source": [ "b[-1] = 20" ] }, { "cell_type": "code", "execution_count": null, "id": "d0426dba-13fc-4719-a32e-04f358f5a6ca", "metadata": {}, "outputs": [], "source": [ "b" ] }, { "cell_type": "code", "execution_count": null, "id": "eceee42a-3433-44b8-8553-8daa5d4a5532", "metadata": {}, "outputs": [], "source": [ "b[:5]" ] }, { "cell_type": "code", "execution_count": null, "id": "da381c6f-3afe-4058-a629-d27c21d5ff0c", "metadata": {}, "outputs": [], "source": [ "b[-5:]" ] }, { "cell_type": "markdown", "id": "72189e58-eea1-4820-aa45-25e86c2df5f8", "metadata": {}, "source": [ "## 2d Array Objects" ] }, { "cell_type": "code", "execution_count": null, "id": "af5a0ef7-08df-4446-95f9-eed7edfcbd21", "metadata": {}, "outputs": [], "source": [ "c = np.arange(15)" ] }, { "cell_type": "code", "execution_count": null, "id": "f4c820e2-7178-4069-8b70-06ad0b8f8984", "metadata": {}, "outputs": [], "source": [ "c" ] }, { "cell_type": "code", "execution_count": null, "id": "2946ff92-4aff-4947-90bd-9b2ca686000e", "metadata": {}, "outputs": [], "source": [ "c = c.reshape((5, 3))" ] }, { "cell_type": "code", "execution_count": null, "id": "cf664db6-8b77-48b5-abca-af7bfbbb6d9c", "metadata": {}, "outputs": [], "source": [ "c" ] }, { "cell_type": "code", "execution_count": null, "id": "fc9303b4-023e-4fe5-b048-4a46f11a2437", "metadata": {}, "outputs": [], "source": [ "2 * c" ] }, { "cell_type": "code", "execution_count": null, "id": "4eb79714-4a05-47c1-920c-9c610050aaf5", "metadata": {}, "outputs": [], "source": [ "c ** 2" ] }, { "cell_type": "code", "execution_count": null, "id": "63c9f787-9ca4-42cc-8ad9-68933d8da2e2", "metadata": {}, "outputs": [], "source": [ "c ** c" ] }, { "cell_type": "code", "execution_count": null, "id": "76b52fb9-bcd9-4525-938a-097798a20743", "metadata": {}, "outputs": [], "source": [ "c.sum()" ] }, { "cell_type": "code", "execution_count": null, "id": "33872eec-3e8e-4b31-99ad-82f8131c633f", "metadata": {}, "outputs": [], "source": [ "c.sum(axis=0)" ] }, { "cell_type": "code", "execution_count": null, "id": "213b1a91-a7b3-4b65-b108-88afd31a82ab", "metadata": {}, "outputs": [], "source": [ "c.sum(axis=1)" ] }, { "cell_type": "code", "execution_count": null, "id": "c8439ba6-ca50-46d4-accb-6515fa10d825", "metadata": {}, "outputs": [], "source": [ "c.mean()" ] }, { "cell_type": "code", "execution_count": null, "id": "333b6782-2d66-4e10-8195-e9de489890c3", "metadata": {}, "outputs": [], "source": [ "c.mean(axis=0)" ] }, { "cell_type": "code", "execution_count": null, "id": "9b8b0f63-2f60-49f7-9ee7-73b859a32192", "metadata": {}, "outputs": [], "source": [ "np.sqrt(c)" ] }, { "cell_type": "markdown", "id": "f4298003-12a2-4846-b8ab-e1ddf7b0f093", "metadata": {}, "source": [ "## Speed Comparisons" ] }, { "cell_type": "code", "execution_count": null, "id": "afd39867-8614-4df4-9d86-926268ea3efe", "metadata": {}, "outputs": [], "source": [ "N = 1_000_000" ] }, { "cell_type": "code", "execution_count": null, "id": "446aa584-26b7-492b-a212-e319d4f78204", "metadata": {}, "outputs": [], "source": [ "%time l = list(range(N))" ] }, { "cell_type": "code", "execution_count": null, "id": "1e4c3a92-a124-49bc-81bf-3b9e1faa9378", "metadata": {}, "outputs": [], "source": [ "l[-5:]" ] }, { "cell_type": "code", "execution_count": null, "id": "26aa348a-9319-4a84-8a2e-742b7067e3d9", "metadata": {}, "outputs": [], "source": [ "%time sum(l)" ] }, { "cell_type": "code", "execution_count": null, "id": "ac3c62c7-043c-408b-9a3e-ced9d93a717d", "metadata": {}, "outputs": [], "source": [ "%timeit sum(l)" ] }, { "cell_type": "code", "execution_count": null, "id": "14d61bc6-0e48-48dd-8c17-9c5cd12391a4", "metadata": {}, "outputs": [], "source": [ "%time a = np.arange(N)" ] }, { "cell_type": "code", "execution_count": null, "id": "3975902a-02d6-4262-a177-3981f0474037", "metadata": {}, "outputs": [], "source": [ "%time a.sum()" ] }, { "cell_type": "code", "execution_count": null, "id": "1c891736-66ff-4f7e-b03a-c19827adbe1f", "metadata": {}, "outputs": [], "source": [ "%timeit a.sum()" ] }, { "cell_type": "code", "execution_count": null, "id": "3688a616-3ae6-48ee-84ca-8e75f681057c", "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": null, "id": "ae69afa1-7851-49b2-b130-3a5edfcc909d", "metadata": {}, "outputs": [], "source": [ "%time sqp = [math.sqrt(x) for x in l]" ] }, { "cell_type": "code", "execution_count": null, "id": "ee2c3c73-ab4a-4941-8166-3bef34b6bdef", "metadata": {}, "outputs": [], "source": [ "%timeit sqp = [math.sqrt(x) for x in l]" ] }, { "cell_type": "code", "execution_count": null, "id": "4d6fc5b3-4b6e-48b2-8ead-1c38b85c655e", "metadata": {}, "outputs": [], "source": [ "sqp[:5]" ] }, { "cell_type": "code", "execution_count": null, "id": "62e0f1b7-a4e1-4839-994a-19dcd35d8fcb", "metadata": {}, "outputs": [], "source": [ "%time sqn = np.sqrt(a)" ] }, { "cell_type": "code", "execution_count": null, "id": "935bf351-3714-4c0a-af8d-d83c859ff069", "metadata": {}, "outputs": [], "source": [ "%timeit sqn = np.sqrt(a)" ] }, { "cell_type": "code", "execution_count": null, "id": "b9a6766f-85cf-492a-a7c4-ee007b5ef2a5", "metadata": {}, "outputs": [], "source": [ "sqn[:5]" ] }, { "cell_type": "markdown", "id": "3065a313-79e4-4638-86bb-f89c4bc7f249", "metadata": {}, "source": [ "## Random Numbers " ] }, { "cell_type": "code", "execution_count": null, "id": "edeb7293-579d-468e-81ad-b14d59e4b606", "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": null, "id": "59445c2f-5cb4-4f0f-bd8e-ba11e8cf09d4", "metadata": {}, "outputs": [], "source": [ "N = 100_000" ] }, { "cell_type": "code", "execution_count": null, "id": "8bb85643-54eb-493f-9ac5-a6852de77e8c", "metadata": {}, "outputs": [], "source": [ "random.gauss(0, 1)" ] }, { "cell_type": "code", "execution_count": null, "id": "3e20c9aa-56e4-4b76-97b8-bf6bd682ed28", "metadata": {}, "outputs": [], "source": [ "%time rnp = [random.gauss(0, 1) for _ in range(N)]" ] }, { "cell_type": "code", "execution_count": null, "id": "0e9e8d97-5b98-4fda-bfdb-74d02ffaa338", "metadata": {}, "outputs": [], "source": [ "rnp[:5]" ] }, { "cell_type": "code", "execution_count": null, "id": "7e11a6a9-bbe4-4b90-9d60-9f6b6280635c", "metadata": {}, "outputs": [], "source": [ "%time rnn = np.random.standard_normal(N) # legacy way" ] }, { "cell_type": "code", "execution_count": null, "id": "a2e91c6d-278c-4c56-841e-6e0b0f2f7379", "metadata": {}, "outputs": [], "source": [ "rnn[:5]" ] }, { "cell_type": "code", "execution_count": null, "id": "c8c57699-ec04-4010-bbb6-66fb6d5d9bdc", "metadata": {}, "outputs": [], "source": [ "from numpy.random import default_rng" ] }, { "cell_type": "code", "execution_count": null, "id": "f3373276-2101-4f89-a0b7-c503eec3948b", "metadata": {}, "outputs": [], "source": [ "rng = default_rng()" ] }, { "cell_type": "code", "execution_count": null, "id": "adc2b095-a2c7-40e4-8353-2e57057e7ba6", "metadata": {}, "outputs": [], "source": [ "%time rnn = rng.standard_normal(N) # new way" ] }, { "cell_type": "code", "execution_count": null, "id": "a07b8502-7000-49f6-9c5d-42d094451411", "metadata": {}, "outputs": [], "source": [ "from pylab import plt\n", "plt.style.use('seaborn-v0_8')\n", "%config InlineBackend.figure_format = 'svg'" ] }, { "cell_type": "code", "execution_count": null, "id": "963052bd-96e6-45e8-9d3d-8f4847b60638", "metadata": {}, "outputs": [], "source": [ "plt.hist(rnn, bins=50);" ] }, { "cell_type": "code", "execution_count": null, "id": "52bf0667-0962-41a1-aae2-6a167a2b3b83", "metadata": {}, "outputs": [], "source": [ "rnn.mean() # first moment" ] }, { "cell_type": "code", "execution_count": null, "id": "72a1f471-2dee-453b-826f-8e84ef540480", "metadata": {}, "outputs": [], "source": [ "rnn.std() # second moment" ] }, { "cell_type": "code", "execution_count": null, "id": "10f2dc0f-907a-4467-89c1-114ef70a54d8", "metadata": {}, "outputs": [], "source": [ "rn = rng.standard_normal(100)" ] }, { "cell_type": "code", "execution_count": null, "id": "1caf57b9-fc2e-4668-8a17-f216030378fc", "metadata": {}, "outputs": [], "source": [ "plt.plot(rn.cumsum());" ] }, { "cell_type": "code", "execution_count": null, "id": "0f5be78e-8b8a-4b50-8a5f-362ceb613070", "metadata": {}, "outputs": [], "source": [ "rn = rng.standard_normal((100, 5))" ] }, { "cell_type": "code", "execution_count": null, "id": "2f79dc9f-1261-4a32-823b-21b9450346a7", "metadata": {}, "outputs": [], "source": [ "plt.plot(rn.cumsum(axis=0));" ] }, { "cell_type": "markdown", "id": "e4dd075c-41c3-41f3-bce6-10f9cf08899e", "metadata": {}, "source": [ "## Valuing Options by MCS" ] }, { "cell_type": "code", "execution_count": null, "id": "25f425a9-ad1d-4e78-8d0f-bf9b1af13ad1", "metadata": {}, "outputs": [], "source": [ "S0 = 36 # initial price\n", "K = 40 # strike price\n", "T = 1.0 # maturity\n", "r = 0.06 # short rate\n", "sigma = 0.2 # volatility" ] }, { "cell_type": "markdown", "id": "e63c1c57-67f3-42a6-9f7f-495c832b3184", "metadata": {}, "source": [ "### Simulating a Stock Price Process" ] }, { "cell_type": "code", "execution_count": null, "id": "97cb7cd7-bd95-427f-9cb0-f152111a435f", "metadata": {}, "outputs": [], "source": [ "M = 50\n", "I = 10_000" ] }, { "cell_type": "code", "execution_count": null, "id": "e6ab5e4b-828b-45e4-9ac0-dfa2a69b3d7c", "metadata": {}, "outputs": [], "source": [ "dt = T / M\n", "dt" ] }, { "cell_type": "code", "execution_count": null, "id": "b4b36ec2-4db3-43c5-949d-e6c29fa47520", "metadata": {}, "outputs": [], "source": [ "S = np.zeros((M + 1, I))" ] }, { "cell_type": "code", "execution_count": null, "id": "13f6795b-f36a-4a5a-972b-8a08f2f038fc", "metadata": {}, "outputs": [], "source": [ "S[0] = S0" ] }, { "cell_type": "code", "execution_count": null, "id": "3546c5a9-f56a-4538-8bce-89a5146f7005", "metadata": {}, "outputs": [], "source": [ "S0" ] }, { "cell_type": "code", "execution_count": null, "id": "ce77d94e-f6e6-4199-99d8-6e51c8d23cdb", "metadata": {}, "outputs": [], "source": [ "rn = rng.standard_normal((M + 1, I))" ] }, { "cell_type": "code", "execution_count": null, "id": "0fa8e6e8-afcc-4d3f-9beb-4db1e59f2431", "metadata": {}, "outputs": [], "source": [ "for t in range(1, M + 1):\n", " S[t] = S[t - 1] * np.exp((r - sigma ** 2 / 2) * dt +\n", " math.sqrt(dt) * sigma * rn[t])" ] }, { "cell_type": "code", "execution_count": null, "id": "24f25435-7d6f-4ccf-ba14-c562cafbe1b5", "metadata": {}, "outputs": [], "source": [ "S" ] }, { "cell_type": "code", "execution_count": null, "id": "c530c2ab-ea39-4a85-ab63-2ec256ac4a9d", "metadata": {}, "outputs": [], "source": [ "S[:, 0]" ] }, { "cell_type": "code", "execution_count": null, "id": "94b0ee8a-5724-4878-92b7-feeab4d3ff44", "metadata": {}, "outputs": [], "source": [ "plt.plot(S[:, :100])\n", "plt.plot(S[:, :100].mean(axis=1), lw=5, c='r'); # mean of the values" ] }, { "cell_type": "code", "execution_count": null, "id": "ea628285-3982-401c-b3a7-2fad7d1ae61a", "metadata": {}, "outputs": [], "source": [ "S[-1].mean()" ] }, { "cell_type": "code", "execution_count": null, "id": "e35c4180-0269-4e90-9e8a-9e0a02805a2d", "metadata": {}, "outputs": [], "source": [ "S0 * math.exp(r * T)" ] }, { "cell_type": "markdown", "id": "8d47e09c-5c98-4fb6-ae12-8fc685a130d1", "metadata": {}, "source": [ "### Valuing a European Put Option" ] }, { "cell_type": "code", "execution_count": null, "id": "69a33b47-03d2-425d-be39-c3e1865a755c", "metadata": {}, "outputs": [], "source": [ "K" ] }, { "cell_type": "code", "execution_count": null, "id": "5dd86052-da1f-444b-ae95-b3196bdba225", "metadata": {}, "outputs": [], "source": [ "h = np.maximum(K - S[-1], 0) # inner value (payoff) at maturity" ] }, { "cell_type": "code", "execution_count": null, "id": "5059db48-16f3-4520-9426-c17dec3cd371", "metadata": {}, "outputs": [], "source": [ "plt.hist(h, bins=50);" ] }, { "cell_type": "code", "execution_count": null, "id": "b601e03e-9a6c-4698-ad9b-d3ae772b1bda", "metadata": {}, "outputs": [], "source": [ "h.mean() * math.exp(-r * T) # present value (MCS estimator) for Europ. put option" ] }, { "cell_type": "markdown", "id": "8fa155f8-3cf7-4676-9953-b356366a9fa8", "metadata": {}, "source": [ "### Excursion: OLS Regression" ] }, { "cell_type": "code", "execution_count": null, "id": "8f1f6555-c5e7-45a3-8e3f-9efe8423dc5f", "metadata": {}, "outputs": [], "source": [ "n = 9" ] }, { "cell_type": "code", "execution_count": null, "id": "7e24ed73-95c3-41a8-99a7-b2c062f91f4c", "metadata": {}, "outputs": [], "source": [ "plt.plot(S[:, n]);" ] }, { "cell_type": "code", "execution_count": null, "id": "f1207f6e-0423-4f04-a4ba-ba8a427ac477", "metadata": {}, "outputs": [], "source": [ "x = np.arange(M + 1)" ] }, { "cell_type": "code", "execution_count": null, "id": "a2b29ec8-8f78-4825-8a9c-703cf6eb0f25", "metadata": {}, "outputs": [], "source": [ "reg_1 = np.polyfit(x, S[:, n], deg=1) # linear regression" ] }, { "cell_type": "code", "execution_count": null, "id": "7bc7dace-987d-46cb-9326-a1c1ead03b20", "metadata": {}, "outputs": [], "source": [ "reg_1" ] }, { "cell_type": "code", "execution_count": null, "id": "83aa16c7-bf62-4798-913f-35d0cfa99745", "metadata": {}, "outputs": [], "source": [ "reg_2 = np.polyfit(x, S[:, n], deg=2) # quadratic regression" ] }, { "cell_type": "code", "execution_count": null, "id": "3e3def92-6246-4683-9df1-86c9f3c500ab", "metadata": {}, "outputs": [], "source": [ "reg_2" ] }, { "cell_type": "code", "execution_count": null, "id": "caa88920-f5ef-4dee-829d-8aadd5f5d32c", "metadata": {}, "outputs": [], "source": [ "reg_3 = np.polyfit(x, S[:, n], deg=3) # cubic regression" ] }, { "cell_type": "code", "execution_count": null, "id": "5d4de97b-2905-48a1-b308-fc544682c6c5", "metadata": {}, "outputs": [], "source": [ "reg_3" ] }, { "cell_type": "code", "execution_count": null, "id": "8fc4154d-75b9-4cb9-8d23-731cb525b89c", "metadata": {}, "outputs": [], "source": [ "plt.plot(S[:, n])\n", "plt.plot(np.polyval(reg_1, x), label='linear')\n", "plt.plot(np.polyval(reg_2, x), label='quadratic')\n", "plt.plot(np.polyval(reg_3, x), label='cubic')\n", "plt.legend();" ] }, { "cell_type": "markdown", "id": "0a2c5731-1f96-435a-9bc0-4cd79b388069", "metadata": {}, "source": [ "### Valuing an American Put Option" ] }, { "cell_type": "markdown", "id": "eddd4eff-44c8-4f17-9e99-6ab1be077e19", "metadata": {}, "source": [ "... based on the Least-Squares Monte Carlo (LSM) algorithm by Longstaff-Schwartz (2001)." ] }, { "cell_type": "code", "execution_count": null, "id": "4cd6356b-aea8-4038-bd04-c2f656a4e539", "metadata": {}, "outputs": [], "source": [ "df = math.exp(-r * dt)\n", "df" ] }, { "cell_type": "code", "execution_count": null, "id": "2b3f40e6-dc51-4e57-a0f3-3205bed13c6a", "metadata": {}, "outputs": [], "source": [ "# list(range(M, 0, -1))" ] }, { "cell_type": "code", "execution_count": null, "id": "4c43c788-c987-4c99-bfe5-11f17c1c84b5", "metadata": {}, "outputs": [], "source": [ "h = np.maximum(K - S, 0)" ] }, { "cell_type": "code", "execution_count": null, "id": "c25c721b-42e8-42fa-a467-5773024692d3", "metadata": {}, "outputs": [], "source": [ "V = h[-1]\n", "for t in range(M - 1, 0, -1):\n", " reg = np.polyfit(S[t], V * df, deg=7)\n", " C = np.polyval(reg, S[t])\n", " V = np.where(h[t] > C, h[t], V * df)\n", "V.mean() * df" ] }, { "cell_type": "markdown", "id": "c5f4612a-5421-42f9-8e69-dc9b8b29ab02", "metadata": {}, "source": [ "\"The
\n", "\n", "http://tpq.io | training@tpq.io | @dyjh " ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }