Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annual return from each company, wen having monthly data given

    I am right now trying to generate annual return volatility for each company using the following formula with the monthly returns. My data contains a single number for all months of a given year for each firm. In the formula y stands for year and m stands for month and i stands for a firm. The units for vola are percentage points. I am not sure how to get to an annual return per company, should I use a loop? I don't even have a clue how to start
    Attached is my provided data, where I already generated the company_id for each company, monthly returns are given by ret, and risk-free rate (rf) = rf
    How to begin? how do I not double count, how to use an efficient way to get the annual return for each company even though the data is monthly? jeeeesus...

    What I tried is:
    gen excess_ret = (ret - rf)^2
    sort company_id
    by company_id: egen vola = 100 * sqrt(12 * sum(excess_ret)
    But I receive an error message "unknown egen function"

    Thank you so so so much for your help!!! Best
    Click image for larger version

Name:	Screenshot 2022-11-19 at 14.51.37.png
Views:	1
Size:	23.7 KB
ID:	1690113

    Attached Files
    Last edited by Pauline Mueller; 19 Nov 2022, 08:55.

  • #2
    Something like this:
    Code:
    by company_id year (month), sort: egen sumsq = total((ret-rf)^2)
    gen vola = 100 * sqrt(12*sumsq)
    Replace year and month in the above by the actual names of the variables containing the year and month. (I have not looked at your attachment--see below.)

    We were all Stata beginners once, and the learning curve can be steep. I recommend you take a look at #5 at https://www.statalist.org/forums/for...uplicate-dates where William Lisowski outlines some very helpful steps for acquiring the basics. It will take a bit of time to follow that advice, but the return on that investment will be very high.

    Before you next post, please read the Forum FAQ for excellent advice about how to maximize your chances of a timely and helpful response. Among the things you will learn there is that attachments are discouraged; many forum members will not download files from people they do not know. The helpful way to show example data is with the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X