Skip to content

Add vectorized FixedRateBonds collection for bulk bond construction - #6

Merged
fumitoh merged 2 commits into
mainfrom
claude/add-fixed-rate-bonds-class-ZIXoC
Feb 28, 2026
Merged

fumitoh merged 2 commits into
mainfrom
claude/add-fixed-rate-bonds-class-ZIXoC

Conversation

@fumitoh

@fumitoh fumitoh commented Feb 28, 2026

Copy link
Copy Markdown
Member

Summary

This PR introduces a new FixedRateBonds class that enables efficient bulk construction and management of fixed-rate bonds using parallel arrays. This complements the existing singular FixedRateBond class and allows users to work with collections of bonds while maintaining individual bond reconstruction on demand.

Key Changes

  • New FixedRateBonds Cython class (lifelib_pyql/portfolio/instruments/bonds.pyx):

    • Stores bond parameters as parallel numpy arrays (settlement days, face amounts, coupons, redemptions, issue dates)
    • Supports both 1D and 2D coupon arrays (with NaN-padding for variable coupon counts)
    • Implements __getitem__ for both integer indexing (returns singular FixedRateBond) and slicing (returns new FixedRateBonds)
    • Handles scalar and array redemptions with automatic broadcasting
    • Converts numpy datetime64[D] values to pyql Date objects on demand
    • Validates array dimensions and lengths at construction time
  • Type definitions (lifelib_pyql/portfolio/instruments/bonds.pxd):

    • Cython extension type declarations for efficient array access
  • API export (lifelib_pyql/portfolio/api.py):

    • Exports FixedRateBonds from the portfolio module for public use
  • Comprehensive test suite (test/test_portfolio_bonds.py):

    • 20+ test cases covering construction, indexing, slicing, property access
    • Validation of consistency between indexed bonds and individually constructed FixedRateBond instances
    • Edge cases: negative indexing, out-of-bounds access, type validation
    • Support for optional issue dates and variable coupon structures

Notable Implementation Details

  • Uses numpy datetime64[D] to pyql Date conversion with NaT handling
  • Lazy bond reconstruction: individual bonds are created only when accessed via indexing
  • Slice operations return new FixedRateBonds instances with properly sliced underlying arrays
  • Supports optional issue dates (None is valid) for bonds without explicit issue date information
  • Validates all input array lengths match the number of schedules at construction time

https://claude.ai/code/session_01JRwjJ46R8sg7EGof6X3ZWH

Add lifelib_pyql.portfolio.instruments.bonds.FixedRateBonds as the
plural/vectorized form of lifelib_pyql.instruments.bonds.FixedRateBond,
following the same pattern as Schedules is to Schedule.

The class stores parallel arrays of bond parameters and reconstructs
individual FixedRateBond objects on demand via indexing. Supports int
indexing (returns singular FixedRateBond) and slice indexing (returns
new FixedRateBonds subset).

https://claude.ai/code/session_01JRwjJ46R8sg7EGof6X3ZWH
Tests cover construction, properties, int/slice/negative indexing,
consistency with singular FixedRateBond, scalar and array redemptions,
2D coupons, optional issue_dates, input validation errors, and
slice-then-index round-tripping.

https://claude.ai/code/session_01JRwjJ46R8sg7EGof6X3ZWH
@fumitoh
fumitoh marked this pull request as ready for review February 28, 2026 14:44
@fumitoh
fumitoh merged commit 2584fc5 into main Feb 28, 2026
2 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants