Profitability ratios are financial metrics offering insights on how good a company is able to generate earnings from revenues, assets and equity. In this post, we will perform a profitability margin analysis with Python by comparing profitability ratios across peer companies. Below are the 5 different related profitability ratios that we will calculate and analyse:
How to Calculate Profitability Ratios
Before starting with the Python code to identify peer companies and analyse profitability margins, we will introduce each of the ratios in this section.
Profitability and Margin Ratios
Margin ratios are very useful to understand how efficient a company is able to transform revenues into operating and net income. We will cover three main metrics in this post, gross profit margin, operating margined net profit margin:
Gross profit margin is useful to know how much cost of goods sold are needed to generate one dollar of sales.The higher the gross profit margin, the more company flows to the operating income and net income from each dollar of revenue.
Gross Profit Margin = Gross Profit / Revenue
Operating profit margin measures how good a company is able to generate profits through its main operating activities:
Operating Profit Margin = Operating Profit / Revenue
Net profit margin indicates how much a company makes in profits for each dollar of sales.
Net Profit Margin = Net Income / Revenue
When performing a financial analysis, we need to be aware that we can extract very insightful conclusions when comparing peers. For example, by comparing net profit margin across companies we can identify which companies are operating more efficiently in transforming sales into profits.
Once we find the best performers in terms of net margin within a group of peers, we could further analyse gross profit margin and operating margin to understand better why the top performer companies have an edge. A higher operating margin indicates that a company has either very competitive gross profit margins or very attractive operating margins. I.e. able to generate a unit of sales spending less on Cost of Goods Sold (higher gross profit margin) or lower Marketing & Sales, Research & Development and G&A (General and Administrative) expenses required to generate a dollar of sales.
Alternatively to compare peer companies, we could perform a time series analysis instead in order to analyse a single company performance across time. This kind of ratio analysis is particularly useful to see company trends. I cover time series analysis of financial ratios in my previous post onanalysing Balance Sheet Financial Ratios with Python.
ROE or ROA are profitability ratios that measure how efficient a company is able to generate a profit based on the equity employed (ROE) or the amount of asset employed (ROA).
We already saw in my previous post how to analyse a company’s return on equity with Python. Therefore, we do not need to go into much details on how to interpret the Return on Equity (ROE) ratio. This financial ratio is simply letting us know how much net income a company generates based on the company shareholders equity.
Similarly, Return on Assets (ROA) indicates how much assets a company employs to generate net income.
ROE = Net Income / Average Total Equity
ROA = Net Income / Average Total Assets
Great, we have covered enough theory. Lets see in the next section how can we first find peer companies and then retrieve profitability ratios using Python.
This is my favourite part of the post. We will be using financialmodelingprep in order to retrieve and process the required financial data. The code presented in this post is very straightforward. Below are the high level steps that the script performs:
1. Find all company peers. To do this step, we will rely on Financialmodelingprep. The API offers and end point to retrieve peers for a selected company. Therefore, we simply need to pass within the url the ticker of the company. Then, the API will return a Python list containing all peers based on the sector and market cap.
2. Loop through each of the stocks to retrieve profitability ratios from the financial ratios API end point.
3. Extract and add each of the financial ratios into a Python dictionary. This will be useful in order to be able to present the financial ratios by company.
4. Convert the Python dictionary into a Pandas DataFrame
Note that we need an API key from financialmodelingprep in order for the code to work. You can get a free API key with 250 requests a day.
import requests
import pandas as pd
stock = 'AAPL'
api_key = 'your api key'
url = f'https://financialmodelingprep.com/api/v4/stock_peers?symbol={stock}&apikey={api_key}'
#1 GET LIST OF PEERS
peers = requests.get(url).json()
peers = peers[0]['peersList']
profitability_ratios = {}
#2 Retrieve Profitability Ratios for each of the peers
for stock in peers:
#3 Add to Python Dictionary
profitability_ratios[stock] = {}
fr = f'https://financialmodelingprep.com/api/v3/ratios-ttm/{stock}?apikey={api_key}'
financial_ratios_ttm = requests.get(fr).json()
profitability_ratios[stock]['Return on Assets'] = financial_ratios_ttm[0]['returnOnAssetsTTM']
profitability_ratios[stock]['Return on Equity'] = financial_ratios_ttm[0]['returnOnEquityTTM']
profitability_ratios[stock]['Gross Profit Margin'] = financial_ratios_ttm[0]['grossProfitMarginTTM']
profitability_ratios[stock]['Opearting Profit Margin'] = financial_ratios_ttm[0]['operatingProfitMarginTTM']
profitability_ratios[stock]['Net Profit Margin'] = financial_ratios_ttm[0]['netProfitMarginTTM']
#4 Convert into Pandas DataFrame
profitability_ratios = pd.DataFrame(profitability_ratios)
profitability_ratios
After running the code, below Pandas DataFrame will show the outcome of the profitability analysis with Python. We get a financial table containing peers from Apple because that is the company that we have passed in our code. Feel free to change the ticker in order to perform a similar profitability analysis for other companies.
Wrapping Up
Is it not great? Using less than 20 lines of Python code, we are able to compare profitability metrics across different peers companies.
We see that Adobe (ADBE) has a very nice net profit margin compared to other companies. We could observe that Adobe higher net profit margin is driven mainly by the high gross profit margin compared to the peers of around 87%. That is probably due to the fact that Adobe has digital products lacking physical substance and are relatively cheap to produce.
However, the operating profit margin is in line with the rest of the peers. Probably due to the amount spend by Adobe in research and development of new products. We could build a common size income statement to look further into the operational expenses of the company.
As next steps, we could improve the Python script to add new functions. For example, we could compute the mean for each of the metrics in order to compare each of the companies to the average of the group.
Hope that you have find the post on how to perform a Profitability Analysis with Python useful. Please note that any of the information included in this post is to be used as financial advise.
The information presented here may be inaccurate and therefore should not be used to make investment decisions.
Previously published at https://codingandfun.com/profitability-analysis-with-python/