Loop for find a minimun

For questions regarding programming in the EViews programming language.

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

lpando08
Posts: 29
Joined: Tue Apr 03, 2018 1:13 pm

Loop for find a minimun

Postby lpando08 » Thu Apr 12, 2018 11:00 am

Hi, I have something like this. I want to create the series hdebt and ldebt outside program, but instead the value is fixed in 60 I want to vary from 10 to 100 and I want the program to find which number between 10 and 100 could minimize the SSR done by :
scalar ssrt_debt=sum_ssr_ldebt+sum_ssr_hdebt

I was trying to do it like this but it doesn't work, I do not know how make the program to give results to hdebt_10 and ldebt_10, hdebt_20 and ldebt_20, etc:

'for !x=10 to 100
'smpl if debt>!x
'series hdebt_!x=1
'smpl if debt <!x
'series ldebt_!x=1
'next


****THIS IS A PART OF THE PROGRAM (NOT COMPLETED)****

smpl if debt>60
series hdebt=1

smpl if debt<60
series ldebt=1
.
.
.

string grps = "h dev fixed flex open closed hdebt ldebt"
.

for %grp {grps} ' country/observation groupings

.

'running VAR and impulse responses
%%%%%%%%%%%%%%%%%%%%%%%%%%

smpl @first 2007q4 if {%grp}=1 and {%grp}(-!lags)=1

'Choosing lag length optimally
%%%%%%%%%%%%%%
if %lag_crit = "wald" then
var var_{%grp}.ls(noconst) 1 !maxlags {varlist}
var_{%grp}.testlags(name=waldlags_{%grp})
!flag=1
!count = !maxlags+1

while !flag
!count = !count-1

if @chisq(waldlags_{%grp}(!count,!vars+1),!vars^2) < 0.05 or !count=2 then
!lags = !count
!flag=0
endif

wend

delete waldlags_{%grp}

endif

if %lag_crit<>"none" and %lag_crit<>"wald" then
var var_{%grp}.ls(noconst) 1 !maxlags {varlist}
var_{%grp}.laglen(!maxlags, vname=lagtests_{%grp})

if %lag_crit = "lr" then
!lags = lagtests_{%grp}(1)
endif
if %lag_crit = "aic" then
!lags = lagtests_{%grp}(3)
endif
if %lag_crit = "sc" then
!lags = lagtests_{%grp}(4)
endif
if %lag_crit = "hq" then
!lags = lagtests_{%grp}(5)
endif

delete lagtests_{%grp}

endif

scalar lags_{%grp}_{%type}_{%lagcrit} = !lags

var var_{%grp}.ls(noconst) 1 !lags {varlist}
var_{%grp}.impulse(!imp_len, m, se=a, imp=chol, matbys=imp_{%grp}) {varlist} @ {gvar}
var_{%grp}.impulse(!imp_len, m, a, se=a, imp=chol, matbys=imp_{%grp}_a) {varlist} @ {gvar}
var_{%grp}.makeresids {residlist}
sym resid_cov_actual = var_{%grp}.@residcov
matrix resid_cov_act = var_{%grp}.@residcov

for %c {%grp}
for !j = 1 to var_{%c}.@neqn
vector(!j) ssr_{%c}(!j)= var_{%c}.@ssr(!j)
vector sum_ssr_{%c}=@sum(ssr_{%c})
next

next

close var_{%grp}

scalar ssrt_debt=sum_ssr_ldebt+sum_ssr_hdebt


Thank you very much!

EViews Matt
EViews Developer
Posts: 301
Joined: Thu Apr 25, 2013 7:48 pm

Re: Loop for find a minimun

Postby EViews Matt » Thu Apr 12, 2018 12:59 pm

Hello,

It sounds like you're trying to create a pair of dummy variables. You can do that directly without setting a sample.

Code: Select all

for !x = 10 to 100 step 10
   series hdebt_!x = debt > !x
   series ldebt_!x = debt < !x
next

lpando08
Posts: 29
Joined: Tue Apr 03, 2018 1:13 pm

Re: Loop for find a minimun

Postby lpando08 » Thu Apr 12, 2018 1:32 pm

But how I can report for example the vector sum_ssr_hdebt_10, sum_ssr_hdebt_20? I can't see them. Where I have to use !x? when I define the group string grps= "h dev fixed felx open closed hdebt_!x ldebt_!x"

like this?

for !x = 10 to 100 step 10
series hdebt_!x = debt > !x
series ldebt_!x = debt < !x
next

string grps = "h dev fixed flex open closed hdebt_!x ldebt!x"

for %grp {grps} ' country/observation groupings

for %c {%grp}
for !j = 1 to var_{%c}.@neqn
vector(!j) ssr_{%c}(!j)= var_{%c}.@ssr(!j)
vector sum_ssr_{%c}=@sum(ssr_{%c})
next

next

EViews Matt
EViews Developer
Posts: 301
Joined: Thu Apr 25, 2013 7:48 pm

Re: Loop for find a minimun

Postby EViews Matt » Thu Apr 12, 2018 3:37 pm

Sure, you can add them to your string of groups. Let the for loop do the work of adding the twenty new names...

Code: Select all

string grps = "h dev fixed flex open closed"

for !x = 10 to 100 step 10
   series hdebt_!x = debt > !x
   series ldebt_!x = debt < !x
   grps = grps + " hdebt_" + @str(!x) + " ldebt_" + @str(!x)
next


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 8 guests