# Creating the equally-weighted Portfolio

A free video tutorial from Alexander Hagmann
Data Scientist | Finance Professional | Entrepreneur
Rating: 4.6 out of 5Instructor rating
19 courses
121,896 students

### Manage Finance Data with Python & Pandas: Unique Masterclass

Analyze Stocks with Pandas, Numpy, Seaborn & ChatGPT. Create, analyze & optimize Index & Portfolios (CAPM, Alpha, Beta)

29:21:07 of on-demand video • Updated April 2024

Step into the Financial Analyst role and give advice on a client´s financial Portfolio (Final Project)
Import large Financial Datasets / historical Prices from Web Sources and analyze, aggregate and visualize them
Calculate Return, Risk, Correlation and Rolling Statistics for Stocks, Indexes and Portfolios
Create, analyze and optimize financial Portfolios and understand the use of the Sharpe Ratio
Intuitively understand Modern Portfolio Theory (CAPM, Beta, Alpha, CML, SML, Risk Diversification) with Real Data examples
Create Interactive Price Charts with Technical Indicators (Volume, OHLC, Candlestick, SMA etc.)
Create Financial Indexes (price-, equal- and value- weighted) and understand the difference between Price Return and Total Return
Easily switch between daily, weekly, monthly and annual returns and understand the benefits of log returns
Start from Zero and learn all the Basics of the powerful Pandas Library
English [Auto]
In this video, we are going to create the equal weighted portfolio consisting of our six portfolio stocks and equal weighted means that at each time stamp the weight of each single stock in the portfolio as one divided by six. So let's first of all, import Penas and we also import no high end matplotlib and also Seabourne. And we define that float up checks have two decimals. So let's do this. And then we import our portfolio stocks from the portfolio stocks we file and we create a daytime index and we save the data frame and the value of stocks. So let's have a look. So these are the adjusted cloth prices over time and actually calculating the return of an equal weighted portfolio. It's pretty simple. So we simply have to calculate the mean return of our six stocks on each timestamp. And therefore, first of all, we create the returns data frame with the percentage change method and we save the returns data frame and the variable rate. So this is the returns that are from the daily returns and as I said, to get the daily returns for our equal weighted portfolio, we simply have to calculate the average the median return of the six stocks on each timestamp. So to say we have to calculate the Rove, I mean, and we can do this with the mean method and pass the one to the access parameter to get the mean values Perreault. So these are the mean values, are the mean returns, Parool per timestamp, and with this we are finished here that the daily returns of the equal weighted portfolio. But as the next step, we also want to create portfolios that are not equal weighted and therefore we have to derive a more general way to get to the weighted average return on each time stamp. And let's have a look. So first of all, we need to define the amount of assets or the amount of stocks that should be in our portfolio. So here in this case, they are working with six stocks and we actually can determine the length of our columns index. So no surprise we have here six. And the next step, we create a list with the weights of our six stocks. So with the equal weighted portfolio, we have six times the weight of one divided by six. So we are creating the list weights and this is a list comprehension so far and range six or six times. We create one divided by six. So let's have a look. So you have six times the weight one divided by six, and now we want to calculate the weighted average return for all the timestamps and therefore on the first step, we have to multiply the return of each stock. So here these are the daily returns and we have to multiply them by their respective weights here. And, uh, we have to do this for each time stamp. And finally, we have to sum up then the weighted daily returns to get the weighted average return for the six stocks. And we can do this year in two steps. Well, first of all, we our returns data frame and we multiply the returns that are frame with the weights list and we pass columns to the excess parameter because we want to apply here the respective weights on our columns. So he had the first weight should always be applied on the Amazon stock, for example. So let's do this here. So these are the awaited daily returns, and to get the weighted average, we have to take the sum so we use some method and pass one to the excess Piramida to some the data frame up ROVA SPRO. And again, here we have the daily returns for our equal weighted portfolio. And as you can see here, we change here two methods. So the modern method and the some method and we can actually combine these two steps into one step by using the DOT product on a matrix multiplication. And we can do this with the DOT method. So we take our returns data frame and we apply a matrix multiplication with the DOT method and we pass our weights list. So this gives us actually the same result. So here we have the daily returns of the equal weighted portfolio. And finally, we create a new column, equal weighted portfolio for our returns data frame by using exactly the code. So let's do this and let's have a look. So here we have the six constituents of our portfolio and the equal weighted portfolio, and for all of them, we have had the daily returns on each timestamp. And now as the next step, we want to aggregate here the daily returns and we want to calculate the annualized mean return and the annualized standard deviation of returns. And therefore we create here the summary data frame. So first of all, we calculate the mean return of our daily returns and the standard deviation of daily returns, and we use the other method. So let's do this and let's have a look. So we actually transpose the other data frame in order to set the index, our stocks and our portfolio. And that's columns from the mean return and the standard deviation of returns. And then it's definitely a good idea to rename the column labels to return and risk. And still raffia the mean and standard deviation of daily returns, and it's more intuitive to analyze them, so we multiply the mean return with 252. So that's the average amount of trading days and we multiply the risk by the square root of 252. So this is nothing new. And finally, we have our summary data frame with the annualized return and the annualized risk. And finally, we can also create a scatterplot. So we use the plot method on our summary data frame and on the X-axis, we have the risk column and on the Y-axis, the return column. So we actually get seven dots, seven points and our scatterplot. And we also annotate the labor for each point of each data. So that's the TICAS and the equal weighted portfolio. And let's have a look here. So here we have the total risk return profile for our six stocks and for our equal weighted portfolio. So this is also nothing new here. So here we have our equal weighted portfolio and these are our six constituents. And finally, to save some time and some code in our next videos, we define a function, analyze the risk and return where we pass a return data frame. So, for example, here. This is our return state, our and we pass the return state iframe to the function and actually the function calculates the mean and the standard deviation and renames the columns and analyzes risk and return. And then finally returns the final summary data frame. So we are defining the function here for the next videos to save some code and some time. And with this, we are finished with this video and I hope to see you also in the next one by.