random sampling/iterations of data series

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
Bobby

random sampling/iterations of data series

Unread post by Bobby »

Hi,

I am new in RATS and I am struggling with a task I want to perform:

I have the data for 150 securities and would like to do the following random sampling/iterations:
1. randomly select equally weighted combinations of 1,2,3 etc securities up to a predefined number (lets say 1000 random samples) per combination
2. Obtain stat characteristics for those samples (either for each of the combinations or on average) like mean return, standard deviation, skew and kurtosis as well as correlation to the market.
3. Be able to save the sampling data in xl format for further management

I checked on the forum but could not find a topic that addresses my problem and will deeply appreciate any help with it

Regards
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: random sampling/iterations of data series

Unread post by TomDoan »

You can't create a Excel file a line at a time, so this just spits the information out as text. It computes the mean, standard deviation, skewness, excess kurtosis and beta coefficient for randomly selected triples.

Code: Select all

********************************************
*
* Create test data
*
dec vect[series] security(150)
dec series market
*
set market 1 500 = %ran(1.0)
do i=1,150
   set security(i) 1 500 = %ran(1.0)
end do i
********************************************
compute nreps=1000   ;* Number of replications
compute todraw=3     ;* Number of securities to draw each time
do rep=1,nreps
   compute scombo=%rancombo(150,todraw)
   set average = 0.0
   do i=1,todraw
      set average = average+security(scombo(i))/todraw
   end do i
   linreg(noprint) average
   # constant market
   stats(noprint) average
   disp ###.#### %mean sqrt(%variance) %skewness %kurtosis %beta(2)
end do rep
Bobby

Re: random sampling/iterations of data series

Unread post by Bobby »

Tom,

Thanks for the quick and prompt reply.

Very much apreciated

Cheers
Bobby

Re: random sampling/iterations of data series

Unread post by Bobby »

Tom and fellow forum users,

I have one more more question in regards to my previous posting, I hope I am not too annoying.

Beside the stats (like mean return, standard deviation, skew and kurtosis as well as correlation to the market) I also would like to get max draw-down for the ietrated portfolios. I define max draw-down as "the biggest percentage-losing period ("peak to valley") experienced by a particular iteration, regardless of whether or not the draw-down consisted of consecutive months of negative performance"

I did get the min and max monthly return for the individual iterations and tried to do some coding myself for draw-down but with no success yet. Any help will be much appreciated

Bobby
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: random sampling/iterations of data series

Unread post by TomDoan »

The tricky part about that is determining what is an actual peak and what is a trough. You might want to look at the CFEAT procedure http://www.estima.com/procs_perl/510/cfeat.src. While that's designed from quarterly or monthly data, it can be applied to higher frequency data as well. You would just want to change the minimum cycle duration. This filters through the local maxima and minima and eliminates those that are "too" local.
Bobby

Re: random sampling/iterations of data series

Unread post by Bobby »

Tom,

Thanks again. I might be the most annoying member of the forum, but I will try again…

Although the procedure is helpful I had bad luck so far incorporating it in the code you sent me previously (which was extremely helpful). I wanted to calculate the drawdown for the “average” vectors and get it to be reported next to the rest of the information (stdev, kurt, beta etc). Is there any way at all for this to be done or I am just wasting my time?

Thanks again heaps

Cheers
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: random sampling/iterations of data series

Unread post by TomDoan »

As I said before, the problem is defining what "peak" and "valley" mean. If you want the overall range of the series, that's easy - you just add the fractiles option to statistics and take %maximum-%minimum. It sounds as if that isn't what you want. The CFEAT procedure eliminates short-term movements to isolate the longer-term peaks and troughs. It certainly produces a lot more information than you need, but if the information that you do need is covered, we can help figure out how to isolate it. If CFEAT isn't picking the top and bottom the way you want, then you'll have to explain how you want those defined.
Bobby

Re: random sampling/iterations of data series

Unread post by Bobby »

