Calculating betas with loop

Course Name: Volatility Trading Strategies for Beginners, Section No: 22, Unit No: 4, Unit type: Notebook

We're, appart of other things, calculating beta for a dataframe from 2015-2022.

For this code:

# A loop to calculate beta values for the entire span
for i in range(2, len(data)):

Why range(2, len(data)) and not  range(0, len(data))?

Hi Daniel,



We start with 2 to ensure that there are enough data points to calculate percentage changes and perform beta calculations using a rolling window of data.



Hope this resolves your query



Thanks

Rushda

Hope this resolves your query

Not really, using 2 instead of 0, means that we use 2 datapoints less, as we're discarding from the calculation the datapoints [0, 1]. I noticed including these datapoints the whole beta calculation changes a lot.

Can you please provide me more details about this thing?

Hi Daniel,



Let's try to break it down:



if data.index[i].year != data.index[i-1].year

This line ensures that we are looking for the row when there is a change in year. Essentially the idea is to take the first day of each year.



So if we take 0, then i-0 will be -1 and for the first row/first year [i=0] it will give us the last row/last year [i=-1] of the dataframe. So that rules out index[0]. You will get different result but it would not be valid. 



For index 1 and 2, whether you take 1 or 2 you will get the same results. All its doing is giving you a day out of the year for the beta calculation.



I hope I was able to clear out the confusion, let me know if this helps.



Thanks

Rushda