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]

st: question concerning translation from Matlab to Mata


From   DC <[email protected]>
To   Statalist Statalist <[email protected]>
Subject   st: question concerning translation from Matlab to Mata
Date   Thu, 10 Mar 2011 17:55:47 -0500

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)


However, variations on this code either gave me "subscript" or
"conformability" errors

Can somebody give some help? Thanks in advance.

Regards,
Marcus


-- 
Marcus Casey, Ph.D.
*
*   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