aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks/Tranche calculator.ipynb
blob: 3b55e4a7f65c14796e6b5a18f4466a3779905c71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import analytics.tranche_basket as bkt\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from analytics.scenarios import run_tranche_scenarios, run_portfolio_scenarios, run_tranche_scenarios_rolldown\n",
    "from analytics import DualCorrTranche, TrancheBasket\n",
    "from utils.db import dbconn\n",
    "from datetime import date\n",
    "\n",
    "value_date = (date.today() - pd.offsets.BDay(1)).date()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_type = 'HY'\n",
    "series = 35\n",
    "tenor = '5yr'\n",
    "value_date = date.today()\n",
    "price = 103.875\n",
    "at_det = [0, 15, 25, 35, 100] if index_type == 'HY' else ['0', '3', '7', '15', '100']\n",
    "tranche_prices= [41.4, 90.6, 109.6, 119.7]\n",
    "\n",
    "#Build another skew to price this new series\n",
    "base_index = TrancheBasket(\"HY\", 33, \"5yr\")\n",
    "base_index.tweak()\n",
    "base_index.build_skew()\n",
    "skew=base_index.skew"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = []\n",
    "for i in range(3):\n",
    "    #set up\n",
    "    rho_floor = tranche.rho[1] if i > 0 else 0.2\n",
    "    rho_min = rho_floor\n",
    "    rho_max =  rho_floor + 0.4\n",
    "    tranche = DualCorrTranche(index_type, series, tenor, attach=at_det[i], detach=at_det[i+1], corr_attach = rho_floor, corr_detach = rho_min + (rho_max -rho_min)/2, tranche_running = 500, value_date=value_date)\n",
    "    tranche._index.tweak([price])\n",
    "    #now loop to find it\n",
    "    for j in range(20):\n",
    "        if tranche.price <= tranche_prices[i]:\n",
    "            rho_min = tranche.rho[1]\n",
    "        else:\n",
    "            rho_max = tranche.rho[1]\n",
    "        tranche.rho[1] = rho_min + (rho_max - rho_min)/2\n",
    "    results.append([tranche.rho[1], tranche.price, tranche.delta, tranche.gamma, tranche.theta(skew=skew), tranche.delta * float(tranche._index.theta())])\n",
    "ss_corr = tranche.rho[1]\n",
    "tranche = DualCorrTranche(index_type, series, tenor, attach=at_det[i+1], detach=100, corr_attach = ss_corr, corr_detach = .999, tranche_running = 500, value_date=value_date)\n",
    "tranche._index.tweak([price])\n",
    "results.append([tranche.rho[1], tranche.price, tranche.delta, tranche.gamma, tranche.theta(skew=skew), tranche.delta * float(tranche._index.theta())])\n",
    "results = pd.DataFrame(results, columns = ['corr', 'price', 'delta', 'gamma', 'theta', 'delta * index_theta'])\n",
    "results['theta_per_delta'] = results['theta'] / results['delta']\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Using another skew\n",
    "mapped_results = []\n",
    "for i in range(3):\n",
    "    tranche = DualCorrTranche(index_type, series, tenor, attach=at_det[i], detach=at_det[i+1], corr_attach = np.nan, corr_detach = 0.1, tranche_running = 500, value_date=value_date)\n",
    "    tranche._index.tweak([price])\n",
    "    tranche.mark(skew=skew)\n",
    "    mapped_results.append([tranche.rho[1], tranche.price, tranche.delta, tranche.gamma, tranche.theta(skew=skew), tranche.corr01/tranche.notional])\n",
    "ss_corr = tranche.rho[1]\n",
    "tranche = DualCorrTranche(index_type, series, tenor, attach=at_det[i+1], detach=100, corr_attach = ss_corr, corr_detach = .999, tranche_running = 500, value_date=value_date)\n",
    "tranche._index.tweak([price])\n",
    "mapped_results.append([tranche.rho[1], tranche.price, tranche.delta, tranche.gamma, tranche.theta(skew=skew), np.nan])\n",
    "mapped_results = pd.DataFrame(mapped_results, columns = ['corr', 'price', 'delta', 'gamma', 'theta', 'corr01'])\n",
    "mapped_results['theta_per_delta'] = mapped_results['theta'] / mapped_results['delta']\n",
    "mapped_results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.1 64-bit",
   "language": "python",
   "name": "python38164bitc40c8740e5d542d7959acb14be96f4f3"
  },
  "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}