Financial Calculations & Valuation
Beginner
Calculate WACC (Weighted Average Cost of Capital) & CAPM in Python
Python script to calculate Weighted Average Cost of Capital (WACC) using CAPM Cost of Equity, After-Tax Cost of Debt, and market capital structure weights.
#numpy
Problem Statement
Accurate DCF valuation requires a precise discount rate that accounts for equity beta and tax shields on corporate debt. This script computes CAPM Cost of Equity and blended WACC.
Formula / Core Logic
Ke = Rf + Beta * (Rm - Rf)
Kd_after_tax = Kd_pre_tax * (1 - Tax_Rate)
WACC = (E/V * Ke) + (D/V * Kd_after_tax)
Installation
pip install numpy
calculate-wacc-weighted-average-cost-of-capital-python.py
def calculate_wacc(market_cap_equity, total_debt, rf_rate, beta, market_return, pre_tax_debt_cost, tax_rate):
total_value = market_cap_equity + total_debt
weight_e = market_cap_equity / total_value
weight_d = total_debt / total_value
cost_of_equity = rf_rate + beta * (market_return - rf_rate)
after_tax_debt = pre_tax_debt_cost * (1 - tax_rate)
wacc = (weight_e * cost_of_equity) + (weight_d * after_tax_debt)
return {'Cost_of_Equity_Pct': round(cost_of_equity * 100, 2), 'After_Tax_Debt_Pct': round(after_tax_debt * 100, 2), 'WACC_Pct': round(wacc * 100, 2)}
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: Should I use Book Value or Market Value of equity for WACC?
Always use Market Capitalization for equity weight and carrying value (or market value of bonds) for debt.