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