MS Excel Example
In the following table, we have data from 1/1/2010 to 1/7/2010. The first column has firm id; the second column has date; the third column has stock prices.
id | date | prices | simple ri | log_ri | ri+1 |
1 | 1/1/2010 | 70 | |||
1 | 1/2/2010 | 72 | 2.857% | 2.817% | 102.857% |
1 | 1/3/2010 | 75 | 4.167% | 4.082% | 104.167% |
1 | 1/4/2010 | 73 | -2.667% | -2.703% | 97.333% |
1 | 1/5/2010 | 74 | 1.370% | 1.361% | 101.370% |
1 | 1/6/2010 | 76 | 2.703% | 2.667% | 102.703% |
1 | 1/7/2010 | 77 | 1.316% | 1.307% | 101.316% |
The fourth and fifth columns have simple and log returns, calculated as:
simple ri = (Price[i] - Price[i-1] ) / Price[i-1] --- (Eq. 1) log ri = ln( Price[i] / Price[i-1] --- (Eq. 2)
where Price[i] is the stock price in the current period, Price[i-1] is the stock price in the previous period, ln is the natural log. To convert simple returns to n-period cumulative returns, we can use the products of the terms (1 + ri) up to period n. Therefore, the fifth column adds a value of 1 to the simple period returns.
Weekly cumulative simple returns
Suppose we wish to find weekly cumulative simple returns from the stock prices, we shall just use the first and the last stock prices of the week and apply equation (1). Therefore, our cumulative weekly simple return is as follows:
weekly simple ri = ( 77 - 70) / 70 = 10.00%
And if we were to find weekly cumulative simple returns from the daily returns, then we would add 1 to each of the period simple_ri, find its product, and deduct 1 at the end. Therefore, the formula for converting simple periodic daily returns to weekly cumulative returns would be :
Cumulative n-period simple returns = (1+simple_r1) * (1+simple_r2) *(1+simple_r3) ... (1+simple_rn) - 1 --- (Eq. 3)
Therefore, applying Equation 3 to our example;
Cumulative weekly simple returns = 102.857% * 104.167% * 97.333% * 101.370% * 102.703% * 101.316% - 1 = 10.00%
Weekly cumulative log returns
Now suppose we wish to find weekly cumulative log returns from the stock prices, again we shall use the first and the last of the stock prices of the week in equation (2). So, our cumulative weekly log return is as follows:
weekly log ri = ln( 77 / 70) = 9.53%
Since log returns are continuously compounded returns, it is normal to see that the log returns are lower than simple returns. To find n-period log returns from daily log returns, we need to just sum up the daily log returns. Therefore :
Cumulative weekly simple returns = 2.817% + 4.082% + (-2.703%) + 1.361% +2.667% + 1.307% = 9.53%
Stata Example
We shall continue to use the same data as above. The Stata do file for all of the following steps can be downloaded from here.
The following lines of code will generate the required data
clear input float date byte(id prices) float wofd 18263 1 70 2600 18264 1 72 2600 18265 1 75 2600 18266 1 73 2600 18267 1 74 2600 18268 1 76 2600 18269 1 77 2600 end format %td date format %tw wofd tsset id date
Now to generate simple and log returns
bys id (date) : gen simple_ri = (price / L.price) -1 bys id (date) : gen log_ri = ln(price / L.price)
Cumulative weekly simple returns
we shall use ascol program. This program can be downloaded from SSC by typing:
ssc install ascol
If daily returns were calculated with Eq. 1 above (i.e. simple returns) and they needed to be converted to cumulative n-periods returns, we shall use the option returns(simple). For this purpose, we would type the following command:
ascol simple_ri, returns(simple) keep(all) toweek
For syntax and option details of ascol, you can type help ascol at the Stata command prompt. We shall just briefly list the option used in the above command. After typing ascol, we need to mention the name of the variable for which cumulative returns are needed.
In our case, it is the simple_ri. Then after the comma, we invoke various program options. Our first option is returns(simple), which tells ascol that our data have simple returns. ascol will apply product method of converting from daily to weekly see Eq. 3 above ). Then we use keep(all) to stop ascol from collapsing the data set to a weekly frequency. Absent this option, the data will be reduced to one observation per ID and weekly_period identifier. The other possibility in this regard is the option price, which can be used if the variable is stock prices. And finally, we used toweek option for converting the data to a weekly frequency. Other possible options in this regard are tomonth, toquarter, and toyear.
Cumulative weekly log returns
If daily returns were calculated using Eq. 2 above (i.e. log returns) and they need to be converted to cumulative n-periods returns, we shall use the option returns(log). For this purpose, we would type the following command:
ascol log_ri , returns(log) keep(all) toweek gen(log_cumRi)
The syntax details remain the same as given above. We have used one additional option gen(log_cum) for naming the new variable as log_cumRi
date | wofd | simple_ri | log_ri | week_s~i | log_cumRi |
01jan2010 | 2010w1 | . | . | .1 | .09531018 |
02jan2010 | 2010w1 | .0285714 | .0281709 | .1 | .09531018 |
03jan2010 | 2010w1 | .0416667 | .040822 | .1 | .09531018 |
04jan2010 | 2010w1 | -.0266667 | -.0270287 | .1 | .09531018 |
05jan2010 | 2010w1 | .0136986 | .0136057 | .1 | .09531018 |
06jan2010 | 2010w1 | .027027 | .0266682 | .1 | .09531018 |
07jan2010 | 2010w1 | .0131579 | .0130721 | .1 | .09531018 |
AOA. Dr.sb you have a lot of contribution in educating people. I request you plz make some videos and upload them on youtube that a lot of people who can understand Urdu /Hindi could get benefit from your stata expertise. It will “SAWABY DARAIN”.