Loop Intial Values over MAXIMIZE Instruction

Econometrics questions and discussions
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Loop Intial Values over MAXIMIZE Instruction

Unread post by rbelhach95 »

Dear Tom,

I would like to loop over my second data file of initial values with the MAXIMIZE instruction, I was able to loop over my FRMLS using the first data file of 49 industries, but having problem with the initial values, I have 8 parameters of initial values for each of the 49 industries. Please see attached code. Your help is very much appreciated.

Rachid
Attachments
code_1.PRG
(1.85 KiB) Downloaded 1078 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by TomDoan »

1. Get rid of the ALLOCATE 49 instruction.

2. What you want is com mux=muxi(count), muT = muTi(count),...

3. Before you do anything else, I would recommend that you go very carefully through your formula to make sure it is actually calculating what you want. With some of the more complicated subexpressions, I would suggest doing the calculation by hand and checking against what the RATS calculation is giving. The poor behavior of the BFGS estimates would not be expected if this were (as written) an infinitely differentiable function.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by rbelhach95 »

Dear Tom,

Thank you so much for your input. I got rid of the ALLOCATE 49 and it worked beautifully. I have rechecked my FRMLS again as you have recommended and they are written correctly. With the actual data convergence seems fine, I have one question: Is there a way to code (a loop e.g.) the program so that in the case of nonconvegence , a different piters with the simplex or more subiterations are executed until convergence is attained? I have looked in the user guide for such options in the MAXIMIZE instruction but could not find it.
Thank you
Rachid
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by TomDoan »

Use %CONVERGED, which is defined by almost any non-linear optimization instruction. See

http://www.estima.com/forum/viewtopic.p ... 711&p=6418
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by rbelhach95 »

Dear Tom,

Thank you so much for your reply. My question was not about how not exclude the vector of coefficients when convergence is not attained, but rather, whether the code could be executed again with a different set of piters or increasing the subiters limit e.g. until convergence is obtained. Your help is very much appreciated
Rachid
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by TomDoan »

rbelhach95 wrote:Dear Tom,

Thank you so much for your reply. My question was not about how not exclude the vector of coefficients when convergence is not attained, but rather, whether the code could be executed again with a different set of piters or increasing the subiters limit e.g. until convergence is obtained. Your help is very much appreciated
Rachid
Same idea.

estimation
if %converged==0
do estimation again perhaps with different options

I wouldn't recommend putting looping back (i.e.

:again
estimation
if %converged==0
goto again

as that's asking for an infinite loop. You could do

do tries=1,5
estimation
if %converged==1
break
end do tries

which would limit you to five tries.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by rbelhach95 »

Dear Tom,

Thank you for your inputs. Please see the attached code, I have tried the infinite loop as written on your last email, but I get an error. What I really need is to first get rid of non-convergence output cases as they mess up my mean square error and bias. I don't see why this wouldn't work for example:

if %converged ==1
break
else {
compute k = k
}

This way each time nonconvergence occurs, new data is generated until convergence is achieved. May be I am misplacing this conditional statement at wrong place in the code. Please advise
Thank you
Rachid
Attachments
code.PRG
(4.29 KiB) Downloaded 1043 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by TomDoan »

The middle set of code should have had a big red / across it. *Don't* do that. Do either the first or the last. And yes, you just want to restart the optimization, not regenerate the data.
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by rbelhach95 »

Dear Tom,

Thank you for your help. I was able at least able to eliminate the cases where estimation does not convergence, I am using the REPORT instruction (please see code attached) How do you use the copy option to save the created table to my desktop for example? Thank you so much.
Rachid
Attachments
code_test.PRG
(4.6 KiB) Downloaded 1084 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by TomDoan »

open copy mycopyfile.xls
report(action=show,window = 'Estimation')
report(action=show,format=xls,unit=copy)

will show one copy on the screen and send one to the file mycopyfile.xls
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by rbelhach95 »

Dear Tom,

Thank you for your reply, your help is very much appreciated. I was able to generate the xls table, but it seems that it includes the nonconvergence cases as well, I don't know why this is still happening after I impose the condition if %convergence==1. Here is my question:

I would like to create a dummy variable (vector) which takes 1 if the maximum likelihood converges and 0 otherwise. is it possible to define a dummy variable in RATS? if so how can I find it in the user's guide?

Please see attached code

Thank you
Attachments
code_test.PRG
(4.74 KiB) Downloaded 1120 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by TomDoan »

Code: Select all

              report(row = new, atcol = 1)  mux  muT  sigmax1  sigmaT1  sigmaTx1 sigmax2  sigmaT2  sigmaTx2

              if %converged == 1

              com mux_hat(k) = mux,  muT_hat(k) = muT
              com sigmax1_hat(k) = sigmax1, sigmaT1_hat(k) = sigmaT1
              com sigmax2_hat(k) = sigmaTx1, sigmaT2_hat(k) = sigmax2
              com sigmaTx1_hat(k) = sigmaT2, sigmaTx2_hat(k) = sigmaTx2


              report(row=current, atcol=1, align=decimal) mux_hat(i)
              report(row=current, atcol=2, align = decimal) muT_hat(i)
              report(row = current, atcol=3, align = decimal) sigmax1_hat(i)
              report(row = current, atcol=4,  align = decimal) sigmaT1_hat(i)
              report(row= current, atcol=5,  align = decimal) sigmaTx1_hat(i)
              report(row = current, atcol=6, align = decimal) sigmax2_hat(i)
              report(row = current, atcol=7,  align = decimal) sigmaT2_hat(i)
              report(row = current, atcol=8,  align = decimal) sigmaTx2_hat(i)
I'm not sure what you want from this, but

(1) This adds a row to the REPORT before it tests %CONVERGED
(2) The IF needs { ... } around the controlled statements. As written, the only instruction that doesn't get executed on %CONVERGED==0 is the first COMPUTE.

It looks like you want K to count only the examples which converge. To do that, you either want to loop back to regenerate the data when it doesn't converge, or change from a do loop to a WHILE:

compute k=0
while k<=n {
....
if %converged==1 {
compute k=k+1
report(row=new,...)
com mux_hat(k)=mux,...
}
}
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by rbelhach95 »

Dear Tom,

Thanks a lot for your reply. I have amended the code a bit like you have suggested and you are right I am trying to regenerate the data as I mentioned in on of my previous emails whenever nonconvergence occurs. However please see attached code, I get the following error and (N=5) separate tables. not one xls sheet. Please advise! Thank you
## Unassigned 613
The Error Occurred At Location 1803 of loop/block
Line 32 of loop/block
Attachments
code_test.PRG
(4.49 KiB) Downloaded 1061 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by TomDoan »

You're doing the REPORT(ACTION=DEFINE,..) inside the loop, and also doing the REPORT(ACTION=SHOW,...) inside the loop. The first needs to be before the WHILE, the latter needs to be after the final }
rbelhach95
Posts: 115
Joined: Sat Feb 09, 2013 9:30 am

Re: Loop Intial Values over MAXIMIZE Instruction

Unread post by rbelhach95 »

Dear Tom,

Thanks for your very useful insights. Please see the amended code, I still get many separate tables and the same error in my previous email. Please advise!
Best
Rachid
Attachments
code_test.PRG
(4.52 KiB) Downloaded 1058 times
Post Reply