notebook changes
This commit is contained in:
@@ -418,7 +418,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 66,
|
"execution_count": null,
|
||||||
"id": "b78e2858",
|
"id": "b78e2858",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@@ -459,13 +459,10 @@
|
|||||||
" settle = datetime.strptime(settle_date_str, '%d-%m-%Y') \n",
|
" settle = datetime.strptime(settle_date_str, '%d-%m-%Y') \n",
|
||||||
" maturity = datetime.strptime(maturity_date_str, '%d-%m-%Y')\n",
|
" maturity = datetime.strptime(maturity_date_str, '%d-%m-%Y')\n",
|
||||||
" time_delta = maturity - settle\n",
|
" time_delta = maturity - settle\n",
|
||||||
" # Use 365.25 for a more accurate average year length\n",
|
|
||||||
" T = time_delta.days / 365.25 \n",
|
" T = time_delta.days / 365.25 \n",
|
||||||
" # Periods (N) must be the ceiling of T * freq, where freq=1 here\n",
|
|
||||||
" periods = int(np.ceil(T * freq)) \n",
|
" periods = int(np.ceil(T * freq)) \n",
|
||||||
" return T, periods\n",
|
" return T, periods\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Get the Time to Maturity (T) and the number of periods (N) for each bond\n",
|
|
||||||
"T_list = []\n",
|
"T_list = []\n",
|
||||||
"N_list = []\n",
|
"N_list = []\n",
|
||||||
"for mat_date in maturities:\n",
|
"for mat_date in maturities:\n",
|
||||||
@@ -473,56 +470,32 @@
|
|||||||
" T_list.append(T)\n",
|
" T_list.append(T)\n",
|
||||||
" N_list.append(N)\n",
|
" N_list.append(N)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# 1. Calculate Durations and Convexities for the three bonds\n",
|
|
||||||
"mac_durations = []\n",
|
"mac_durations = []\n",
|
||||||
"modified_durations = []\n",
|
|
||||||
"per_durations = []\n",
|
|
||||||
"convexities = []\n",
|
"convexities = []\n",
|
||||||
"\n",
|
"\n",
|
||||||
"for y, r, N in zip(yields, coupon_rates, N_list):\n",
|
"for y, r, N in zip(yields, coupon_rates, N_list):\n",
|
||||||
" dur = calculate_macualy_duration(y, face_value, r, N, freq)\n",
|
" dur = calculate_macualy_duration(y, face_value, r, N, freq)\n",
|
||||||
" dur_mod = calculate_modified_duration(dur, y)\n",
|
|
||||||
" dur_per = calculate_periodic_duration(dur, freq)\n",
|
|
||||||
" conv = calculate_convexity(y, face_value, r, N, freq)\n",
|
" conv = calculate_convexity(y, face_value, r, N, freq)\n",
|
||||||
" mac_durations.append(dur)\n",
|
" mac_durations.append(dur)\n",
|
||||||
" modified_durations.append(dur_per)\n",
|
|
||||||
" per_durations.append(dur_per)\n",
|
|
||||||
" convexities.append(conv)\n",
|
" convexities.append(conv)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"mac_durations = np.array(mac_durations)\n",
|
"mac_durations = np.array(mac_durations)\n",
|
||||||
"modified_durations = np.array(modified_durations)\n",
|
|
||||||
"per_durations = np.array(per_durations)\n",
|
|
||||||
"convexities = np.array(convexities)\n",
|
"convexities = np.array(convexities)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# 2. Define the Coefficient Matrix (A) and the Target Vector (b)\n",
|
"A = np.vstack([mac_durations, convexities, np.ones(3)])\n",
|
||||||
"A = np.vstack([durations, convexities, np.ones(3)])\n",
|
|
||||||
"b = np.array([10, 160, 1])\n",
|
"b = np.array([10, 160, 1])\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# 3. Solve the Linear System (weights = A\\b)\n",
|
|
||||||
"weights = np.linalg.solve(A, b)\n",
|
"weights = np.linalg.solve(A, b)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# 4. Print Results\n",
|
"# 4. Print Results\n",
|
||||||
"print(\"--- Bond Portfolio Immunization ---\")\n",
|
"print(f\"Durations: {mac_durations.round(4)}\")\n",
|
||||||
"print(f\"Settle Date: {settles}\")\n",
|
"print(f\"Convexities: {convexities.round(4)}\")\n",
|
||||||
"print(f\"Maturity Dates: {maturities}\")\n",
|
|
||||||
"print(f\"Calculated Periods (N): {N_list}\")\n",
|
|
||||||
"print(f\"Calculated Durations: {durations.round(4)}\")\n",
|
|
||||||
"print(f\"Calculated Convexities: {convexities.round(4)}\")\n",
|
|
||||||
"print(\"\\nCoefficient Matrix (A):\")\n",
|
"print(\"\\nCoefficient Matrix (A):\")\n",
|
||||||
"print(A.round(4))\n",
|
"print(A.round(4))\n",
|
||||||
"print(\"\\n--- Solution ---\")\n",
|
"print(\"\\nSolution\")\n",
|
||||||
"print(f\"The Required Weights are:\\n{weights.round(4)}\")\n",
|
"print(f\"Weights:\\n{weights.round(4)}\")"
|
||||||
"print(f\"Verification (A @ weights): {(A @ weights).round(4)}\")"
|
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "e02704c5",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": []
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
Reference in New Issue
Block a user