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.
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%
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
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”.