[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: stata and portfolios

From   "Martin Weiss" <>
To   <>
Subject   st: RE: stata and portfolios
Date   Sat, 14 Nov 2009 13:33:09 +0100


Here is an example code, maybe you can clarify your request with it...

set obs 100

gen stock=_n
gen numofshares=100000+int(10000*runiform())
gen shareprice=50+int(10*runiform())

//expand to # of months
expand 12
bys stock: replace shareprice=shareprice+rnormal(0,3)
//get time period (after expansion!)
bys stock: gen month=_n

gen marketcap=numofshares*shareprice

bys month: egen rank=rank(marketcap)

//example for second month
gen byte topportfolio = rank<=10 & month==2
gen byte bottomportfolio = rank>=90 & month==2

bys stock (month): gen return=shareprice[_n]/shareprice[_n-1]-1

//portfolio top 10% market cap
//for second month of sample period 
summ return if topportfolio, detail
//bottom 10% for second month
//in terms of market cap
summ return if bottomportfolio, detail


-----Original Message-----
[] On Behalf Of qing ye
Sent: Samstag, 14. November 2009 13:08
Subject: st: stata and portfolios

Deal all,

I am wondering whether I can write code in stata to do portfolios analysis.

By portfolio analysis, I mean

1.  I want to sort stocks based on some criteria (e.g. market
capitalization) on some dates (e.g. every month over the sample
2. construct serval portfolios based on the ranking of this variable (
top 10%, 20%, bottom 10% etc).
3. calculate the return and summary statistics for each portfolio.
4. maybe run some regression (e.g. CAPM) using the portfolio returns etc
5. get the results reported out

Can stata do such analysis? where I can get more information on how to
write such code, is there any template code that I can study on?

Thanks for your help!
*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index