In this video we discuss how to calculate stock return, excess stock return, excess market return and estimate beta using CAPM model. We discuss how to calculate weekly, monthly or yearly stock beta in R. In next video we will discuss how to calculate rolling beta in R.
Website: thedatahall.com
As an Amazon Associate, I earn from qualifying purchases.
Show More Show Less View Video Transcript
0:00
Welcome to the DataHoll YouTube channel
0:02
In this video we are going to talk about how do we calculate weekly, monthly or yearly beta in our
0:10
So we first are going to show you how do we calculate beta, then how do we calculate beta for each stock, for each year, for each month, and for each week, right
0:22
So this is how it is going to be. We are going to use CAPM to calculate the betas
0:27
So firstly, let's load this package. Let's load this package. If you haven't installed the Tideaverse package
0:32
you first need to install this Tideaverse package. I have already installed, so I'm just going to load this package
0:38
Next, I'm going to load this example data set that I've created for this video purposes
0:44
We have different stocks. For example, we have Apple, Microsoft, etc. In this data set, we have their stock prices, the SNP 500 index
0:54
and the risk-free rate. Let me show you the structure of this data
0:59
You can see that these are numerical variables, but this symbol is a character variable
1:05
and date is considered as character variable. Although we need to tell are that this is a date variable
1:11
and it should not be read as a character variable. So the way is to take this data frame
1:19
this data data frame and create a column date, which is already there, but we would just
1:24
replace this column and specify this function, DMY, which would tell our date is in the date, month, and year format
1:37
And that would tell R that we have date column, which should be read as date rather than as
1:44
a character column. Now once that is done what we need to do is we need to calculate the risk return the returns the market return because we have index and we have stock prices right So what we do is we take the data frame the data data frame group by symbol
2:02
and this is necessary. Otherwise, R would not know that there are different firms
2:07
We create this R-I column, which is stock return, that would be log of price divided by lag price
2:15
Similarly, we create market return. We create R-I-R-F, which is the excess stock return
2:20
MKTRF, which is excess market return, and we only select the variable that we are interested
2:27
in in this video. The rest of the variables would be related. So if I can show you this data frame
2:33
we only have date, symbol, RIRF, and the excess market return. Next I am going to execute a regression
2:40
where I use the LM function. It regress the RIRF, which is excess stop return on excess market
2:47
return and the data frame that we need to use is called data if I execute this the
2:55
regression had been executed but we do not see any results in the console we just see this model
3:01
object created if I can show it the coefficients we can see that this is the beta for our data now
3:07
we had multiple firms in our data and we just have one beta the reason is that it took all the data and
3:16
and executed the regression on that. If you only want to look into the market, the beta
3:24
because over here it shows the intercept and the beta as well
3:28
If you just want the beta, then you would extract the second element from this list
3:35
Now this just gives us the beta for whole data, but this is not what we are interested in
3:41
We are usually interested in a beta for each firm. So this is where we discuss how do we generate a Vita for each stock The idea is quite simple We take the data frame we group by symbol This is where it tells are that there are different firms
3:57
and these firms are separated by a column called symbol. We summarize that would create a column beta
4:05
and that would be the coefficient from the regression or this LM function that we have already discussed over here
4:12
regression of excess stock return or excess market return, and we get the same
4:17
second element as we discussed over here. If I execute this, it would create an object over here
4:25
which gives us, we have four stocks and we get a beta for each stock
4:31
Now sometimes you might be working on certain research project where you'd
4:36
want to calculate beta for each form for each year, right? For that we just, we first want to calculate a column we want to create a column that would
4:47
because in our data, we do not have a column that will specify the ear variable
4:52
So we need to extract ear from the date and create a column that would have ears over here, right
5:01
The rest of the code is exactly the same as we did over here, except that when we created
5:08
we calculated beta for each stock, we just had to group by a symbol
5:13
But if we want to create, calculate the, the beta for each stock each year so you would group by the symbol and the ear
5:21
If I execute this and show you the beta year you can see that for Apple we had 12
5:28
years of data for each stock we have 12 years of data and we got 12 betas. Similarly if
5:33
you move forward we want beta for each month then we take the data frame
5:39
mutate month where we take the flow date function so we want to the floor date So this flow date have an option of month And this is just the formatting right
5:54
This is just the format function. It wouldn't matter if you don't use the format function and this element over here
6:01
The code would work exactly fine. But the format function is a great way of showing, you know, formatting the data in a way that it looks like the first month of 2000
6:13
the second month of 2011 and so on and so forth. This formatting is done by this format function
6:22
So the idea is simple. We take the exact same code, but instead of year
6:26
we write the month, the column that we just created over here. And if I execute this
6:31
we should get beta for each month, each form, right? We have these betas for Apple stock
6:40
Now for year, the idea is exactly the same. We just create a column that holds the weeks of the year
6:50
We use this week as opposed to month and we replace the format
6:56
If I can show you the data, you can see that the week column is this is the first week of 2011
7:03
second, third and we have 52 weeks for 2000 and for each year, right
7:11
And next is the same code. as previous and instead of month or year we replace this group by with a week so we
7:22
group by symbol and week and the rest of the case remains the the code remains the same
7:28
it is going to take time because we are calculating a lot of betas so these are the
7:35
weekly betas that we have so I hope this video was useful do subscribe to this channel
7:40
do hit the bell icon and thanks for watching this video Thank you
#Investing
#Stocks & Bonds


