choosing k from real values

This is the development of a 2nd Edition of Enders' RATS Programming Manual.

Moderator: TomDoan

vtsa
Posts: 19
Joined: Thu Oct 10, 2013 7:25 am

choosing k from real values

Unread post by vtsa »

Dear Tom Doan,
I have a question about how to choose k from the real values. I have a rpf in which the code is written as follows:

dec vector[series] mean(26)
com lmax = 36
set trend = t
dec vector[integer] reglist
compute reglist = || constant| trend ||
dec vector[series] sine(10) dsine(10)
dec vector[series] cosine(10) dcosine(10)
com header$ = || 'Series' | 'k' | 'Tau' | 'F-value' | 'DF' | 'Lags'| 'T'||
report(action=define,hlab=header$)
com i = 23
sta(noprint) [series]i ; com cap_T = %nobs , start = 1961:2+612-%nobs
do k = 1,3
set sine(k) = sin(2.*%pi*k*t/%nobs) ; dif sine(k) / dsine(k)
set cosine(k) = cos(2.*%pi*k*t/%nobs) ; dif cosine(k) / dcosine(k)
end do k
...
I want to choose k from the range [0.1,0.2,0.3,...,2.8,2.9,3.0]. But do loop selects k from integer values. How can I change the code in order to select k from the real values such as [0.1,0.2,0.3,...,2.8,2.9,3.0].
I willl appreciate your response,
Kind Regards,
TomDoan
Posts: 7776
Joined: Wed Nov 01, 2006 4:36 pm

Re: choosing k from real values

Unread post by TomDoan »

DOFOR with %SEQA looks like it is what you need. For instance, one of the examples on that page uses a doubly nested grid using this:

Code: Select all

dec real lower upper
dofor lower = %seqa(-.2,.1,5)
  dofor upper = %seqa(1.6,.1,5)
     sweep(group=(thresh>=lower)+(thresh>upper))
     # spread
     # constant spread{1 2}
     compute rss=%scalar(%sigma)
     if rss<rssbest
        compute lowerbest=lower,upperbest=upper,rssbest=rss
  end dofor upper
end dofor lower
vtsa
Posts: 19
Joined: Thu Oct 10, 2013 7:25 am

Re: choosing k from real values

Unread post by vtsa »

Dear Tom Doan,
When I changed the code and run the program, I got the following message:
*

Code: Select all

calendar(q) 1987:01
allocate 2015:04
open data cad.rat
data(format=rat,org=columns) / cad
*
set y = cad
*
graph
# y
*
table
Series             Obs       Mean        Std Error      Minimum       Maximum
CAD                  116 -2.5887029528  3.2252496038 -9.8292520000  4.8927893830
Y                    116 -2.5887029528  3.2252496038 -9.8292520000  4.8927893830

*dec vector[series] mean(26)
*com lmax = 36
set trend = t
dec vector[integer] reglist
compute reglist = || constant| trend ||
dec vector[series] sine(10) dsine(10)
dec vector[series] cosine(10) dcosine(10)
com header$ = || 'Series' | 'k' | 'Tau' | 'F-value' | 'DF'  | 'Lags'| 'T'||

report(action=define,hlab=header$)
dec real k
com i = 2
	sta(noprint) [series]i   ; com cap_T = %nobs  , start = 1987:2+116-%nobs
	dofor k = %seqa(0.1,.1,50)
		set sine(k)   = sin(2.*%pi*k*t/%nobs)  ; dif sine(k) / dsine(k)
		set cosine(k) = cos(2.*%pi*k*t/%nobs)  ; dif cosine(k) / dcosine(k)
	end dofor k
Subscripting Error. Had Array Reference with (). Needed (INTEGER)
## SX27. Illegal Combination of Data Types for Operation
>>>> set sine(k) <<<<

I couldn’t solve this problem. Could You advise me how to change the code to run the program.
I will appreciate your response,
Thank You in advance.
TomDoan
Posts: 7776
Joined: Wed Nov 01, 2006 4:36 pm

Re: choosing k from real values

Unread post by TomDoan »

This will adapt the number of elements of the VECT[SERIES] to the number of elements in the sequence.

Code: Select all

compute kf=%seqa(0.1,.1,50)
dec vect[series] sine(%sizef(kf)) dsine(%size(kf)) cosine(%size(kf)) dcosine(%size(kf))
do k = 1,%size(kf)
   set sine(k) = sin(2.*%pi*kf(k)*t/%nobs) ; dif sine(k) / dsine(k)
   set cosine(k) = cos(2.*%pi*kf(k)*t/%nobs) ; dif cosine(k) / dcosine(k)
end do k
vtsa
Posts: 19
Joined: Thu Oct 10, 2013 7:25 am

fractional single k frequency

Unread post by vtsa »

Dear Tom,
I set up a fourier program that chooses fractional (not integer) value of k from the interval 0.1<k<2.0 where I select k=0.1 as increments of the selected frecuencies. I just want to be sure that I set up the program correctly. if there is anything wrong, please can you correct my mistakes.
The file is attached.
I will appreciate your response,
Kind regards!
TomDoan
Posts: 7776
Joined: Wed Nov 01, 2006 4:36 pm

Re: choosing k from real values

Unread post by TomDoan »

That looks like it would work fine---you can check fairly easily by just displaying the kf(k) value and %rss in the loop and verifying that it's finding the minimizer.

There's actually an easier to way to do that now with FIND with the METHOD=GRID.

Code: Select all

compute kf=%seqa(0.05,.2,20)
nonlin kk
find(trace,method=grid,grid=||kf||) min %rss
   set sin1  = sin((2*%pi/nobs)*kk*t)
   set cos1  = cos((2*%pi/nobs)*kk*t)
   linreg(noprint) y ; # constant sin1 cos1
end find
linreg(noprint) y ; # constant sin1 cos1
prj fit
graph 2
# fit
# y
vtsa
Posts: 19
Joined: Thu Oct 10, 2013 7:25 am

Re: choosing k from real values

Unread post by vtsa »

Great!
Thank You very much.
Post Reply