Fixed Income API Reference¶
financelib.quant.fixed_income.present_value(cash_flows: List[float], discount_rate: float, periods: Optional[List[float]] = None) -> float
¶
Calculate present value of a stream of cash flows.
PV = sum(CF_t / (1 + r)^t)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cash_flows
|
List[float]
|
List of future cash flows. |
required |
discount_rate
|
float
|
Discount rate per period. |
required |
periods
|
Optional[List[float]]
|
Custom period timings. If None, uses 1, 2, 3, ... |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Present value. |
Example
present_value([100, 100, 1100], 0.05) 1136.16...
Source code in financelib/quant/fixed_income.py
financelib.quant.fixed_income.bond_price(face_value: float, coupon_rate: float, ytm: float, periods: int, frequency: int = 2) -> float
¶
Calculate the clean price of a fixed-rate bond.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
face_value
|
float
|
Par/face value of the bond. |
required |
coupon_rate
|
float
|
Annual coupon rate (e.g., 0.05 for 5%). |
required |
ytm
|
float
|
Yield to maturity (annualized). |
required |
periods
|
int
|
Number of years to maturity. |
required |
frequency
|
int
|
Coupon payments per year (1=annual, 2=semi-annual). |
2
|
Returns:
| Type | Description |
|---|---|
float
|
Bond price. |
Source code in financelib/quant/fixed_income.py
financelib.quant.fixed_income.yield_to_maturity(price: float, face_value: float, coupon_rate: float, periods: int, frequency: int = 2, tol: float = 1e-08, max_iter: int = 200) -> float
¶
Calculate yield to maturity using Newton-Raphson method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
price
|
float
|
Current market price. |
required |
face_value
|
float
|
Par/face value. |
required |
coupon_rate
|
float
|
Annual coupon rate. |
required |
periods
|
int
|
Years to maturity. |
required |
frequency
|
int
|
Coupon payments per year. |
2
|
tol
|
float
|
Convergence tolerance. |
1e-08
|
max_iter
|
int
|
Maximum iterations. |
200
|
Returns:
| Type | Description |
|---|---|
float
|
Annualized yield to maturity. |
Source code in financelib/quant/fixed_income.py
financelib.quant.fixed_income.duration(face_value: float, coupon_rate: float, ytm: float, periods: int, frequency: int = 2, modified: bool = True) -> float
¶
Calculate Macaulay or modified duration of a bond.
Macaulay duration measures the weighted average time to receive cash flows. Modified duration measures price sensitivity to yield changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
face_value
|
float
|
Par/face value. |
required |
coupon_rate
|
float
|
Annual coupon rate. |
required |
ytm
|
float
|
Yield to maturity. |
required |
periods
|
int
|
Years to maturity. |
required |
frequency
|
int
|
Coupon payments per year. |
2
|
modified
|
bool
|
If True, return modified duration. |
True
|
Returns:
| Type | Description |
|---|---|
float
|
Duration in years. |
Source code in financelib/quant/fixed_income.py
financelib.quant.fixed_income.convexity(face_value: float, coupon_rate: float, ytm: float, periods: int, frequency: int = 2) -> float
¶
Calculate the convexity of a bond.
Convexity measures the curvature of the price-yield relationship, providing a second-order correction to duration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
face_value
|
float
|
Par/face value. |
required |
coupon_rate
|
float
|
Annual coupon rate. |
required |
ytm
|
float
|
Yield to maturity. |
required |
periods
|
int
|
Years to maturity. |
required |
frequency
|
int
|
Coupon payments per year. |
2
|
Returns:
| Type | Description |
|---|---|
float
|
Convexity value. |