Tom,

Sorry for all the trouble. I tried to integrate the procedure in the previous code you sent me, but with little success. I did some minuscule additions to the original and currently the main looks like:


********************************
compute nreps=100
compute todraw=2
do rep=1,nreps
compute scombo=%rancombo(247,todraw)
set average = 0.0
do i=1,todraw
set average = average+security(scombo(i))/todraw
end do i
linreg(noprint) average
# constant market
stats(fractiles, noprint) average
Table(noprint) / average
disp ###.#### %mean sqrt(%variance) %skewness %kurtosis sqrt(%rsquared) %beta(2) %minimum %maximum %maximum-%minimum
end do rep

***************************************

Again with the risk of pushing my luck I thought that may be if I try to explain better what I mean by drawdown it might make it easier for you to help me.

I was thinking of using the “Average” series as input and create new series which are:
1. the max of (LN running sum of “average” – ie. Cumulative return month by month) and running sum of average at T-1 …. This would be my peak calculation.
2. LN running sum of “average” ……. This would be my actual return

Then I would look for the min of 2 minus 1 and seek to include the exponent of it minus 1 in the last line of the code


In excel the above calculations would look like this (with formulas for line3):

A B C D
1 data =+LN(1+A3) =MAX(SUM(B$2:$B3),C2) =+SUM(B$2:$B3)-C3
2 2.3% 2.3% 2.3% 0.0%
3 2.4% 2.3% 4.6% 0.0%
4 -3.1% -3.1% 4.6% -3.1%
5 0.1% 0.1% 4.6% -3.0%
6 3.8% 3.7% 5.3% 0.0%

I was hoping to ba able to incorporate those calculations in the initial code you sent me. I easily created the new LN “Average” series but got stuck there

As an alternative way of trying to explain what I mean by drawdown I attach an xl function I use for calculation of drawdown

Hope it helps and sorry for all the trouble

Cheers

Bobby

****************************************
Function DRAWDOWN(aReturnVector)
' Calculates drawdown vector from a vector of log returns
'
' Input: a vector of fund log returns
' Output: a drawdown vector
' Comment: it doesn't matter whether the log returns are arranged in cols or rows
'
' Andreas Steiner, February 2007
' http://www.andreassteiner.net/performanceanalysis

n = WorksheetFunction.Max(aReturnVector.Columns.Count, aReturnVector.Rows.Count)

ReDim aDrawdownVector(1 To n)
ReDim aSortedDrawdownVector(1 To n)
ReDim aCumReturn(1 To n)

aCumReturn(1) = aReturnVector(1)
aMax = aCumReturn(1)
aDrawdownVector(1) = aMax - aCumReturn(1)

For i = 2 To n
aCumReturn(i) = aCumReturn(i - 1) + aReturnVector(i)
aMax = WorksheetFunction.Max(aCumReturn(i), aMax)
aDrawdownVector(i) = aMax - aCumReturn(i)
Next i

For i = 1 To n
aSortedDrawdownVector(i) = -WorksheetFunction.Large(aDrawdownVector, i)
Next i

DRAWDOWN = WorksheetFunction.Transpose(aSortedDrawdownVector)

End Function
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: random sampling/iterations of data series

Unread post by TomDoan »

This will do that calculation. I assume you're interest in computing the maximum value for the drawdown rather than graphing it.

Code: Select all

compute n=500
set average 1 n = .01+%ran(.10)
*
acc average 1 n cumavg
set(first=cumavg) maxcum 1 n = %max(maxcum{1},cumavg)
set drawdown = maxcum-cumavg
graph
# drawdown
This is very similar to the Excel macro, except that it does the calculation of the cumulated returns and the maximum cumulated returns in separate passes through the data, rather than one.
Bobby

Re: random sampling/iterations of data series

Unread post by Bobby »

Tom,

This is exactly what I want.

Thank you for your help. Much appreciated.

Cheers
Post Reply