Extracting values for matrix

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

RobHayward
Posts: 11
Joined: Fri Feb 19, 2010 3:21 am

Extracting values for matrix

Postby RobHayward » Sat May 01, 2010 11:12 pm

Hi,

I am using Eviews 7 and I am trying to take a number of observations in series y when series x has an extreme value. I create series z to show the extreme and then try to use this to extract values from y. I have used the following code, but the matrix results remains full of zero. If I try to run the final stage in the command window, I am told that "An unnamed object cannot be converted to a string".

Any hints greatly appreciated.

Rob

Code: Select all

wf a 1990 2010
series x=nrnd
series y=@trend
frml z=x>@quantile(x,0.95)
matrix(2,1) results
for !a = 1 to 11
if @elem(z,@otod(!a))=1 then
results(!a,1)=@elem(y,@otod(!a))
results (!a+1,1)=@elem(y,@otod(!a+1))
next
endif

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13312
Joined: Tue Sep 16, 2008 5:38 pm

Re: Extracting values for matrix

Postby EViews Gareth » Sun May 02, 2010 12:25 am

Hard to know where to start here..... :D. I believe what you're trying to do is find the observation number of the 95th quantile of X, and find the observation number of the next observation of X (which is, surely, just 1 plus the number you just found).

First of all, with only 21 observations, the 95% quantile is just going to be the maximum value, isn't it (in which case you could just use @max and @imax).

Secondly you've closed your for loop before you've closed your if statement, even though you opened the if statement after the for loop. Thus I think you meant to have endif followed by next, and not the other way round.

Thirdly your for loop only runs from 1-11. It is possible that the 95th quantile is after observation 11. I think you meant to run the loop until 21.

Fourthly, you've created a matrix with only 2 rows in it, but you're assigning the 95th quantile to the !ath row, even though !a could be equal to 21.

Fifthly you had a space between results and (!a+1,1).

Sixthly, you're assigning @elem(y,@otod(!a+1)) to the matrix. But If you run !a from 1 to 21 (as I presume you meant to?), then !a+1 could equal 22, and there aren't 22 observations in Y.

Now assuming I've interpreted what you were trying to do correctly, I think this is a corrected version:

Code: Select all

wf a 1990 2010
series x=nrnd
series y=@trend
frml z=x>@quantile(x,0.95)
matrix(2,1) results
for !a = 1 to 21
if @elem(z,@otod(!a))=1 then
results(1,1)=@elem(y,@otod(!a))
results(2,1)=@elem(y,@otod(!a+1))
endif
next


I haven't corrected point 6, since I'm not sure what you intended to do in that case.


Now having fixed the errors, there are some pointers... You appear to be assigning the !ath value of Y, even though you've set Y equal to @trend. Given that Y is equal to @trend, the !ath value of Y is simply equal to (!a-1) (since @trend starts at 0). Thus you could do:

Code: Select all

wf a 1990 2010
rndseed 3
series x=nrnd
frml z=x>@quantile(x,0.95)
matrix(2,1) results
for !a = 1 to 21
if @elem(z,@otod(!a))=1 then
results(1,1)=!a-1
results(2,1)=!a
endif
next



Finally, if you want to be really smart, you could probably write this program using samples and the @convert function instead...

Code: Select all

wf a 1990 2010
series x=nrnd
series y=@trend
smpl if x>@quantile(x,0.95) or x(-1)>@quantile(x,0.95)
matrix results = @convert(y)


Note I set the sample to be the observation of X that is in the 95th quantile, and the observation after that.
Follow us on Twitter @IHSEViews

RobHayward
Posts: 11
Joined: Fri Feb 19, 2010 3:21 am

Re: Extracting values for matrix

Postby RobHayward » Sun May 02, 2010 5:00 am

Thanks very much. I appreciate your patience. As you can see, I have a lot to learn.

I was just creating a practice worksheet as I am trying to automate an event study that will select extreme values (0.95 percentile) in the x series and then take the returns in the y series around this event window. I only used @trend as a short cut to create a series. Sorry for the confusion. In reality this is a series of over 1000 observations that are foreign exchange returns. The results matrix is supposed to store the returns for each event window as a column with each row as a separate point in the event window. For example, the first row would be -5 and the 11th row would be +5. The event itself is 0.

As there will be more than one event your elegant @convert solution will not work as it stands. I wonder if it is possible to use @convert with a specification of the sample for each event. I am thinking of a sample object along the lines of sm1 = @first+ia-5 @first+ia+5.

Anyway, thanks again. I will do some more work and post here if I manage to make progress.

RobHayward
Posts: 11
Joined: Fri Feb 19, 2010 3:21 am

Re: Extracting values for matrix

Postby RobHayward » Sat May 15, 2010 11:54 pm

