Counterparty Risk and CVA › CVA Calculation Implementation
Quantitative FinanceAll chapters Counterparty Risk and CVA › CVA Calculation ImplementationOne section · Sections in this chapter6 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
With exposure profiles computed, we now calculate CVA by combining expected exposures with default probabilities derived from CDS spreads. This implementation brings together the theoretical framework we developed earlier with practical computation.
import numpy as np
def calculate_default_probabilities(cds_spread, recovery_rate, times):
"""
Calculate marginal default probabilities from CDS spread.
Uses constant hazard rate assumption.
"""
lgd = 1 - recovery_rate
hazard_rate = cds_spread / lgd
# Survival probabilities
survival_probs = np.exp(-hazard_rate * times)
# Marginal default probabilities (probability of default in each period)
marginal_pds = np.zeros(len(times))
marginal_pds[0] = 1 - survival_probs[0]
marginal_pds[1:] = survival_probs[:-1] - survival_probs[1:]
return marginal_pds, survival_probs
# Counterparty credit parameters
cds_spread = 0.015 # 150 basis points
recovery_rate = 0.40 # 40% recovery
lgd = 1 - recovery_rate
marginal_pds, survival_probs = calculate_default_probabilities(
cds_spread, recovery_rate, times
)The hazard rate extracted from the CDS spread reflects the market's assessment of the counterparty's creditworthiness. A 150 basis point CDS spread for a counterparty with 40% recovery implies a hazard rate of approximately 2.5% per year. This hazard rate means that, conditional on surviving to any given moment, there is roughly a 2.5% annual probability of defaulting in the next year. The exponential survival function then translates this instantaneous rate into cumulative survival and default probabilities at each future date.

import numpy as np
def calculate_discount_factors(r0, times):
"""Calculate discount factors using initial rate."""
return np.exp(-r0 * times)
def calculate_cva(expected_exposure, marginal_pds, discount_factors, lgd):
"""
Calculate CVA as discounted expected loss.
CVA = LGD * sum(DF * EE * marginal_PD)
"""
# Align dimensions - use midpoint exposures for each period
n_periods = len(marginal_pds) - 1
cva = 0.0
for i in range(1, len(marginal_pds)):
# Average exposure in period
avg_ee = 0.5 * (expected_exposure[i - 1] + expected_exposure[i])
# Discount to present
df = discount_factors[i]
# Add contribution
cva += lgd * df * avg_ee * marginal_pds[i]
return cva
discount_factors = calculate_discount_factors(r0, times)
cva = calculate_cva(expected_exposure, marginal_pds, discount_factors, lgd)CVA Calculation Results ======================================== Notional: $1,000,000 CDS Spread: 150 bps Recovery Rate: 40% Loss Given Default: 60% Peak Expected Exposure: $48,277 Peak PFE (99%): $248,235 CVA: $3,547 CVA as % of Notional: 0.3547%
The CVA of approximately 0.25% of notional represents the upfront price of counterparty credit risk for this swap. This amount should be charged to the counterparty or reserved against potential losses. For our portfolio of derivatives, CVA can become a substantial adjustment to firm-wide valuations. A large dealer with hundreds of billions of dollars in derivative notional can have aggregate CVA in the billions, making it a material component of the firm's balance sheet.
