Bookmark and Share

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: question concerning translation from Matlab to Mata


From   "Brian P. Poi" <[email protected]>
To   [email protected]
Subject   Re: st: question concerning translation from Matlab to Mata
Date   Thu, 10 Mar 2011 18:09:53 -0500



On 3/10/2011 5:55 PM, DC wrote:
Hi All,

I'm attempting to translate some code for an optimal stopping problem
that I had previously written in Matlab to Mata and I've encountered a
problem
in writing down a likelihood function.

A simplified version in Matlab would be a multinomial logit with three
choices, choice 0 parameter normalized to zero, :
...
[n k]=size(x);
D = 1+exp(x*b(1:k))+exp(x*b(k+1:2*k));
p_0 = 1./D;
p_1 = exp(x*b(1:k))./D;
p_2 = exp(x*b(k+1:2*k))./D;
lnlike=-sum( (y==0).*log(p_0) + (y==1).*log(p_1) +(y==2).*log(p_2));
....
However is it not clear how to specify and estimate something like
this using Mata
I tried the following:

void lmle(todo,b,y, x,lnf, S, H)
{
	k = cols(x)
	b1 = b[1::k,1]
	b2 = b[k+1::2*k,1]
	xb1 = x*b1'
	xb2 = x*b2'
	
	d = 1 :+ (exp(xb1) + exp(xb1))
	p_0 = 1 :/ d
	p_1 = exp(xb1) :/ d
	p_2 = exp(xb2) :/ d
	lnf = (y==0) :* log(p_0) + (y==1) :* log(p_1)  + (y==2) :* log(p_2)	
}

S = optimize_init()
optimize_init_evaluator(S,&lmle())
optimize_init_evaluatortype(S, "gf0")
optimize_init_argument(S,1,y)
optimize_init_argument(S,2,x)
optimize_init_params(S, J(1,cols(x),0))

b = optimize(S)



One immediate problem is that -optimize- passes the parameters as a row vector, not a column vector, so the lines

> 	b1 = b[1::k,1]
> 	b2 = b[k+1::2*k,1]

should probably be

 	b1 = b[1, 1::k]
 	b2 = b[1, k+1::2*k]

That may or may not be the only problem.

   -- Brian Poi
   -- [email protected]

*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index