{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Scenario Context\n", "Scenario contexts enable the user to price and calculate risk under varying market states and pricing environments." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from gs_quant.instrument import IRSwaption\n", "from gs_quant.risk import MarketDataPattern, MarketDataShock, MarketDataShockType, MarketDataShockBasedScenario, \\\n", " RollFwd, CurveScenario, IndexCurveShift\n", "from gs_quant.session import Environment, GsSession" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n", "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create and price a swaption\n", "swaption = IRSwaption('Pay', '5y', 'USD', expiration_date='3m')\n", "base_price = swaption.price()\n", "base_price" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### MarketDataShockBasedScenario\n", "Allows the user to create a bespoke market data shock" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "MarketDataShockBasedScenario?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Scenario Creation: Shock all points on the Vol Curve by 1bp\n", "ir_vol_scenario = MarketDataShockBasedScenario(\n", " shocks={\n", " MarketDataPattern('IR Vol'): MarketDataShock(MarketDataShockType.Absolute, 1e-4)\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Price swaption under scenario\n", "with ir_vol_scenario:\n", " scenario_price = swaption.price()\n", "\n", "scenario_price" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Swaption Price Comparison: Base vs. Shocked Vol Curves\n", "diff = scenario_price - base_price\n", "diff" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Comparing Parallel Bump Scenario w/ Vega\n", "from gs_quant.risk import IRVegaParallel\n", "vega = swaption.calc(IRVegaParallel)\n", "vega" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Curve Scenario \n", "A predefined scenario used to modify the shape of the curve with bespoke transformations - by applying both parallel and slope shifts. \n", "* market_data_pattern - Market pattern for matching curve assets (required parameter)\n", "* parallel_shift - A constant (X bps) which shifts all points by the same amount\n", "* curve_shift - A double which represents the net rate change (X bps) between tenorStart and tenorEnd\n", "* pivot_point – The tenor in years (float) at which there is zero rate change, which is between tenor_start and tenor_end inclusive, informing the type of curve shift. If not specified, pivot_point is the midpoint of tenor_start and tenor_end\n", "* tenor_start – The tenor, in years, (float) which is the starting point of the curve shift (required parameter is curve_shift is specified)\n", "* tenor_end – The tenor, in years, (float) which is the end point of the curve shift (required parameter is curve_shift is specified)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "CurveScenario?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# Scenario Creation: modify the Vol Curve by a 5bp parallel shift, 1bp slope shift pivoted at 5y point (up to 50y)\n", "curve_scenario = CurveScenario(market_data_pattern=MarketDataPattern('IR', 'USD'), parallel_shift=5,\n", " curve_shift=1, pivot_point=5, tenor_end=50, tenor_start=0)\n", "\n", "with curve_scenario:\n", " swaption_scenario_price = swaption.price()\n", "\n", "# Look at the difference between scenario and base prices\n", "print('Base price: {:,.2f}'.format(base_price))\n", "print('Scenario price: {:,.2f}'.format(swaption_scenario_price))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RollFwd Scenario\n", "A predefined scenario used to evolve market data and trades over a period of time\n", "* date - Absolute or Relative Date to shift markets to\n", "* realise_fwd - Roll along the forward curve or roll in spot space\n", "* holiday_calendar - Calendar to use if relative date is specified in the date parameter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "RollFwd?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# RollFwd Scenario - Roll forward 1 month\n", "base_price = swaption.price()\n", "with RollFwd(date='1m', holiday_calendar='NYC', realise_fwd=False):\n", " fwd_price = swaption.price()\n", "\n", "print('Base price: {:,.2f}'.format(base_price))\n", "print('Scenario price: {:,.2f}'.format(fwd_price))\n", "print('Diff: {:,.2f}'.format(fwd_price - base_price))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Index Curve Shift Scenario \n", "A predefined scenario used to modify the shape of the index curve. This allows the user to easily shock the curve including parallel shift and slope shift. Users can even specify which part of the curve for parallel shift through custom bucket. \n", "* market_data_pattern - Market pattern for matching curve assets\n", "* rate_option - Rate option of the index curve.\n", "* tenor - Tenor of the index curve.\n", "* floor - Floor size in bps of the index curve.\n", "* annualised_parallel_shift – Parallel shift in bps.\n", "* annualised_slope_shift – Annual slope shift in bps with pivot = 0.\n", "* cutoff – Cutoff time in years to stop applying the shift.\n", "* bucket_start - Start date for the custom bucket.\n", "* bucket_end - End date for the custom bucket.\n", "* bucket_shift - Bucket shift in bps for the custom bucket." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "IndexCurveShift?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Scenario Creation: modify the index curve of this swaption by a 1bp parallel shift, 1bp slope shift, pivoted default to 0 year with floor value -1bp (up to 50y)\n", "index_curve_shift = IndexCurveShift(rate_option=\"USD-LIBOR-BBA\", tenor=\"3m\", annualised_parallel_shift=1,\n", " annualised_slope_shift=1, floor=-1, cutoff=50)\n", "\n", "with index_curve_shift:\n", " swaption_scenario_price = swaption.price()\n", "\n", "# Look at the difference between scenario and base prices\n", "print('Base price: {:,.2f}'.format(base_price))\n", "print('Scenario price: {:,.2f}'.format(swaption_scenario_price))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }