Contenido del Curso
Introduction to Portfolio Management with Python
Introduction to Portfolio Management with Python
Portfolio Returns
In the coming two chapters, we'll examine two essential concepts for comprehensive portfolio analysis: return and risk, starting with return.
What is Return?
It represents the profitability of an investment and can be either positive or negative, depending on whether the investment has increased or decreased in value.
Common Types of Return
Next, we will discuss various methods for calculating returns.
Absolute Return
The simplest return to calculate is absolute return, which is simply the difference between an asset's current price and its previous price. This can be computed using the following formula:
Below is a code example for computing absolute return, using stock shares from Apple, Meta, and Amazon as assets to compute returns for each:
# Importing pandas library import pandas as pd # Creating DataFrame `stocks` with values of stock prices of Apple, Meta and Amazon # for the period from `2024-09-06` to `2024-09-10` stocks = pd.DataFrame(index = ['2024-09-06', '2024-09-09', '2024-09-10'], columns = ['Apple', 'Meta', 'Amazon'], data = [[220.14, 499.60, 170.56], [220.88, 505.90, 176.25], [219.74, 504.56, 179.40]]) print('Stock data:') print(stocks) print('Absolute returns:') print(stocks.diff())
We use the diff
method here because it computes the difference between each current and previous value in a sequence, which is precisely what we need for calculating absolute return.
As we can see, for each company, the absolute return value on 2024-09-09 is the difference between the stock price on 2024-09-09 and the stock price on 2024-09-06.
However, this type of return has a significant disadvantage: it doesn't account for the scale of the asset's price.
To understand, why scale matters, let's consider an example.
Relative Return
A significantly better measure of profit is relative return, which computes price changes relatively to the previous value of an asset, that is computed using the next expression with just the same notation as in the case of absolute return:
Here is a code example for computing relative return, under the same conditions as those used for absolute return:
# Importing pandas library import pandas as pd # Creating DataFrame `stocks` with values of stock prices of Apple, Meta and Amazon # for the period from `2024-09-06` to `2024-09-10` stocks = pd.DataFrame(index = ['2024-09-06', '2024-09-09', '2024-09-10'], columns = ['Apple', 'Meta', 'Amazon'], data = [[220.14, 499.60, 170.56], [220.88, 505.90, 176.25], [219.74, 504.56, 179.40]]) print('Stock data:') print(stocks) print('Returns:') print(stocks.pct_change())
In this case, the pct_change
method is used to calculate relative return, as it performs the required computations based on the specified formula, similar to how the diff
method is used for absolute return.
Here we can see, for instance, that values of returns on 2024-09-09 computed directly using the above formula, where the current values is the stock prices on 2024-09-09 and the previous values is stock prices on 2024-09-06.
Return of Portfolio
Returning to portfolio analysis, it's important to note that the portfolio return is calculated by considering the return of each individual asset within the portfolio and their corresponding weights:
Here is a code example for calculating the portfolio's return, using weights based on market capitalization, as calculated in the previous chapter, and stock data from the previous examples:
# Importing pandas and numpy libraries import pandas as pd import numpy as np # Creating DataFrame `stocks` with values of stock prices of Apple, Meta and Amazon # for the period from `2024-09-06` to `2024-09-10` stocks = pd.DataFrame(index = ['2024-09-06', '2024-09-09', '2024-09-10'], columns = ['Apple', 'Meta', 'Amazon'], data = [[220.14, 499.60, 170.56], [220.88, 505.90, 176.25], [219.74, 504.56, 179.40]]) # Defining list of portfolios weights weights = [0.5185185185185185, 0.19753086419753088, 0.28395061728395066] print('Stock data:') print(stocks) print('Returns:') print(stocks.pct_change()) print('Portfolio returns:') print(np.sum(stocks.pct_change() * weights, axis = 1))
As before, we use stocks.pct_change
to calculate returns, then multiply each company's return by its corresponding weight. Finally, for each day, we sum the weighted returns to obtain the portfolio’s return.
Cumulative Return
The last important concept to cover in this chapter is the cumulative return.
It represents the total performance over time, indicating how much the price has changed since the start.
At a given moment of time, cumulative return can be calculated using the following expression:
Here is a code example for calculating cumulative returns, following the context of our previous examples:
# Importing pandas and numpy libraries import pandas as pd import numpy as np # Creating DataFrame `stocks` with values of stock prices of Apple, Meta and Amazon # for the period from `2024-09-06` to `2024-09-10` stocks = pd.DataFrame(index = ['2024-09-06', '2024-09-09', '2024-09-10'], columns = ['Apple', 'Meta', 'Amazon'], data = [[220.14, 499.60, 170.56], [220.88, 505.90, 176.25], [219.74, 504.56, 179.40]]) # Defining list of portfolis weights weights = [0.5185185185185185, 0.19753086419753088, 0.28395061728395066] print('Stock data:') print(stocks) print('Returns:') print(stocks.pct_change()) print('Cumulative returns:') print((1 + stocks.pct_change()).cumprod() - 1)
Here, as before, we will use the stocks.pct_change
construction to calculate the returns of stock shares.
However, this time we will add 1 to each return element-wise, then use the cumprod
method to multiply the returns accordingly.
Finally, we will subtract 1 from the resulting values.
Summary
In conclusion, we have explored the definition of financial return, various types of returns, and how to compute them using Python.
In the upcoming chapters, we will take a closer look at the applications of returns for building effective portfolios and their estimation.
¡Gracias por tus comentarios!