Message Boards Message Boards

0
|
4930 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Not evaluting a function during optimization

Posted 10 years ago
myghxw[n_Integer] := Module[
    {xis, wis, getw, x},

    (* get xi *)
    xis = NSolve[HermiteH[n, x] == 0];
    xis = Table[xis[[i]][[1]][[2]], {i, 1, Length[xis]}];


    (* get wi *)
    getw[xi_] := 2^(n - 1)*n!*Sqrt[Pi]/(n^2*HermiteH[n - 1, xi]^2);
    wis = getw[#] & /@ xis;

    {xis, wis}

]

{xs,ws}=myghxw[20];


voles1={29, 15, 15, 16, 27};
kvoles1= 5;

(* m7 = Log Gamma Model *)
mylikLogGamma[data_, kdata_, f0_?NumericQ, alpha_?NumericQ, beta_?NumericQ] := Module[
    {K, pj, fj, N0, loglik, above, below},

    K = kdata;

    (* Using Gauss Hermite *)
    (* Evaluted at each xs *)

    pj = Table[
         Exp[-xs[[i]]^2./beta]^j
         *
         (1.-Exp[-xs[[i]]^2./beta])^(K-j)
         *
         xs[[i]]^(2.*alpha-2.)
         , {i, 1, Length[xs]}
         , {j, 0, K}
       ];

    (* Put weights on *)
    pj = ws.pj;

    pj = Table[Binomial[K, j], {j, 0, K}]*pj;

    fj = Prepend[PadRight[data,K], f0];

    N0 = Sum[fj[[j]], {j, 1, Length[fj]}] ;
    above = LogGamma[N0 + 1] ;
    below = Sum[LogGamma[fj[[j]] + 1], {j, 1, Length[fj]}] ;
    loglik = fj.Log[pj] ;

    loglik = above - below + loglik;

    N[loglik]

]

(* Test for fitting*)

myLogGammaFit[data_, kdata_]:= Module[
    {testfun,ans},

    testfun[f0_, alpha_, beta_]:= Module[
       {ans2},

       ans2 = mylikLogGamma[data,kdata, f0, alpha, beta];

       If[ Im[ans2] !=0, ans2=-9999];

       ans2
    ];

    ans=NMaximize[
       {testfun[f0,alpha,beta], 
         f0 >= 0
         && alpha>0
         && beta>0
       },
       {f0, alpha, beta},
       Method -> {"RandomSearch", "SearchPoints" -> 200}
    ]//Quiet;

    ans

]


myLogGammaFit[voles1,kvoles1]

Spent two days, still can't figure our WHY????

It does not seem to be evaluating???

enter image description here

POSTED BY: Casper YC
2 Replies

What is happening is that NMaximize first evaluates its first argument analytically to get, if possible, a symbolic expression that it then works with (Remember that NMaximize does not have any Hold Attributes). You have suppressed the error messages which point to this by using Quiet. Thus, it ends up with the expression mylikLogGamma[data, kdata, f0, alpha, beta] which does not evaluate further because you have set the pattern for mylikLogGamma to require f0, alpha, and beta to be numeric.

The fix for this is to set the pattern for your testfun function that resides within the Module inside of myLogGammaFit to require its arguments to be numerical, thus keeping NMaximize from evaluating it and thus ending up with mylikLogGamma[data, kdata, f0, alpha, beta] rather than the unevaluated testfun[f0, alpha, beta]. Here is the required change:

myLogGammaFit[data_, kdata_] :=
 Module[{testfun, ans},
  testfun[f0_?NumberQ, alpha_?NumberQ, beta_?NumberQ] :=

   Module[{ans2},
    ans2 = mylikLogGamma[data, kdata, f0, alpha, beta];
    If[Im[ans2] != 0, ans2 = -9999];
    ans2];
  ans = NMaximize[{testfun[f0, alpha, beta], 
     f0 >= 0 && alpha > 0 && beta > 0}, {f0, alpha, beta}, 
    Method -> {"RandomSearch", "SearchPoints" -> 200}];
  ans]
POSTED BY: David Reiss
Posted 10 years ago

Thanks !

POSTED BY: Casper YC
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract