Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

FW: st: RE: RE: Calculate variances of subsamples


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   FW: st: RE: RE: Calculate variances of subsamples
Date   Thu, 3 Jun 2010 20:19:08 +0200

<>


So Knuth came back to me privately to inquire about details of example code
I sent earlier. 


The code creates a "myfile.dta" that collects all the results returned by
-rolling- into one big dataset using -append-. It also tries to recreate
Knuth`s "3105.dta" file that I cannot possibly have, so I make up numbers
via -rnormal()-. Maybe I should have called the thing "3106.dta", so it does
not overwrite anything, but there you go...

I do not understand his comment "* here is a problem, because stock is not
in the varlist.". Did you not say that you wanted to repeat the -rolling-
calculation for 1000 stocks? Maybe the name is "share" or something similar?


HTH
Martin


-----Original Message-----
From: Lars Knuth [mailto:[email protected]] 
Sent: Donnerstag, 3. Juni 2010 18:44
To: [email protected]
Subject: Re: st: RE: RE: Calculate variances of subsamples

Dear Martin,

thanks for taking the time to come up with this example. I appreciate
it very much. I just have basic knowledge of STATA, but I am eager to
learn more since I want to concentrate on using STATA instead of
Eviews and Matlab.

Those examples help me the most to get to know insights in a software.
I just have the problem that I can understand the code partially, but
sometimes I have problems to understand some lines or the connection
between those.

I want to ask you whether you could explain it a bit. I am sending the
code including some questions:

*create resultsfile
*Lars: myfile.dta is not my data file 3105.dta right?
*Lars: Why do I have to erase a file from the disk in the beginning?
cap erase myfile.dta
Lars: Why do I need the value of the return code of the cap before?
di in red _rc

clear*
gen start=.
gen end=.
gen _stat_1=.
gen stock=.
gen str15 kindofreturn=""

* This should save the data to myfile.dta. But actually it did not
create such a file.
save myfile, replace

*get "3105.dta"
clear*
*8 stocks
set obs 8
gen byte stock=_n

*5 time periods
* Expand makes the dataset 5 times larger. Is this given by my dataset?
expand 5
* Sorting by stock?
bys stock: gen byte time=_n

* I guess I can delete these lines, because it just generates some
random numbers.
gen double exret=rnormal()
gen double msciret=rnormal()
gen double msftret=rnormal()
gen double appret=rnormal()
gen double geret=rnormal()
gen double pgret=rnormal()
gen double jnjret=rnormal()
gen double bpret=rnormal()

* Does this overwrite the old 3105 file only temporaly or permanently?
save 3105, replace

use "3105.dta", clear
*Return calculation
gen double grexret=ex[_n]/ex[_n-1]-1 if _n>1
gen double grmsciret=msci[_n]/msci[_n-1]-1 if _n>1
gen double grmsftret=msft[_n]/msft[_n-1]-1 if _n>1
gen double grappret=app[_n]/app[_n-1]-1 if _n>1
gen double grgeret=ge[_n]/ge[_n-1]-1 if _n>1
gen double grpgret=pg[_n]/pg[_n-1]-1 if _n>1
gen double grjnjret=jnj[_n]/jnj[_n-1]-1 if _n>1
gen double grbpret=bp[_n]/bp[_n-1]-1 if _n>1

*loop to get -rolling- results for each stock
*and each return

* This goes through exret, msciret and msftret.
foreach ret in exret msciret msftret{

*start inner loop
* here is a problem, because stock is not in the varlist.
su stock, mean
qui forv i=1/`r(max)'{
       preserve
       keep if stock==`i'
       tsset time
       rolling r(Var), window(2) clear: su `ret'
       gen stock=`i'
       gen kindofreturn="`ret'"
       * I guess here is an important part I do not understand.
       * Append appends myfile to the existing dataset, then the
dataset is saved again.
       append using myfile
       save myfile, replace
       restore
}
*end inner loop

}

u myfile, clear
ren _stat_1 Variance
sort stock kindofreturn start
l, sepby(stock kindofreturn) noo
***********

Thanks in advance again and best greetings from the sunny Netherlands!

Lars






2010/6/2 Martin Weiss <[email protected]>:
>
> <>
>
> So here is an example that avoids -postfile- for Lars` case:
>
>
> ***********
> //create resultsfile
> cap erase myfile.dta
> di in red _rc
>
> clear*
> gen start=.
> gen end=.
> gen _stat_1=.
> gen stock=.
> gen str15 kindofreturn=""
>
> save myfile, replace
>
> //get "3105.dta"
> clear*
> //8 stocks
> set obs 8
> gen byte stock=_n
>
> //5 time periods
> expand 5
> bys stock: gen byte time=_n
>
> gen double exret=rnormal()
> gen double msciret=rnormal()
> gen double msftret=rnormal()
> gen double appret=rnormal()
> gen double geret=rnormal()
> gen double pgret=rnormal()
> gen double jnjret=rnormal()
> gen double bpret=rnormal()
>
> save 3105, replace
>
> //-use- "3105"
> u 3105, clear
>
> //Return calculation
> gen double grexret=ex[_n]/ex[_n-1]-1 if _n>1
> gen double grmsciret=msci[_n]/msci[_n-1]-1 if _n>1
> gen double grmsftret=msft[_n]/msft[_n-1]-1 if _n>1
> gen double grappret=app[_n]/app[_n-1]-1 if _n>1
> gen double grgeret=ge[_n]/ge[_n-1]-1 if _n>1
> gen double grpgret=pg[_n]/pg[_n-1]-1 if _n>1
> gen double grjnjret=jnj[_n]/jnj[_n-1]-1 if _n>1
> gen double grbpret=bp[_n]/bp[_n-1]-1 if _n>1
>
>
> //loop to get -rolling- results for each stock
> //and each return
>
> foreach ret in exret msciret msftret{
>
> //start inner loop
> su stock, mean
> qui forv i=1/`r(max)'{
>        preserve
>        keep if stock==`i'
>        tsset time
>        rolling r(Var), window(2) clear: su `ret'
>        gen stock=`i'
>        gen kindofreturn="`ret'"
>        append using myfile
>        save myfile, replace
>        restore
> }
> //end inner loop
>
> }
>
> u myfile, clear
> ren _stat_1 Variance
> sort stock kindofreturn start
> l, sepby(stock kindofreturn) noo
> ***********
>
>
> HTH
> Martin
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Martin Weiss
> Sent: Mittwoch, 2. Juni 2010 20:27
> To: [email protected]
> Subject: st: RE: Calculate variances of subsamples
>
>
> <>
>
> You could of course issue the -rolling- call with -clear- present, -save-
> the result to a new file and reload your "3105.dta" to start anew for the
> next stock. The datasets thus -saved- could be -append-ed to form one big
> dataset afterwards. -postfile- is also an option, as always.
>
> BTW, you may be better of with the lag operator "L." for your return
> calculations.
>
>
> HTH
> Martin
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Lars Knuth
> Sent: Mittwoch, 2. Juni 2010 20:22
> To: statalist
> Subject: st: Calculate variances of subsamples
>
> Dear listers,
>
> I have to say thanks to Martin, the recommendation of rolling was
> great. Unfortunately, I have now a few problems with the
> implementation.
> 1. -rolling- works with the "clear" option, but without it does not
> ("rolling r(Var), window(60) clear: summarize exret" works)
> 2. I need the data to calculate and store the variances for more than
> 1000 stock price returns in the end, so can I somehow keep all the
> data and then perform -rolling- in a loop?
> 3. Is there also an opportunity to perform the return calculation in a
loop?
>
> I am attaching parts of the code I have so far. Any ideas would be of
> great help to me.
> Thanks in advance!
>
> clear*
> use "C:\...\3105.dta", clear
>
> gen int time=_n
> * Return calculation
> gen double exret=ex[_n]/ex[_n-1]-1 if _n>1
> gen double msciret=msci[_n]/msci[_n-1]-1 if _n>1
> gen double msftret=msft[_n]/msft[_n-1]-1 if _n>1
> gen double appret=app[_n]/app[_n-1]-1 if _n>1
> gen double geret=ge[_n]/ge[_n-1]-1 if _n>1
> gen double pgret=pg[_n]/pg[_n-1]-1 if _n>1
> gen double jnjret=jnj[_n]/jnj[_n-1]-1 if _n>1
> gen double bpret=bp[_n]/bp[_n-1]-1 if _n>1
>
> tsset time
>
> * Rolling
> rolling r(Var), window(60): summarize exret
> rolling r(Var), window(60): summarize msciret
> rolling r(Var), window(60): summarize msftret
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index