{"id":13699779,"url":"https://github.com/ynouri/pysabr","last_synced_at":"2025-05-04T17:30:55.527Z","repository":{"id":30583508,"uuid":"95929861","full_name":"ynouri/pysabr","owner":"ynouri","description":"SABR model Python implementation","archived":false,"fork":false,"pushed_at":"2022-04-21T20:41:39.000Z","size":3585,"stargazers_count":494,"open_issues_count":6,"forks_count":84,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-04-13T08:20:03.670Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ynouri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-30T23:07:11.000Z","updated_at":"2025-04-12T11:36:55.000Z","dependencies_parsed_at":"2022-07-13T08:21:24.430Z","dependency_job_id":null,"html_url":"https://github.com/ynouri/pysabr","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ynouri%2Fpysabr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ynouri%2Fpysabr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ynouri%2Fpysabr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ynouri%2Fpysabr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ynouri","download_url":"https://codeload.github.com/ynouri/pysabr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252371644,"owners_count":21737372,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T20:00:43.293Z","updated_at":"2025-05-04T17:30:54.531Z","avatar_url":"https://github.com/ynouri.png","language":"Jupyter Notebook","funding_links":[],"categories":["Python","Python：量化金融第一生态"],"sub_categories":["Financial Instruments and Pricing","二、金融工具定价与估值"],"readme":"# pysabr\nPython implementation of SABR model.\n\n![Lognormal SABR vs Normal SABR](./notebooks/Lognormal%20SABR%20vs%20Normal%20SABR.png \"Lognormal SABR vs Normal SABR\")\n\n\n# Introduction\nSABR (Stochastic Alpha Beta Rho) is a financial volatility smile model widely used for interest rates options such as swaptions or cap/floors. This Python library implements its Hagan 2002 specification. For more information about the model itself, please consult the [original paper](./papers/Hagan%20-%20Managing%20Smile%20Risk%20-%202002.pdf) or [Wikipedia](https://en.wikipedia.org/wiki/SABR_volatility_model).\n\n# Requirements\nCore pySABR functions require `numpy` \u0026 `scipy` to run. The web microservice is based on `falcon`, which can itself be run with `waitress` (Windows) or `gunicorn` (Linux). Finally, the Excel function wrapper for the web microservice requires Windows and Excel 2013+.\n\n# Installation\n```bash\npip install pysabr\n\n```\n\n# Examples\n\n`pysabr` provides two interface levels:\n* A high-level, SABR model object interface, that lets the user work with the standard market inputs (ATM normal vol) and easily access model results (SLN or N vols, option premiums, density).\n* A low-level interface to the Hagan expansions formulas and to the Black Scholes model.\n\n## Notebook: Lognormal vs Normal SABR\n\n[This example notebook](./notebooks/Lognormal%20SABR%20vs%20Normal%20SABR.ipynb) runs an interesting comparison between the Lognormal and Normal SABR expansions available in Hagan's 2002 paper. Make sure to check it out!\n\n## SABR model object\n\nInterpolate a volatility using ATM normal vol input:\n```Python\nfrom pysabr import Hagan2002LognormalSABR\n# Forward = 2.5%, Shift = 3%, ATM Normal Vol = 40bps\n# Beta = 0.5, Rho = -20%, Volvol = 0.30\nsabr = Hagan2002LognormalSABR(f=0.025, shift=0.03, t=1., v_atm_n=0.0040,\n                              beta=0.5, rho=-0.2, volvol=0.30)\nk = 0.025\nsabr.lognormal_vol(k) * 100\n# returns 7.27\nsabr.normal_vol(k) *1e4\n# returns 40\n```\n\nCalibrate alpha, rho and volvol from a discrete shift-lognormal smile:\n```Python\nfrom pysabr import Hagan2002LognormalSABR\nimport numpy as np\nsabr = Hagan2002LognormalSABR(f=2.5271/100, shift=3/100, t=10, beta=0.5)\nk = np.array([-0.4729, 0.5271, 1.0271, 1.5271, 1.7771, 2.0271, 2.2771, 2.4021,\n              2.5271, 2.6521, 2.7771, 3.0271, 3.2771, 3.5271, 4.0271, 4.5271,\n              5.5271]) / 100\nv_sln = np.array([19.641923, 15.785344, 14.305103, 13.073869, 12.550007, 12.088721,\n              11.691661, 11.517660, 11.360133, 11.219058, 11.094293, 10.892464,\n              10.750834, 10.663653, 10.623862, 10.714479, 11.103755])\n[alpha, rho, volvol] = sabr.fit(k, v_sln)\n# returns [0.025299981543599154, -0.24629917636394097, 0.2908005625794777]\n```\n\n## Hagan 2002 lognormal expansion\n\nInterpolate a shifted-lognormal volatility:\n```Python\nfrom pysabr import hagan_2002_lognormal_sabr as hagan2002\n[s, k, f, t, alpha, beta, rho, volvol] = [0.03, 0.02, 0.025, 1.0, 0.025, 0.50, -0.24, 0.29]\nhagan2002.lognormal_vol(k + s, f + s, t, alpha, beta, rho, volvol)\n# returns 0.11408307\n```\n\nCalibrate alpha from an ATM lognormal vol:\n```Python\nfrom pysabr import hagan_2002_lognormal_sabr as hagan2002\n[v_atm_sln, f, t, beta, rho, volvol] = [0.60, 0.02, 1.5, 1.0, 0.0, 0.0]\nhagan2002.alpha(v_atm_sln, f, t, beta, rho, volvol)\n# returns 0.60\n```\n\n## Black Scholes\n\nCompute an option premium using Black formula:\n```Python\nfrom pysabr import black\n[k, f, t, v, r, cp] = [0.012, 0.013, 10., 0.20, 0.02, 'call']\nblack.lognormal_call(k, f, t, v, r, cp) * 1e5\n# returns 296.8806106707276\n```\n\n# Web microservice\n\npySABR includes a web microservice exposing the two main functions of the library: volatility interpolation and alpha calibration. Those two\nfunctions are available through a simple REST API:\n\n```bash\n# Returns a lognormal vol\ncurl http://127.0.0.1:5000/sabr?k=1.0\u0026f=1.0\u0026t=1.0\u0026a=0.20\u0026b=1.0\u0026r=0.0\u0026n=0.2\n\n# Returns a calibrated alpha parameter\ncurl\nhttp://127.0.0.1:5000/alpha?v=0.6\u0026f=1.0\u0026t=1.0\u0026b=1.0\u0026r=0.0\u0026n=0.2\n```\n\nTo run the microservice on Linux:\n```bash\ngunicorn -b '0.0.0.0:5000' web.app:app \u0026\u003e\u003e pysabr_web.log \u0026\n```\n\nTo run the microservice on Windows:\n```bash\npython -mwaitress --port=5000 web.app:app\n```\n\n# Excel wrapper\n\nThe web microservice can conveniently be called from Excel 2013+ using the ```WEBSERVICE``` spreadsheet function. For even more convenience, pySABR provides a small VBA wrapper mapping directly to the /sabr and /alpha resources. VBA code is available under [pySABR_web.bas](./web/pySABR_web.bas)\n\n\n# Run the tests\n```bash\n$ python -m pytest\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fynouri%2Fpysabr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fynouri%2Fpysabr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fynouri%2Fpysabr/lists"}