Python by Examples: Financial Calculations for Cost of Capital
In today’s article, we explore how Python can help us perform fundamental financial calculations such as WACC, the cost of equity, and the…
Python by Examples: Financial Calculations for Cost of Capital
In today’s article, we explore how Python can help us perform fundamental financial calculations such as WACC, the cost of equity, and the cost of debt. We’ll translate financial formulas into running code examples to demonstrate how to programmatically derive these key metrics.

We’ll cover:
- Calculating WACC with Python, using both direct function and class-based approaches.
- Implementing the cost of equity using the modified CAPM and the build-up model.
- Determining the cost of debt, with after-tax considerations and detailed breakdowns.
Each section includes complete Python examples to help you implement these calculations in your own projects.
WACC Calculation
Overview: This section demonstrates how to compute the Weighted Average Cost of Capital (WACC). The first example uses a simple function, while the second example packages the process into a reusable class structure, clarifying how debt, tax, and equity combine to form the WACC.
Python Sample 1:
#!/usr/bin/env python3
# Example 1: WACC calculation using a simple function
def calculate_wacc(cost_debt_pre_tax, tax_rate, debt_weight, cost_equity, equity_weight):
cost_debt_after_tax = cost_debt_pre_tax * (1 - tax_rate)
wacc = (cost_debt_after_tax * debt_weight) + (cost_equity * equity_weight)
return wacc
def main():
cost_debt_pre_tax = 0.06 # 6.00%
tax_rate = 0.22 # 22.0%
debt_weight = 0.34 # 34%
cost_equity = 0.18 # 18.00%
equity_weight = 0.66 # 66%
result = calculate_wacc(cost_debt_pre_tax, tax_rate, debt_weight, cost_equity, equity_weight)
print("Calculated WACC: {:.2%}".format(result))
if __name__ == '__main__':
main()
Python Sample 2:
#!/usr/bin/env python3
# Example 2: WACC calculation using a class-based approach
class CapitalStructure:
def __init__(self, cost_debt_pre_tax, tax_rate, debt_weight, cost_equity, equity_weight):
self.cost_debt_pre_tax = cost_debt_pre_tax
self.tax_rate = tax_rate
self.debt_weight = debt_weight
self.cost_equity = cost_equity
self.equity_weight = equity_weight
def after_tax_debt(self):
return self.cost_debt_pre_tax * (1 - self.tax_rate)
def compute_wacc(self):
return (self.after_tax_debt() * self.debt_weight) + (self.cost_equity * self.equity_weight)
def run_wacc_calculation():
capital = CapitalStructure(cost_debt_pre_tax=0.06,
tax_rate=0.22,
debt_weight=0.34,
cost_equity=0.18,
equity_weight=0.66)
wacc = capital.compute_wacc()
print("WACC (class approach): {:.2%}".format(wacc))
if __name__ == '__main__':
run_wacc_calculation()
Cost of Equity Calculation Using CAPM & Build-Up Models
Overview: This section provides Python implementations for calculating the cost of equity using the modified CAPM and the build-up model.
Python Sample 1:
#!/usr/bin/env python3
# Calculate the cost of equity using the Modified CAPM model
def modified_capm(risk_free, beta, risk_premium_market, risk_premium_small, specific_risk_adjustment):
expected_return = risk_free + (beta * risk_premium_market) + risk_premium_small + specific_risk_adjustment
return expected_return
def main():
risk_free = 0.0385 # 3.85%
beta = 1.2 # Example beta value
risk_premium_market = 0.0746 # 7.46%
risk_premium_small = 0.0480 # 4.80%
specific_risk_adjustment = 0.0 # No adjustment
cost_equity = modified_capm(risk_free, beta, risk_premium_market, risk_premium_small, specific_risk_adjustment)
print("Cost of Equity (Modified CAPM): {:.2%}".format(cost_equity))
if __name__ == '__main__':
main()
Python Sample 2:
#!/usr/bin/env python3
# Calculate the cost of equity using the Build-Up Model
def build_up_model(risk_free, market_premium, size_premium, company_specific_premium):
expected_return = risk_free + market_premium + size_premium + company_specific_premium
return expected_return
def main():
risk_free = 0.0385 # 3.85%
market_premium = 0.0746 # 7.46%
size_premium = 0.0480 # 4.80%
company_specific_premium = 0.0 # No additional premium
cost_equity = build_up_model(risk_free, market_premium, size_premium, company_specific_premium)
print("Cost of Equity (Build-Up Model): {:.2%}".format(cost_equity))
if __name__ == '__main__':
main()
Cost of Debt Calculation Overview
Overview: In this section, we calculate the cost of debt before and after tax adjustments. The first example illustrates the basic calculation, while the second offers a structured approach for breaking down the debt cost components.
Python Sample 1:
#!/usr/bin/env python3
# Compute the after-tax cost of debt
def calculate_after_tax_debt(cost_debt_pre_tax, tax_rate):
return cost_debt_pre_tax * (1 - tax_rate)
def main():
cost_debt_pre_tax = 0.06 # 6.00%
tax_rate = 0.22 # 22%
cost_debt_after_tax = calculate_after_tax_debt(cost_debt_pre_tax, tax_rate)
print("After-Tax Cost of Debt: {:.2%}".format(cost_debt_after_tax))
if __name__ == '__main__':
main()
Python Sample 2:
#!/usr/bin/env python3
# Detailed breakdown of cost of debt calculation
class DebtCostCalculator:
def __init__(self, cost_debt_pre_tax, tax_rate):
self.cost_debt_pre_tax = cost_debt_pre_tax
self.tax_rate = tax_rate
def compute_after_tax_cost(self):
return self.cost_debt_pre_tax * (1 - self.tax_rate)
def display_breakdown(self):
after_tax = self.compute_after_tax_cost()
print("Pre-tax Cost of Debt: {:.2%}".format(self.cost_debt_pre_tax))
print("Tax Rate: {:.2%}".format(self.tax_rate))
print("After-Tax Cost of Debt: {:.2%}".format(after_tax))
def main():
calculator = DebtCostCalculator(cost_debt_pre_tax=0.06, tax_rate=0.22)
calculator.display_breakdown()
if __name__ == '__main__':
main()
Conclusion:
In summary, our examples demonstrate how Python can be used to effectively calculate key components of a company’s cost of capital. By implementing functions and classes to compute WACC, as well as the cost of equity and debt, you can build robust financial analyses. These code samples serve as a practical guide to help you integrate these calculations into real-world financial models.
메타데이터
- post_id
- 538db87f738f
- slug
- python-by-examples-financial-calculations-for-cost-of-capital-538db87f738f
- url
- https://medium.com/@mb20261/python-by-examples-financial-calculations-for-cost-of-capital-538db87f738f
- canonical_url
- https://medium.com/@mb20261/python-by-examples-financial-calculations-for-cost-of-capital-538db87f738f
- author_url
- https://medium.com/@mb20261
- status
- ok
- fetched_at
- 2026-07-17 17:40:40