Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Stata Mata error message: "too many tokens" r(3000)
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Stata Mata error message: "too many tokens" r(3000)
Date
Mon, 23 May 2011 19:23:44 +0100
As you surmise, this is nothing to do with -tokens()-, but I guess
1. You are asking Mata to digest something too big. No system has an
indefinitely extensible mouth or gut.
2. Adding /// is not going to help one bit. Ideally they would all be
stripped any way. It's not out of the question that extra /// make
some aspects worse.
I am guessing, as said, but I think you just need to get to work on
this code and break it up into more steps.
There is a lot of structure here that could be exploited. For example,
each definition of D1 etc. has a pair of terms -cn*p[1] +cn*p[1] etc.
which look like 0 to me. Also, much could be recast as vector or
matrix operations.
However only the lines that bite need to be fixed.
Nick
On Mon, May 23, 2011 at 6:30 PM, Lydia Pikyi Cheung <[email protected]> wrote:
> Dear Statalist users,
>
> I am trying to solve a system of 20 equations in 20 unknowns using
> Mata's optimize(), following
> http://www.stata.com/support/faqs/lang/nl.html (bottom of page).
> However, every time I parse the function that evaluates my objective
> function, I receive this error, and Stata stops on the exact same line
> of my code every time:
>
> too many tokens
> (31 lines skipped)
> ------------------------------------------
> r(3000);
>
> I have not used tokens() or any similar string function in my code. (A
> simplified version of my code is attached below.)
>
> I found a similar case on Statalist
> (http://www.stata.com/statalist/archive/2010-02/msg01318.html) but is
> unanswered.
>
> The only possible cause to this error I can think of is that maybe my
> line of code is too long. I have tried breaking down a long line of
> code using "///" (as you do in Stata), but I still have the same error
> message. (I cannot believe that Mata would have a limit to the length
> of objftn(); my objftn() below is not that long either.)
>
> Any ideas? Thanks in advance.
>
> = = = = = = = = = = = = = = = = = = = = = = = =
>
> clear
> version 11
> mata: mata clear
> mata:
>
> void objftn(todo,p,lnf,S,H)
> {
> external real scalar
> c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,an,bn,cn
> real scalar D1,D2,D3,D4,D5,D6,D7,D8,D9,D10,D11,D12,D13,D14,D15,D16,D17,D18,D19,D20
>
> D1= an -bn*p[1] -cn*p[1] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4] +cn*p[5]
> +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11] +cn*p[12]
> +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18] +cn*p[19]
> +cn*p[20];
> D2= an -bn*p[2] -cn*p[2] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4] +cn*p[5]
> +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11] +cn*p[12]
> +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18] +cn*p[19]
> +cn*p[20];
> D3= an -bn*p[3] -cn*p[3] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4] +cn*p[5]
> +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11] +cn*p[12]
> +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18] +cn*p[19]
> +cn*p[20];
> D4= an -bn*p[4] -cn*p[4] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4] +cn*p[5]
> +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11] +cn*p[12]
> +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18] +cn*p[19]
> +cn*p[20];
> D5= an -bn*p[5] -cn*p[5] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4] +cn*p[5]
> +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11] +cn*p[12]
> +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18] +cn*p[19]
> +cn*p[20];
> D6= an -bn*p[6] -cn*p[6] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4] +cn*p[5]
> +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11] +cn*p[12]
> +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18] +cn*p[19]
> +cn*p[20];
> D7= an -bn*p[7] -cn*p[7] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4] +cn*p[5]
> +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11] +cn*p[12]
> +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18] +cn*p[19]
> +cn*p[20];
> D8= an -bn*p[8] -cn*p[8] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4] +cn*p[5]
> +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11] +cn*p[12]
> +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18] +cn*p[19]
> +cn*p[20];
> D9= an -bn*p[9] -cn*p[9] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4] +cn*p[5]
> +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11] +cn*p[12]
> +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18] +cn*p[19]
> +cn*p[20];
> D10= an -bn*p[10] -cn*p[10] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D11= an -bn*p[11] -cn*p[11] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D12= an -bn*p[12] -cn*p[12] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D13= an -bn*p[13] -cn*p[13] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D14= an -bn*p[14] -cn*p[14] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D15= an -bn*p[15] -cn*p[15] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D16= an -bn*p[16] -cn*p[16] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D17= an -bn*p[17] -cn*p[17] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D18= an -bn*p[18] -cn*p[18] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D19= an -bn*p[19] -cn*p[19] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
> D20= an -bn*p[20] -cn*p[20] +cn*p[1] +cn*p[2] +cn*p[3] +cn*p[4]
> +cn*p[5] +cn*p[6] +cn*p[7] +cn*p[8] +cn*p[9] +cn*p[10] +cn*p[11]
> +cn*p[12] +cn*p[13] +cn*p[14] +cn*p[15] +cn*p[16] +cn*p[17] +cn*p[18]
> +cn*p[19] +cn*p[20];
>
> if (p[1]>0 & p[2]>0 & p[3]>0 & p[4]>0 & p[5]>0 & p[6]>0 & p[7]>0 &
> p[8]>0 & p[9]>0 & p[10]>0 & p[11]>0 & p[12]>0 & p[13]>0 & p[14]>0 &
> p[15]>0 & p[16]>0 & p[17]>0 & p[18]>0 & p[19]>0 & p[20]>0) {
> lnf=((p[1]-c1)*(-bn) +D1 +(p[2]-c2)*cn +(p[3]-c3)*cn +(p[4]-c4)*cn
> +(p[5]-c5)*cn)^2 \
> ((p[2]-c2)*(-bn) +D2 +(p[1]-c1)*cn +(p[3]-c3)*cn +(p[4]-c4)*cn
> +(p[5]-c5)*cn)^2 \
> ((p[3]-c3)*(-bn) +D3 +(p[2]-c2)*cn +(p[1]-c1)*cn +(p[4]-c4)*cn
> +(p[5]-c5)*cn)^2 \
> ((p[4]-c4)*(-bn) +D4 +(p[2]-c2)*cn +(p[3]-c3)*cn +(p[1]-c1)*cn
> +(p[5]-c5)*cn)^2 \
> ((p[5]-c5)*(-bn) +D5 +(p[2]-c2)*cn +(p[3]-c3)*cn +(p[4]-c4)*cn
> +(p[1]-c1)*cn)^2 \
> ((p[6]-c6)*(-bn) +D6 +(p[7]-c7)*cn +(p[8]-c8)*cn +(p[9]-c9)*cn
> +(p[10]-c10)*cn)^2 \
> ((p[7]-c7)*(-bn) +D7 +(p[6]-c6)*cn +(p[8]-c8)*cn +(p[9]-c9)*cn
> +(p[10]-c10)*cn)^2 \
> ((p[8]-c8)*(-bn) +D8 +(p[7]-c7)*cn +(p[6]-c6)*cn +(p[9]-c9)*cn
> +(p[10]-c10)*cn)^2 \
> ((p[9]-c9)*(-bn) +D9 +(p[7]-c7)*cn +(p[8]-c8)*cn +(p[6]-c6)*cn
> +(p[10]-c10)*cn)^2 \
> ((p[10]-c10)*(-bn) +D10 +(p[7]-c7)*cn +(p[8]-c8)*cn +(p[9]-c9)*cn
> +(p[6]-c6)*cn)^2 \
> ((p[11]-c11)*(-bn) +D11 +(p[12]-c12)*cn +(p[13]-c13)*cn
> +(p[14]-c14)*cn +(p[15]-c15)*cn)^2 \
> ((p[12]-c12)*(-bn) +D12 +(p[11]-c11)*cn +(p[13]-c13)*cn
> +(p[14]-c14)*cn +(p[15]-c15)*cn)^2 \
> ((p[13]-c13)*(-bn) +D13 +(p[12]-c12)*cn +(p[11]-c11)*cn
> +(p[14]-c14)*cn +(p[15]-c15)*cn)^2 \
> ((p[14]-c14)*(-bn) +D14 +(p[12]-c12)*cn +(p[13]-c13)*cn
> +(p[11]-c11)*cn +(p[15]-c15)*cn)^2 \
> ((p[15]-c15)*(-bn) +D15 +(p[12]-c12)*cn +(p[13]-c13)*cn
> +(p[14]-c14)*cn +(p[11]-c11)*cn)^2 \
> ((p[16]-c16)*(-bn) +D16 +(p[17]-c17)*cn +(p[18]-c18)*cn
> +(p[19]-c19)*cn +(p[20]-c20)*cn)^2 \
> ((p[17]-c17)*(-bn) +D17 +(p[16]-c16)*cn +(p[18]-c18)*cn
> +(p[19]-c19)*cn +(p[20]-c20)*cn)^2 \
> ((p[18]-c18)*(-bn) +D18 +(p[17]-c17)*cn +(p[16]-c16)*cn
> +(p[19]-c19)*cn +(p[20]-c20)*cn)^2 \
> ((p[19]-c19)*(-bn) +D19 +(p[17]-c17)*cn +(p[18]-c18)*cn
> +(p[16]-c16)*cn +(p[20]-c20)*cn)^2 \
> ((p[20]-c20)*(-bn) +D20 +(p[17]-c17)*cn +(p[18]-c18)*cn
> +(p[19]-c19)*cn +(p[16]-c16)*cn)^2
> }
> else {
> lnf=1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14\1e+14
> }
> }
>
> c1=0.9; c2=0.9; c3=0.9; c4=0.9; c5=0.9; c6=0.9; c7=0.9; c8=0.9;
> c9=0.9; c10=0.9;
> c11=0.9; c12=0.9; c13=0.9; c14=0.9; c15=0.9; c16=0.9; c17=0.9;
> c18=0.9; c19=0.9; c20=0.9;
> an=0.16; bn=0.384; cn=0.016;
>
> S=optimize_init()
> optimize_init_evaluator(S,&objftn())
> optimize_init_evaluatortype(S,"v0")
> optimize_init_params(S,J(1,20,1))
> optimize_init_which(S,"min")
> optimize_init_trace_value(S,"off")
> p=optimize(S)
> p
>
> end
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/