├── .gitignore ├── LICENSE ├── Project.md ├── README.md ├── files ├── ASP2017_Midterm.pdf ├── ASP2018_Midterm.pdf ├── ASP2019_Midterm.pdf ├── ASP2020_Midterm.pdf ├── ASP2021_Midterm.pdf ├── ASP2022_Midterm.pdf ├── ASP_Problems.pdf ├── Bachelier_Model.pdf ├── Choi_Jaehyuk_Github.png ├── Choi_Jaehyuk_Python.png ├── Copula.pdf ├── HestonPoisson-Slides.pdf ├── ImpVol.pdf ├── MCmethod.pdf ├── NSVh_Slides.pdf ├── NumIntegral.pdf ├── OUSV-KL-Slides.pdf ├── Prob_Stat_Review.pdf ├── SABRmodel.pdf ├── SV_Simulation.pdf ├── SpreadBasketOption.pdf ├── Variance-Derivatives.pdf └── syllabus.pdf ├── past-years ├── 2016-17-M1 │ └── README.md ├── 2017-18-M1 │ ├── Project.md │ └── README.md ├── 2018-19-M1 │ ├── Project.md │ ├── README.md │ └── StudentList.md ├── 2019-20-M1 │ ├── Project.md │ └── README.md ├── 2020-21-M3 │ ├── Project.md │ └── README.md ├── 2021-22-M3 │ ├── Project.md │ └── README.md └── 2022-23-M1 │ ├── Project.md │ └── README.md └── py ├── BlackScholes_FunctionVsClass.ipynb ├── BlackScholes_ImpliedVol.ipynb ├── BlackScholes_MC.ipynb ├── BsmNdMc_Demo.ipynb ├── Copula_Demo.ipynb ├── CorrelatedNormals_Demo.ipynb ├── Debug.ipynb ├── HW1 ├── HW1.ipynb └── HW1_Solution.ipynb ├── HW2 ├── KrekelEtAl2004-Wilmott-BasketOption.ipynb ├── KrekelEtAl2004-Wilmott-BasketOption.xlsx ├── TestCode_BasketSpread.ipynb └── option_models │ └── basket.py ├── HW3 ├── Demo_Advanced_Import.ipynb ├── Demo_GHQ.ipynb ├── TestCode_SABR.ipynb └── option_models │ ├── __init__.py │ └── sabr.py ├── KrekelEtAl2004-Wilmott-BasketOption.ipynb ├── KrekelEtAl2004-Wilmott-BasketOption.xlsx ├── MC_Demo.ipynb ├── NumIntegral.ipynb ├── PythonCrashCourse_Derek_Banas.ipynb ├── PythonCrashCourse_Numpy.ipynb ├── SabrModel_Demo.ipynb ├── TestCode_Normal.ipynb └── Test_Debug.py /.gitignore: -------------------------------------------------------------------------------- 1 | # User specific 2 | py/_* 3 | 4 | # Byte-compiled / optimized / DLL files 5 | __pycache__/ 6 | *.py[cod] 7 | *$py.class 8 | 9 | # C extensions 10 | *.so 11 | 12 | # Distribution / packaging 13 | .Python 14 | build/ 15 | develop-eggs/ 16 | dist/ 17 | downloads/ 18 | eggs/ 19 | .eggs/ 20 | lib/ 21 | lib64/ 22 | parts/ 23 | sdist/ 24 | var/ 25 | wheels/ 26 | *.egg-info/ 27 | .installed.cfg 28 | *.egg 29 | MANIFEST 30 | 31 | # PyInstaller 32 | # Usually these files are written by a python script from a template 33 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 34 | *.manifest 35 | *.spec 36 | 37 | # Installer logs 38 | pip-log.txt 39 | pip-delete-this-directory.txt 40 | 41 | # Unit test / coverage reports 42 | htmlcov/ 43 | .tox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | 53 | # Translations 54 | *.mo 55 | *.pot 56 | 57 | # Django stuff: 58 | *.log 59 | local_settings.py 60 | db.sqlite3 61 | 62 | # Flask stuff: 63 | instance/ 64 | .webassets-cache 65 | 66 | # Scrapy stuff: 67 | .scrapy 68 | 69 | # Sphinx documentation 70 | docs/_build/ 71 | 72 | # PyBuilder 73 | target/ 74 | 75 | # Jupyter Notebook 76 | .ipynb_checkpoints 77 | 78 | # pyenv 79 | .python-version 80 | 81 | # celery beat schedule file 82 | celerybeat-schedule 83 | 84 | # SageMath parsed files 85 | *.sage.py 86 | 87 | # Environments 88 | .env 89 | .venv 90 | env/ 91 | venv/ 92 | ENV/ 93 | env.bak/ 94 | venv.bak/ 95 | 96 | # Spyder project settings 97 | .spyderproject 98 | .spyproject 99 | 100 | # Rope project settings 101 | .ropeproject 102 | 103 | # mkdocs documentation 104 | /site 105 | 106 | # mypy 107 | .mypy_cache/ 108 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Peking University HSBC Business School 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Applied Stochastic Processes 2023-24 Module 3 (Spring 2024) 2 | 3 | ## Announcements 4 | * The WeChat group will be created by TA. (No 1-to-1 chat please.) 5 | * Email is the preferred method of communication. The class mailing list will be created as PHBS.ASP@allmail.net. 6 | 7 | ## Course Slides and Other Resources 8 | * Prelims: [Probability Statistics Review](files/Prob_Stat_Review.pdf) 9 | * Past Midterm Exams: [All Exams](files/ASP_Problems.pdf) ([2017-18](files/ASP2017_Midterm.pdf), [2018-19](files/ASP2018_Midterm.pdf), [2019-20](files/ASP2019_Midterm.pdf), [2020-21](files/ASP2020_Midterm.pdf), [2021-22](files/ASP2021_Midterm.pdf), [2022-23](files/ASP2022_Midterm.pdf)) 10 | * Term Projects: [Current](Project.md), [2022-23](past-years/2022-23-M1/Project.md), [2021-22](past-years/2021-22-M3/Project.md), [2020-21](past-years/2020-21-M3/Project.md), [2019-20](past-years/2019-20-M1/Project.md), [2018-19](past-years/2018-19-M1/Project.md), [2017-18](past-years/2017-18-M1/Project.md) 11 | * `PyFeng` package ([PyPI](https://pypi.org/project/pyfeng/) \| [Github](https://github.com/PyFE/PyFENG) \| [Documentation](https://pyfeng.readthedocs.io/)) 12 | * Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) | [Py demo](py/MC_Demo.ipynb)) 13 | * Black-Scholes model ([Py demo](py/BlackScholes_ImpliedVol.ipynb), [MC demo](py/BlackScholes_MC.ipynb)): Also see Ch. 10 of [StoFin Course Notes](https://github.com/PHBS/StoFin/blob/master/files/SCFA_Notes.pdf) 14 | * Bachelier (Normal) model ([Slides](files/Bachelier_Model.pdf)) from **Stochastic Finance** class 15 | * Implied volatility ([Slides](files/ImpVol.pdf) | [Py demo](py/BlackScholes_ImpliedVol.ipynb)) 16 | * Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)) 17 | * SABR model ([Slides](files/SABRmodel.pdf)) | NSVh model ([Slides](files/NSVh_Slides.pdf)) 18 | * SV model ([Slides](files/SV_Simulation.pdf)): Advanced methods for [Heston Model](files/HestonPoisson-Slides.pdf) and [OUSV Model](files/OUSV-KL-Slides.pdf) 19 | * Variance Derivatives ([Slides](files/Variance-Derivatives.pdf)) 20 | * Numerical Integral ([Slides](files/NumIntegral.pdf)) 21 | * Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 22 | 23 | ## Lectures 24 | No | Date | Contents 25 | --- | :---: | --- 26 | __01__ | 2.20 Tues | Course overview, Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 27 | __02__ | 2.23 Fri | Continued ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 28 | __03__ | 2.27 Tues | Numpy crach course ([Py Demo](py/PythonCrashCourse_Numpy.ipynb)). Python crash course ([Py Demo](py/PythonCrashCourse_Derek_Banas.ipynb)). More [cheatsheets](https://ehmatthes.github.io/pcc/cheatsheets/README.html) also available in [MLF CMS](http://cms.phbs.pku.edu.cn/claroline/document/document.php?cidReset=true&cidReq=FN570). 29 | __04__ | 3.01 Fri | Black-Scholes implementation ([Py Demo](py/BlackScholes_FunctionVsClass.ipynb)). Implied volatility ([Slides](files/ImpVol.pdf) \| [Py demo](py/BlackScholes_ImpliedVol.ipynb)). Bachelier model ([Slides](files/Bachelier_Model.pdf)). Black-Scholes-Merton and Bachelier option pricing with MC ([Py Demo](py/BlackScholes_MC.ipynb)). 30 | __05__ | 3.05 Tues | Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)). Correlated Normal RNs ([Slides](files/MCmethod.pdf) \| [Py Demo](py/CorrelatedNormals_Demo.ipynb)), [HW2: [Spread/Basket option implementation](py/HW2/TestCode_BasketSpread.ipynb), Due next Thursday] 31 | __06__ | 3.08 Fri | SABR model ([Slides](files/SABRmodel.pdf): Volatility smile) 32 | __07__ | 3.12 Tues | SABR model continued ([Slides](files/SABRmodel.pdf): Local volatility model, Model intro), Introduction to [PyFENG](https://github.com/PyFE/PyFENG) package 33 | __08__ | 3.15 Fri | SABR model continued ([Slides](files/SABRmodel.pdf): Euler/Milstein method, Conditional MC), Py Demo ([SABR](py/SabrModel_Demo.ipynb), [BsmNdMc](BsmNdMc_Demo.ipynb)), Python Import ([Py Demo](py/HW3/Demo_Advanced_Import.ipynb)), HW3: [MC method for SABR](py/HW3/TestCode_SABR.ipynb) 34 | __09__ | 3.19 Tues | SV Model Simulation for Project ([Slides](files/SV_Simulation.pdf)) 35 | __10__ | 3.22 Fri | Research Presentation: Heston model simulation method ([Slides](files/HestonMC-Slides.pdf)) 36 | __11__ | 3.26 Tues | SV Model Simulation for Project ([Slides](files/SV_Simulation.pdf)), Github pull-request (PR). Suggested [project topics](Project.md) 37 | __12__ | 3.29 Fri | 38 | __13__ | 4.02 Tues | [Past Exams](files/ASP_Problems.pdf) Review 39 | __14__ | 4.07 __Sun__ | Midterm Exam ([Solution](files/ASP2022_Midterm.pdf)) 40 | __15__ | 4.09 Tues | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 41 | __16__ | 4.12 Fri | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 42 | __17__ | 4.16 Tues | Research Presentation: NSVh model and Normal SABR ([Slides](files/NSVh_Slides.pdf)) 43 | __18__ | 4.19 Fri | Course project presentation 44 | 45 | 46 | 47 | ## Homeworks: 48 | * ### __Set 0__: (Due by XXX) 49 | * Register on Github.com and send your ID and student number to Prof. Choi via email (jaehyuk@phbs.pku.edu.cn). Use your __full name__ in your profile. Accept invitation to the [PHBS organization](https://github.com/orgs/PHBS/people) from TA. Install [Github Desktop](https://desktop.github.com/). 50 | * Install [Anaconda](https://www.anaconda.com/download/) Python distribution (__3.X version__, not 2.X version). Anaconda distribution is core Python + useful scientific computation libraries (e.g., numpy, scipy, pandas) + package management system (pip or conda) 51 | * Send the screenshot of Github desktop and Anaconda installed to TA. (Example: [Github Desktop](files/Choi_Jaehyuk_Github.png), [Anaconda Spyder](files/Choi_Jaehyuk_Python.png)) 52 | 56 | * ### __Set 1__ [Due by 3.16] Pricing basket and spread option using MC. [Starter Code](py/HW2/TestCode_BasketSpread.ipynb) 57 | * Create a designated repository `YOUR_GITHUB_ID/PHBS_ASP_2023` for your HW and project. Tick Initialize this repository with a README and select python under .gitignore 58 | * Copy `HW2` folder from `ASP` repository to `YOUR_GITHUB_ID/PHBS_ASP_2023` repository. 59 | * Upload your HW to the folder `HW2` and update the repository. (`Commit to master` and `Fetch Origin`). 60 | * ### __Set 2__ [Due by 3.30] Simulating SABR model. [Starter Code](py/HW3/TestCode_SABR.ipynb) 61 | 62 | ## Course Project: [Project Description](files/Project.md) (Previous year: [2017](past-years/2017-18-M1/Project.md) | [2018](past-years/2018-19-M1/Project.md) | [2019](past-years/2019-20-M1/Project.md) | [2019](past-years/2019-20-M1/Project.md) | [2020](past-years/2020-21-M3/Project.md) | [2021](past-years/2021-22-M3/Project.md)) 63 | 64 | ## Classes: 65 | * Lectures: Tues & Fri 10:30 AM – 12:20 PM 66 | * Venue: PHBS Building, Room 313 67 | 68 | ## Instructor: [Jaehyuk Choi](http://www.jaehyukchoi.net/phbs_en) 69 | * Office: PHBS Building, Room 755 70 | * Phone: 86-755-2603-0568 71 | * Email: jaehyuk@phbs.pku.edu.cn 72 | * Office Hours: Mon 7-9 PM 73 | 74 | ## Teaching Assistance: Xin Yang (杨鑫) 75 | * Email: xinyang30@stu.pku.edu.cn 76 | * TA Office Hour: __TBA__ (Room 213/214) 77 | 78 | ## Course overview: 79 | Applied Stochastic Processes (ASP) is intended for students who are 80 | seeking advanced knowledge in stochastic calculus and are eventually interested in jobs in 81 | financial engineering. As the name indicates, the course will emphasize on applications such as 82 | numerical calculation and programming. On completion of this course, the students will learn 83 | how financial observations (e.g. stock prices and FX rate) are modeled with stochastic 84 | processes and how they can be computed using analytics or computer simulations. 85 | 86 | ## Prerequisites: 87 | [Stochastic Finance](https://github.com/PHBS/StoFin) (FIN 519), a year 1 required course for a quantitative finance program, is a prerequisite for the ASP since it provides theoretical background. Undergraduate-level knowledge in probability, statistics, linear algebra, and programming skills (Python) are also highly recommended. 88 | 89 | ## Extra Reading Materials 90 | * Monte Carlo Methods in Finance by Peter Jaeckel 91 | * Option Valuation Under Stochastic Volatility by Alan Lewis 92 | * [Stochastic Calculus and Financial Applications](http://www-stat.wharton.upenn.edu/~steele/StochasticCalculus.html) by J. Michael Steele 93 | ([Stochastic finance course notes](https://github.com/PHBS/2018.M3.StoFin/blob/master/files/SCFA_Notes.pdf)) 94 | 95 | ## Assessment/Grading Details 96 | Attendance 20%, Mid-term Exam 30%, Assignments 20%, Course Project 30% 97 | * __Midterm exam__: 4.06 __Wed__. Open-book exam without computer/phone/calculator use. No final exam. 98 | * __Course project__: Presentation (Last week). Group up to X people. 99 | * __Attendance__: Randomly checked. The score is calculated as 20 – 2`x`(#of absence). Leave requests should be made 24 hours before with supporting documents, except for emergencies. Job interview/internship cannot be a valid reason for leave 100 | * __Grade__ in letters (e.g., A+, A-, ... ,D+, D, F). __A- or above < 30% and B- or below > 10%__. 101 | -------------------------------------------------------------------------------- /files/ASP2017_Midterm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/ASP2017_Midterm.pdf -------------------------------------------------------------------------------- /files/ASP2018_Midterm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/ASP2018_Midterm.pdf -------------------------------------------------------------------------------- /files/ASP2019_Midterm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/ASP2019_Midterm.pdf -------------------------------------------------------------------------------- /files/ASP2020_Midterm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/ASP2020_Midterm.pdf -------------------------------------------------------------------------------- /files/ASP2021_Midterm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/ASP2021_Midterm.pdf -------------------------------------------------------------------------------- /files/ASP2022_Midterm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/ASP2022_Midterm.pdf -------------------------------------------------------------------------------- /files/ASP_Problems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/ASP_Problems.pdf -------------------------------------------------------------------------------- /files/Bachelier_Model.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/Bachelier_Model.pdf -------------------------------------------------------------------------------- /files/Choi_Jaehyuk_Github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/Choi_Jaehyuk_Github.png -------------------------------------------------------------------------------- /files/Choi_Jaehyuk_Python.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/Choi_Jaehyuk_Python.png -------------------------------------------------------------------------------- /files/Copula.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/Copula.pdf -------------------------------------------------------------------------------- /files/HestonPoisson-Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/HestonPoisson-Slides.pdf -------------------------------------------------------------------------------- /files/ImpVol.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/ImpVol.pdf -------------------------------------------------------------------------------- /files/MCmethod.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/MCmethod.pdf -------------------------------------------------------------------------------- /files/NSVh_Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/NSVh_Slides.pdf -------------------------------------------------------------------------------- /files/NumIntegral.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/NumIntegral.pdf -------------------------------------------------------------------------------- /files/OUSV-KL-Slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/OUSV-KL-Slides.pdf -------------------------------------------------------------------------------- /files/Prob_Stat_Review.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/Prob_Stat_Review.pdf -------------------------------------------------------------------------------- /files/SABRmodel.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/SABRmodel.pdf -------------------------------------------------------------------------------- /files/SV_Simulation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/SV_Simulation.pdf -------------------------------------------------------------------------------- /files/SpreadBasketOption.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/SpreadBasketOption.pdf -------------------------------------------------------------------------------- /files/Variance-Derivatives.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/Variance-Derivatives.pdf -------------------------------------------------------------------------------- /files/syllabus.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/files/syllabus.pdf -------------------------------------------------------------------------------- /past-years/2016-17-M1/README.md: -------------------------------------------------------------------------------- 1 | # Applied Stochastic Processes (2016-17 M1) 2 | -------------------------------------------------------------------------------- /past-years/2017-18-M1/Project.md: -------------------------------------------------------------------------------- 1 | # List of the course project repositories 2 | 3 | (Number next to the link is the presentation order.) 4 | 5 | * [Project-bdh456](https://www.github.com/PHBS-2017-ASP-Classroom/Project-bdh456): 11 (Thurs), BSM implied volatility (Jackel 2016) 6 | * [Project-choichoi](https://www.github.com/PHBS-2017-ASP-Classroom/Project-choichoi): 9 (Thurs), Normal implied volatility (Le Floc'h 2016) 7 | * [Project-project_team_tgw](https://www.github.com/PHBS-2017-ASP-Classroom/Project-project_team_tgw): 6 (Mon), Normal implied volatility (Choi 2007) 8 | * [Project-team-666-plus](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team-666-plus): 12 (Thurs), Efficient first guess for SABR calibrations [Le Floc’h and Kennedy, 2014](https://ssrn.com/abstract_id=2467231) (see also [here](https://www.clarusft.com/sabr-calibration-a-simple-explicit-initial-guess/)) 9 | * [Project-team_0001](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_0001): 10 (Thurs), Basket/Spread option pricing from an improved analytic normal model approach 10 | * [Project-team_07_heston_model](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_07_heston_model): 3 (Mon), Heston model 11 | * [Project-team_222](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_222): 4 (Mon), SABR model by (Kennedy, 2011) 12 | * [Project-team_911_project](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_911_project): 1 (Mon), Option pricing by CEV nodel 13 | * [Project-team_asap](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_asap): 5 (Mon), Basket option pricing by Levy's log-normal moment matching 14 | * [Project-team_ccch](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_ccch): 8 (Thurs), Analytic methods for spread options (Li, Kirk's approximation) 15 | * [Project-team_djz_](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_djz_): 13 (Thurs), Sum of all BSM models (Choi, 2017) 16 | * [Project-team_eddie](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_eddie): 7 (Mon), Basket option procing by Ju's Taylor expansion 17 | * [Project-team_final-project](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_final-project): 2 (Mon), Analytic methods for spread options (Li, Kirk's approximation) 18 | * [Project-team_zxc](https://www.github.com/PHBS-2017-ASP-Classroom/Project-team_zxc): 14 (Thurs), Analytic approximation of normal implied volatility (Le Floc'h 2016) 19 | -------------------------------------------------------------------------------- /past-years/2017-18-M1/README.md: -------------------------------------------------------------------------------- 1 | # Applied Stochastic Processes (FIN 514, 2017-18 Module 1) 2 | 3 | ## Announcements 4 | * Project [presentation schedule](Projects.MD) 5 | * Mid-term exam will be held at Rm 403 (not in the usual classroom) See the exam [preview](files/exam_preview.md). 6 | * Check out the topic suggestions for final projects in each starter code. 7 | * The starter code for [HW2](https://github.com/PHBS-2017-ASP-Classroom/BSMmodel_Base) was updaded. I fixed the implied vol validation part. 8 | * TA office hours announced as [below](#teaching-assistance-葛德生-devon-ge). 9 | * Please create your GitHub account for homework/project. Make sure to add your full name (not only ID). Then, click the classroom invidation link I sent to email. 10 | * I set up class mailing list phbs.asp@allmail.net and sent out the first email. __If you're not in the list, please contact me.__ I sent the invitation link for GitHub classroom for uploading homework. 11 | * See this wikipage for [python install](https://github.com/PHBS/2017.M1.ASP/wiki/Python-Resources) and [downloading github desktop install file](https://github.com/PHBS/2017.M1.ASP/wiki/Github-Desktop-Download). The Github install files are now in [CMS for this course](http://cms.phbs.pku.edu.cn/claroline/document/document.php?cidReset=true&cidReq=FIN514). 12 | 13 | ## Lectures: 14 | * __18 (11/9 Thurs)__ Project presentations 15 | * __17 (11/6 Mon)__ Project presentations 16 | * __16 (11/2 Thurs)__ Copula contniued ([Py demo](py/Demo_Copula.ipynb)), NSVh model, guideline for course project 17 | * __15 (10/30 Mon)__ Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 18 | * __14__ (10/26 Thurs) Mid-term Exam (Open-book but no computer use. __Rm 403__) [Solution](files/ASP_Midterm_Solution.pdf) 19 | * __NO CLASS__ on 11/23 Mon 20 | * __13__ (10/19 Thurs): [Preview for mid-term exam](files/exam_preview.md) 21 | * __12__ (10/16 Mon): Suggested topics for final projects, OOP in python 22 | * __11__ (10/12 Thurs): Conditional MC method for SABR model ([Slides](files/SABRmodel.pdf)), HW4 [Starter Code](https://github.com/PHBS-2017-ASP-Classroom/SABRModel_Base). Demo on [Gauss-Hermite quadrature](https://github.com/PHBS-2017-ASP-Classroom/SABRmodel_Base/blob/master/Demo_GHQ.ipynb), [python import](https://github.com/PHBS-2017-ASP-Classroom/SABRmodel_Base/blob/master/Demo_Advanced_Import.ipynb) and [vectorization](https://github.com/PHBS-2017-ASP-Classroom/SABRmodel_Base/blob/master/Demo_Vectorize.ipynb). 23 | * __10__ (10/09 Mon): SABR model continued ([Slides](files/SABRmodel.pdf)) 24 | * __09__ (09/28 Thurs): Starter code for [HW3](https://github.com/PHBS-2017-ASP-Classroom/SpreadBasketOptions_Base). SABR model ([Slides](files/SABRmodel.pdf)) 25 | * __08__ (09/25 Tues): Spread/Basket options continued(HW3) ([Slides](files/SpreadBasketOption.pdf)), MC simulation for BSM and normal models ([Py demo](py/BlackScholes_MC.ipynb)) 26 | * __07__ (09/21 Thu): [HW2](https://github.com/PHBS-2017-ASP-Classroom/BSMmodel_Base), Github desktop, Python debugging, Spread/Basket options (HW3) ([Slides](files/SpreadBasketOption.pdf)) 27 | * __06__ (09/20 Wed): Stochastic process review. ([Course note]( https://github.com/PHBS/2016.M3.StoFin/blob/master/files/Notes%20Steele.pdf) from stochastic finance course) 28 | * __05__ (09/18 Mon): Implied volatility ([Slides](files/ImpVol.pdf), [Py demo](py/BlackScholes_ImpliedVol.ipynb)), Vector and Matrix in numpy and RN generation for correlated normals ([Py demo](py/BlackScholes_VectorMatrix.ipynb)) 29 | * __04__ (09/14 Thu): Random number generation continued: Box-Muller/Marsaglia method. [Normal model](files/NormalModel.pdf). 30 | * __03__ (09/11 Mon): Python crash course ([Cheatsheet](py/Cheatsheet_Derek_Banas.ipynb), [Black-Scholes implementation](py/BlackScholes_FunctionVsClass.ipynb) in notebook) 31 | * __02__ (09/07 Thu): Scientific computing, Monte Carlo method, Random number generation ([Slides](files/MCmethod.pdf), [Py demo](py/MC_Demo.ipynb)). [Grouping for HW/projects, Software installation] 32 | * __01__ (09/04 Mon): Course overview ([Syllabus](files/syllabus.pdf)), Probability Statistics Review ([Slides](files/ProbStatsReview.pdf)) 33 | 34 | ## Course Project: 35 | Among the 3 HW problems, do an in-depth research on one topic. 36 | 37 | * [HW2](https://github.com/PHBS-2017-ASP-Classroom/BSMmodel_Base): Black-Scholes-Merton (lognormal) vs Bachelier (normal) model: price, greeks and implied volatility 38 | * [HW3](https://github.com/PHBS-2017-ASP-Classroom/SpreadBasketOptions_Base): Spread/Basket (multi-asset) option pricing 39 | * [HW4](https://github.com/PHBS-2017-ASP-Classroom/SABRModel_Base): SABR model (Stochastic volatility) 40 | 41 | You are very welcome to do the project on your own original idea and you will get additional credit for that. Otherwise, pick one from my suggestions which are basically understanding and implementing literatures. The github repository for the project should be consist of 42 | 43 | * Core implementation (.py): python class and functions 44 | * Make sure to comment in detail. 45 | * Put them in a separate subfolder (e.g., option_models) Do not mix with testing/manual notebook files 46 | * Documentation/Manual (.ipynb): one Jupyter notebook file briefly describing the method (base theory, equations, SDE, strength/weakness, etc), the function prototype and arguments (manual style) and the usage examples 47 | * The best examples are from numpy documentation: [example](https://docs.scipy.org/doc/numpy-1.10.1/reference/routines.polynomials.hermite.html) 48 | * Validation/Test (.ipynb): one Jupyter notebook file briefly test the code/model. 49 | * Be a bit creative here 50 | * BSM/Normal model: make sure to include the analytic-vs-numerical risk test. 51 | * SV (SABR/Heston): make sure that the price converge to BSM/Normal if alpha(vov parameter) goes to 0 52 | * Spread/Basket: make sure that the price is same as single asset BSM if the weigit is 1 for only one asset and zero otherwise. 53 | 54 | Other guidelines for the course project: 55 | * The contribution will be individually graded. Make sure to show the contribution via github desktop commits (not online upload). 56 | * The presentation next week doesn't have to be complete. Show your plan and understanding so far, e.g. function prototypes & arguments, etc and the tests to put on. 57 | 58 | ## Homeworks: 59 | ### __Set 4__ ([Starter Code](https://github.com/PHBS-2017-ASP-Classroom/SABRModel_Base))[Due by 10/23 Mon 11 PM, By group]: 60 | 61 | SABR model option pricing: 62 | * Volatility smile calibration with 3 option prices (or volatilities) using provided Hagan's formula 63 | * Monte-Carlo and Conditional MC methods 64 | 65 | ### __Set 3__ ([Starter Code](https://github.com/PHBS-2017-ASP-Classroom/SpreadBasketOptions_Base))[Due by 10/10 Tues 11 PM, By group]: 66 | 67 | Pricing basket/spread options with Monte-Carlo with control variate. 68 | 69 | ### __Set 2__ ([Starter Code](https://github.com/PHBS-2017-ASP-Classroom/BSMmodel_Base))[Due by 9/30 Sat 11 PM, By group]: 70 | 71 | Write code for Black-Scholes-Merton and Normal model: vanilla option (call/put) price, delta, vega, CDF (digital), and price from Monte-Carlo. Implement them using python class. I am going to provide the base code from which you can build your own. 72 | 73 | ### __Set 1__ [Due by 9.18 Mon 11 PM, Individual]: 74 | 75 | Assume that you keep throwing a coin (H/T probabilitly p/q=1-p) __until you get two heads in a row__? Write a python function to compute this expected number of coin throw using __Monte Carlo__ method. Get one answer by averaging N simulations, and obtain M answers. Get the mean and standard deviation increasing N with fixed M (e.g. say N=100,200,400,800 and M=1000). Run the same experiment for p = 0.5, 0.25, 0.05. __Please comment on your codes so that I can understand what your code does.__ 76 | 77 | You can find the true answer for p=q=0.5 in the [HW answer](https://github.com/PHBS/2016.M3.StoFin/blob/master/files/StoFin_HW_Solution.pdf) of Stochastic finance course. You can easily generalize for general p/q. 78 | 79 | ## Classes: 80 | * Lectures: Monday & Thursday 1:30 – 3:20 PM 81 | * Venue: PHBS Building, Room 313 82 | 83 | ## Instructor: [Jaehyuk Choi](http://www.jaehyukchoi.net/phbs_en) 84 | * Office: PHBS Building, Room 755 85 | * Phone: 86-755-2603-0568 86 | * Email: jaehyuk@phbs.pku.edu.cn 87 | * Office Hour: Monday & Thursday 11AM – 12PM or by appointment 88 | 89 | ## Teaching Assistance: 葛德生 (Devon Ge) 90 | While TA may not know the course contents, TA can help you on python programming issues. So please take advantage of the TA office hours. 91 | * Email: 1701213756@sz.pku.edu.cn 92 | * TA Office Hour: Tuesday 10:30AM - 12:20PM, Friday 1:30PM - 3:20PM (Room 213/214) 93 | 94 | ## Course overview: 95 | Applied Stochastic Processes (ASP) is intended for the students who are 96 | seeking advanced knowledge in stochastic calculus and are eventually interested in the jobs in 97 | financial engineering. As the name indicates, the course will emphasis on applications such as 98 | numerical calculation and programming. On completion of this course, the students will learn 99 | how financial observations (e.g. stock prices and FX rate) are modelled with stochastic 100 | processes and how they can be computed using analytics or computer simulations. 101 | 102 | ## Prerequisites: 103 | Undergraduate-level knowledge in probability, statistics, linear algebra and 104 | programming skill (R recommended) are highly recommended. [Stochastic Finance](https://github.com/PHBS/2016.M3.StoFin) (FIN 519), 105 | a year 1 required course for quantitative finance program, is also highly recommended as it 106 | provides theoretical background. However these are not mandatory prerequisites. The 107 | students without these recommended prerequisites are still encouraged to take this course if 108 | interested, but are expected to take extra efforts. 109 | 110 | ## Textbooks and Reading Materials 111 | * Monte Carlo Methods in Finance by Peter Jaeckel 112 | * Option Valuation Under Stochastic Volatility by Alan Lewis 113 | * [Stochastic Calculus and Financial Applications](http://www-stat.wharton.upenn.edu/~steele/StochasticCalculus.html) by J. Michael Steele 114 | ([Stochastic finance course notes](https://github.com/PHBS/2016.M3.StoFin/blob/master/files/Notes%20Steele.pdf)) 115 | 116 | ## Assessment/Grading Details 117 | Attendance 20%, Mid-term Exam 25%, Assignments 25%, Course Project 30% 118 | (Mid-term exam will be taken on Oct 26 in the 7th week. There will be no final exam.) 119 | -------------------------------------------------------------------------------- /past-years/2018-19-M1/Project.md: -------------------------------------------------------------------------------- 1 | # ASP Course Project 2 | 3 | ## Project List [[link](https://github.com/PHBS/pyfedev_ASP2018/projects)] 4 | * __Team Jieqiang Tang__: [SABR Simulation] [Efficient SABR simulation by Leitao et al](http://dx.doi.org/10.1016/j.amc.2016.08.030) 5 | * __Team Junjie Zhang__: [BSM Implied Volatility] Analytic approximation of BSM implied volatility ([Jackel 2016](jaeckel.16mb.com/LetsBeRational.pdf)): implement the method, include it to BSM class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 6 | * __Team Linsheng Zhuang__: [[Heston Model](https://en.wikipedia.org/wiki/Heston_model)]: Option pricing by exact Monte-Carlo + Fast Fourier Transform (FFT) method. Contact me for more information. 7 | * __Team Ting Xie__:[[Heston Model](https://en.wikipedia.org/wiki/Heston_model)]: Option pricing by exact Monte-Carlo + Fast Fourier Transform (FFT) method. Contact me for more information. 8 | * __Team Raphael Thomas__: Heston model simulation 9 | * ... 10 | 11 | ## Topics 12 | * Among the topics and HWs covered in the class, choose an in-depth research on one topic. You are also welcome to do the project on your own original idea. Otherwise, pick one from my suggestions which are basically understanding and implementing literatures. Topics includes 13 | * BSM (lognormal) vs Bachelier (normal) model 14 | * Spread/Basket/Asian option pricing 15 | * SABR model and other stochastic volatility models 16 | 17 | ## Repository: 18 | * https://github.com/PHBS/pyfedev_ASP2018 19 | 20 | ## File requirements 21 | * Core implementation (.py): python class and functions 22 | * Make sure to comment in detail. 23 | * __Integrate into the `pyfe` folder.__ Do not mix with testing/manual notebook files. 24 | * Documentation/Manual (__.ipynb__): one Jupyter notebook file briefly describing the method (base theory, equations, SDE, strength/weakness, etc), the function prototype and arguments (manual style) and the usage examples 25 | * The best examples are from numpy documentation: [example](https://docs.scipy.org/doc/numpy-1.10.1/reference/routines.polynomials.hermite.html) 26 | * Validation/Test (__.py__): one python file briefly test the code/model. 27 | * Think creatively how to avoid possible errors. For example, 28 | * BSM/Normal model: make sure to include the analytic-vs-numerical risk test. 29 | * SV (SABR/Heston): make sure that the price converge to BSM/Normal if alpha(vov parameter) goes to 0 30 | * Spread/Basket: make sure that the price is same as single asset BSM if the weigit is 1 for only one asset and zero otherwise. 31 | 32 | ## Other guidelines 33 | * The contribution will be individually graded. Make sure to show the contribution via github desktop commits (not online upload). 34 | * The presentation next week doesn't have to be complete. Show your plan and understanding so far, e.g. function prototypes & arguments, etc and the tests to put on. 35 | 36 | ## Suggested Papers 37 | * [BSM Implied Volatility] Analytic approximation of BSM implied volatility ([Jackel 2016](jaeckel.16mb.com/LetsBeRational.pdf)): implement the method, include it to BSM class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 38 | 39 | * [[Heston Model](https://en.wikipedia.org/wiki/Heston_model)]: Option pricing by exact Monte-Carlo + Fast Fourier Transform (FFT) method. Contact me for more information. 40 | 41 | * [SABR approximations] Various improvement to Hagan's original paper: [Paulot](https://arxiv.org/abs/0906.0658), [Obloj](https://arxiv.org/abs/0708.0998), [Balland](http://janroman.dhis.org/finance/OIS/Artiklar%20%C3%B6vrigt/SABR%20goes%20Normal.pdf) 42 | , etc. Contact me for more information. 43 | 44 | * [SABR Simulation] [Efficient SABR simulation by Leitao et al](http://dx.doi.org/10.1016/j.amc.2016.08.030) 45 | 46 | * [SABR Simulation/Interpolation] SABR simulation by stochastic sollocation Monte Carlo method ( [paper 1](https://ssrn.com/abstract=2529691), [paper 2](https://ssrn.com/abstract=2529684) ) 47 | 48 | * [Vanna-Volga method] a smile interpolation method ([Wiki](https://en.wikipedia.org/wiki/Vanna-Volga_pricing), [BSM](https://arxiv.org/abs/0904.1074), [normal](https://arxiv.org/abs/1810.07457) ) 49 | 50 | ## Completed Papers 51 | * ~[Normal Implied Volatility]~ Analytic approximation of normal implied volatility ([Choi et al 2007](http://jaehyukchoi.com/research/normvol/index.html)): (Contact me if you want to take this project) implement the method, include it to Normal class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 52 | * ~[Normal Implied Volatility]~ Analytic approximation of normal implied volatility ([Le Floc'h 2016](https://ssrn.com/abstract=2420757), [some discussion](https://www.clarusft.com/analytic-implied-basis-point-volatility/)): implement the method, include it to Normal class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 53 | * ~[Spread option]~ Very accurate analytic approximation for spread options by [Li et al, 2006](https://ssrn.com/abstract_id=952747). 54 | Also implement Kirk's approximation covered in class and [(Bjersund, Stensland 2014)](http://ssrn.com/abstract_id=1145206). Implement each method in a new class. In python notebook, summarize the method, write a quick help and report strength and weakness. Compare the three methods. 55 | * ~[Basket+Asian option]~ Pick one method in the following 3 methods in survey of basket option pricing (Krekel at al, 2004, Wilmott magazine, July, 82-89): Implement the method in a new class. In python notebook, summarize the method, write a quick help and report strength and weakness. 56 | * a) Beisser's conditional expectation 57 | * c) Levy's log-normal moment matching 58 | * d) Ju's Taylor expansion 59 | * ~[Basket/Spread/Asian Option]~ A unified numerical method for both spread and basket options: [Choi (2017)](http://papers.ssrn.com/abstract_id=2913048): Contact me 60 | * ~[CEV Model]~ Option pricing under Constant Elasticity of Variance (CEV) model (formula available many on-line sources): 61 | implement the method, include it to Normal class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 62 | * ~[SABR Option Pricing]~ Arbitrage-free pricing method by [Kennedy et al, 2011](http://www.tandfonline.com/doi/abs/10.1080/1350486X.2011.646523) ([Download](http://ssrn.com/abstract_id=2043504)): simpler approach introduced in class is enough. Implement the method, create a new class ModelKennedy in sabr.py, and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 63 | * ~[SABR Calibration]~ Efficient first guess for SABR calibrations to 3 option prices by [Le Floc’h and Kennedy, 2014](https://ssrn.com/abstract_id=2467231) (see also [here](https://www.clarusft.com/sabr-calibration-a-simple-explicit-initial-guess/)): Implement the method, add to sabr.py, and write a code comparing the efficient against the dumb initial guess. In python notebook, summarize the method, write a quick help and report strength and weakness. 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /past-years/2018-19-M1/README.md: -------------------------------------------------------------------------------- 1 | # Applied Stochastic Processes (FIN 514, 2018-19 Module 1) 2 | 3 | ## Announcements 4 | * [Course project](files/Project.md): [pyfedev_ASP2018](https://github.com/PHBS/pyfedev_ASP2018) 5 | * [Midterm exam solution](files/ASP2018_Midterm.pdf) is uploaded 6 | * HW4 ([Starter Code](py/HW4/TestCode_SABR.ipynb)) is due by 10.26 (Fri) 7 | * HW3 ([Starter Code](py/HW3/TestCode_BasketSpread.ipynb)) is due by 10.9 (Tues) 8 | * HW2 ([Starter Code](py/HW2/HW2.ipynb)) is due by 9.21 (Fri), [Solution](py/HW2/HW2_Solution.ipynb) was uploaded. 9 | * Python Crash Course will be on 9.12 (Wed) **1:30 PM** (changed). Class mailing list created. 10 | * Email is the preferred method of communication. Class mailing list will be created as PHBS.ASP@allmail.net. 11 | 12 | ## Course Slides and Past Exams 13 | * Prelims: [Probability Statistics Review](files/Prob_Stat_Review.pdf) | [MC Method](files/MCmethod.pdf) ([Py demo](py/MC_Demo.ipynb)) 14 | * Past Exam: 2016-17 StoFin [Midterm](files/SF2016_Midterm.pdf) and [Final](files/SF2016_Final.pdf) | [2017-18 ASP](files/ASP2017_Midterm.pdf) | [2018-19 ASP](files/ASP2018_Midterm.pdf) 15 | * Black-Scholes model ([Py demo](py/BlackScholes_ImpliedVol.ipynb), [MC demo](py/BlackScholes_MC.ipynb)): Also see Ch. 10 of [StoFin Course Notes](https://github.com/PHBS/2018.M3.StoFin/blob/master/files/SCFA_Notes.pdf) 16 | * Normal (Bachelier) model ([Slides](files/Normal_Model.pdf)) 17 | * Implied volatility ([Slides](files/ImpVol.pdf), [Py demo](py/BlackScholes_ImpliedVol.ipynb)) 18 | * Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)) 19 | * SABR model ([Slides](files/SABRmodel.pdf)) 20 | * Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 21 | 22 | ## Other Resources 23 | * **Python** course offered by Jake Zhao: Tue & Fri 10:30 AM (Rm 225) 24 | 25 | ## Lectures 26 | * __18__ (11.09 Tues): Course project presentation 27 | * __17__ (11.06 Tues): Research Presentation (NSVh model) and HW4 review 28 | * __16__ (11.02 Fri): Research Presentation (Sum of BSM models) and HW3 review 29 | * __15__ (10.30 Tues): Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)), Github Pull-request 30 | * __14__ (10.26 Fri): Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 31 | * __13__ (10.23 Tues): Midterm exam ([Solution](files/ASP2018_Midterm.pdf)) 32 | * __12__ (10.19 Fri): Review for midterm exam 33 | * __NO CLASS__ on 10.16 Tues 34 | * __11__ (10.12 Fri): SABR model ([Slides](files/SABRmodel.pdf)): Conditional MC method 35 | * __10__ (10.09 Tues): HW2 review, SABR model ([Slides](files/SABRmodel.pdf)), Stochastic Finance review 36 | * __09__ (09.28 Fri): SABR model([Slides](files/SABRmodel.pdf): Volatility smile, Local volatility model) 37 | * __08__ (09.25 Tues): [Spread/Basket option implementation](py/TestCode_BasketSpread.ipynb), Debugging in Python, Import([Py Demo](py/HW4/Demo_Advanced_Import.ipynb)) 38 | * __07__ (09.21 Fri): [Black-Scholes Implementation](https://github.com/PHBS-2017-ASP-Classroom/BSMmodel_Base), Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)) 39 | * __06__ (09.18 Tues): Black-Scholes and Normal models in MC ([Py Demo](py/BlackScholes_MC.ipynb)), Normal model ([Slides](files/Normal_Model.pdf)), Correlated Normal RNs ([Py Demo](py/CorrelatedNormals_Demo.ipynb)) 40 | * __05__ (09.14 Fri): HW2, Black-Scholes implementation ([Py Demo](py/BlackScholes_FunctionVsClass.ipynb)), Implied volatility ([Slides](files/ImpVol.pdf), [Py demo](py/BlackScholes_ImpliedVol.ipynb)) 41 | * __04__ (__09.12 Wed__ instead of __10.16 Tues__): Python crash course ([Basic](py/PythonCrashCourse_Derek_Banas.ipynb) | [Numpy](py/PythonCrashCourse_Numpy.ipynb)). More [cheatsheets](https://ehmatthes.github.io/pcc/cheatsheets/README.html) also available in [MLF CMS](http://cms.phbs.pku.edu.cn/claroline/document/document.php?cidReset=true&cidReq=FN570). 42 | * __03__ (09.11 Tues): Continued ([Py demo](py/MC_Demo.ipynb)) 43 | * __02__ (09.07 Fri): Scientific computing, Monte Carlo method, Random number generation ([Slides](files/MCmethod.pdf)). 44 | * __01__ (09.04 Tues): Course overview ([Syllabus](files/syllabus.pdf)), Probability Statistics Review ([Slides](files/Prob_Stat_Review.pdf)) 45 | 46 | ## Homeworks: 47 | * ### __Set 4__ [Due by 10.26 Fri] Simulating SABR model [Starter Code](py/HW4/TestCode_SABR.ipynb) 48 | * ### __Set 3__ [Due by 10.9 Tues] Pricing basket and spread option using MC [Starter Code](py/HW3/TestCode_BasketSpread.ipynb) 49 | * ### __Set 2__ [Due by 9.21 Fri] Pricing corporate (default) bond by MC simulation. [Starter Code](py/HW2/HW2.ipynb), [Solution](py/HW2/HW2_Solution.ipynb) 50 | 51 | * ### __Set 1__ [Due by 09.11 Tues] 52 | * Register on Github.com and send your ID to TA via email. Use your __full name__ in your profile. Accept invitation to the [PHBS organization](https://github.com/orgs/PHBS/people) from TA. Install [Github Desktop](https://desktop.github.com/) (available on `Machine Learning` [CMS](http://cms.phbs.pku.edu.cn/claroline/course/index.php?cid=FN570)). 53 | * Install [Anaconda](https://www.anaconda.com/download/) Python distribution (__3.X version__, 2.X version). Anaconda distribution is core Python + useful scientific computation libraries (e.g., numpy, scipy, pandas) + package management system (pip or conda) 54 | * Send the screenshot of both softwares installed to TA. (Example: [Github Desktop](files/Choi_Jaehyuk_Github.png), [Anaconda Spyder](files/Choi_Jaehyuk_Python.png)) 55 | 56 | ## Classes: 57 | * Lectures: Tues & Fri 1:30 – 3:20 PM 58 | * Venue: PHBS Building, Room 211 59 | 60 | ## Instructor: [Jaehyuk Choi](http://www.jaehyukchoi.net/phbs_en) 61 | * Office: PHBS Building, Room 755 62 | * Phone: 86-755-2603-0568 63 | * Email: jaehyuk@phbs.pku.edu.cn 64 | * Office Hour: Tues & Fri 10:30 – 11:30 AM or by appointment 65 | 66 | ## Teaching Assistance: Yeda Du (都业达) 67 | * Email: 1601213511@sz.pku.edu.cn 68 | * TA Office Hour: Mon and Thurs, 10:30 AM - 12:30 PM (Room 213/214) 69 | 70 | ## Course overview: 71 | Applied Stochastic Processes (ASP) is intended for the students who are 72 | seeking advanced knowledge in stochastic calculus and are eventually interested in the jobs in 73 | financial engineering. As the name indicates, the course will emphasis on applications such as 74 | numerical calculation and programming. On completion of this course, the students will learn 75 | how financial observations (e.g. stock prices and FX rate) are modelled with stochastic 76 | processes and how they can be computed using analytics or computer simulations. 77 | 78 | ## Prerequisites: 79 | [Stochastic Finance](https://github.com/PHBS/2017.M3.StoFin) (FIN 519), a year 1 required course for quantitative finance program, is also highly recommended as it provides theoretical background. Undergraduate-level knowledge in probability, statistics, linear algebra and programming skill (Python) are highly recommended. The students without these recommended prerequisites are expected to take extra efforts. 80 | 81 | ## Textbooks and Reading Materials 82 | * Monte Carlo Methods in Finance by Peter Jaeckel 83 | * Option Valuation Under Stochastic Volatility by Alan Lewis 84 | * [Stochastic Calculus and Financial Applications](http://www-stat.wharton.upenn.edu/~steele/StochasticCalculus.html) by J. Michael Steele 85 | ([Stochastic finance course notes](https://github.com/PHBS/2016.M3.StoFin/blob/master/files/Notes%20Steele.pdf)) 86 | 87 | ## Assessment/Grading Details 88 | Attendance 20%, Mid-term Exam 30%, Assignments 20%, Course Project 30% 89 | * __Midterm exam__: 10.23 Tues. Open-book exam without computer/phone/calculator use. No final exam. 90 | * __Course project__: Presentation (11.09 Fri). Group up to 3 people. 91 | * __Attendance__: Randomly checked. The score is calculated as 20 – 2`x`(#of absence). Leave request should be made 24 hours before with supporting documents, except for emergency. Job interview/internship cannot be a valid reason for leave 92 | * __Grade__ in letters (e.g., A+, A-, ... ,D+, D, F). __A- or above < 30% and C+ or below > 10%__. 93 | -------------------------------------------------------------------------------- /past-years/2018-19-M1/StudentList.md: -------------------------------------------------------------------------------- 1 | # List of the students' course repositories 2 | 3 | * [JunhanLi](https://www.github.com/JunhanLi/PHBS_ASP_2018) 4 | * [shaokunxu](https://www.github.com/shaokunxu/PHBS_ASP_2018) 5 | * [zhuanglinsheng](https://www.github.com/zhuanglinsheng/PHBS_ASP_2018) 6 | * [AtomMe](https://www.github.com/AtomMe/PHBS_ASP_2018) 7 | * [Jinfengwangwang](https://www.github.com/Jinfengwangwang/PHBS_ASP_2018) 8 | * [yumengwang123](https://www.github.com/yumengwang123/PHBS_ASP_2018) 9 | * [XieTing1995](https://www.github.com/XieTing1995/PHBS_ASP_2018) 10 | * [Davethedata](https://www.github.com/Davethedata/PHBS_ASP_2018) 11 | * [labro](https://www.github.com/labro/PHBS_ASP_2018) 12 | * [XiaoqianZhu1997](https://www.github.com/XiaoqianZhu1997/PHBS_ASP_2018) 13 | * [thomasraph](https://github.com/thomasraph/ASP.2018.Raph) 14 | * [rvilaseca](https://www.github.com/rvilaseca/PHBS_ASP_2018) 15 | * [nico-busch](https://www.github.com/nico-busch/PHBS_ASP_2018) 16 | * [ewagerus](https://www.github.com/ewagerus/PHBS_ASP_2018) 17 | * [Noam-Peleg](https://www.github.com/Noam-Peleg/PHBS_ASP_2018) 18 | -------------------------------------------------------------------------------- /past-years/2019-20-M1/Project.md: -------------------------------------------------------------------------------- 1 | # ASP Course Project 2 | * Course project is due on 11.15 (Friday) night. 3 | 4 | ## [Repository](https://github.com/PHBS/pyfedev-ASP) and [Project List](https://github.com/PHBS/pyfedev-ASP/projects) 5 | * https://github.com/PHBS/pyfedev-ASP 6 | 7 | ## Presentation 8 | ### 11.05 Tue 9 | * [Computing Moments from Laplace Transform](https://github.com/PHBS/pyfedev-ASP/projects/12): Aiyu CAO, Rong CHEN 10 | * [Exact Simulation of the Ornstein-Uhlenbeck Driven Stochastic Volatility Model](https://github.com/PHBS/pyfedev-ASP/projects/23): Lu Jinlei, Minghui CAO 11 | * [Exact simulation of the 3/2 model](https://github.com/PHBS/pyfedev-ASP/projects/17): Li Gong, Lei Wang 12 | * [(Conditional) Monte-Carlo simulation of GARCH-diffusions](https://github.com/PHBS/pyfedev-ASP/projects/26): Yao Li, Erik Nielsen, Hao Li 13 | * Zhang Shiqi 14 | ### 11.08 Fri 15 | * [Exact simulation of diffusions](https://github.com/PHBS/pyfedev-ASP/projects/25): Zhang Zijie, Zhong Lin 16 | * [Simple and efficient simulation of the Heston stochastic volatility model](https://github.com/PHBS/pyfedev-ASP/projects/24): Liu Sheng, Jinchao Du 17 | * [Exact Simulation of Heston Model](https://github.com/PHBS/pyfedev-ASP/projects/13): Zhibin GUO, Yunxia SHI 18 | * [GARCH-diffusion model](https://github.com/PHBS/pyfedev-ASP/projects/14): Xiangrong LI, Mingcong LI 19 | * [Exact Simulation of SABR Model](https://github.com/PHBS/pyfedev-ASP/projects/15): Xinlei Chang, Jian Zhou 20 | 21 | ## Do do 22 | * Create new project wiht **title**, **short description**, and **team**. 23 | 24 | ## Topics 25 | * Among the topics and HWs covered in the class, choose an in-depth research on one topic. You are also welcome to do the project on your own original idea. Otherwise, pick one from my suggestions which are basically understanding and implementing literatures. Topics includes 26 | * BSM (lognormal) vs Bachelier (normal) model 27 | * Spread/Basket/Asian option pricing 28 | * SABR model and other stochastic volatility models 29 | 30 | ## File requirements 31 | * Core implementation (.py): python class and functions 32 | * Make sure to comment in detail. Example: [bsm.py](https://github.com/PHBS/pyfedev-ASP/blob/master/pyfe/bsm.py) 33 | * __Integrate into the `pyfe` folder.__ Do not mix with testing/manual notebook files. 34 | * Documentation/Test (__.ipynb__): one Jupyter notebook file briefly describing the method (base theory, equations, SDE, strength/weakness, etc), the function prototype and arguments (manual style) and the usage examples 35 | * The best examples are from numpy documentation: [example](https://docs.scipy.org/doc/numpy-1.10.1/reference/routines.polynomials.hermite.html) 36 | * Also include examples and tests of your class (e.g., test against reference parameters) 37 | * __Put into the `test` folder. The file name should be Test_YOURPROJECT.ipynb__ 38 | 39 | ## Other guidelines 40 | * Make sure to show the contribution via github desktop commits (not online upload). 41 | * The project doesn't have to be complete by the presentation. If you need more time, show your plan, understanding, progress so far, e.g. function prototypes & arguments, etc and the tests to put on. 42 | 43 | ## Suggested Topics and Papers 44 | ### * Simulations of Heston model 45 | * [Heston Model](https://en.wikipedia.org/wiki/Heston_model) 46 | * Broadie, M., Kaya, Ö., 2006. Exact Simulation of Stochastic Volatility and Other Affine Jump Diffusion Processes. Operations Research 54, 217–231. https://doi.org/10.1287/opre.1050.0247 | Glasserman, P., Kim, K.-K., 2011. Gamma expansion of the Heston stochastic volatility model. Finance Stoch 15, 267–296. https://doi.org/10.1007/s00780-009-0115-y 47 | * Kahl, C., Jäckel, P., 2006. Fast strong approximation Monte Carlo schemes for stochastic volatility models. Quantitative Finance 6, 513–536. https://doi.org/10.1080/14697680600841108 48 | * Andersen, L., 2008. Simple and efficient simulation of the Heston stochastic volatility model. The Journal of Computational Finance 11, 1–42. https://doi.org/10.21314/JCF.2008.189 49 | * Lord, R., Koekkoek, R., Dijk, D.V., 2010. A comparison of biased simulation schemes for stochastic volatility models. Quantitative Finance 10, 177–194. https://doi.org/10.1080/14697680802392496 50 | 51 | ### * GARCH-diffusion model 52 | * Barone-Adesi, G., Rasmussen, H., Ravanelli, C., 2005. An option pricing formula for the GARCH diffusion model. Computational Statistics & Data Analysis, 2nd CSDA Special Issue on Computational Econometrics 49, 287–310. https://doi.org/10.1016/j.csda.2004.05.014 53 | * PhD Thesis of Ravanelli, C., University of Lugano, Switzerland, https://doc.rero.ch/record/4229/files/1_2003ECO001.pdf 54 | * Papadopoulos, Y.A., Lewis, A.L., 2018. A First Option Calibration of the GARCH Diffusion Model by a PDE Method. arXiv:1801.06141 [q-fin]. 55 | 56 | ### * Exact Simulation Scheme 57 | * __General SDE__: Beskos, A., Roberts, G.O., 2005. Exact simulation of diffusions. Ann. Appl. Probab. 15, 2422–2444. https://doi.org/10.1214/105051605000000485 58 | * __OU SV Model__: Li, C., Wu, L., 2019. Exact simulation of the Ornstein–Uhlenbeck driven stochastic volatility model. European Journal of Operational Research 275, 768–779. https://doi.org/10.1016/j.ejor.2018.11.057 59 | * __Heston Model__: Broadie, M., Kaya, Ö., 2006. Exact Simulation of Stochastic Volatility and Other Affine Jump Diffusion Processes. Operations Research 54, 217–231. https://doi.org/10.1287/opre.1050.0247 | Glasserman, P., Kim, K.-K., 2011. Gamma expansion of the Heston stochastic volatility model. Finance Stoch 15, 267–296. https://doi.org/10.1007/s00780-009-0115-y 60 | * __3/2 SV Model__: Baldeaux, J., 2012. Exact simulation of the 3/2 model. Int. J. Theor. Appl. Finan. 15, 1250032. https://doi.org/10.1142/S021902491250032X 61 | * __SABR Model__: Cai, N., Song, Y., Chen, N., 2017. Exact Simulation of the SABR Model. Operations Research 65, 931–951. https://doi.org/10.1287/opre.2017.1617 62 | * __Computing Moments from Laplace Transform__: Choudhury, G.L., Lucantoni, D.M., 1996. Numerical Computation of the Moments of a Probability Distribution from its Transform. Operations Research 44, 368–381. https://doi.org/10.1287/opre.44.2.368 63 | 64 | ### * Rough Volatility 65 | * McCrickerd, R., Pakkanen, M.S., 2018. Turbocharging Monte Carlo pricing for the rough Bergomi model. Quantitative Finance 18, 1877–1886. https://doi.org/10.1080/14697688.2018.1459812 66 | * Bennedsen, M., Lunde, A., Pakkanen, M.S., 2017. Hybrid scheme for Brownian semistationary processes. Finance Stoch 21, 931–965. https://doi.org/10.1007/s00780-017-0335-5 67 | * J. Gatheral's [python code](https://tpq.io/p/rough_volatility_with_python.html) 68 | ### * Unfinished Projects 69 | * [BSM Implied Volatility] Analytic approximation of BSM implied volatility ([Jackel 2016](jaeckel.16mb.com/LetsBeRational.pdf)): implement the method, include it to BSM class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 70 | 71 | ## Completed Papers 72 | * ~[Normal Implied Volatility]~ Analytic approximation of normal implied volatility ([Choi et al 2007](http://jaehyukchoi.com/research/normvol/index.html)): (Contact me if you want to take this project) implement the method, include it to Normal class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 73 | * ~[Normal Implied Volatility]~ Analytic approximation of normal implied volatility ([Le Floc'h 2016](https://ssrn.com/abstract=2420757), [some discussion](https://www.clarusft.com/analytic-implied-basis-point-volatility/)): implement the method, include it to Normal class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 74 | * ~[Spread option]~ Very accurate analytic approximation for spread options by [Li et al, 2006](https://ssrn.com/abstract_id=952747). 75 | Also implement Kirk's approximation covered in class and [(Bjersund, Stensland 2014)](http://ssrn.com/abstract_id=1145206). Implement each method in a new class. In python notebook, summarize the method, write a quick help and report strength and weakness. Compare the three methods. 76 | * ~[Basket+Asian option]~ Pick one method in the following 3 methods in survey of basket option pricing (Krekel at al, 2004, Wilmott magazine, July, 82-89): Implement the method in a new class. In python notebook, summarize the method, write a quick help and report strength and weakness. 77 | * a) Beisser's conditional expectation 78 | * c) Levy's log-normal moment matching 79 | * d) Ju's Taylor expansion 80 | * ~[Basket/Spread/Asian Option]~ A unified numerical method for both spread and basket options: [Choi (2017)](http://papers.ssrn.com/abstract_id=2913048): Contact me 81 | * ~[CEV Model]~ Option pricing under Constant Elasticity of Variance (CEV) model (formula available many on-line sources): 82 | implement the method, include it to Normal class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 83 | * ~[SABR Option Pricing]~ Arbitrage-free pricing method by [Kennedy et al, 2011](http://www.tandfonline.com/doi/abs/10.1080/1350486X.2011.646523) ([Download](http://ssrn.com/abstract_id=2043504)): simpler approach introduced in class is enough. Implement the method, create a new class ModelKennedy in sabr.py, and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 84 | * ~[SABR Calibration]~ Efficient first guess for SABR calibrations to 3 option prices by [Le Floc’h and Kennedy, 2014](https://ssrn.com/abstract_id=2467231) (see also [here](https://www.clarusft.com/sabr-calibration-a-simple-explicit-initial-guess/)): Implement the method, add to sabr.py, and write a code comparing the efficient against the dumb initial guess. In python notebook, summarize the method, write a quick help and report strength and weakness. 85 | * ~[Vanna-Volga method]~ a smile interpolation method ([Wiki](https://en.wikipedia.org/wiki/Vanna-Volga_pricing), [BSM](https://arxiv.org/abs/0904.1074), [Normal](https://arxiv.org/abs/1810.07457)) 86 | * ~[SABR Simulation]~ [Efficient SABR simulation by Leitao et al](http://dx.doi.org/10.1016/j.amc.2016.08.030) 87 | * ~[SABR approximations]~ Various improvement to Hagan's original paper: [Paulot](https://arxiv.org/abs/0906.0658), [Obloj](https://arxiv.org/abs/0708.0998), [Balland](http://janroman.dhis.org/finance/OIS/Artiklar%20%C3%B6vrigt/SABR%20goes%20Normal.pdf) 88 | , etc. Contact me for more information. 89 | * ~[SABR Simulation/Interpolation]~ SABR simulation by stochastic collocation Monte Carlo method ( [paper 1](https://ssrn.com/abstract=2529691), [paper 2](https://ssrn.com/abstract=2529684) ) 90 | 91 | -------------------------------------------------------------------------------- /past-years/2019-20-M1/README.md: -------------------------------------------------------------------------------- 1 | # Applied Stochastic Processes (FIN 514, 2019-20 Module 1) 2 | 3 | ## Announcements 4 | * Email is the preferred method of communication. Class mailing list will be created as PHBS.ASP@allmail.net. 5 | * **Course project is due on 11.15 (Friday) night.** 6 | 7 | ## Course Slides and Other Resources 8 | * Prelims: [Probability Statistics Review](files/Prob_Stat_Review.pdf) 9 | * Past Exam: [2017-18 Midterm](files/ASP2017_Midterm.pdf), [2018-19 Midterm](files/ASP2018_Midterm.pdf) 10 | * Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) | [Py demo](py/MC_Demo.ipynb)) 11 | * Black-Scholes model ([Py demo](py/BlackScholes_ImpliedVol.ipynb), [MC demo](py/BlackScholes_MC.ipynb)): Also see Ch. 10 of [StoFin Course Notes](https://github.com/PHBS/2018.M3.StoFin/blob/master/files/SCFA_Notes.pdf) 12 | * Normal (Bachelier) model ([Slides](files/Normal_Model.pdf)) from **Stochastic Finance** class 13 | * Implied volatility ([Slides](files/ImpVol.pdf) | [Py demo](py/BlackScholes_ImpliedVol.ipynb)) 14 | * Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)) 15 | * SABR model ([Slides](files/SABRmodel.pdf)) | NSVh model ([Slides](files/NSVh_Slides.pdf)) 16 | * Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 17 | 18 | ## Lectures 19 | No | Date | Contents 20 | --- | :---: | --- 21 | __01__ | 9.03 Tue | Course overview, Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 22 | __02__ | 9.06 Fri | Continued ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 23 | __03__ | 9.10 Tue | Black-Scholes-Merton and Normal option pricing with MC ([Py Demo](py/BlackScholes_MC.ipynb)), Normal model ([Slides](files/Normal_Model.pdf)), Black-Scholes implementation ([Py Demo](py/BlackScholes_FunctionVsClass.ipynb)), Implied volatility ([Slides](files/ImpVol.pdf), [Py demo](py/BlackScholes_ImpliedVol.ipynb)) 24 | __04__ | 9.11 Wed | Python crash course ([Basic](py/PythonCrashCourse_Derek_Banas.ipynb) \| [Numpy](py/PythonCrashCourse_Numpy.ipynb)). More [cheatsheets](https://ehmatthes.github.io/pcc/cheatsheets/README.html) also available in [MLF CMS](http://cms.phbs.pku.edu.cn/claroline/document/document.php?cidReset=true&cidReq=FN570). HW1 25 | __05__ | 9.17 Tue | Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)), Correlated Normal RNs ([Slides](files/MCmethod.pdf) \| [Py Demo](py/CorrelatedNormals_Demo.ipynb)) 26 | __06__ | 9.20 Fri | Spread/Basket options continued, HW2: [Spread/Basket option implementation](py/HW2/TestCode_BasketSpread.ipynb) 27 | __07__ | 9.24 Tue | SABR model ([Slides](files/SABRmodel.pdf): Volatility smile, Local volatility model) 28 | __08__ | 9.27 Fri | SABR model continued ([Slides](files/SABRmodel.pdf): Model intro, Euler/Milstein method). 29 | x | x | __No Class: National Day Week__ 30 | __09__ | 10.08 Tue | SABR model continued ([Slides](files/SABRmodel.pdf): Conditional MC method), Python Import ([Py Demo](py/HW3/Demo_Advanced_Import.ipynb)), Suggested [project topics](files/Project.md). HW3: [MC method for SABR](py/HW3/TestCode_SABR.ipynb) 31 | __10__ | 10.11 Fri | SV Model Simulation for Project ([Slides](files/SV_Simulation.pdf)) 32 | __11__ | 10.15 Tue | Research Presentation: NSVh model and Normal SABR ([Slides](files/NSVh_Slides.pdf)), Introduction to PyFE, Github Pull-request 33 | __12__ | 10.18 Fri | Review for midterm exam (Past Exams: [2017-18](files/ASP2017_Midterm.pdf), [2018-19](files/ASP2018_Midterm.pdf)) 34 | __13__ | 10.22 Tue | Midterm Exam ([Solution](files/ASP2019_Midterm.pdf)) 35 | __14__ | 10.25 Fri | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 36 | __15__ | 10.29 Tue | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 37 | __16__ | 11.01 Fri | Research Presentation: (Sum of BSM models) and HW3 review 38 | __17__ | 11.05 Tue | Course project presentation 39 | __18__ | 11.08 Fri | Course project presentation 40 | 41 | ## Homeworks: 42 | * ### __Set 0__: (Due by 9.6 Fri) 43 | * Register on Github.com and send your ID and student number to Prof. Choi via email (jaehyuk@phbs.pku.edu.cn). Use your __full name__ in your profile. Accept invitation to the [PHBS organization](https://github.com/orgs/PHBS/people) from TA. Install [Github Desktop](https://desktop.github.com/). 44 | * Install [Anaconda](https://www.anaconda.com/download/) Python distribution (__3.X version__, not 2.X version). Anaconda distribution is core Python + useful scientific computation libraries (e.g., numpy, scipy, pandas) + package management system (pip or conda) 45 | * Send the screenshot of Github desktop and Anaconda installed to TA. (Example: [Github Desktop](files/Choi_Jaehyuk_Github.png), [Anaconda Spyder](files/Choi_Jaehyuk_Python.png)) 46 | * ### __Set 1__ [Due by 9.19 Fri] Simple corporate (default) bond pricing by MC simulation. [Starter Code](py/HW1/HW1.ipynb) 47 | * ### __Set 2__ [Due by 9.27 Fri] Pricing basket and spread option using MC. [Starter Code](py/HW2/TestCode_BasketSpread.ipynb) 48 | * ### __Set 3__ [Due by 10.17 Fri] Simulating SABR model. [Starter Code](py/HW3/TestCode_SABR.ipynb) 49 | * ### __Set 4__ 50 | 51 | ## Course Project: [Project Description](files/Project.md) (Previous year: [2018](https://github.com/PHBS/2018.M1.ASP/blob/master/files/Project.md)) 52 | 53 | ## Classes: 54 | * Lectures: Tues & Fri 1:30 – 3:20 PM 55 | * Venue: PHBS Building, Room 209 56 | 57 | ## Instructor: [Jaehyuk Choi](http://www.jaehyukchoi.net/phbs_en) 58 | * Office: PHBS Building, Room 755 59 | * Phone: 86-755-2603-0568 60 | * Email: jaehyuk@phbs.pku.edu.cn 61 | * Office Hour: Tues 11:30AM-12:30PM & Fri 3:30-4:30 PM 62 | 63 | ## Teaching Assistance: TBA 64 | * Email: xxxx@pku.edu.cn 65 | * TA Office Hour: TBA (Room 213/214) 66 | 67 | ## Course overview: 68 | Applied Stochastic Processes (ASP) is intended for the students who are 69 | seeking advanced knowledge in stochastic calculus and are eventually interested in the jobs in 70 | financial engineering. As the name indicates, the course will emphasis on applications such as 71 | numerical calculation and programming. On completion of this course, the students will learn 72 | how financial observations (e.g. stock prices and FX rate) are modelled with stochastic 73 | processes and how they can be computed using analytics or computer simulations. 74 | 75 | ## Prerequisites: 76 | [Stochastic Finance](https://github.com/PHBS/2018.M3.StoFin) (FIN 519), a year 1 required course for quantitative finance program, is a prerequisite for the ASP since it provides theoretical background. Undergraduate-level knowledge in probability, statistics, linear algebra and programming skill (Python) are also highly recommended. 77 | 78 | ## Extra Reading Materials 79 | * Monte Carlo Methods in Finance by Peter Jaeckel 80 | * Option Valuation Under Stochastic Volatility by Alan Lewis 81 | * [Stochastic Calculus and Financial Applications](http://www-stat.wharton.upenn.edu/~steele/StochasticCalculus.html) by J. Michael Steele 82 | ([Stochastic finance course notes](https://github.com/PHBS/2018.M3.StoFin/blob/master/files/SCFA_Notes.pdf)) 83 | 84 | ## Assessment/Grading Details 85 | Attendance 20%, Mid-term Exam 30%, Assignments 20%, Course Project 30% 86 | * __Midterm exam__: 10.22 Tues. Open-book exam without computer/phone/calculator use. No final exam. 87 | * __Course project__: Presentation (Last week). Group up to X people. 88 | * __Attendance__: Randomly checked. The score is calculated as 20 – 2`x`(#of absence). Leave request should be made 24 hours before with supporting documents, except for emergency. Job interview/internship cannot be a valid reason for leave 89 | * __Grade__ in letters (e.g., A+, A-, ... ,D+, D, F). __A- or above < 30% and B- or below > 10%__. 90 | -------------------------------------------------------------------------------- /past-years/2020-21-M3/Project.md: -------------------------------------------------------------------------------- 1 | # ASP Course Project 2 | * Course project is due on 11.15 (Friday) night. 3 | 4 | ## [Repository](https://github.com/pyfe/pyfeng) and [Project List](https://github.com/pyfe/pyfeng/projects) 5 | 6 | ## Presentation and Deadline 7 | * Presentation: 5.07 (Friday) 10 minutes by each group. 8 | * Pull request deadline: 5.9 (Sunday) 9 | 10 | ## File requirements 11 | * Core implementation (.py): python class and functions 12 | * Make sure to add docstring in detail. Example: [bsm.py](https://github.com/PHBS/pyfeng/blob/master/pyfeng/bsm.py) 13 | * __Integrate into the `pyfeng` folder.__ Make pull-requests (pr). 14 | * Documentation/Test (__.ipynb__): one Jupyter notebook file briefly describing the method (base theory, equations, SDE, strength/weakness, etc), the function prototype and arguments (manual style) and the usage examples 15 | * The best examples are from numpy documentation: [example](https://docs.scipy.org/doc/numpy-1.10.1/reference/routines.polynomials.hermite.html) 16 | * Also include examples and tests of your class (e.g., test against reference parameters) 17 | * __Put into the `test` folder. The file name should be Test_YOURPROJECT.ipynb__ 18 | 19 | ## Suggested Topics and Papers 20 | * Ju's Taylor expansion method for basket/Asian options 21 | * Team [[Report](https://github.com/LantianXue/2001212267_2001212401/blob/main/PHBS_ASP_Project/Ju2002_asian_and_basket_option.ipynb)]: [@Feng-Yuze](https://github.com/Feng-Yuze) and [@LantianXue](https://github.com/LantianXue). 22 | * Ju, N. (2002). Pricing Asian and Basket Options Via Taylor Expansion. Journal of Computational Finance, 5(3), 79–103. https://doi.org/10.21314/JCF.2002.088 23 | * (Performance Comparison) Krekel, M., de Kock, J., Korn, R., & Man, T.-K. (2004). An analysis of pricing methods for basket options. Wilmott Magazine, 2004(7), 82–89. See the [notebook](https://github.com/PyFE/PyfengForPapers/blob/main/ipynb/KrekelEtAl2004-Wilmott-BasketOption.ipynb) in PyfengForPapers. 24 | * Implement the method in a new class. In python notebook, summarize the method, write a quick help and report strength and weakness. 25 | * Johnson's SU distribution approximation for basket/Asian options 26 | * Team [[Report](https://github.com/chenyingong/PHBS_ASP_Project/blob/main/Test_Jsu.ipynb)]: [@chenyingong](https://github.com/chenyingong). 27 | * Posner, S. E., & Milevsky, M. A. (1998). Valuing exotic options by approximating the SPD with higher moments. The Journal of Financial Engineering, 7(2). 28 | * Also see Ju (2002) for perfomance comparison 29 | * `Nsvh1` is equivalent to the Johnson's SU distribution. See `Nsvh1.calibrate_vsk` is implemented. 30 | * Tight lower bound for Basket/Asian option 31 | * Team [[Report](https://github.com/daifengqi/PyFENG/blob/asp-v1-daifeng/LowerBound.ipynb)]: [@daifengqi](https://github.com/daifengqi) 32 | * Rogers, L. C. G., & Shi, Z. (1995). The value of an Asian option. Journal of Applied Probability, 32(4), 1077–1088. 33 | * Exact Asian Option Pricing by expansions. 34 | * Team [[Report](https://github.com/Zaynmalivski/ASP/blob/master/py/Test_ExactAsian.ipynb)]: [@Zaynmalivski](https://github.com/Zaynmalivski) and [@cy-wang15](https://github.com/cy-wang15) 35 | * Linetsky, V. (2004). Spectral Expansions for Asian (Average Price) Options. Operations Research, 52(6), 856–867. https://doi.org/10.1287/opre.1040.0113 36 | * Conditional MC simulation for OUSV and Garch 37 | * Team [[Report](https://github.com/PanyuLi/PHBS_ASP_Project/blob/main/Test_CondMC_OUSV_Garch.ipynb)]: [@PanyuLi](https://github.com/PanyuLi) and [@jiangxunmu](https://github.com/jiangxunmu). 38 | * Ornstein Uhlenbeck Simulation. ([Vasicek Model](https://en.wikipedia.org/wiki/Vasicek_model) for interest rate) 39 | * Approximate price formula for uncorrelated GARCH model is implemented: Barone-Adesi, G., Rasmussen, H., Ravanelli, C., 2005. An option pricing formula for the GARCH diffusion model. Computational Statistics & Data Analysis, 2nd CSDA Special Issue on Computational Econometrics 49, 287–310. https://doi.org/10.1016/j.csda.2004.05.014 40 | * Fourier inversion formula for OUSV model is implemented. 41 | * Almost-Exact simulation for Heston model 42 | * Team [[Report](https://github.com/RAY185/PHBS_ASP_Project/blob/main/Test_Heston_MCAe.ipynb)]: [@CharlieSCC](https://github.com/CharlieSCC) and [@RAY185](https://github.com/RAY185) 43 | * Almost-Exact simulation for 3/2 model 44 | * Team [[Report](https://github.com/Hejinzefinance/PHBS_ASP_Project/blob/main/test_for_32model.ipynb)]: [@Hejinzefinance](https://github.com/Hejinzefinance) and [@1901212564](https://github.com/1901212564). 45 | * Conditional MC simulation for Heston, 3/2 and 4/2 models with QE-M scheme 46 | * Team [[Report](https://github.com/XueyangHu/PHBS_ASP_Project/blob/main/Test_Heston_Sv32_QE_CondMC.ipynb)]: [@Delia810](https://github.com/Delia810) and [@XueyangHu](https://github.com/XueyangHu). 47 | * (QE-M scheme) Andersen, L. (2008). Simple and efficient simulation of the Heston stochastic volatility model. The Journal of Computational Finance, 11(3), 1–42. https://doi.org/10.21314/JCF.2008.189 48 | * (Comparison for various simulation schemes) Van Haastrecht, A., & Pelsser, A. (2010). Efficient, almost exact simulation of the heston stochastic volatility model. International Journal of Theoretical and Applied Finance, 13(01), 1–43. https://doi.org/10.1142/S0219024910005668 49 | * (4/2 model) Grasselli, M. (2017). The 4/2 Stochastic Volatility Model: A Unified Approach for the Heston and the 3/2 Model. Mathematical Finance, 27(4), 1013–1034. https://doi.org/10.1111/mafi.12124 50 | 51 | 52 | ## Topics 53 | * Among the topics and HWs covered in the class, choose an in-depth research on one topic. You are also welcome to do the project on your own original idea. Otherwise, pick one from my suggestions which are basically understanding and implementing literatures. Topics includes 54 | * Spread/Basket/Asian option pricing 55 | * SABR other stochastic volatility models 56 | * Copula 57 | 58 | ### * Simulations of Heston model 59 | * [Heston Model](https://en.wikipedia.org/wiki/Heston_model) 60 | * ~Broadie, M., Kaya, Ö., 2006.~ Exact Simulation of Stochastic Volatility and Other Affine Jump Diffusion Processes. Operations Research 54, 217–231. https://doi.org/10.1287/opre.1050.0247 | Glasserman, P., Kim, K.-K., 2011. Gamma expansion of the Heston stochastic volatility model. Finance Stoch 15, 267–296. https://doi.org/10.1007/s00780-009-0115-y 61 | * ~Andersen, L., 2008.~ Simple and efficient simulation of the Heston stochastic volatility model. The Journal of Computational Finance 11, 1–42. https://doi.org/10.21314/JCF.2008.189 62 | * Kahl, C., Jäckel, P., 2006. Fast strong approximation Monte Carlo schemes for stochastic volatility models. Quantitative Finance 6, 513–536. https://doi.org/10.1080/14697680600841108 63 | * Lord, R., Koekkoek, R., Dijk, D.V., 2010. A comparison of biased simulation schemes for stochastic volatility models. Quantitative Finance 10, 177–194. https://doi.org/10.1080/14697680802392496 64 | 65 | ### * Rough Volatility 66 | * McCrickerd, R., Pakkanen, M.S., 2018. Turbocharging Monte Carlo pricing for the rough Bergomi model. Quantitative Finance 18, 1877–1886. https://doi.org/10.1080/14697688.2018.1459812 67 | * Bennedsen, M., Lunde, A., Pakkanen, M.S., 2017. Hybrid scheme for Brownian semistationary processes. Finance Stoch 21, 931–965. https://doi.org/10.1007/s00780-017-0335-5 68 | * J. Gatheral's [python code](https://tpq.io/p/rough_volatility_with_python.html) 69 | 70 | ### * GARCH-diffusion model 71 | * Barone-Adesi, G., Rasmussen, H., Ravanelli, C., 2005. An option pricing formula for the GARCH diffusion model. Computational Statistics & Data Analysis, 2nd CSDA Special Issue on Computational Econometrics 49, 287–310. https://doi.org/10.1016/j.csda.2004.05.014 72 | * PhD Thesis of Ravanelli, C., University of Lugano, Switzerland, https://doc.rero.ch/record/4229/files/1_2003ECO001.pdf 73 | * Papadopoulos, Y.A., Lewis, A.L., 2018. A First Option Calibration of the GARCH Diffusion Model by a PDE Method. arXiv:1801.06141 [q-fin]. 74 | 75 | ### * Exact Simulation Scheme 76 | * __General SDE__: Beskos, A., Roberts, G.O., 2005. Exact simulation of diffusions. Ann. Appl. Probab. 15, 2422–2444. https://doi.org/10.1214/105051605000000485 77 | * __OU SV Model__: Li, C., Wu, L., 2019. Exact simulation of the Ornstein–Uhlenbeck driven stochastic volatility model. European Journal of Operational Research 275, 768–779. https://doi.org/10.1016/j.ejor.2018.11.057 78 | * __Heston Model__: Broadie, M., Kaya, Ö., 2006. Exact Simulation of Stochastic Volatility and Other Affine Jump Diffusion Processes. Operations Research 54, 217–231. https://doi.org/10.1287/opre.1050.0247 | Glasserman, P., Kim, K.-K., 2011. Gamma expansion of the Heston stochastic volatility model. Finance Stoch 15, 267–296. https://doi.org/10.1007/s00780-009-0115-y 79 | * __3/2 SV Model__: Baldeaux, J., 2012. Exact simulation of the 3/2 model. Int. J. Theor. Appl. Finan. 15, 1250032. https://doi.org/10.1142/S021902491250032X 80 | * __SABR Model__: Cai, N., Song, Y., Chen, N., 2017. Exact Simulation of the SABR Model. Operations Research 65, 931–951. https://doi.org/10.1287/opre.2017.1617 81 | * __Computing Moments from Laplace Transform__: Choudhury, G.L., Lucantoni, D.M., 1996. Numerical Computation of the Moments of a Probability Distribution from its Transform. Operations Research 44, 368–381. https://doi.org/10.1287/opre.44.2.368 82 | 83 | ### * CEV Model 84 | * Option pricing under Constant Elasticity of Variance (CEV) model (formula available many on-line sources): 85 | implement the method, include it to Normal class and write a thorough test code. In python notebook, summarize the method, write a quick help and report strength and weakness. 86 | * Basic model is completed. Need to check Greeks (Delta/Gamma/Vega/Theta) 87 | -------------------------------------------------------------------------------- /past-years/2020-21-M3/README.md: -------------------------------------------------------------------------------- 1 | # Applied Stochastic Processes 2020-21 Module 3 (Spring 2021) 2 | 3 | ## Announcements 4 | * Email is the preferred method of communication. Class mailing list will be created as PHBS.ASP@allmail.net. 5 | 6 | ## Course Slides and Other Resources 7 | * Prelims: [Probability Statistics Review](files/Prob_Stat_Review.pdf) 8 | * Past Midterm Exams: [All Exams](files/ASP_Problems.pdf) ([2017-18](files/ASP2017_Midterm.pdf), [2018-19](files/ASP2018_Midterm.pdf), [2019-20](files/ASP2019_Midterm.pdf), [2020-21](files/ASP2020_Midterm.pdf)) 9 | * `PyFeng` package ([PyPI](https://pypi.org/project/pyfeng/) \| [Github](https://github.com/PyFE/PyFENG) \| [Documentation](https://pyfeng.readthedocs.io/)) 10 | * Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) | [Py demo](py/MC_Demo.ipynb)) 11 | * Black-Scholes model ([Py demo](py/BlackScholes_ImpliedVol.ipynb), [MC demo](py/BlackScholes_MC.ipynb)): Also see Ch. 10 of [StoFin Course Notes](https://github.com/PHBS/StoFin/blob/master/files/SCFA_Notes.pdf) 12 | * Normal (Bachelier) model ([Slides](files/Normal_Model.pdf)) from **Stochastic Finance** class 13 | * Implied volatility ([Slides](files/ImpVol.pdf) | [Py demo](py/BlackScholes_ImpliedVol.ipynb)) 14 | * Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)) 15 | * SABR model ([Slides](files/SABRmodel.pdf)) | NSVh model ([Slides](files/NSVh_Slides.pdf)) 16 | * Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 17 | 18 | ## Lectures 19 | No | Date | Contents 20 | --- | :---: | --- 21 | __01__ | 9.03 Tue | Course overview, Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 22 | __02__ | 3.12 Fri | Continued ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 23 | __03__ | 3.16 Tue | Python crash course ([Py Demo](py/PythonCrashCourse_Derek_Banas.ipynb)). More [cheatsheets](https://ehmatthes.github.io/pcc/cheatsheets/README.html) also available in [MLF CMS](http://cms.phbs.pku.edu.cn/claroline/document/document.php?cidReset=true&cidReq=FN570). 24 | __04__ | 3.19 Fri | Numpy crach course ([Py Demo](py/PythonCrashCourse_Numpy.ipynb)). Black-Scholes implementation ([Py Demo](py/BlackScholes_FunctionVsClass.ipynb)). Implied volatility ([Slides](files/ImpVol.pdf) \| [Py demo](py/BlackScholes_ImpliedVol.ipynb)). 25 | __05__ | 3.23 Tue | Bachelier model ([Slides](files/Normal_Model.pdf)). Black-Scholes-Merton and Bachelier option pricing with MC ([Py Demo](py/BlackScholes_MC.ipynb)). Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)). Correlated Normal RNs ([Slides](files/MCmethod.pdf) \| [Py Demo](py/CorrelatedNormals_Demo.ipynb)) 26 | __06__ | 3.26 Fri | Spread/Basket options continued, [HW2: [Spread/Basket option implementation](py/HW2/TestCode_BasketSpread.ipynb), Due next Friday] 27 | __07__ | 3.30 Tue | SABR model ([Slides](files/SABRmodel.pdf): Volatility smile), Suggested [project topics](files/Project.md) 28 | __08__ | 4.02 Fri | SABR model continued ([Slides](files/SABRmodel.pdf): Local volatility model, Model intro), Introduction to [PyFENG](https://github.com/PyFE/PyFENG) package 29 | __09__ | 4.06 Tue | SABR model continued ([Slides](files/SABRmodel.pdf): Euler/Milstein method, Conditional MC), Github pull-request (PR), Py Demo ([SABR](py/SabrModel_Demo.ipynb), [BsmNdMc](BsmNdMc_Demo.ipynb)), HW3: [MC method for SABR](py/HW3/TestCode_SABR.ipynb) 30 | __10__ | 4.09 Fri | Python Import ([Py Demo](py/HW3/Demo_Advanced_Import.ipynb)), SV Model Simulation for Project ([Slides](files/SV_Simulation.pdf)) 31 | __11__ | 4.13 Tue | SV Model Simulation for Project ([Slides](files/SV_Simulation.pdf)), [Past Exams](files/ASP_Problems.pdf) Review 32 | __12__ | 4.16 Fri | [Past Exams](files/ASP_Problems.pdf) Review 33 | __13__ | 4.20 Tue | Midterm Exam ([Solution](files/ASP2020_Midterm.pdf)) 34 | __14__ | 4.23 Fri | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 35 | __15__ | 4.27 Tue | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 36 | __16__ | 4.28 Wed | Research Presentation: NSVh model and Normal SABR ([Slides](files/NSVh_Slides.pdf)) 37 | __17__ | 5.04 Tue | Course project presentation 38 | __18__ | 5.07 Fri | Course project presentation 39 | 40 | 41 | 42 | ## Homeworks: 43 | * ### __Set 0__: (Due by 3.12 Fri) 44 | * Register on Github.com and send your ID and student number to Prof. Choi via email (jaehyuk@phbs.pku.edu.cn). Use your __full name__ in your profile. Accept invitation to the [PHBS organization](https://github.com/orgs/PHBS/people) from TA. Install [Github Desktop](https://desktop.github.com/). 45 | * Install [Anaconda](https://www.anaconda.com/download/) Python distribution (__3.X version__, not 2.X version). Anaconda distribution is core Python + useful scientific computation libraries (e.g., numpy, scipy, pandas) + package management system (pip or conda) 46 | * Send the screenshot of Github desktop and Anaconda installed to TA. (Example: [Github Desktop](files/Choi_Jaehyuk_Github.png), [Anaconda Spyder](files/Choi_Jaehyuk_Python.png)) 47 | * ### __Set 1__ [Due by XXX] Simple corporate (default) bond pricing by MC simulation. [Starter Code](py/HW1/HW1.ipynb) 48 | * ### __Set 2__ [Due by XXX] Pricing basket and spread option using MC. [Starter Code](py/HW2/TestCode_BasketSpread.ipynb) 49 | * ### __Set 3__ [Due by XXX] Simulating SABR model. [Starter Code](py/HW3/TestCode_SABR.ipynb) 50 | 51 | ## Course Project: [Project Description](files/Project.md) (Previous year: [2017](past-years/2017-18-M1/Project.md) | [2018](past-years/2018-19-M1/Project.md) | [2019](past-years/2019-20-M1/Project.md)) 52 | 53 | ## Classes: 54 | * Lectures: Tues & Fri 1:30 – 3:20 PM 55 | * Venue: PHBS Building, Room 417 56 | 57 | ## Instructor: [Jaehyuk Choi](http://www.jaehyukchoi.net/phbs_en) 58 | * Office: PHBS Building, Room 755 59 | * Phone: 86-755-2603-0568 60 | * Email: jaehyuk@phbs.pku.edu.cn 61 | * Office Hour: Tuesday 9-10 PM, Friday 3:30-4:30 PM 62 | 63 | ## Teaching Assistance: TBA 64 | * Email: xxxx@pku.edu.cn 65 | * TA Office Hour: TBA (Room 213/214) 66 | 67 | ## Course overview: 68 | Applied Stochastic Processes (ASP) is intended for the students who are 69 | seeking advanced knowledge in stochastic calculus and are eventually interested in the jobs in 70 | financial engineering. As the name indicates, the course will emphasis on applications such as 71 | numerical calculation and programming. On completion of this course, the students will learn 72 | how financial observations (e.g. stock prices and FX rate) are modelled with stochastic 73 | processes and how they can be computed using analytics or computer simulations. 74 | 75 | ## Prerequisites: 76 | [Stochastic Finance](https://github.com/PHBS/StoFin) (FIN 519), a year 1 required course for quantitative finance program, is a prerequisite for the ASP since it provides theoretical background. Undergraduate-level knowledge in probability, statistics, linear algebra and programming skill (Python) are also highly recommended. 77 | 78 | ## Extra Reading Materials 79 | * Monte Carlo Methods in Finance by Peter Jaeckel 80 | * Option Valuation Under Stochastic Volatility by Alan Lewis 81 | * [Stochastic Calculus and Financial Applications](http://www-stat.wharton.upenn.edu/~steele/StochasticCalculus.html) by J. Michael Steele 82 | ([Stochastic finance course notes](https://github.com/PHBS/2018.M3.StoFin/blob/master/files/SCFA_Notes.pdf)) 83 | 84 | ## Assessment/Grading Details 85 | Attendance 20%, Mid-term Exam 30%, Assignments 20%, Course Project 30% 86 | * __Midterm exam__: 10.22 Tues. Open-book exam without computer/phone/calculator use. No final exam. 87 | * __Course project__: Presentation (Last week). Group up to X people. 88 | * __Attendance__: Randomly checked. The score is calculated as 20 – 2`x`(#of absence). Leave request should be made 24 hours before with supporting documents, except for emergency. Job interview/internship cannot be a valid reason for leave 89 | * __Grade__ in letters (e.g., A+, A-, ... ,D+, D, F). __A- or above < 30% and B- or below > 10%__. 90 | -------------------------------------------------------------------------------- /past-years/2021-22-M3/Project.md: -------------------------------------------------------------------------------- 1 | # ASP Course Project 2 | 3 | ## [Repository](https://github.com/pyfe/pyfeng) and [Project List](https://github.com/pyfe/pyfeng/projects) 4 | 5 | ## List of Projects 6 | * Solving Risk Parity Weights [[Report](https://github.com/lililiyf/ASP/blob/master/RiskParity-ASP-Project-2022.ipynb)] 7 | * [@lililiyf](https://github.com/lililiyf) and [@TinaWang39](https://github.com/TinaWang39) 8 | * Bai X, Scheinberg K, Tutuncu R (2016) Least-squares approach to risk parity in portfolio selection. Quantitative Finance 16:357–376. 9 | * Almost Exact Simulation of the 3/2 Volatility Model [[Report](https://github.com/AHrmnd/ASP-1/blob/master/py/AE_theory.ipynb)] 10 | * [@AHrmnd](https://github.com/AHrmnd) and [@FLXE-Feidou](https://github.com/FLXE-Feidou) 11 | * Simulation of the GARCH Diffusion model [[Report](https://github.com/Harveydentsun/ASP_Project/blob/main/Garch-diffusion%20model.ipynb)] 12 | * [@Harveydentsun](https://github.com/Harveydentsun) and [@yogaball](https://github.com/yogaball) 13 | https://doi.org/10.1080/14697688.2015.1031815 14 | * Lifted Heston Model [[Report](https://github.com/Cecilia525/ASP-Project/blob/main/Report-Lifted%20Heston_v4.ipynb)] 15 | * [@jyyccc](https://github.com/jyyccc) and [@Cecilia525](https://github.com/Cecilia525) 16 | * Jaber EA (2019) Lifting the Heston model. Quantitative Finance 19:1995–2013. https://doi.org/10.1080/14697688.2019.1615113 17 | * Approximate Stochastic Volatility Model Option Pricing [[Report](https://github.com/zwc00098/ASP_Project/blob/main/Test_Ball%20and%20Roma_v2.0.ipynb)] 18 | * [@zwc00098](https://github.com/zwc00098) 19 | * Ball CA, Roma A (1994) Stochastic Volatility Option Pricing. Journal of Financial and Quantitative Analysis 29:589–607. https://doi.org/10.2307/2331111 20 | * Simulation of Rough Volatility Model [[Report](https://github.com/Baiwisher/PyFENG/blob/rVolatility/Report_rVolatility.ipynb)] 21 | * [@polarbluebear](https://github.com/polarbluebear) and [@Baiwisher](https://github.com/Baiwisher) 22 | * Ma J, Wu H (2021) A fast algorithm for simulation of rough volatility models. Quantitative Finance 0:1–16. https://doi.org/10.1080/14697688.2021.1970213 23 | 24 | ## Presentation and Deadline 25 | * Presentation: 4.22 (Friday) 10 minutes by each group. 26 | * Pull request deadline: 4.30 (Saturday) 27 | 28 | ## File requirements 29 | * `PyFENG` repository: 30 | * Core implementation (.py): python class and functions 31 | * Make sure to add docstring in detail. 32 | * Example: [bsm.py](https://github.com/PYFE/pyfeng/blob/master/pyfeng/bsm.py) 33 | * Specify equation/formula number or page in the original paper 34 | * The best examples of docstring are from numpy documentation: [example](https://docs.scipy.org/doc/numpy-1.10.1/reference/routines.polynomials.hermite.html) 35 | * __Integrate into the `PyFENG` repository__ by making pull-requests (pr). 36 | * __Do not__ place any `.ipynb` file. 37 | * Your repository (`PHBS_ASP_2021` or any name is OK) 38 | * Test (__.ipynb__): one Jupyter notebook file briefly describing the method (base theory, equations, SDE, strength/weakness, etc). 39 | * Include some test examples (e.g., the same parameter sets from the paper) 40 | * See past years' proejct: 41 | * [2020-21](https://github.com/PHBS/ASP/blob/master/past-years/2020-21-M1/Project.md) 42 | * [2019-20](https://github.com/PHBS/ASP/blob/master/past-years/2019-20-M1/Project.md) 43 | * [2018-19](https://github.com/PHBS/ASP/blob/master/past-years/2018-19-M1/Project.md) 44 | * [2017-18](https://github.com/PHBS/ASP/blob/master/past-years/2017-18-M1/Project.md) 45 | 46 | # Project Topics 47 | 48 | ## Topics 49 | * Among the topics and HWs covered in the class, choose an in-depth research on one topic. You are also welcome to do the project on your own original idea. Otherwise, pick one from my suggestions which are basically understanding and implementing literatures. Topics includes 50 | * Spread/Basket/Asian option pricing 51 | * SV models (SABR, Heston, etc) 52 | * Rough volatility 53 | * Copula 54 | 55 | ### * Rough Volatility 56 | * Ma J, Wu H (2021) A fast algorithm for simulation of rough volatility models. Quantitative Finance 0:1–16. https://doi.org/10.1080/14697688.2021.1970213 57 | * Jaber EA (2019) Lifting the Heston model. Quantitative Finance 19:1995–2013. https://doi.org/10.1080/14697688.2019.1615113 58 | * Horvath B, Jacquier A, Muguruza A (2019) Functional central limit theorems for rough volatility. arXiv:171103078 [math, q-fin] http://arxiv.org/abs/1711.03078 59 | * McCrickerd R, Pakkanen MS (2018) Turbocharging Monte Carlo pricing for the rough Bergomi model. Quantitative Finance 18:1877–1886. https://doi.org/10.1080/14697688.2018.1459812 60 | * Bennedsen, M., Lunde, A., Pakkanen, M.S., 2017. Hybrid scheme for Brownian semistationary processes. Finance Stoch 21, 931–965. https://doi.org/10.1007/s00780-017-0335-5 61 | * J. Gatheral's [python code](https://tpq.io/p/rough_volatility_with_python.html) 62 | 63 | ### * Spread/Basket/Asian Option 64 | * [[Partially](https://github.com/PyFE/pyfedev/blob/master/pyfe/bsm_multi_choi2018.py) implemented] Choi J (2018) Sum of all Black-Scholes-Merton models: An efficient pricing method for spread, basket, and Asian options. Journal of Futures Markets 38:627–644. https://doi.org/10.1002/fut.21909 65 | * R implementation: https://github.com/PyFE/SumBSM-R 66 | * [Most methods implemented] Krekel M, de Kock J, Korn R, Man T-K (2004) An analysis of pricing methods for basket options. Wilmott Magazine 2004:82–89 67 | 68 | ### Option pricing with Fourier Transform 69 | * Lord R, Kahl C (2010) Complex Logarithms in Heston-Like Models. Mathematical Finance 20:671–694. https://doi.org/10.1111/j.1467-9965.2010.00416.x 70 | * Kahl C, Jäckel P (2005) Not-so-complex Logarithms in the Heston Model. Wilmott 19 71 | * [Implemented but not perfect] __OUSV:__ Schöbel R, Zhu J (1999) Stochastic Volatility With an Ornstein–Uhlenbeck Process: An Extension. Rev Financ 3:23–46. https://doi.org/10.1023/A:1009803506170 72 | 73 | ### * GARCH-diffusion model 74 | * [Implemented] Barone-Adesi G, Rasmussen H, Ravanelli C (2005) An option pricing formula for the GARCH diffusion model. Computational Statistics & Data Analysis 49:287–310. https://doi.org/10.1016/j.csda.2004.05.014 75 | * Tubikanec I, Tamborrino M, Lansky P, Buckwar E (2021) Qualitative properties of numerical methods for the inhomogeneous geometric Brownian motion. arXiv:200310193 [cs, math] http://arxiv.org/abs/2003.10193 76 | * [Implemented] Euler/Milstein/Log scheme (in class) 77 | * [????] Capriotti L, Jiang Y, Shaimerdenova G (2018) Approximation methods for inhomogeneous geometric brownian motion. Int J Theor Appl Finan 22:1850055. https://doi.org/10.1142/S0219024918500553 78 | * __IGBM:__ Zhao B (2009) Inhomogeneous Geometric Brownian Motion. SSRN Electronic Journal. https://doi.org/10.2139/ssrn.1429449 79 | * [Consider] PhD Thesis of Ravanelli, C., University of Lugano, Switzerland, https://doc.rero.ch/record/4229/files/1_2003ECO001.pdf 80 | * [No method] Papadopoulos, Y.A., Lewis, A.L., 2018. A First Option Calibration of the GARCH Diffusion Model by a PDE Method. arXiv:1801.06141 [q-fin]. 81 | 82 | ### * SABR model 83 | * [Implemented but not verified] Cai N, Song Y, Chen N (2017) Exact Simulation of the SABR Model. Oper Res 65:931–951. https://doi.org/10.1287/opre.2017.1617 84 | * [Implemented but not verified] Leitao Á, Grzelak LA, Oosterlee CW (2017) On a one time-step Monte Carlo simulation approach of the SABR model: Application to European options. Applied Mathematics and Computation 293:461–479. https://doi.org/10.1016/j.amc.2016.08.030 85 | 86 | ### * [Heston Model](https://en.wikipedia.org/wiki/Heston_model) (SV) 87 | * [No need] Broadie M, Kaya Ö (2006) Exact Simulation of Stochastic Volatility and Other Affine Jump Diffusion Processes. Operations Research 54:217–231. https://doi.org/10.1287/opre.1050.0247 88 | * [Implemented] Glasserman P, Kim K-K (2011) Gamma expansion of the Heston stochastic volatility model. Finance Stoch 15:267–296. https://doi.org/10.1007/s00780-009-0115-y 89 | * [Implemented] Andersen, L., 2008. Simple and efficient simulation of the Heston stochastic volatility model. The Journal of Computational Finance 11, 1–42. https://doi.org/10.21314/JCF.2008.189 90 | * [Implemented / 2022 Thesis] Almost exact simulation 91 | 92 | ### * OUSV Model 93 | * [Implemented] Euler/Milstein 94 | * [Implemented] Li C, Wu L (2019) Exact simulation of the Ornstein–Uhlenbeck driven stochastic volatility model. European Journal of Operational Research 275:768–779. https://doi.org/10.1016/j.ejor.2018.11.057 95 | * [2021 Thesis] Almost exact simulation 96 | 97 | ### * 3/2 SV model 98 | * [Implemented but not verified] Baldeaux, J., 2012. Exact simulation of the 3/2 model. Int. J. Theor. Appl. Finan. 15, 1250032. https://doi.org/10.1142/S021902491250032X 99 | * [Implemented but not verified] Almost exact simulation 100 | 101 | ### * Other Simulation-related Papers 102 | * [Implemented but not verified] __General SDE__: Beskos, A., Roberts, G.O., 2005. Exact simulation of diffusions. Ann. Appl. Probab. 15, 2422–2444. https://doi.org/10.1214/105051605000000485 103 | * [Implemented] __Computing Moments from Laplace Transform__: Choudhury, G.L., Lucantoni, D.M., 1996. Numerical Computation of the Moments of a Probability Distribution from its Transform. Operations Research 44, 368–381. https://doi.org/10.1287/opre.44.2.368 104 | -------------------------------------------------------------------------------- /past-years/2021-22-M3/README.md: -------------------------------------------------------------------------------- 1 | # Applied Stochastic Processes 2021-22 Module 3 (Spring 2022) 2 | 3 | ## Announcements 4 | * Email is the preferred method of communication. Class mailing list will be created as PHBS.ASP@allmail.net. 5 | 6 | ## Course Slides and Other Resources 7 | * Prelims: [Probability Statistics Review](files/Prob_Stat_Review.pdf) 8 | * Past Midterm Exams: [All Exams](files/ASP_Problems.pdf) ([2017-18](files/ASP2017_Midterm.pdf), [2018-19](files/ASP2018_Midterm.pdf), [2019-20](files/ASP2019_Midterm.pdf), [2020-21](files/ASP2020_Midterm.pdf)) 9 | * `PyFeng` package ([PyPI](https://pypi.org/project/pyfeng/) \| [Github](https://github.com/PyFE/PyFENG) \| [Documentation](https://pyfeng.readthedocs.io/)) 10 | * Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) | [Py demo](py/MC_Demo.ipynb)) 11 | * Black-Scholes model ([Py demo](py/BlackScholes_ImpliedVol.ipynb), [MC demo](py/BlackScholes_MC.ipynb)): Also see Ch. 10 of [StoFin Course Notes](https://github.com/PHBS/StoFin/blob/master/files/SCFA_Notes.pdf) 12 | * Normal (Bachelier) model ([Slides](files/Normal_Model.pdf)) from **Stochastic Finance** class 13 | * Implied volatility ([Slides](files/ImpVol.pdf) | [Py demo](py/BlackScholes_ImpliedVol.ipynb)) 14 | * Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)) 15 | * SABR model ([Slides](files/SABRmodel.pdf)) | NSVh model ([Slides](files/NSVh_Slides.pdf)) 16 | * Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 17 | 18 | ## Lectures 19 | No | Date | Contents 20 | --- | :---: | --- 21 | __01__ | 2.22 Tue | Course overview, Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 22 | __02__ | 2.25 Fri | Continued ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 23 | __03__ | 3.01 Tue | Python crash course ([Py Demo](py/PythonCrashCourse_Derek_Banas.ipynb)). More [cheatsheets](https://ehmatthes.github.io/pcc/cheatsheets/README.html) also available in [MLF CMS](http://cms.phbs.pku.edu.cn/claroline/document/document.php?cidReset=true&cidReq=FN570). Numpy crach course ([Py Demo](py/PythonCrashCourse_Numpy.ipynb)). 24 | __04__ | 3.04 Fri |Black-Scholes implementation ([Py Demo](py/BlackScholes_FunctionVsClass.ipynb)). Implied volatility ([Slides](files/ImpVol.pdf) \| [Py demo](py/BlackScholes_ImpliedVol.ipynb)). 25 | __05__ | 3.08 Tue | Bachelier model ([Slides](files/Normal_Model.pdf)). Black-Scholes-Merton and Bachelier option pricing with MC ([Py Demo](py/BlackScholes_MC.ipynb)). Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)). Correlated Normal RNs ([Slides](files/MCmethod.pdf) \| [Py Demo](py/CorrelatedNormals_Demo.ipynb)) 26 | __06__ | 3.11 Fri | Spread/Basket options continued, [HW2: [Spread/Basket option implementation](py/HW2/TestCode_BasketSpread.ipynb), Due next Friday] 27 | __07__ | 3.15 Tue | SABR model ([Slides](files/SABRmodel.pdf): Volatility smile), Suggested [project topics](files/Project.md) 28 | __08__ | 3.18 Fri | SABR model continued ([Slides](files/SABRmodel.pdf): Local volatility model, Model intro), Introduction to [PyFENG](https://github.com/PyFE/PyFENG) package 29 | __09__ | 3.22 Tue | SABR model continued ([Slides](files/SABRmodel.pdf): Euler/Milstein method, Conditional MC), Github pull-request (PR), Py Demo ([SABR](py/SabrModel_Demo.ipynb), [BsmNdMc](BsmNdMc_Demo.ipynb)), HW3: [MC method for SABR](py/HW3/TestCode_SABR.ipynb) 30 | __10__ | 3.25 Fri | Python Import ([Py Demo](py/HW3/Demo_Advanced_Import.ipynb)), SV Model Simulation for Project ([Slides](files/SV_Simulation.pdf)) 31 | __11__ | 3.29 Tue | SV Model Simulation for Project ([Slides](files/SV_Simulation.pdf)), [Past Exams](files/ASP_Problems.pdf) Review 32 | __12__ | 4.01 Fri | [Past Exams](files/ASP_Problems.pdf) Review 33 | __13__ | 4.06 __Wed__ | Midterm Exam ([Solution](files/ASP2020_Midterm.pdf)) 34 | __14__ | 4.88 Fri | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 35 | __15__ | 4.12 Tue | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 36 | __16__ | 4.15 Wed | Research Presentation: NSVh model and Normal SABR ([Slides](files/NSVh_Slides.pdf)) 37 | __17__ | 4.19 Tue | Course project presentation 38 | __18__ | 4.22 Fri | Course project presentation 39 | 40 | 41 | 42 | ## Homeworks: 43 | * ### __Set 0__: (Due by 2.24 Fri) 44 | * Register on Github.com and send your ID and student number to Prof. Choi via email (jaehyuk@phbs.pku.edu.cn). Use your __full name__ in your profile. Accept invitation to the [PHBS organization](https://github.com/orgs/PHBS/people) from TA. Install [Github Desktop](https://desktop.github.com/). 45 | * Install [Anaconda](https://www.anaconda.com/download/) Python distribution (__3.X version__, not 2.X version). Anaconda distribution is core Python + useful scientific computation libraries (e.g., numpy, scipy, pandas) + package management system (pip or conda) 46 | * Send the screenshot of Github desktop and Anaconda installed to TA. (Example: [Github Desktop](files/Choi_Jaehyuk_Github.png), [Anaconda Spyder](files/Choi_Jaehyuk_Python.png)) 47 | * ### __Set 1__ [Due by XXX] Simple corporate (default) bond pricing by MC simulation. [Starter Code](py/HW1/HW1.ipynb) 48 | * ### __Set 2__ [Due by XXX] Pricing basket and spread option using MC. [Starter Code](py/HW2/TestCode_BasketSpread.ipynb) 49 | * ### __Set 3__ [Due by XXX] Simulating SABR model. [Starter Code](py/HW3/TestCode_SABR.ipynb) 50 | 51 | ## Course Project: [Project Description](files/Project.md) (Previous year: [2017](past-years/2017-18-M1/Project.md) | [2018](past-years/2018-19-M1/Project.md) | [2019](past-years/2019-20-M1/Project.md)) 52 | 53 | ## Classes: 54 | * Lectures: Tues & Fri 1:30 – 3:20 PM 55 | * Venue: PHBS Building, Room 229 56 | 57 | ## Instructor: [Jaehyuk Choi](http://www.jaehyukchoi.net/phbs_en) 58 | * Office: PHBS Building, Room 755 59 | * Phone: 86-755-2603-0568 60 | * Email: jaehyuk@phbs.pku.edu.cn 61 | * Office Hour: __TBA__ 62 | 63 | ## Teaching Assistance: NING Lei (宁磊) 64 | * Email: leining@stu.pku.edu.cn 65 | * TA Office Hour: __TBA__ (Room 213/214) 66 | 67 | ## Course overview: 68 | Applied Stochastic Processes (ASP) is intended for the students who are 69 | seeking advanced knowledge in stochastic calculus and are eventually interested in the jobs in 70 | financial engineering. As the name indicates, the course will emphasis on applications such as 71 | numerical calculation and programming. On completion of this course, the students will learn 72 | how financial observations (e.g. stock prices and FX rate) are modelled with stochastic 73 | processes and how they can be computed using analytics or computer simulations. 74 | 75 | ## Prerequisites: 76 | [Stochastic Finance](https://github.com/PHBS/StoFin) (FIN 519), a year 1 required course for quantitative finance program, is a prerequisite for the ASP since it provides theoretical background. Undergraduate-level knowledge in probability, statistics, linear algebra and programming skill (Python) are also highly recommended. 77 | 78 | ## Extra Reading Materials 79 | * Monte Carlo Methods in Finance by Peter Jaeckel 80 | * Option Valuation Under Stochastic Volatility by Alan Lewis 81 | * [Stochastic Calculus and Financial Applications](http://www-stat.wharton.upenn.edu/~steele/StochasticCalculus.html) by J. Michael Steele 82 | ([Stochastic finance course notes](https://github.com/PHBS/2018.M3.StoFin/blob/master/files/SCFA_Notes.pdf)) 83 | 84 | ## Assessment/Grading Details 85 | Attendance 20%, Mid-term Exam 30%, Assignments 20%, Course Project 30% 86 | * __Midterm exam__: 4.06 __Wed__. Open-book exam without computer/phone/calculator use. No final exam. 87 | * __Course project__: Presentation (Last week). Group up to X people. 88 | * __Attendance__: Randomly checked. The score is calculated as 20 – 2`x`(#of absence). Leave request should be made 24 hours before with supporting documents, except for emergency. Job interview/internship cannot be a valid reason for leave 89 | * __Grade__ in letters (e.g., A+, A-, ... ,D+, D, F). __A- or above < 30% and B- or below > 10%__. 90 | -------------------------------------------------------------------------------- /past-years/2022-23-M1/Project.md: -------------------------------------------------------------------------------- 1 | # ASP Course Project 2 | 3 | ## [Repository](https://github.com/pyfe/pyfeng) 4 | 5 | ## List of Projects 6 | * Timer Option: Implementation of time-discretization (Andersen's QE) scheme for Heston/SABR models [[Report](https://github.com/Wuzhuoqun/Final-Project/blob/main/Report%20final_20221031.ipynb)] 7 | * [Zhou Gongqi](https://github.com/Zhou-Gongqi) / [Wu Zhuoqun](https://github.com/Wuzhuoqun) 8 | * Li M, Mercurio F (2015) Analytic Approximation of Finite‐Maturity Timer Option Prices. Journal of Futures Markets 35:245–273. https://doi.org/10.1002/fut.21659 9 | * Li C (2016) Bessel Processes, Stochastic Volatility, and Timer Options. Mathematical Finance 26:122–148. https://doi.org/10.1111/mafi.12041 10 | * SABR Model Low-bias Simulation Scheme [[Report](https://github.com/Lu-Tianzeng/ASP_project/blob/main/A%20low-bias%20SABR%20model.ipynb)] 11 | * [Lu Tianzeng](https://github.com/Lu-Tianzeng) / [Hu Zhijie](https://github.com/hzjdeem) 12 | * Chen B, Oosterlee CW, Van Der Weide H (2012) A low-bias simulation scheme for the SABR stochastic volatility model. Int J Theor Appl Finan 15:1250016. 13 | * Stochastic volatility inspired (SVI) model [[Report](https://github.com/BoomerBoom/SVI/blob/main/Test_for_SVI.ipynb)] 14 | * [Liu Can](https://github.com/BoomerBoom) 15 | * [Original Slides](http://faculty.baruch.cuny.edu/jgatheral/madrid2004.pdf) 16 | * Gatheral J, Jacquier A (2013) Arbitrage-free SVI volatility surfaces. [arXiv:12040646 [q-fin]](http://arxiv.org/abs/1204.0646) 17 | * Gatheral J, Jacquier A (2011) Convergence of Heston to SVI. Quantitative Finance 11:1129–1132. https://doi.org/10.1080/14697688.2010.550931 18 | * Snowball Pricing with MC method 19 | * Team 1: [Wu Hao](https://github.com/pkuWu) / [Yang Shuming](https://github.com/PkuYang). [[Report](https://github.com/pkuWu/ASP_Pre_SnowBall)] 20 | * Team 2: [Chen Wanqing](https://github.com/qwq-cwq) / [Chen Ziying](https://github.com/rachelczy). [[Report](https://github.com/qwq-cwq/PHBS_ASP_project/blob/main/snowball_docs/snowball_v2.ipynb)] 21 | 22 | ## Presentation and Deadline 23 | * Presentation: 10.27 (Thurs) 20 minutes by each group. 24 | * Pull request deadline: 11.01 (Tues) 25 | 26 | ## File requirements 27 | * `PyFENG` repository: 28 | * Core implementation (.py): python class and functions 29 | * Make sure to add docstring in detail. 30 | * Example: [bsm.py](https://github.com/PYFE/pyfeng/blob/master/pyfeng/bsm.py) 31 | * Specify equation/formula number or page in the original paper 32 | * The best examples of docstring are from numpy documentation: [example](https://docs.scipy.org/doc/numpy-1.10.1/reference/routines.polynomials.hermite.html) 33 | * __Integrate into the `PyFENG` repository__ by making pull-requests (pr). 34 | * __Do not__ place any `.ipynb` file. 35 | * Your repository (`PHBS_ASP_2021` or any name is OK) 36 | * Test (__.ipynb__): one Jupyter notebook file briefly describing the method (base theory, equations, SDE, strength/weakness, etc). 37 | * Include some test examples (e.g., the same parameter sets from the paper) 38 | * See past years' proejct: 39 | * [2021-22](https://github.com/PHBS/ASP/blob/master/past-years/2021-22-M3/Project.md) 40 | * [2020-21](https://github.com/PHBS/ASP/blob/master/past-years/2020-21-M3/Project.md) 41 | * [2019-20](https://github.com/PHBS/ASP/blob/master/past-years/2019-20-M1/Project.md) 42 | * [2018-19](https://github.com/PHBS/ASP/blob/master/past-years/2018-19-M1/Project.md) 43 | * [2017-18](https://github.com/PHBS/ASP/blob/master/past-years/2017-18-M1/Project.md) 44 | 45 | # Project Topics 46 | 47 | ## Topics 48 | * Among the topics and HWs covered in the class, choose an in-depth research on one topic. You are also welcome to do the project on your own original idea. Otherwise, pick one from my suggestions which are basically understanding and implementing literatures. Topics includes 49 | * Spread/Basket/Asian option pricing 50 | * SV models (SABR, Heston, etc) 51 | * Rough volatility 52 | * Copula 53 | 54 | ### * Rough Volatility 55 | * Ma J, Wu H (2021) A fast algorithm for simulation of rough volatility models. Quantitative Finance 0:1–16. https://doi.org/10.1080/14697688.2021.1970213 56 | * Jaber EA (2019) Lifting the Heston model. Quantitative Finance 19:1995–2013. https://doi.org/10.1080/14697688.2019.1615113 57 | * Horvath B, Jacquier A, Muguruza A (2019) Functional central limit theorems for rough volatility. arXiv:171103078 [math, q-fin] http://arxiv.org/abs/1711.03078 58 | * McCrickerd R, Pakkanen MS (2018) Turbocharging Monte Carlo pricing for the rough Bergomi model. Quantitative Finance 18:1877–1886. https://doi.org/10.1080/14697688.2018.1459812 59 | * Bennedsen, M., Lunde, A., Pakkanen, M.S., 2017. Hybrid scheme for Brownian semistationary processes. Finance Stoch 21, 931–965. https://doi.org/10.1007/s00780-017-0335-5 60 | * J. Gatheral's [python code](https://tpq.io/p/rough_volatility_with_python.html) 61 | 62 | ### * Spread/Basket/Asian Option 63 | * [[Partially](https://github.com/PyFE/pyfedev/blob/master/pyfe/bsm_multi_choi2018.py) implemented] Choi J (2018) Sum of all Black-Scholes-Merton models: An efficient pricing method for spread, basket, and Asian options. Journal of Futures Markets 38:627–644. https://doi.org/10.1002/fut.21909 64 | * R implementation: https://github.com/PyFE/SumBSM-R 65 | * [Most methods implemented] Krekel M, de Kock J, Korn R, Man T-K (2004) An analysis of pricing methods for basket options. Wilmott Magazine 2004:82–89 66 | 67 | ### Option pricing with Fourier Transform 68 | * Lord R, Kahl C (2010) Complex Logarithms in Heston-Like Models. Mathematical Finance 20:671–694. https://doi.org/10.1111/j.1467-9965.2010.00416.x 69 | * Kahl C, Jäckel P (2005) Not-so-complex Logarithms in the Heston Model. Wilmott 19 70 | * [Implemented but not perfect] __OUSV:__ Schöbel R, Zhu J (1999) Stochastic Volatility With an Ornstein–Uhlenbeck Process: An Extension. Rev Financ 3:23–46. https://doi.org/10.1023/A:1009803506170 71 | 72 | ### * GARCH-diffusion model 73 | * [Implemented] Barone-Adesi G, Rasmussen H, Ravanelli C (2005) An option pricing formula for the GARCH diffusion model. Computational Statistics & Data Analysis 49:287–310. https://doi.org/10.1016/j.csda.2004.05.014 74 | * Tubikanec I, Tamborrino M, Lansky P, Buckwar E (2021) Qualitative properties of numerical methods for the inhomogeneous geometric Brownian motion. arXiv:200310193 [cs, math] http://arxiv.org/abs/2003.10193 75 | * [Implemented] Euler/Milstein/Log scheme (in class) 76 | * [????] Capriotti L, Jiang Y, Shaimerdenova G (2018) Approximation methods for inhomogeneous geometric brownian motion. Int J Theor Appl Finan 22:1850055. https://doi.org/10.1142/S0219024918500553 77 | * __IGBM:__ Zhao B (2009) Inhomogeneous Geometric Brownian Motion. SSRN Electronic Journal. https://doi.org/10.2139/ssrn.1429449 78 | * [Consider] PhD Thesis of Ravanelli, C., University of Lugano, Switzerland, https://doc.rero.ch/record/4229/files/1_2003ECO001.pdf 79 | * [No method] Papadopoulos, Y.A., Lewis, A.L., 2018. A First Option Calibration of the GARCH Diffusion Model by a PDE Method. arXiv:1801.06141 [q-fin]. 80 | 81 | ### * SABR model 82 | * [Implemented but not verified] Cai N, Song Y, Chen N (2017) Exact Simulation of the SABR Model. Oper Res 65:931–951. https://doi.org/10.1287/opre.2017.1617 83 | * [Implemented but not verified] Leitao Á, Grzelak LA, Oosterlee CW (2017) On a one time-step Monte Carlo simulation approach of the SABR model: Application to European options. Applied Mathematics and Computation 293:461–479. https://doi.org/10.1016/j.amc.2016.08.030 84 | 85 | ### * [Heston Model](https://en.wikipedia.org/wiki/Heston_model) (SV) 86 | * [No need] Broadie M, Kaya Ö (2006) Exact Simulation of Stochastic Volatility and Other Affine Jump Diffusion Processes. Operations Research 54:217–231. https://doi.org/10.1287/opre.1050.0247 87 | * [Implemented] Glasserman P, Kim K-K (2011) Gamma expansion of the Heston stochastic volatility model. Finance Stoch 15:267–296. https://doi.org/10.1007/s00780-009-0115-y 88 | * [Implemented] Andersen, L., 2008. Simple and efficient simulation of the Heston stochastic volatility model. The Journal of Computational Finance 11, 1–42. https://doi.org/10.21314/JCF.2008.189 89 | * [Implemented / 2022 Thesis] Almost exact simulation 90 | 91 | ### * OUSV Model 92 | * [Implemented] Euler/Milstein 93 | * [Implemented] Li C, Wu L (2019) Exact simulation of the Ornstein–Uhlenbeck driven stochastic volatility model. European Journal of Operational Research 275:768–779. https://doi.org/10.1016/j.ejor.2018.11.057 94 | * [2021 Thesis] Almost exact simulation 95 | 96 | ### * 3/2 SV model 97 | * [Implemented but not verified] Baldeaux, J., 2012. Exact simulation of the 3/2 model. Int. J. Theor. Appl. Finan. 15, 1250032. https://doi.org/10.1142/S021902491250032X 98 | * [Implemented but not verified] Almost exact simulation 99 | 100 | ### * Other Simulation-related Papers 101 | * [Implemented but not verified] __General SDE__: Beskos, A., Roberts, G.O., 2005. Exact simulation of diffusions. Ann. Appl. Probab. 15, 2422–2444. https://doi.org/10.1214/105051605000000485 102 | * [Implemented] __Computing Moments from Laplace Transform__: Choudhury, G.L., Lucantoni, D.M., 1996. Numerical Computation of the Moments of a Probability Distribution from its Transform. Operations Research 44, 368–381. https://doi.org/10.1287/opre.44.2.368 103 | 104 | ### Exotic Derivatives 105 | * Timer Option 106 | * Implementation of time-discretization (Andersen's QE) scheme for Heston/SABR models 107 | * Li M, Mercurio F (2015) Analytic Approximation of Finite‐Maturity Timer Option Prices. Journal of Futures Markets 35:245–273. https://doi.org/10.1002/fut.21659 108 | * Li C (2016) Bessel Processes, Stochastic Volatility, and Timer Options. Mathematical Finance 26:122–148. https://doi.org/10.1111/mafi.12041 109 | * (Other related papers) 110 | * Bernard C, Cui Z (2011) Pricing timer options. JCF 15:69–104. https://doi.org/10.21314/JCF.2011.228 111 | * Zeng P, Kwok YK, Zheng W (2015) Fast hilbert transform algorithms for pricing discrete timer options under stochastic volatility models. Int J Theor Appl Finan 18:1550046. https://doi.org/10.1142/S0219024915500466 112 | * Zheng W, Zeng P (2016) Pricing timer options and variance derivatives with closed-form partial transform under the 3/2 model. Applied Mathematical Finance 23:344–373. https://doi.org/10.1080/1350486X.2017.1285242 113 | * SABR Model Simulation 114 | * Chen B, Oosterlee CW, Van Der Weide H (2012) A low-bias simulation scheme for the SABR stochastic volatility model. Int J Theor Appl Finan 15:1250016. https://doi.org/10.1142/S0219024912500161 115 | * [Implemented but not verified] Leitao Á, Grzelak LA, Oosterlee CW (2017) On a one time-step Monte Carlo simulation approach of the SABR model: Application to European options. Applied Mathematics and Computation 293:461–479. https://doi.org/10.1016/j.amc.2016.08.030 116 | * Stochastic volatility inspired (SVI) model (1-person project) 117 | * [Original Slides](http://faculty.baruch.cuny.edu/jgatheral/madrid2004.pdf) 118 | * Gatheral J, Jacquier A (2013) Arbitrage-free SVI volatility surfaces. [arXiv:12040646 [q-fin]](http://arxiv.org/abs/1204.0646) 119 | * Gatheral J, Jacquier A (2011) Convergence of Heston to SVI. Quantitative Finance 11:1129–1132. https://doi.org/10.1080/14697688.2010.550931 120 | * Snowball 121 | * Recently very popular in China. [Link](https://www.risk.net/derivatives/7894576/sales-of-chinas-snowball-notes-fall) 122 | 123 | ### Old Topics 124 | * Almost Exact Simulation of the 3/2 Volatility Model [[Report](https://github.com/AHrmnd/ASP-1/blob/master/py/AE_theory.ipynb)] 125 | * [@AHrmnd](https://github.com/AHrmnd) and [@FLXE-Feidou](https://github.com/FLXE-Feidou) 126 | * Simulation of the GARCH Diffusion model [[Report](https://github.com/Harveydentsun/ASP_Project/blob/main/Garch-diffusion%20model.ipynb)] 127 | * [@Harveydentsun](https://github.com/Harveydentsun) and [@yogaball](https://github.com/yogaball) 128 | https://doi.org/10.1080/14697688.2015.1031815 129 | * Lifted Heston Model [[Report](https://github.com/Cecilia525/ASP-Project/blob/main/Report-Lifted%20Heston_v4.ipynb)] 130 | * [@jyyccc](https://github.com/jyyccc) and [@Cecilia525](https://github.com/Cecilia525) 131 | * Jaber EA (2019) Lifting the Heston model. Quantitative Finance 19:1995–2013. https://doi.org/10.1080/14697688.2019.1615113 132 | * Approximate Stochastic Volatility Model Option Pricing [[Report](https://github.com/zwc00098/ASP_Project/blob/main/Test_Ball%20and%20Roma_v2.0.ipynb)] 133 | * [@zwc00098](https://github.com/zwc00098) 134 | * Ball CA, Roma A (1994) Stochastic Volatility Option Pricing. Journal of Financial and Quantitative Analysis 29:589–607. https://doi.org/10.2307/2331111 135 | * Simulation of Rough Volatility Model [[Report](https://github.com/Baiwisher/PyFENG/blob/rVolatility/Report_rVolatility.ipynb)] 136 | * [@polarbluebear](https://github.com/polarbluebear) and [@Baiwisher](https://github.com/Baiwisher) 137 | * Ma J, Wu H (2021) A fast algorithm for simulation of rough volatility models. Quantitative Finance 0:1–16. https://doi.org/10.1080/14697688.2021.1970213 138 | -------------------------------------------------------------------------------- /past-years/2022-23-M1/README.md: -------------------------------------------------------------------------------- 1 | # Applied Stochastic Processes 2022-23 Module 1 (Fall 2022) 2 | 3 | ## Announcements 4 | * Email is the preferred method of communication. Class mailing list will be created as PHBS.ASP@allmail.net. 5 | 6 | ## Course Slides and Other Resources 7 | * Prelims: [Probability Statistics Review](files/Prob_Stat_Review.pdf) 8 | * Past Midterm Exams: [All Exams](files/ASP_Problems.pdf) ([2017-18](files/ASP2017_Midterm.pdf), [2018-19](files/ASP2018_Midterm.pdf), [2019-20](files/ASP2019_Midterm.pdf), [2020-21](files/ASP2020_Midterm.pdf), [2021-22](files/ASP2021_Midterm.pdf), [2022-23](files/ASP2022_Midterm.pdf)) 9 | * `PyFeng` package ([PyPI](https://pypi.org/project/pyfeng/) \| [Github](https://github.com/PyFE/PyFENG) \| [Documentation](https://pyfeng.readthedocs.io/)) 10 | * Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) | [Py demo](py/MC_Demo.ipynb)) 11 | * Black-Scholes model ([Py demo](py/BlackScholes_ImpliedVol.ipynb), [MC demo](py/BlackScholes_MC.ipynb)): Also see Ch. 10 of [StoFin Course Notes](https://github.com/PHBS/StoFin/blob/master/files/SCFA_Notes.pdf) 12 | * Normal (Bachelier) model ([Slides](files/Normal_Model.pdf)) from **Stochastic Finance** class 13 | * Implied volatility ([Slides](files/ImpVol.pdf) | [Py demo](py/BlackScholes_ImpliedVol.ipynb)) 14 | * Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)) 15 | * SABR model ([Slides](files/SABRmodel.pdf)) | NSVh model ([Slides](files/NSVh_Slides.pdf)) 16 | * Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 17 | 18 | ## Lectures 19 | No | Date | Contents 20 | --- | :---: | --- 21 | __01__ | 8.29 Mon | Course overview, Scientific computing, MC method, RN generation ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 22 | __02__ | 9.01 Thurs | Continued ([Slides](files/MCmethod.pdf) \| [Py demo](py/MC_Demo.ipynb)) 23 | __03__ | 9.05 Mon | Python crash course ([Py Demo](py/PythonCrashCourse_Derek_Banas.ipynb)). More [cheatsheets](https://ehmatthes.github.io/pcc/cheatsheets/README.html) also available in [MLF CMS](http://cms.phbs.pku.edu.cn/claroline/document/document.php?cidReset=true&cidReq=FN570). Numpy crach course ([Py Demo](py/PythonCrashCourse_Numpy.ipynb)). 24 | __04__ | 9.08 Thurs |Black-Scholes implementation ([Py Demo](py/BlackScholes_FunctionVsClass.ipynb)). Implied volatility ([Slides](files/ImpVol.pdf) \| [Py demo](py/BlackScholes_ImpliedVol.ipynb)). 25 | __05__ | 9.14 Wed | Bachelier model ([Slides](files/Normal_Model.pdf)). Black-Scholes-Merton and Bachelier option pricing with MC ([Py Demo](py/BlackScholes_MC.ipynb)). Spread/Basket options ([Slides](files/SpreadBasketOption.pdf)). Correlated Normal RNs ([Slides](files/MCmethod.pdf) \| [Py Demo](py/CorrelatedNormals_Demo.ipynb)) 26 | __06__ | 9.15 Thurs | Spread/Basket options continued, [HW2: [Spread/Basket option implementation](py/HW2/TestCode_BasketSpread.ipynb), Due next Thursday] 27 | __07__ | 9.19 Mon | SABR model ([Slides](files/SABRmodel.pdf): Volatility smile), Suggested [project topics](Project.md) 28 | __08__ | 9.22 Thurs | SABR model continued ([Slides](files/SABRmodel.pdf): Local volatility model, Model intro), Introduction to [PyFENG](https://github.com/PyFE/PyFENG) package 29 | __09__ | 9.26 Mon | SABR model continued ([Slides](files/SABRmodel.pdf): Euler/Milstein method, Conditional MC), Github pull-request (PR), Py Demo ([SABR](py/SabrModel_Demo.ipynb), [BsmNdMc](BsmNdMc_Demo.ipynb)), HW3: [MC method for SABR](py/HW3/TestCode_SABR.ipynb) 30 | __10__ | 9.29 Thurs | Python Import ([Py Demo](py/HW3/Demo_Advanced_Import.ipynb)), SV Model Simulation for Project ([Slides](files/SV_Simulation.pdf)) 31 | __11__ | 10.06 Thurs | SV Model Simulation for Project ([Slides](files/SV_Simulation.pdf)) 32 | __12__ | 10.10 Mon | [Past Exams](files/ASP_Problems.pdf) Review 33 | __13__ | 10.13 Thurs | Midterm Exam ([Solution](files/ASP2022_Midterm.pdf)) 34 | __14__ | 10.17 Mon | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 35 | __15__ | 10.20 Thurs | Copula ([Slides](files/Copula.pdf), [Py demo](py/Demo_Copula.ipynb)) 36 | __16__ | 10.24 Mon | Research Presentation: NSVh model and Normal SABR ([Slides](files/NSVh_Slides.pdf)) 37 | __17__ | 10.26 Wed | Research Presentation: Heston model simulation method ([Slides](files/HestonMC-Slides.pdf)) 38 | __18__ | 10.27 Thurs | Course project presentation 39 | 40 | 41 | 42 | ## Homeworks: 43 | * ### __Set 0__: (Due by XXX) 44 | * Register on Github.com and send your ID and student number to Prof. Choi via email (jaehyuk@phbs.pku.edu.cn). Use your __full name__ in your profile. Accept invitation to the [PHBS organization](https://github.com/orgs/PHBS/people) from TA. Install [Github Desktop](https://desktop.github.com/). 45 | * Install [Anaconda](https://www.anaconda.com/download/) Python distribution (__3.X version__, not 2.X version). Anaconda distribution is core Python + useful scientific computation libraries (e.g., numpy, scipy, pandas) + package management system (pip or conda) 46 | * Send the screenshot of Github desktop and Anaconda installed to TA. (Example: [Github Desktop](files/Choi_Jaehyuk_Github.png), [Anaconda Spyder](files/Choi_Jaehyuk_Python.png)) 47 | * ### __Set 1__ [Due by 9.9 Fri] Generate a function for generating standard normal RN following Problem 2 of 2021.M3 midterm exam. After drawing 1e6 RNs, check if they are truly standard normal RNs. 48 | * Draw histogram using `matplotlib.pyplot` 49 | * Calculate mean/variance/skewness/kurtosis 50 | * ### __Set 1__ [Due by XXX] Simple corporate (default) bond pricing by MC simulation. [Starter Code](py/HW1/HW1.ipynb) 51 | * ### __Set 2__ [Due by XXX] Pricing basket and spread option using MC. [Starter Code](py/HW2/TestCode_BasketSpread.ipynb) 52 | * ### __Set 3__ [Due by XXX] Simulating SABR model. [Starter Code](py/HW3/TestCode_SABR.ipynb) 53 | 54 | ## Course Project: [Project Description](files/Project.md) (Previous year: [2017](past-years/2017-18-M1/Project.md) | [2018](past-years/2018-19-M1/Project.md) | [2019](past-years/2019-20-M1/Project.md) | [2019](past-years/2019-20-M1/Project.md) | [2020](past-years/2020-21-M3/Project.md) | [2021](past-years/2021-22-M3/Project.md)) 55 | 56 | ## Classes: 57 | * Lectures: Mon & Thurs 3:30 – 5:20 PM 58 | * Venue: PHBS Building, Room 211 59 | 60 | ## Instructor: [Jaehyuk Choi](http://www.jaehyukchoi.net/phbs_en) 61 | * Office: PHBS Building, Room 755 62 | * Phone: 86-755-2603-0568 63 | * Email: jaehyuk@phbs.pku.edu.cn 64 | * Office Hour: Wed 1-3 PM 65 | 66 | ## Teaching Assistance: Su Nan (苏南) 67 | * Email: sunan@stu.pku.edu.cn 68 | * TA Office Hour: __TBA__ (Room 213/214) 69 | 70 | ## Course overview: 71 | Applied Stochastic Processes (ASP) is intended for the students who are 72 | seeking advanced knowledge in stochastic calculus and are eventually interested in the jobs in 73 | financial engineering. As the name indicates, the course will emphasis on applications such as 74 | numerical calculation and programming. On completion of this course, the students will learn 75 | how financial observations (e.g. stock prices and FX rate) are modelled with stochastic 76 | processes and how they can be computed using analytics or computer simulations. 77 | 78 | ## Prerequisites: 79 | [Stochastic Finance](https://github.com/PHBS/StoFin) (FIN 519), a year 1 required course for quantitative finance program, is a prerequisite for the ASP since it provides theoretical background. Undergraduate-level knowledge in probability, statistics, linear algebra and programming skill (Python) are also highly recommended. 80 | 81 | ## Extra Reading Materials 82 | * Monte Carlo Methods in Finance by Peter Jaeckel 83 | * Option Valuation Under Stochastic Volatility by Alan Lewis 84 | * [Stochastic Calculus and Financial Applications](http://www-stat.wharton.upenn.edu/~steele/StochasticCalculus.html) by J. Michael Steele 85 | ([Stochastic finance course notes](https://github.com/PHBS/2018.M3.StoFin/blob/master/files/SCFA_Notes.pdf)) 86 | 87 | ## Assessment/Grading Details 88 | Attendance 20%, Mid-term Exam 30%, Assignments 20%, Course Project 30% 89 | * __Midterm exam__: 4.06 __Wed__. Open-book exam without computer/phone/calculator use. No final exam. 90 | * __Course project__: Presentation (Last week). Group up to X people. 91 | * __Attendance__: Randomly checked. The score is calculated as 20 – 2`x`(#of absence). Leave request should be made 24 hours before with supporting documents, except for emergency. Job interview/internship cannot be a valid reason for leave 92 | * __Grade__ in letters (e.g., A+, A-, ... ,D+, D, F). __A- or above < 30% and B- or below > 10%__. 93 | -------------------------------------------------------------------------------- /py/BlackScholes_MC.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Option pricing with MC Simulations" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "import matplotlib.pyplot as plt\n", 18 | "import pyfeng as pf\n", 19 | "import scipy.stats as spst" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "sigma = 0.2\n", 29 | "texp = 1\n", 30 | "spot = 100\n", 31 | "m_bs = pf.Bsm(sigma)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 3, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "# Generate random numbers\n", 41 | "z = np.random.normal(size=100000)\n", 42 | "#anti-thetic" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 4, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "(-0.0006055366781411985, 1.0025231767688696)" 54 | ] 55 | }, 56 | "execution_count": 4, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "np.mean(z), np.std(z)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "## Black-Scholes model (Geomegric Brownian Motion)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 5, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "[ 95.58846095 162.00533109 114.39066961 ... 126.40456905 114.30328996\n", 82 | " 104.58264143]\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "# Black-Scholes-Merton model (Geometric BM)\n", 88 | "s_bsm = spot * np.exp(sigma*np.sqrt(texp)*z - sigma**2*texp/2)\n", 89 | "print(s_bsm)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 6, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "(99.99838825660521, 20.260426506988097)" 101 | ] 102 | }, 103 | "execution_count": 6, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "## Sanity check\n", 110 | "np.mean(s_bsm), np.std(s_bsm)\n", 111 | "#spst.describe(s_bsm)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 7, 117 | "metadata": { 118 | "scrolled": true 119 | }, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "image/png": "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\n", 124 | "text/plain": [ 125 | "
" 126 | ] 127 | }, 128 | "metadata": { 129 | "needs_background": "light" 130 | }, 131 | "output_type": "display_data" 132 | } 133 | ], 134 | "source": [ 135 | "# the histogram of the data\n", 136 | "n, bins, patches = plt.hist(s_bsm, 50, facecolor='green', alpha=0.7)\n", 137 | "plt.grid(True)\n", 138 | "plt.show()" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 8, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "4.310116158007301\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "### Price a call option\n", 156 | "\n", 157 | "strike = 110\n", 158 | "#cp_sign = 1\n", 159 | "price = np.mean(np.fmax(s_bsm - strike, 0))\n", 160 | "print( price )" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 9, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "data": { 170 | "text/plain": [ 171 | "4.292010941409885" 172 | ] 173 | }, 174 | "execution_count": 9, 175 | "metadata": {}, 176 | "output_type": "execute_result" 177 | } 178 | ], 179 | "source": [ 180 | "# Exact BSM price\n", 181 | "m_bs.price(strike, spot, texp)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 10, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "6.94359770223014\n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "### Can price different options without extra computation\n", 199 | "\n", 200 | "strike = 98\n", 201 | "cp = -1\n", 202 | "price = np.mean(np.fmax(cp*(s_bsm - strike), 0))\n", 203 | "print( price )" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 11, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "data": { 213 | "text/plain": [ 214 | "6.925974960378305" 215 | ] 216 | }, 217 | "execution_count": 11, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "# Exact price\n", 224 | "m_bs.price(strike, spot, texp, cp=-1)" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "## Bachelier Model (Arithmetic Brownian Motion)" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 12, 237 | "metadata": {}, 238 | "outputs": [], 239 | "source": [ 240 | "sigma = 20\n", 241 | "texp = 1\n", 242 | "spot = 100\n", 243 | "m_norm = pf.Norm(sigma)" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 13, 249 | "metadata": {}, 250 | "outputs": [ 251 | { 252 | "data": { 253 | "text/plain": [ 254 | "(100000,)" 255 | ] 256 | }, 257 | "execution_count": 13, 258 | "metadata": {}, 259 | "output_type": "execute_result" 260 | } 261 | ], 262 | "source": [ 263 | "s_norm = spot + sigma * np.sqrt(texp) * z\n", 264 | "s_norm.shape" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 14, 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "7.029318044708391\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "strike = 102\n", 282 | "cp_sign = 1\n", 283 | "price = np.mean(np.fmax(s_norm - strike, 0))\n", 284 | "print( price )" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 15, 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "data": { 294 | "text/plain": [ 295 | "7.018706624094294" 296 | ] 297 | }, 298 | "execution_count": 15, 299 | "metadata": {}, 300 | "output_type": "execute_result" 301 | } 302 | ], 303 | "source": [ 304 | "# Exact price\n", 305 | "m_norm.price(strike, spot, texp)" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 16, 311 | "metadata": {}, 312 | "outputs": [ 313 | { 314 | "name": "stdout", 315 | "output_type": "stream", 316 | "text": [ 317 | "7.039141746655447\n" 318 | ] 319 | } 320 | ], 321 | "source": [ 322 | "strike = 98\n", 323 | "cp = -1\n", 324 | "price = np.mean(np.fmax(cp*(s_norm - strike), 0))\n", 325 | "print( price )" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 17, 331 | "metadata": {}, 332 | "outputs": [ 333 | { 334 | "data": { 335 | "text/plain": [ 336 | "7.018706624094294" 337 | ] 338 | }, 339 | "execution_count": 17, 340 | "metadata": {}, 341 | "output_type": "execute_result" 342 | } 343 | ], 344 | "source": [ 345 | "# Exact price\n", 346 | "m_norm.price(strike, spot, texp, cp=-1)" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": null, 352 | "metadata": {}, 353 | "outputs": [], 354 | "source": [] 355 | } 356 | ], 357 | "metadata": { 358 | "anaconda-cloud": {}, 359 | "kernelspec": { 360 | "display_name": "Python 3 (ipykernel)", 361 | "language": "python", 362 | "name": "python3" 363 | }, 364 | "language_info": { 365 | "codemirror_mode": { 366 | "name": "ipython", 367 | "version": 3 368 | }, 369 | "file_extension": ".py", 370 | "mimetype": "text/x-python", 371 | "name": "python", 372 | "nbconvert_exporter": "python", 373 | "pygments_lexer": "ipython3", 374 | "version": "3.9.7" 375 | } 376 | }, 377 | "nbformat": 4, 378 | "nbformat_minor": 1 379 | } 380 | -------------------------------------------------------------------------------- /py/BsmNdMc_Demo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%load_ext autoreload\n", 10 | "%autoreload 2" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "import numpy as np\n", 20 | "import pandas as pd\n", 21 | "import scipy.stats as spst\n", 22 | "import scipy.integrate as spint\n", 23 | "from scipy.optimize import newton\n", 24 | "import matplotlib.pyplot as plt\n", 25 | "import warnings\n", 26 | "\n", 27 | "#import sys\n", 28 | "#sys.path.insert(sys.path.index('')+1, 'D:/Github/PyFeng')\n", 29 | "import pyfeng as pf" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "# Basket Option" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 3, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "sigma = 0.4*np.ones(4)\n", 46 | "spot = np.ones(4)*100\n", 47 | "texp=5" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 4, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "10000\n" 60 | ] 61 | }, 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "(1, 10000, 4)" 66 | ] 67 | }, 68 | "execution_count": 4, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "m = pf.BsmNdMc(sigma, cor=0.5, rn_seed=1234)\n", 75 | "m.simulate(tobs = [texp], n_path=10000)\n", 76 | "print(m.n_path)\n", 77 | "m.path.shape" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 5, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "50000\n", 90 | "(1, 50000, 4)\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "m.simulate(tobs = [texp], n_path=50000, store=2)\n", 96 | "print(m.n_path)\n", 97 | "print(m.path.shape)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 6, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "array([55.05442979, 48.24139219, 42.29187964, 37.11853211, 32.62517125,\n", 109 | " 28.73942945, 25.37967374, 22.46038799, 19.92272369, 17.71628982,\n", 110 | " 15.80171962])" 111 | ] 112 | }, 113 | "execution_count": 6, 114 | "metadata": {}, 115 | "output_type": "execute_result" 116 | } 117 | ], 118 | "source": [ 119 | "# varying strikes\n", 120 | "payoff = lambda x: np.fmax(np.mean(x, axis=1) - strike, 0)\n", 121 | "\n", 122 | "strikes = np.arange(50, 151, 10)\n", 123 | "price = []\n", 124 | "for strike in strikes:\n", 125 | " price.append(m.price_european(spot, texp, payoff))\n", 126 | "np.array(price)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 7, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "array([ 4.6049753 , 7.87316825, 11.99891419, 16.91824336, 22.53129533,\n", 138 | " 28.73942945, 35.47288913, 42.65874825, 50.22366433, 58.11022683,\n", 139 | " 66.27677878])" 140 | ] 141 | }, 142 | "execution_count": 7, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "# varying forwards\n", 149 | "payoff = lambda x: np.fmax(np.mean(x, axis=1) - strike, 0)\n", 150 | "strike = 100\n", 151 | "price = []\n", 152 | "for spot in np.arange(50, 151, 10):\n", 153 | " price.append(m.price_european(spot, texp, payoff))\n", 154 | "np.array(price)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 8, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "array([18.94495855, 20.45829493, 22.49840945, 24.87785108, 30.11378007,\n", 166 | " 35.57949546, 41.05473026, 46.42608331, 51.61861158, 56.59050047,\n", 167 | " 65.9322128 ])" 168 | ] 169 | }, 170 | "execution_count": 8, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "# varying except sigma1=100%\n", 177 | "strike = spot = 100\n", 178 | "payoff = lambda x: np.fmax(np.mean(x, axis=1) - strike, 0)\n", 179 | "price = []\n", 180 | "for sigma1 in np.array([5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 100])/100:\n", 181 | " sigma = sigma1 * np.ones(4)\n", 182 | " sigma[0] = 1\n", 183 | " #print(sigma)\n", 184 | " m = pf.BsmNdMc(sigma, cor=0.5, rn_seed=1234)\n", 185 | " m.simulate(tobs = [texp], n_path=400000)\n", 186 | " price.append(m.price_european(spot, texp, payoff))\n", 187 | "np.array(price)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "# Simpon's method for integration" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "Let's integrate numerically.\n", 209 | "\n", 210 | "$$ \\int_0^T exp(-a*t) dt = \\frac{1}{a}(1 - exp(-a T))$$" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 9, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "0.09999999979388464" 222 | ] 223 | }, 224 | "execution_count": 9, 225 | "metadata": {}, 226 | "output_type": "execute_result" 227 | } 228 | ], 229 | "source": [ 230 | "a = 10\n", 231 | "texp = 2\n", 232 | "exact = (1 - np.exp(-a*texp))/a\n", 233 | "exact" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 10, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "name": "stdout", 243 | "output_type": "stream", 244 | "text": [ 245 | "0.10000088446767287 8.84673788226209e-07\n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "def f(t, a):\n", 251 | " return np.exp(-a*t)\n", 252 | "\n", 253 | "n_step = 100\n", 254 | "tobs = np.arange(0, n_step+1)/n_step * texp\n", 255 | "simp = spint.simps(f(tobs, a), dx=texp/n_step)\n", 256 | "#simp = spint.simps(f(tobs, a), dx=1) * texp/n_step\n", 257 | "print(simp, simp-exact)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "# For SABR Model\n", 265 | "## Integration of sigma(t)" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 11, 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [ 274 | "# You can use BsmNdMc because sigma_t is also a geometric Brownian motion\n", 275 | "\n", 276 | "vov = 0.3\n", 277 | "texp = 5\n", 278 | "m = pf.BsmNdMc(vov, rn_seed=1234)\n", 279 | "tobs = np.arange(0, 101)/100*texp" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 12, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [ 288 | "_ = m.simulate(tobs = tobs, n_path=1000)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 13, 294 | "metadata": {}, 295 | "outputs": [ 296 | { 297 | "name": "stdout", 298 | "output_type": "stream", 299 | "text": [ 300 | "(101, 1000, 1)\n", 301 | "(101, 1000)\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "print(m.path.shape)\n", 307 | "sigma_path = np.squeeze(m.path)\n", 308 | "print(sigma_path.shape)" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 14, 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [ 317 | "sigma_final = sigma_path[-1,:]" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 15, 323 | "metadata": {}, 324 | "outputs": [ 325 | { 326 | "data": { 327 | "text/plain": [ 328 | "(1000,)" 329 | ] 330 | }, 331 | "execution_count": 15, 332 | "metadata": {}, 333 | "output_type": "execute_result" 334 | } 335 | ], 336 | "source": [ 337 | "int_var = spint.simps(sigma_path**2, dx=1, axis=0)/100\n", 338 | "int_var.shape" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 16, 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "data": { 348 | "text/plain": [ 349 | "1.007675560307406" 350 | ] 351 | }, 352 | "execution_count": 16, 353 | "metadata": {}, 354 | "output_type": "execute_result" 355 | } 356 | ], 357 | "source": [ 358 | "np.mean(sigma_final)" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": null, 364 | "metadata": {}, 365 | "outputs": [], 366 | "source": [] 367 | } 368 | ], 369 | "metadata": { 370 | "kernelspec": { 371 | "display_name": "Python 3 (ipykernel)", 372 | "language": "python", 373 | "name": "python3" 374 | }, 375 | "language_info": { 376 | "codemirror_mode": { 377 | "name": "ipython", 378 | "version": 3 379 | }, 380 | "file_extension": ".py", 381 | "mimetype": "text/x-python", 382 | "name": "python", 383 | "nbconvert_exporter": "python", 384 | "pygments_lexer": "ipython3", 385 | "version": "3.8.11" 386 | } 387 | }, 388 | "nbformat": 4, 389 | "nbformat_minor": 4 390 | } 391 | -------------------------------------------------------------------------------- /py/CorrelatedNormals_Demo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Correlated (multivariate) normal random variables" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import numpy as np\n", 17 | "import scipy.stats as spst\n", 18 | "import matplotlib.pyplot as plt\n", 19 | "\n", 20 | "np.set_printoptions(precision=3)" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "## Two correlated normal random variables" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "xnorm = np.random.normal(size=10000)\n", 37 | "ynorm = np.random.normal(size=10000)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 3, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "array([[ 1.00e+00, -9.33e-04],\n", 49 | " [-9.33e-04, 1.00e+00]])" 50 | ] 51 | }, 52 | "execution_count": 3, 53 | "metadata": {}, 54 | "output_type": "execute_result" 55 | } 56 | ], 57 | "source": [ 58 | "np.corrcoef(xnorm, ynorm)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 4, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [ 67 | "rho = 0.75\n", 68 | "ynorm = rho*xnorm + np.sqrt(1-rho**2)*ynorm" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 5, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "array([[1. , 0.753],\n", 80 | " [0.753, 1. ]])" 81 | ] 82 | }, 83 | "execution_count": 5, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "np.corrcoef(xnorm, ynorm)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "## Sigma (std) and correlation matrix" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 6, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "[2 5 2] \n", 109 | " [[ 1. 0.7 -0.2]\n", 110 | " [ 0.7 1. 0.5]\n", 111 | " [-0.2 0.5 1. ]]\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "sig_v = np.array([2, 5, 2])\n", 117 | "cor_m = np.array([[1, 0.7, -0.2], [0.7, 1, 0.5], [-0.2, 0.5, 1]])\n", 118 | "print(sig_v, '\\n', cor_m)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 7, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "image/png": "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\n", 129 | "text/plain": [ 130 | "
" 131 | ] 132 | }, 133 | "metadata": { 134 | "needs_background": "light" 135 | }, 136 | "output_type": "display_data" 137 | } 138 | ], 139 | "source": [ 140 | "# You can also create random Correlation matrixs\n", 141 | "\n", 142 | "n_asset = 5\n", 143 | "eig_val = np.random.uniform(size=n_asset)\n", 144 | "eig_val *= n_asset / np.sum(eig_val)\n", 145 | "# sum of eig_val = n_asset\n", 146 | "\n", 147 | "np.random.seed(123456)\n", 148 | "cor_m2 = spst.random_correlation.rvs(eig_val, tol=1e-8)\n", 149 | "_ = plt.matshow(cor_m2, cmap=plt.cm.Blues)" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "### Construct Covariance Matrix" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 8, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "array([[2],\n", 168 | " [5],\n", 169 | " [2]])" 170 | ] 171 | }, 172 | "execution_count": 8, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "sig_v[:,None]" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 9, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "array([[ 4. , 7. , -0.8],\n", 190 | " [ 7. , 25. , 5. ],\n", 191 | " [-0.8, 5. , 4. ]])" 192 | ] 193 | }, 194 | "execution_count": 9, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "sig_v[:,None] * sig_v * cor_m" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "Covariance and correlation matrices:\n", 208 | "$$ \\Sigma_{ij} = \\sigma_i R_{ij} \\sigma_j $$" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 10, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | "[[ 4. 7. -0.8]\n", 221 | " [ 7. 25. 5. ]\n", 222 | " [-0.8 5. 4. ]]\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "cov_m = sig_v[:,None] * cor_m * sig_v \n", 228 | "print(cov_m)" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": { 234 | "collapsed": true 235 | }, 236 | "source": [ 237 | "### Cholesky decomposition of covariance matrix" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 11, 243 | "metadata": { 244 | "scrolled": true 245 | }, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "[[ 2. 0. 0. ]\n", 252 | " [ 3.5 3.571 0. ]\n", 253 | " [-0.4 1.792 0.792]]\n" 254 | ] 255 | } 256 | ], 257 | "source": [ 258 | "chol_m = np.linalg.cholesky(cov_m)\n", 259 | "print(chol_m)" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 12, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "name": "stdout", 269 | "output_type": "stream", 270 | "text": [ 271 | "[[ 4. 7. -0.8]\n", 272 | " [ 7. 25. 5. ]\n", 273 | " [-0.8 5. 4. ]] \n", 274 | "\n", 275 | "[[0. 0. 0.]\n", 276 | " [0. 0. 0.]\n", 277 | " [0. 0. 0.]]\n" 278 | ] 279 | } 280 | ], 281 | "source": [ 282 | "# Let's verify that L x L^T = Covariance\n", 283 | "\n", 284 | "print( chol_m @ chol_m.T, '\\n' )\n", 285 | "print( chol_m @ chol_m.T - cov_m )" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 13, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "data": { 295 | "text/plain": [ 296 | "array([[ 2.719e-01, -4.250e-01, 5.670e-01, ..., -5.622e-01, 1.596e-03,\n", 297 | " 7.755e-02],\n", 298 | " [ 9.897e-01, -1.693e+00, 4.740e-01, ..., 6.823e-01, 4.753e-01,\n", 299 | " -1.457e+00],\n", 300 | " [ 1.557e+00, 9.918e-01, -1.198e+00, ..., -1.838e+00, 1.876e+00,\n", 301 | " 4.622e-01]])" 302 | ] 303 | }, 304 | "execution_count": 13, 305 | "metadata": {}, 306 | "output_type": "execute_result" 307 | } 308 | ], 309 | "source": [ 310 | "# Now let's create multivariate normal random variables following the covariance matrix\n", 311 | "# First, create standard normals (3 x 1000)\n", 312 | "\n", 313 | "znorm_m = np.random.normal(size=(3, 1000))\n", 314 | "znorm_m" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 14, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "data": { 324 | "text/plain": [ 325 | "array([[ 1.084, 0.021, -0. ],\n", 326 | " [ 0.021, 1.005, -0.038],\n", 327 | " [-0. , -0.038, 1.045]])" 328 | ] 329 | }, 330 | "execution_count": 14, 331 | "metadata": {}, 332 | "output_type": "execute_result" 333 | } 334 | ], 335 | "source": [ 336 | "np.round(np.cov(znorm_m),3)" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 15, 342 | "metadata": {}, 343 | "outputs": [ 344 | { 345 | "name": "stdout", 346 | "output_type": "stream", 347 | "text": [ 348 | "(3, 1000)\n" 349 | ] 350 | }, 351 | { 352 | "data": { 353 | "text/plain": [ 354 | "array([[ 5.437e-01, -8.499e-01, 1.134e+00, ..., -1.124e+00, 3.192e-03,\n", 355 | " 1.551e-01],\n", 356 | " [ 4.486e+00, -7.531e+00, 3.677e+00, ..., 4.683e-01, 1.703e+00,\n", 357 | " -4.931e+00],\n", 358 | " [ 2.899e+00, -2.078e+00, -3.264e-01, ..., -7.911e-03, 2.337e+00,\n", 359 | " -2.276e+00]])" 360 | ] 361 | }, 362 | "execution_count": 15, 363 | "metadata": {}, 364 | "output_type": "execute_result" 365 | } 366 | ], 367 | "source": [ 368 | "# Then multiply C^T\n", 369 | "\n", 370 | "#xnorm_m = znorm_m @ chol_m.transpose()\n", 371 | "xnorm_m = chol_m @ znorm_m\n", 372 | "print(xnorm_m.shape)\n", 373 | "xnorm_m" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": 16, 379 | "metadata": { 380 | "scrolled": true 381 | }, 382 | "outputs": [ 383 | { 384 | "name": "stdout", 385 | "output_type": "stream", 386 | "text": [ 387 | "[[ 4. 7. -0.8]\n", 388 | " [ 7. 25. 5. ]\n", 389 | " [-0.8 5. 4. ]]\n", 390 | "Cov from sample:\n", 391 | " [[ 4.335 7.734 -0.793]\n", 392 | " [ 7.734 26.609 4.908]\n", 393 | " [-0.793 4.908 3.921]]\n", 394 | "Error of Cov matrix:\n", 395 | " [[ 0.335 0.734 0.007]\n", 396 | " [ 0.734 1.609 -0.092]\n", 397 | " [ 0.007 -0.092 -0.079]]\n" 398 | ] 399 | } 400 | ], 401 | "source": [ 402 | "# Let's verify that X = C * Z follows the covariance\n", 403 | "print(cov_m)\n", 404 | "cov_m_sample = np.cov( xnorm_m )\n", 405 | "print( 'Cov from sample:\\n', cov_m_sample )\n", 406 | "print( 'Error of Cov matrix:\\n', cov_m_sample - cov_m )" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 17, 412 | "metadata": {}, 413 | "outputs": [ 414 | { 415 | "name": "stdout", 416 | "output_type": "stream", 417 | "text": [ 418 | "[[ 1. 0.7 -0.2]\n", 419 | " [ 0.7 1. 0.5]\n", 420 | " [-0.2 0.5 1. ]]\n", 421 | "Corr from sample:\n", 422 | " [[ 1. 0.72 -0.192]\n", 423 | " [ 0.72 1. 0.48 ]\n", 424 | " [-0.192 0.48 1. ]]\n", 425 | "Error:\n", 426 | " [[-2.220e-16 2.010e-02 7.570e-03]\n", 427 | " [ 2.010e-02 0.000e+00 -1.954e-02]\n", 428 | " [ 7.570e-03 -1.954e-02 0.000e+00]]\n" 429 | ] 430 | } 431 | ], 432 | "source": [ 433 | "# also check the correation\n", 434 | "print(cor_m)\n", 435 | "cor_m_sample = np.corrcoef( xnorm_m )\n", 436 | "print( 'Corr from sample:\\n', cor_m_sample )\n", 437 | "print( 'Error:\\n', cor_m_sample - cor_m )" 438 | ] 439 | }, 440 | { 441 | "cell_type": "markdown", 442 | "metadata": { 443 | "collapsed": true 444 | }, 445 | "source": [] 446 | } 447 | ], 448 | "metadata": { 449 | "anaconda-cloud": {}, 450 | "kernelspec": { 451 | "display_name": "Python 3 (ipykernel)", 452 | "language": "python", 453 | "name": "python3" 454 | }, 455 | "language_info": { 456 | "codemirror_mode": { 457 | "name": "ipython", 458 | "version": 3 459 | }, 460 | "file_extension": ".py", 461 | "mimetype": "text/x-python", 462 | "name": "python", 463 | "nbconvert_exporter": "python", 464 | "pygments_lexer": "ipython3", 465 | "version": "3.8.11" 466 | } 467 | }, 468 | "nbformat": 4, 469 | "nbformat_minor": 1 470 | } 471 | -------------------------------------------------------------------------------- /py/Debug.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "scrolled": true 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Pixiedust database opened successfully\n" 15 | ] 16 | }, 17 | { 18 | "data": { 19 | "text/html": [ 20 | "\n", 21 | "
\n", 22 | " \n", 23 | " \n", 24 | " \n", 25 | " Pixiedust version 1.1.17\n", 26 | "
\n", 27 | " " 28 | ], 29 | "text/plain": [ 30 | "" 31 | ] 32 | }, 33 | "metadata": {}, 34 | "output_type": "display_data" 35 | } 36 | ], 37 | "source": [ 38 | "import os\n", 39 | "import numpy as np\n", 40 | "import pandas as pd\n", 41 | "import matplotlib.pyplot as plt\n", 42 | "import copy\n", 43 | "################\n", 44 | "import pixiedust\n", 45 | "################\n", 46 | "path = os.getcwd()\n", 47 | "os.chdir('D:\\Mega\\PHBS\\@ASP\\pyfedev-ASP')\n", 48 | "#os.chdir('C:\\\\Users\\jaehyukchoi\\Documents\\GitHub\\pyfedev')\n", 49 | "import pyfe\n", 50 | "os.chdir(path)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 2, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "fwd = 100\n", 60 | "strike = np.arange(50,151,10)\n", 61 | "texp = 10\n", 62 | "\n", 63 | "params = {\"sigma\": 0.2, \"vov\": 0.75, \"rho\": -0.25, \"beta\":1}\n", 64 | "sabr_mc_model = pyfe.SabrCondMcBsmModel(**params)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": { 71 | "pixiedust": { 72 | "displayParams": {} 73 | } 74 | }, 75 | "outputs": [], 76 | "source": [ 77 | "%%pixie_debugger\n", 78 | "sabr_model.price(strike, fwd, texp)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [] 87 | } 88 | ], 89 | "metadata": { 90 | "kernelspec": { 91 | "display_name": "Python [default]", 92 | "language": "python", 93 | "name": "python3" 94 | }, 95 | "language_info": { 96 | "codemirror_mode": { 97 | "name": "ipython", 98 | "version": 3 99 | }, 100 | "file_extension": ".py", 101 | "mimetype": "text/x-python", 102 | "name": "python", 103 | "nbconvert_exporter": "python", 104 | "pygments_lexer": "ipython3", 105 | "version": "3.5.6" 106 | } 107 | }, 108 | "nbformat": 4, 109 | "nbformat_minor": 2 110 | } 111 | -------------------------------------------------------------------------------- /py/HW1/HW1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# HW 1: Corporate Bond Pricing (due by 9.17 Tues)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "We are going to compute the price of a corporate bond (subject to default) with Monte-Carlo simulation. Assume that \n", 15 | "* the default time of a company follows the exponential distribution with intensity $\\lambda=$__`def_rate`__. \n", 16 | "* the riskfree interest rate is $r_f=$__`rf_rate`__ and the maturity of the bond is $T=$__`mat`__. \n", 17 | "* in the case of default, you can recover some portion ($R=$__`recovery_rate`__) of the face value.\n", 18 | "* the coupon is 0%, i.e., it is a zero-coupon bond.\n", 19 | "* the face value of the bond is 1.0\n", 20 | "* use compound rate for discounting; the price of the default-free bond is $e^{-r_f T}$\n", 21 | "\n", 22 | "The Problem 1 of the [2017 ASP Midterm Exam](../files/ASP2017_Midterm.pdf) will be helpful.\n", 23 | "\n", 24 | "### Instruction to upload your HW\n", 25 | "* Create a repository named __`PHBS_ASP_2019`__ (and clone it to your PC)\n", 26 | "* Copy this file to __`PHBS_ASP_2019/HW1/HW1.ipynb`__ (Please use the same name for repository and ipynb file)\n", 27 | "* Add solution code.\n", 28 | "* Run your your code to make sure that there's no error.\n", 29 | "* Upload (commit and sync) your file." 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "### 1. First, let's create a pricing function and check the std " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 1, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "import numpy as np" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "def_rate = 0.1\n", 55 | "rf_rate = 0.03\n", 56 | "recovery = 0.3\n", 57 | "mat = 10" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "(10.022552951049857, 10.0)" 74 | ] 75 | }, 76 | "execution_count": 3, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "# First generate exponential random numbers\n", 83 | "# Although you can generate directly using fault_time = np.random.exponential(scale=), let's use uniform random numbers.\n", 84 | "n_sample = 10000\n", 85 | "U = np.random.uniform(size=n_sample)\n", 86 | "default_time = -(1/def_rate)*np.log(U)\n", 87 | "\n", 88 | "# You can check if the RNs are correct by comparing the means\n", 89 | "(default_time.mean(), 1/def_rate)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 4, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "0.0" 101 | ] 102 | }, 103 | "execution_count": 4, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "# Put your code here to price the corporate bond\n", 110 | "\n", 111 | "def corp_bond(mat=1, def_rate=0.03, rf_rate=0.04, recovery=0.3, n_sample=1e4):\n", 112 | " ### <-- YOUR ANSWER HERE\n", 113 | " ### <-- YOUR ANSWER HERE\n", 114 | " ### <-- YOUR ANSWER HERE\n", 115 | " return 0.0\n", 116 | "\n", 117 | "# Call your function\n", 118 | "corp_bond(mat, def_rate, rf_rate, recovery, n_sample)\n", 119 | "\n", 120 | "# Find the mean and std by calling the function 100 times. \n", 121 | "\n", 122 | "### <-- YOUR ANSWER HERE\n", 123 | "### <-- YOUR ANSWER HERE\n", 124 | "### <-- YOUR ANSWER HERE\n" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "### 2. Now, let's improve the function by reducing the MC variations.\n", 132 | "1. Use antithetic method: If `U` is uniform random variable, so is `1-U`\n", 133 | "2. Also shift the RNs to match the mean, `1/def_rate`" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 5, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "data": { 143 | "text/plain": [ 144 | "(10.000000000000002, 10.0)" 145 | ] 146 | }, 147 | "execution_count": 5, 148 | "metadata": {}, 149 | "output_type": "execute_result" 150 | } 151 | ], 152 | "source": [ 153 | "# For example, antithetic method mean\n", 154 | "n_sample = 10000\n", 155 | "U = np.random.uniform(size=n_sample/2)\n", 156 | "default_time = -(1/def_rate)*np.log(np.concatenate((U,1-U),axis=0))\n", 157 | "\n", 158 | "# Mean-matching means\n", 159 | "default_time += 1/def_rate-default_time.mean()\n", 160 | "(default_time.mean(), 1/def_rate)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 6, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "# No include the two new features: `antithetic` and `mean_match`\n", 170 | "\n", 171 | "def corp_bond_cv(mat=1, def_rate=0.03, rf_rate=0.04, recovery=0.3, n_sample=1e4, antithetic=True, mean_match=True):\n", 172 | " ### <--\n", 173 | " if(antithetic):\n", 174 | " 0\n", 175 | " \n", 176 | " if(mean_match):\n", 177 | " 0\n", 178 | " \n", 179 | " ### <--\n", 180 | " return 0.0\n", 181 | "\n", 182 | "# Find the mean and std by calling the function 100 times for (i) antithetic (ii) mean_match and (iii) both\n", 183 | "\n", 184 | "### <-- YOUR ANSWER HERE\n", 185 | "### <-- YOUR ANSWER HERE\n", 186 | "### <-- YOUR ANSWER HERE" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": { 192 | "collapsed": true 193 | }, 194 | "source": [ 195 | "### 3. Finally, what is the analytic value of the corporate bond? How does it compare to your MC result above?" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 7, 201 | "metadata": {}, 202 | "outputs": [], 203 | "source": [ 204 | "### Put the analytic expression for the corporate bond price\n", 205 | "\n", 206 | "### <-- YOUR ANSWER HERE\n", 207 | "### <-- YOUR ANSWER HERE\n", 208 | "### <-- YOUR ANSWER HERE" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [] 217 | } 218 | ], 219 | "metadata": { 220 | "anaconda-cloud": {}, 221 | "kernelspec": { 222 | "display_name": "Python [default]", 223 | "language": "python", 224 | "name": "python3" 225 | }, 226 | "language_info": { 227 | "codemirror_mode": { 228 | "name": "ipython", 229 | "version": 3 230 | }, 231 | "file_extension": ".py", 232 | "mimetype": "text/x-python", 233 | "name": "python", 234 | "nbconvert_exporter": "python", 235 | "pygments_lexer": "ipython3", 236 | "version": "3.5.6" 237 | } 238 | }, 239 | "nbformat": 4, 240 | "nbformat_minor": 1 241 | } 242 | -------------------------------------------------------------------------------- /py/HW1/HW1_Solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# HW 2: Corporate Bond Pricing (due by 9.21 Fri)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "We are going to compute the price of a corporate bond (subject to default) with Monte-Carlo simulation. Assume that \n", 15 | "* the default time of a company follows the exponential distribution with intensity $\\lambda=$__`def_rate`__. \n", 16 | "* the riskfree interest rate is $r_f=$__`rf_rate`__ and the maturity of the bond is $T=$__`mat`__. \n", 17 | "* in the case of default, you can recover some portion ($R=$__`recovery_rate`__) of the face value.\n", 18 | "* the coupon is 0%, i.e., it is a zero-coupon bond.\n", 19 | "* the face value of the bond is 1.0\n", 20 | "* use compound rate for discounting; the price of the default-free bond is $e^{-r_f T}$\n", 21 | "\n", 22 | "The Problem 1 of the [2017 ASP Midterm Exam](../files/ASP2017_Midterm.pdf) will be helpful.\n", 23 | "\n", 24 | "### Instruction to upload your HW\n", 25 | "* Create a repository named __`PHBS_ASP_2018`__ (and clone it to your PC)\n", 26 | "* Copy this file to __`PHBS_ASP_2018/HW2/HW2.ipynb`__ (Please use the same name for repository and ipynb file)\n", 27 | "* Adding more code.\n", 28 | "* Run your your code to make sure that there's no error.\n", 29 | "* Upload (commit and sync) your file." 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "### 1. First, let's create a pricing function and check the std " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 1, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "import numpy as np" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "def_rate = 0.1\n", 55 | "rf_rate = 0.03\n", 56 | "recovery = 0.3\n", 57 | "mat = 10" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "metadata": { 69 | "scrolled": true 70 | }, 71 | "outputs": [ 72 | { 73 | "data": { 74 | "text/plain": [ 75 | "(10.018452487063167, 10.0)" 76 | ] 77 | }, 78 | "execution_count": 3, 79 | "metadata": {}, 80 | "output_type": "execute_result" 81 | } 82 | ], 83 | "source": [ 84 | "# First generate exponential random numbers\n", 85 | "# Although you can generate directly using fault_time = np.random.exponential(scale=), let's use uniform random numbers.\n", 86 | "n_sample = 10000\n", 87 | "U = np.random.uniform(size=n_sample)\n", 88 | "default_time = -(1/def_rate)*np.log(U)\n", 89 | "\n", 90 | "# You can check if the RNs are correct by comparing the means\n", 91 | "(default_time.mean(), 1/def_rate)" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 4, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "(0.440636012835171, 0.002278125142615344)" 103 | ] 104 | }, 105 | "execution_count": 4, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "# Put your code here to price the corporate bond\n", 112 | "\n", 113 | "def corp_bond(def_rate=0.1, rf_rate=0.03, recovery=0.3, mat=10, n_sample=10000):\n", 114 | " U = np.random.uniform(size=n_sample)\n", 115 | " default_time = -(1/def_rate)*np.log(U)\n", 116 | " price = np.where(default_time>mat, \n", 117 | " np.exp(-rf_rate*mat), recovery*np.exp(-rf_rate*default_time))\n", 118 | " return np.mean(price)\n", 119 | "\n", 120 | "# Call your function\n", 121 | "corp_bond(def_rate, rf_rate, recovery, mat)\n", 122 | "\n", 123 | "# Find the mean and std by calling the function 100 times.\n", 124 | "arr = [corp_bond() for i in range(100)]\n", 125 | "np.mean(arr), np.std(arr)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "### 2. Now, let's improve the function by reducing the MC variations.\n", 133 | "1. Use antithetic method: If `U` is uniform random variable, so is `1-U`\n", 134 | "2. Also shift the RNs to match the mean, `1/def_rate`" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 5, 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "(10.000000000000002, 10.0)" 146 | ] 147 | }, 148 | "execution_count": 5, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "# For example, antithetic method mean\n", 155 | "n_sample = 10000\n", 156 | "U = np.random.uniform(size=int(n_sample/2))\n", 157 | "default_time = -(1/def_rate)*np.log(np.concatenate((U,1-U),axis=0))\n", 158 | "\n", 159 | "# Mean-matching means\n", 160 | "default_time += 1/def_rate-default_time.mean()\n", 161 | "(default_time.mean(), 1/def_rate)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 6, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "data": { 171 | "text/plain": [ 172 | "array([[0.4404722 , 0.00178332],\n", 173 | " [0.44042633, 0.00154062],\n", 174 | " [0.44040178, 0.00155355]])" 175 | ] 176 | }, 177 | "execution_count": 6, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "# No include the two new features: `antithetic` and `mean_match`\n", 184 | "\n", 185 | "def corp_bond_cv(def_rate=0.1, rf_rate=0.03, recovery=0.3, mat=10,\n", 186 | " antithetic=False, mean_match=False, n_sample=10000):\n", 187 | " \n", 188 | " if(antithetic):\n", 189 | " U = np.random.uniform(size=int(n_sample/2))\n", 190 | " U = np.concatenate((U,1-U), axis=0)\n", 191 | " else:\n", 192 | " U = np.random.uniform(size=n_sample)\n", 193 | "\n", 194 | " default_time = -(1/def_rate)*np.log(U)\n", 195 | " \n", 196 | " if(mean_match):\n", 197 | " default_time += 1/def_rate - default_time.mean()\n", 198 | " \n", 199 | " price = np.where(default_time>mat, np.exp(-rf_rate*mat), recovery*np.exp(-rf_rate*default_time))\n", 200 | " return np.mean(price) \n", 201 | "\n", 202 | "# Find the mean and std by calling the function 100 times for (i) antithetic (ii) mean_match and (iii) both\n", 203 | "arr1 = [corp_bond_cv(antithetic=True, mean_match=False) for i in range(100)]\n", 204 | "arr2 = [corp_bond_cv(antithetic=False, mean_match=True) for i in range(100)]\n", 205 | "arr3 = [corp_bond_cv(antithetic=True, mean_match=True) for i in range(100)]\n", 206 | "\n", 207 | "np.array( [ np.mean(arr1), np.std(arr1), \n", 208 | " np.mean(arr2), np.std(arr2), \n", 209 | " np.mean(arr3), np.std(arr3) ] ).reshape([3,2])" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": { 215 | "collapsed": true 216 | }, 217 | "source": [ 218 | "### 3. Finally, what is the analytic value of the corporate bond? How does it compare to your MC result above?" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 7, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "0.44040907156462505" 230 | ] 231 | }, 232 | "execution_count": 7, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "### Put the analytic expression for the corporate bond price\n", 239 | "\n", 240 | "def corp_bond_an(def_rate=0.1, rf_rate=0.03, recovery=0.3, mat=10):\n", 241 | " price = (recovery*def_rate*(1-np.exp((-rf_rate-def_rate)*mat)))/(rf_rate+def_rate)+np.exp((-rf_rate-def_rate)*mat)\n", 242 | " return np.mean(price)\n", 243 | "\n", 244 | "corp_bond_an()" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": {}, 250 | "source": [] 251 | }, 252 | { 253 | "cell_type": "markdown", 254 | "metadata": {}, 255 | "source": [] 256 | } 257 | ], 258 | "metadata": { 259 | "anaconda-cloud": {}, 260 | "kernelspec": { 261 | "display_name": "Python [default]", 262 | "language": "python", 263 | "name": "python3" 264 | }, 265 | "language_info": { 266 | "codemirror_mode": { 267 | "name": "ipython", 268 | "version": 3 269 | }, 270 | "file_extension": ".py", 271 | "mimetype": "text/x-python", 272 | "name": "python", 273 | "nbconvert_exporter": "python", 274 | "pygments_lexer": "ipython3", 275 | "version": "3.5.6" 276 | } 277 | }, 278 | "nbformat": 4, 279 | "nbformat_minor": 1 280 | } 281 | -------------------------------------------------------------------------------- /py/HW2/KrekelEtAl2004-Wilmott-BasketOption.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PHBS/ASP/6c8d180d5223501c9212a17f93c3155bc4602608/py/HW2/KrekelEtAl2004-Wilmott-BasketOption.xlsx -------------------------------------------------------------------------------- /py/HW2/TestCode_BasketSpread.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Basket option implementation with Bachelier model CV" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "%load_ext autoreload\n", 17 | "%autoreload 2" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "import numpy as np\n", 27 | "import pyfeng as pf\n", 28 | "\n", 29 | "#import option_models as opt\n", 30 | "from option_models import basket" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 3, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# A trivial test case 1: \n", 40 | "# one asset have 100% weight (the others zero)\n", 41 | "# the case should be equivalent to the BSM or Normal model price\n", 42 | "\n", 43 | "spot = np.ones(4) * 100\n", 44 | "vol = np.ones(4) * 0.4\n", 45 | "weights = np.array([1, 0, 0, 0])\n", 46 | "divr = np.zeros(4)\n", 47 | "intr = 0\n", 48 | "cor_m = 0.5*np.identity(4) + 0.5\n", 49 | "texp = 5\n", 50 | "strike = 120" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "array([[1. , 0.5, 0.5, 0.5],\n", 62 | " [0.5, 1. , 0.5, 0.5],\n", 63 | " [0.5, 0.5, 1. , 0.5],\n", 64 | " [0.5, 0.5, 0.5, 1. ]])" 65 | ] 66 | }, 67 | "execution_count": 4, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "cor_m" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 5, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "[1 0 0 0]\n", 86 | "26.570984651213593\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "print(weights)\n", 92 | "\n", 93 | "np.random.seed(123456)\n", 94 | "price_basket = basket.basket_price_mc(strike, spot, vol*spot, weights, texp, cor_m, bsm=False)\n", 95 | "print(price_basket)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 6, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "26.570984651213593 26.570845957870503\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "# Compare the price to normal model formula\n", 113 | "\n", 114 | "norm1 = pf.Norm(sigma=40)\n", 115 | "price_norm = norm1.price(strike, spot[0], texp, cp=1)\n", 116 | "print(price_basket, price_norm)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 7, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "[[1. 0.9999 0.9999 0.9999]\n", 129 | " [0.9999 1. 0.9999 0.9999]\n", 130 | " [0.9999 0.9999 1. 0.9999]\n", 131 | " [0.9999 0.9999 0.9999 1. ]]\n", 132 | "26.57211110181949 26.570845957870503\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "# A trivial test case 2\n", 138 | "# all assets almost perfectly correlated:\n", 139 | "# the case should be equivalent to the BSM or Normal model price\n", 140 | "\n", 141 | "spot = np.ones(4) * 100\n", 142 | "vol = np.ones(4) * 0.4\n", 143 | "weights = np.ones(4) * 0.25\n", 144 | "divr = np.zeros(4)\n", 145 | "intr = 0\n", 146 | "cor_m = 0.0001*np.identity(4) + 0.9999*np.ones((4,4))\n", 147 | "texp = 5\n", 148 | "strike = 120\n", 149 | "\n", 150 | "print( cor_m )\n", 151 | "\n", 152 | "np.random.seed(123456)\n", 153 | "price_basket = basket.basket_price_mc(strike, spot, vol*spot, weights, texp, cor_m, bsm=False)\n", 154 | "print(price_basket, price_norm)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 8, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "# A full test set for basket option with exact price\n", 164 | "\n", 165 | "spot = np.ones(4) * 100\n", 166 | "vol = np.ones(4) * 0.4\n", 167 | "weights = np.ones(4) * 0.25\n", 168 | "divr = np.zeros(4)\n", 169 | "intr = 0\n", 170 | "cor_m = 0.5*np.identity(4) + 0.5\n", 171 | "texp = 5\n", 172 | "strike = 100\n", 173 | "price_exact = 28.0073695" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 9, 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "data": { 183 | "text/plain": [ 184 | "(array([0.25, 0.25, 0.25, 0.25]),\n", 185 | " array([[1. , 0.5, 0.5, 0.5],\n", 186 | " [0.5, 1. , 0.5, 0.5],\n", 187 | " [0.5, 0.5, 1. , 0.5],\n", 188 | " [0.5, 0.5, 0.5, 1. ]]))" 189 | ] 190 | }, 191 | "execution_count": 9, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "weights, cor_m" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 10, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "28.22782882893394 28.0073695\n" 210 | ] 211 | } 212 | ], 213 | "source": [ 214 | "price_basket = basket.basket_price_mc(strike, spot, vol*spot, weights, texp, cor_m, bsm=False)\n", 215 | "print(price_basket, price_exact)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "# [To Do] Basket option implementation based on BSM model\n", 223 | "## Write the similar test for BSM" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 11, 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "name": "stdout", 233 | "output_type": "stream", 234 | "text": [ 235 | "0.0\n" 236 | ] 237 | } 238 | ], 239 | "source": [ 240 | "price_basket = basket.basket_price_mc(strike, spot, vol, weights, texp, cor_m, bsm=True)\n", 241 | "print(price_basket)" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 12, 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "name": "stdout", 251 | "output_type": "stream", 252 | "text": [ 253 | "[1 0 0 0]\n" 254 | ] 255 | } 256 | ], 257 | "source": [ 258 | "# A trivial test case 1: \n", 259 | "# one asset have 100% weight (the others zero)\n", 260 | "# the case should be equivalent to the BSM or Normal model price\n", 261 | "\n", 262 | "spot = np.ones(4) * 100\n", 263 | "vol = np.ones(4) * 0.4\n", 264 | "weights = np.array([1, 0, 0, 0])\n", 265 | "divr = np.zeros(4)\n", 266 | "intr = 0\n", 267 | "cor_m = 0.5*np.identity(4) + 0.5\n", 268 | "texp = 5\n", 269 | "strike = 120\n", 270 | "\n", 271 | "print(weights)\n", 272 | "\n", 273 | "np.random.seed(123456)\n", 274 | "price_basket = basket.basket_price_mc(strike, spot, vol, weights, texp, cor_m, bsm=True)" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 13, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "name": "stdout", 284 | "output_type": "stream", 285 | "text": [ 286 | "0.0 28.713486748445934\n" 287 | ] 288 | } 289 | ], 290 | "source": [ 291 | "bsm1 = pf.Bsm(sigma=vol[0])\n", 292 | "price_bsm = bsm1.price(strike, spot[0], texp, cp=1)\n", 293 | "print(price_basket, price_bsm)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "metadata": {}, 299 | "source": [ 300 | "# Spread option implementation based on normal model" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 14, 306 | "metadata": {}, 307 | "outputs": [], 308 | "source": [ 309 | "# A full test set for spread option\n", 310 | "\n", 311 | "spot = np.array([100, 96])\n", 312 | "vol = np.array([0.2, 0.1])\n", 313 | "weights = np.array([1, -1])\n", 314 | "divr = np.array([1, 1])*0.05\n", 315 | "intr = 0.1\n", 316 | "cor = 0.5\n", 317 | "cor_m = np.array([[1, cor], [cor, 1]])\n", 318 | "texp = 1\n", 319 | "strike = 0\n", 320 | "price_exact = 8.5132252" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 15, 326 | "metadata": {}, 327 | "outputs": [ 328 | { 329 | "name": "stdout", 330 | "output_type": "stream", 331 | "text": [ 332 | "8.317680907159142 8.5132252\n" 333 | ] 334 | } 335 | ], 336 | "source": [ 337 | "# MC price based on normal model\n", 338 | "# make sure that the prices are similar\n", 339 | "\n", 340 | "np.random.seed(123456)\n", 341 | "price_spread = basket.basket_price_mc(strike, spot, vol*spot, weights, texp, cor_m, intr=intr, divr=divr, bsm=False)\n", 342 | "print(price_spread, price_exact)" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "# Spread option implementation based on BSM model" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 16, 355 | "metadata": {}, 356 | "outputs": [ 357 | { 358 | "data": { 359 | "text/plain": [ 360 | "0.0" 361 | ] 362 | }, 363 | "execution_count": 16, 364 | "metadata": {}, 365 | "output_type": "execute_result" 366 | } 367 | ], 368 | "source": [ 369 | "# Once the implementation is finished the BSM model price should also work\n", 370 | "price_spread = basket.basket_price_mc(\n", 371 | " strike, spot, vol*spot, weights, texp, cor_m, intr=intr, divr=divr, bsm=True)\n", 372 | "price_spread" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 17, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "name": "stdout", 382 | "output_type": "stream", 383 | "text": [ 384 | "8.513225229545505 0.0\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "# You also test Kirk's approximation\n", 390 | "kirk = pf.BsmSpreadKirk(vol, cor=cor, divr=divr, intr=intr)\n", 391 | "price_kirk = kirk.price(strike, spot, texp)\n", 392 | "print(price_kirk, price_spread)" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "# [To Do] Complete the implementation of basket_price_norm_analytic\n", 400 | "# Compare the MC stdev of BSM basket prices from with and without CV" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 18, 406 | "metadata": {}, 407 | "outputs": [], 408 | "source": [ 409 | "# The basket option example from above\n", 410 | "spot = np.ones(4) * 100\n", 411 | "vol = np.ones(4) * 0.4\n", 412 | "weights = np.array([1, 1, 1, 1])/4\n", 413 | "divr = np.zeros(4)\n", 414 | "intr = 0\n", 415 | "cor_m = 0.5*np.identity(4) + 0.5\n", 416 | "texp = 5\n", 417 | "strike = 120" 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": 19, 423 | "metadata": {}, 424 | "outputs": [ 425 | { 426 | "data": { 427 | "text/plain": [ 428 | "0.0" 429 | ] 430 | }, 431 | "execution_count": 19, 432 | "metadata": {}, 433 | "output_type": "execute_result" 434 | } 435 | ], 436 | "source": [ 437 | "### Make sure that the analytic normal price is correctly implemented\n", 438 | "basket.basket_price_norm_analytic(strike, spot, vol*spot, weights, texp, cor_m, intr=intr, divr=divr)" 439 | ] 440 | }, 441 | { 442 | "cell_type": "code", 443 | "execution_count": 20, 444 | "metadata": {}, 445 | "outputs": [], 446 | "source": [ 447 | "# Run below about 100 times and get the mean and stdev\n", 448 | "\n", 449 | "### Returns 2 prices, without CV and with CV \n", 450 | "price_basket = basket.basket_price_mc_cv(strike, spot, vol, weights, texp, cor_m)" 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": 21, 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "[0. 0.]\n" 463 | ] 464 | } 465 | ], 466 | "source": [ 467 | "print(price_basket)" 468 | ] 469 | } 470 | ], 471 | "metadata": { 472 | "anaconda-cloud": {}, 473 | "kernelspec": { 474 | "display_name": "Python 3 (ipykernel)", 475 | "language": "python", 476 | "name": "python3" 477 | }, 478 | "language_info": { 479 | "codemirror_mode": { 480 | "name": "ipython", 481 | "version": 3 482 | }, 483 | "file_extension": ".py", 484 | "mimetype": "text/x-python", 485 | "name": "python", 486 | "nbconvert_exporter": "python", 487 | "pygments_lexer": "ipython3", 488 | "version": "3.8.11" 489 | } 490 | }, 491 | "nbformat": 4, 492 | "nbformat_minor": 1 493 | } 494 | -------------------------------------------------------------------------------- /py/HW2/option_models/basket.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Sep 19 22:56:58 2017 4 | 5 | @author: jaehyuk 6 | """ 7 | import numpy as np 8 | import scipy.stats as ss 9 | import pyfeng as pf 10 | 11 | def basket_check_args(spot, vol, corr_m, weights): 12 | ''' 13 | This function simply checks that the size of the vector (matrix) are consistent 14 | ''' 15 | n = spot.size 16 | assert( n == vol.size ) 17 | assert( corr_m.shape == (n, n) ) 18 | return None 19 | 20 | def basket_price_mc_cv( 21 | strike, spot, vol, weights, texp, cor_m, 22 | intr=0.0, divr=0.0, cp=1, n_samples=10000 23 | ): 24 | # price1 = MC based on BSM 25 | rand_st = np.random.get_state() # Store random state first 26 | price1 = basket_price_mc( 27 | strike, spot, vol, weights, texp, cor_m, 28 | intr, divr, cp, True, n_samples) 29 | 30 | ''' 31 | compute price2: mc price based on normal model 32 | make sure you use the same seed 33 | 34 | # Restore the state in order to generate the same state 35 | np.random.set_state(rand_st) 36 | price2 = basket_price_mc( 37 | strike, spot, spot*vol, weights, texp, cor_m, 38 | intr, divr, cp, False, n_samples) 39 | ''' 40 | price2 = 0 41 | 42 | ''' 43 | compute price3: analytic price based on normal model 44 | 45 | price3 = basket_price_norm_analytic( 46 | strike, spot, vol, weights, texp, cor_m, intr, divr, cp) 47 | ''' 48 | price3 = 0 49 | 50 | # return two prices: without and with CV 51 | return np.array([price1, price1 - (price2 - price3)]) 52 | 53 | 54 | def basket_price_mc( 55 | strike, spot, vol, weights, texp, cor_m, 56 | intr=0.0, divr=0.0, cp=1, bsm=True, n_samples = 100000 57 | ): 58 | basket_check_args(spot, vol, cor_m, weights) 59 | 60 | div_fac = np.exp(-texp*divr) 61 | disc_fac = np.exp(-texp*intr) 62 | forward = spot / disc_fac * div_fac 63 | 64 | cov_m = vol * cor_m * vol[:,None] 65 | chol_m = np.linalg.cholesky(cov_m) # L matrix in slides 66 | 67 | n_assets = spot.size 68 | znorm_m = np.random.normal(size=(n_assets, n_samples)) 69 | 70 | if( bsm ) : 71 | ''' 72 | PUT the simulation of the geometric brownian motion below 73 | ''' 74 | prices = np.zeros_like(znorm_m) 75 | else: 76 | # bsm = False: normal model 77 | prices = forward[:,None] + np.sqrt(texp) * chol_m @ znorm_m 78 | 79 | price_weighted = weights @ prices 80 | 81 | price = np.mean( np.fmax(cp*(price_weighted - strike), 0) ) 82 | return disc_fac * price 83 | 84 | 85 | def basket_price_norm_analytic( 86 | strike, spot, vol, weights, 87 | texp, cor_m, intr=0.0, divr=0.0, cp=1 88 | ): 89 | 90 | ''' 91 | The analytic (exact) option price under the normal model 92 | 93 | 1. compute the forward of the basket 94 | 2. compute the normal volatility of basket 95 | 3. plug in the forward and volatility to the normal price formula 96 | 97 | norm = pf.Norm(sigma, intr=intr, divr=divr) 98 | norm.price(strike, spot, texp, cp=cp) 99 | 100 | PUT YOUR CODE BELOW 101 | ''' 102 | 103 | 104 | 105 | return 0.0 106 | -------------------------------------------------------------------------------- /py/HW3/Demo_Advanced_Import.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Now we use advanced _import_ methods\n", 8 | "\n", 9 | "\n", 10 | "Check out directory structure\n", 11 | "\n", 12 | "Check out the contents of `/option_models/__init__.py`\n", 13 | "\n", 14 | "```python\n", 15 | "from . import bsm\n", 16 | "from . import normal\n", 17 | "from . import sabr\n", 18 | "```\n" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# when directory is imported, __init__.py is executed\n", 28 | "# from __init__.py all sub modules are imported as well\n", 29 | "\n", 30 | "import option_models as opt" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "(,\n", 42 | " option_models.bsm.Model)" 43 | ] 44 | }, 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "# you may call\n", 52 | "opt.bsm.price, opt.bsm.Model" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 3, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "from option_models import bsm\n", 62 | "# You can even do\n", 63 | "#from option_models import bsm as bs" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 4, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "(,\n", 75 | " option_models.bsm.Model)" 76 | ] 77 | }, 78 | "execution_count": 4, 79 | "metadata": {}, 80 | "output_type": "execute_result" 81 | } 82 | ], 83 | "source": [ 84 | "bsm.price, bsm.Model" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 5, 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "data": { 94 | "text/plain": [ 95 | "True" 96 | ] 97 | }, 98 | "execution_count": 5, 99 | "metadata": {}, 100 | "output_type": "execute_result" 101 | } 102 | ], 103 | "source": [ 104 | "# The objects imported different ways are all same (as class type)\n", 105 | "bsm.Model == opt.bsm.Model" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 6, 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "# you can create instances \n", 115 | "bsm_model1 = bsm.Model(vol=0.2, texp=1)\n", 116 | "bsm_model2 = opt.bsm.Model(vol=0.2, texp=1)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 7, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "False" 128 | ] 129 | }, 130 | "execution_count": 7, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "# But class instances are diffferent !!\n", 137 | "bsm_model1 == bsm_model2" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "## You cal also import a particular class or functions" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 8, 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "data": { 154 | "text/plain": [ 155 | "True" 156 | ] 157 | }, 158 | "execution_count": 8, 159 | "metadata": {}, 160 | "output_type": "execute_result" 161 | } 162 | ], 163 | "source": [ 164 | "from option_models.bsm import Model\n", 165 | "Model == bsm.Model\n", 166 | "# However this is confusing. which Model?" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 9, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "data": { 176 | "text/plain": [ 177 | "(True, True)" 178 | ] 179 | }, 180 | "execution_count": 9, 181 | "metadata": {}, 182 | "output_type": "execute_result" 183 | } 184 | ], 185 | "source": [ 186 | "# Always better to be explicit\n", 187 | "from option_models.bsm import Model as BsmModel\n", 188 | "from option_models.normal import Model as NormalModel\n", 189 | "\n", 190 | "BsmModel == bsm.Model, NormalModel == opt.normal.Model" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": { 203 | "collapsed": true 204 | }, 205 | "source": [ 206 | "## And more coming.." 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": {}, 213 | "outputs": [], 214 | "source": [] 215 | } 216 | ], 217 | "metadata": { 218 | "anaconda-cloud": {}, 219 | "kernelspec": { 220 | "display_name": "Python 3 (ipykernel)", 221 | "language": "python", 222 | "name": "python3" 223 | }, 224 | "language_info": { 225 | "codemirror_mode": { 226 | "name": "ipython", 227 | "version": 3 228 | }, 229 | "file_extension": ".py", 230 | "mimetype": "text/x-python", 231 | "name": "python", 232 | "nbconvert_exporter": "python", 233 | "pygments_lexer": "ipython3", 234 | "version": "3.8.11" 235 | } 236 | }, 237 | "nbformat": 4, 238 | "nbformat_minor": 1 239 | } 240 | -------------------------------------------------------------------------------- /py/HW3/option_models/__init__.py: -------------------------------------------------------------------------------- 1 | from . import sabr -------------------------------------------------------------------------------- /py/HW3/option_models/sabr.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Oct 10 4 | 5 | @author: jaehyuk 6 | """ 7 | 8 | import numpy as np 9 | import pyfeng as pf 10 | import abc 11 | 12 | class ModelABC(abc.ABC): 13 | beta = 1 # fixed (not used) 14 | vov, rho = 0.0, 0.0 15 | sigma, intr = None, None 16 | 17 | ### Numerical Parameters 18 | dt = 0.1 19 | n_path = 10000 20 | 21 | def __init__(self, sigma, vov=0, rho=0.0, beta=1.0, intr=0.0): 22 | self.sigma = sigma 23 | self.vov = vov 24 | self.rho = rho 25 | self.beta = beta 26 | self.intr = intr 27 | 28 | def base_model(self, sigma=None): 29 | if sigma is None: 30 | sigma = self.sigma 31 | 32 | if self.beta == 0: 33 | return pf.Norm(sigma, intr=self.intr) 34 | elif self.beta == 1: 35 | return pf.Bsm(sigma, intr=self.intr) 36 | else: 37 | raise ValueError(f'0