Introduction
The weighted Average Cost of Capital (WACC) is widely used in many different models. For instance, it is used in Economic Value Added and discounted Cash Flow models.
In this article, I am going to explain how easily and quickly WACC can be calculated using Python. I am using Microsoft as an example.
Below are the steps to calculate WACC:
- Go Over the Formula
- Import Packages
- Calculate WACC (This is section is divided into 3 parts)
a. Equity Part
b. Debt Part
c. Tax Shield
d. WACC Calculation
- Go Over the Formula

Some extra definitions:
Cost of Equity: The required return by the shareholders given the risks they assume for investing in this company.
Cost of Debt: The interest rate that a company pays on its debts less than the tax benefit being tax deductible.
2. Import Packages
# Import Libraries and packages
import yfinance as yf
import pandas as pd
import numpy as np3. Calculate WACC
a. Equity Part
Let's start by calculating the equity part:
First, we calculate the risk free rate (we are going to use the treasury bill rate):
treasury_yield10 = yf.Ticker("^TNX")
risk_free_rate = round(treasury_yield10.info['regularMarketPrice']/100,2) Now, we calculate the market expected return (we will assume the average return of the S&P500 which is 10%):
sp500_teturn = 0.10We calculate the beta of Microsoft:
stock = yf.Ticker("MSFT")
beta = stock.info["beta"]With all the information above, we can calculate the cost of equity:
cost_of_equity = round(risk_free_rate + beta*(sp500_teturn - risk_free_rate),2)
cost_of_equity
Now, we find the weight of equity:
To do this part, we will need to import the balance sheet.
stock_bal = stock.balance_sheet
stock_balSince Assets = Liabilities + Equity, we divide the equity by the assets.
weight_of_equity = stock_bal.loc["Total Stockholder Equity"][0]/ stock_bal.loc["Total Assets"][0]b. Debt Part
Before we start, we need the financial statement:
# get stock financial statement
stock_Fin = stock.financials
stock_Fin We start with the cost of debt:
# Cost of Debt
cost_of_debt = round(stock_Fin.loc["Interest Expense"][0] *-1 / stock_bal.loc["Total Liab"][0],2)
cost_of_debt
Then, we do the weight of debt:
weight_of_debt = round(stock_bal.loc["Total Liab"][0]/ stock_bal.loc["Total Assets"][0],2)
weight_of_debt
c. Tax Shield
# Tax Rate
tax_rate = round(stock_Fin.loc["Income Tax Expense"][0] / stock_Fin.loc["Income Before Tax"][0],2)
tax_rate
d. WACC Calculation
WACC = round((weight_of_equity * cost_of_equity) + ((weight_of_debt * cost_of_debt ) * (1-tax_rate)),2)
WACC
Here you have it.
Microsoft WACC is 0.05 or 5%. This means Microsoft must pay an average of $0.05 to raise an additional $1. WACC is used in other models such as discounted cash flow model.
Finally,
You can find the full code HERE.
For further reading, you can check Investopedia and Corporate Finance Institute.
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.