Financial Calculations & Valuation
Advanced
Fixed-Income Bond Portfolio Immunization & Duration Matching in Python
Construct an interest-rate-immunized bond portfolio by matching asset duration to liability horizon using linear algebra in Python NumPy.
#numpy#pandas
Problem Statement
Pension funds and insurance companies immunize their portfolios against interest rate shocks by matching asset duration exactly to liability duration.
Formula / Core Logic
Solves w1*D1 + w2*D2 = Liability_Duration subject to weight sum = 1.
Installation
pip install numpy pandas
bond-immunization-duration-matching-python.py
import numpy as np
\.def immunize_bond_portfolio(durations, target_liability_duration):
d = np.array(durations)
# Simple 2-bond portfolio weights solving duration match
w1 = (target_liability_duration - d[1]) / (d[0] - d[1])
w2 = 1.0 - w1
return {'Bond_1_Weight': round(w1, 4), 'Bond_2_Weight': round(w2, 4)}
Get the Complete 100+ Python Quant & Automation Bundle
Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).
Buy Full Bundle on Gumroad ($19) →Frequently Asked Questions
Q: Why does immunization protect against interest rate risk?
Price risk and reinvestment risk offset each other perfectly when portfolio duration equals the investment horizon.