Page 1 of 1

Why does RATS overwrite the eqs in models in 9.00, not 8.30?

Posted: Thu May 19, 2016 3:01 pm
by macro
This code sample

Code: Select all


inquire(regressorlist) begin_est end_est
# dlgdpk{0 to max_lags} dlcnsk{0 to max_lags}

declare vector[model] models(2)

*** first process
linreg(print, define = eq) dlgdpk begin_est end_est
# dlgdpk{1 to max_lags} constant

compute models(1) = models(1) + eq

*** second process
linreg(print, define = eq) dlcnsk begin_est end_est
# dlcnsk{1 to max_lags} constant

compute models(2) = models(2) + eq

display %modelgetcoeffs(models(1))
display %modelgetcoeffs(models(2))
displays this in RATS 9.00g

Code: Select all

display %modelgetcoeffs(models(1))
      0.40078
      0.02690
      0.28305
     -0.18290
     -0.13991
      0.07910
      0.04388
     -0.14215
      0.00474

display %modelgetcoeffs(models(2))
      0.40078
      0.02690
      0.28305
     -0.18290
     -0.13991
      0.07910
      0.04388
     -0.14215
      0.00474
which is incorrect; the second equation is overwriting the first in the models vector. In RATS 8.3, however, the code works correctly:

Code: Select all

display %modelgetcoeffs(models(1))
      0.24257
      0.11083
     -0.02308
      0.05987
     -0.05790
     -0.00599
     -0.02424
     -0.19942
      0.00682

display %modelgetcoeffs(models(2))
      0.40078
      0.02690
      0.28305
     -0.18290
     -0.13991
      0.07910
      0.04388
     -0.14215
      0.00474
The values themselves aren't important for this example, hence why I omitted the data, but was this bug introduced in some revision of version 9.00? It broke pre-existing code when I migrated to RATS 9.00.

Re: Why does RATS overwrite the eqs in models in 9.00, not 8

Posted: Thu May 19, 2016 4:12 pm
by TomDoan
Actually, the bug is with 8.3. MODELS aren't not intended to be snapshots of the contents of their members at the time they are defined. If they were, you wouldn't be able to do bootstrapping and Monte Carlo where you keep changing the coefficients. With 8.3, the specific operation of model=model+equation was making a copy of the equation rather than using the equation itself.

Re: Why does RATS overwrite the eqs in models in 9.00, not 8

Posted: Tue May 31, 2016 11:26 am
by macro
Is there any chance you could post a version (or email me a link) of RATS 9.00 that doesn't fix this bug? Per your recommendations on other posts, I've been using the "model = model + eq" syntax extensively, and whatever version of RATS 9.00 includes the change to "model = model + eq" syntax broke a considerable amount of working code. A lot of my code also uses other changes that were first introduced in version 9.00 (like the fixes to hashes of series/models), so I can't run the code on 8.3 or earlier versions either.

Unless I'm missing something, it looks like this specific change isn't backwards-compatible, and that sets us back considerably on our project. If possible, do you have a version that incorporates all other bug fixes in version 9.00 that we've talked about in the past, except this change? We weren't expecting a breaking change when we updated to a newer version of 9.00.

Re: Why does RATS overwrite the eqs in models in 9.00, not 8

Posted: Tue May 31, 2016 12:49 pm
by TomDoan
We would understandably be reluctant to put a bug back into the program. A simple workaround would be to use something like

compute models(1) = models(1) + %eqnlag(eq,0)

rather than

compute models(1) = models(1) + eq

as the %eqnlag function creates a copy of the current equation and thus forces the current copy into the model rather than the original.

Re: Why does RATS overwrite the eqs in models in 9.00, not 8

Posted: Tue May 31, 2016 12:58 pm
by macro
I'll refactor my code and give that a shot. Thank you.

Also, can you post (or send me) a link to the update announcement that mentions this change? It would be useful for documentation purposes, but I wasn't able to find anything on the RATS 9.00 page or in the announcements on the forum. Since this change breaks backward compatibility (and previously recommended syntax), it would be useful to have documentation on it.

Re: Why does RATS overwrite the eqs in models in 9.00, not 8

Posted: Wed Jun 01, 2016 10:01 am
by macro
I'm having trouble getting the workaround to work. Here's a simple example:

Code: Select all

calendar(q) 1950 1

compute begin_samp = 1969:1
compute end_fcast = 2005:4
compute max_lags = 4
               
open data "data.xls"
    data(format = xls, org = columns) begin_samp end_fcast
close data

inquire(regressorlist) begin_est end_est
# lgdpk{0 to max_lags} lcnsk{0 to max_lags} dlpxfe{0 to max_lags} lpotgdpk{0 to max_lags}

declare model mdl
declare vector[real] aic_values(max_lags)
declare vector[equation] aic_eqs(max_lags)

dofor s = lgdpk lcnsk dlpxfe
    do i = 1, max_lags
        linreg(print, define = eq) s begin_est end_est
        # lgdpk{1 to i} lcnsk{1 to i} dlpxfe{1 to i} lpotgdpk{1 to i} constant
        
        compute aic_values(i) = -2.0 * %logl / %nobs + 2.0 * %nreg / %nobs
        compute aic_eqs(i) = %eqnlag(eq, 0)
    end do i
    
    compute num_lags = %minindex(aic_values)
    compute mdl = mdl + %eqnlag(aic_eqs(num_lags), 0)
end dofor s

forecast(model = mdl, from = end_est+1, to = end_fcast, noprint)
On RATS 9.00g x64, this crashes. There is no error message in interactive or batch mode; RATS simply crashes and Windows catches the exception.

This is just a simple, somewhat trivial, example, but it's representative of our larger code base.

Re: Why does RATS overwrite the eqs in models in 9.00, not 8

Posted: Tue Jul 05, 2016 8:19 am
by macro
Hi Tom. Do you have any updates on possible fixes for this? The workaround using %eqnlag that you suggested earlier does not work, per the previous post, and unfortunately this is a major part of our project.

Re: Why does RATS overwrite the eqs in models in 9.00, not 8

Posted: Tue Jul 05, 2016 11:23 am
by TomDoan
If you make sure that you create a distinct equation for each value of S (here done with a HASH[EQUATION]) then it should work

Code: Select all

    declare hash[equation] aicbest

    dofor s = lgdpk lcnsk dlpxfe
        do i = 1, max_lags
            linreg(print, define = aic_eqs(i)) s begin_est end_est
            # lgdpk{1 to i} lcnsk{1 to i} dlpxfe{1 to i} lpotgdpk{1 to i} constant

            compute aic_values(i) = -2.0 * %logl / %nobs + 2.0 * %nreg / %nobs
        end do i

        compute num_lags = %minindex(aic_values)
        compute aicbest(%l(s))=aic_eqs(num_lags)        
        compute mdl = mdl + aicbest(%l(s))
    end dofor s