Back to code sutra Archive
Vectorizing Cumulative Sums for Actuarial Projections
Wednesday, January 21, 2026The Challenge
Actuarial models often require calculating cumulative sums of cash flows (e.g., premiums, claims) or other period-by-period aggregates over many policy durations, where explicit Python loops can lead to slow execution.
The Sutra Snippet
python
import numpy as np
# Assume 'premiums_per_period' and 'claims_per_period' are NumPy arrays
# representing cash flows over many periods (e.g., 1000 periods).
periods = 1000
premiums_per_period = np.full(periods, 100.0) # Example: constant premiums
claims_per_period = np.random.rand(periods) * 50 # Example: random small claims
# Introduce some larger, infrequent claims for realism
large_claim_indices = np.random.randint(0, periods, int(periods * 0.01))
claims_per_period[large_claim_indices] = np.random.rand(len(large_claim_indices)) * 1000
# The Sutra: Use numpy.cumsum for efficient cumulative aggregation
cumulative_premiums = np.cumsum(premiums_per_period)
cumulative_claims = np.cumsum(claims_per_period)
# This also applies to net cash flows or other derived series:
net_cash_flow_per_period = premiums_per_period - claims_per_period
cumulative_net_cash_flow = np.cumsum(net_cash_flow_per_period)
print(f"Cumulative Premiums (first 5): {cumulative_premiums[:5]}")
print(f"Cumulative Claims (first 5): {cumulative_claims[:5]}")
print(f"Cumulative Net Cash Flow (first 5): {cumulative_net_cash_flow[:5]}")
PRO
Efficiency Gain
NumPy's `cumsum` function provides a highly optimized, C-implemented vectorized operation that dramatically reduces computation time by avoiding Python's interpreter overhead associated with explicit `for` loops, especially for large datasets common in actuarial projections.