For the event study, I have sorted out the code to identify values of x that are extreme (say 95th percentile) using a new variable; use the row of the new variable to select values of y in a window around the extreme reading identified by z. Put these in a matrix and then calculate the mean of each matrix row, which is the average for each day in the event study. Now I am trying to automate the extreme (so that 99th percentile and 90% percentile can be extracted) and the window length.

The "results" matrix is not now being filled with the y values that are around the extreme event. I am not sure why this is the case because it worked when I had raw numbers for the matrix placing.

Any thoughts would be welcome.

Rob

Code: Select all

wf u 1 2000
' create two series
series x=nrnd
series y=nrnd
'create a counter to count the number of extreme events
!rowcounter=1
'dialog box for inputs
%extreme="Extreme value"
%window="Window Length"
!action=0
!action=@uidialog("edit", %extreme, "Enter Value for Extreme")
If !action=-1 then stop
endif
'the window is the post event period.  Therefore the whole window is 2*!w +1
!action=@uidialog( "edit", %window, "Enter Value for Window Length")
If !action=-1 then stop
endif 

'convert the strings into scalar
!ma=@val(%extreme)
!win=@val(%window)

'make a matrix for the initial results. This will be scalled according to the extreme and window selection.
matrix ((2*!win+1),(1-!ma)*@obs(x)) results


'create a new series z that is one when x is in the percentile identified by !ma. 
series z= x>@quantile(x, !ma)
'Count down each obervation of x until a window before the end.   
!a=0
for !a = 1 +!win to @obs(x)-!win

'if z is 1 (signalling that x is extreme) take the y readings before and after the event. !b is a counter that will identify the row in the result matrix and will select the data in the window to put into the matrix

if @elem(z,@otod(!a))=1 then
for !b = !win*(-1) to !win
results(!b+!win+1,!rowcounter)=@elem(y,@otod(!a+(!b)))

next
 
'move the row counter along one
!rowsounter=!rowcounter+1
next
endif




'create row vectors for each time in the event
for !c = 1 to (2*!win) +1
rowvector r(!c)=@rowextract(results, !c)
'calculate the mean value of each event
scalar s!c=@mean(r!c)

vector (!c) av
av (!c) = s!c
next

'delete z
'delete s1 s2 r1 r2

show av

RobHayward
Posts: 11
Joined: Fri Feb 19, 2010 3:21 am

Re: Extracting values for matrix

Postby RobHayward » Mon May 17, 2010 1:25 am

This works. (I think).

It is programme for an event study. There are two series, x and y. Extreme values of x are the event and the values of y in the event window are extracted and the average for each event day is calculated.

You are asked for an extreme value (say 0.95) for the percentile of x that is going to be called 'extreme' and for an event window (which will be the number of days after the event).

The next step (I think) is to make an adjustment for extreme negative (say below 0.05) and to try to prevent over-lapping samples.

Any hints warmly welcomed.

Rob

Code: Select all

wf u 1 2000
' create two series
series x=nrnd
series y=nrnd
'create a counter to count the number of extreme events
!rowcounter=1
'dialog box for inputs
%extreme="Extreme value"
%window="Window Length"
!action=0
!action=@uidialog("edit", %extreme, "Enter Value for Extreme")
If !action=-1 then stop
endif
'the window is the post event period.  Therefore the whole window is 2*!w +1
!action=@uidialog( "edit", %window, "Enter Value for Window Length")
If !action=-1 then stop
endif 

'convert the strings into scalar
!ma=@val(%extreme)
!win=@val(%window)

'make a matrix for the initial results. This will be scalled according to the extreme selection.
matrix ((2*!win+1),(1-!ma)*@obs(x)) results


'create a new series z that is one when x is in the percentfile identified by !ma. 
series z= x>@quantile(x, !ma)
'Count down each obervation of x until the half window before the end.  The -1 will change to another number if the event window is more than 1. 
!a=0
!f=0
for !a = 1 +!win to @obs(x)-!win

'if z is 1 (signalling that x is extreme) take the y readings before and after the event. !b is a counter that will identify the row in the result matrix and will select the data in the window to put into the matrix

if @elem(z,@otod(!a))=1 then
for !f= 1 to 2*!win+1
results(!f,!rowcounter)=@elem(y,@otod(!a-!win-1+!f))

next
!rowcounter=!rowcounter+1

endif
next



'create row vectors for each time in the event
for !c = 1 to (2*!win)+1
rowvector r!c=@rowextract(results, !c)

'calculate the mean value of each event
scalar s!c=@mean(r!c)
next
vector (2*!win+1) av
for !e = 1 to (2*!win+1)
av (!e) = s!e
next

'delete z
'for !g = 1 to 2*!win+1
'delete s!g
'delete r!g

'next
show av




Return to “Programming”

Who is online

Users browsing this forum: No registered users and 17 guests