Greetings,
I'm trying to improve the time efficiency of a single line of Mata code that accounts for about half of total run time in an ML estimation program I've written. I'd like some advice on tweaking the Mata code that calculates the LL. The LL function comes from a binomial model and the part of the LL at issue is:
\[V = \sum\limits_{i = 1}^{N - 1} {\sum\limits_{j = i + 1}^N {{A_{ij}}\ln ({P_{ij}}) + } } \left( {K - {A_{ij}}} \right)\ln \left( {1 - {P_{ij}}} \right)\]
where N is the number of subjects. The Aij and Pij are already calculated, and are obtained from N X N symmetrical matrices. K is an integer. The Mata code I'm currently using here is just one line:
I know there is some inefficiency here, since I do the multiplication for the whole N X N A and P matrices, and then just sum the upper (or lower) part I need, but I'm stuck on a good way to avoid that, or otherwise improve this. As I indicated above, this one line is about half of total run time for a process that can be quite slow for large N (say N = 500), so improving this one line would be very helpful.
So, this is sort of a speed challenge. Here's some code to create a data example if you want to mess with this:
Regards, Mike
I'm trying to improve the time efficiency of a single line of Mata code that accounts for about half of total run time in an ML estimation program I've written. I'd like some advice on tweaking the Mata code that calculates the LL. The LL function comes from a binomial model and the part of the LL at issue is:
\[V = \sum\limits_{i = 1}^{N - 1} {\sum\limits_{j = i + 1}^N {{A_{ij}}\ln ({P_{ij}}) + } } \left( {K - {A_{ij}}} \right)\ln \left( {1 - {P_{ij}}} \right)\]
where N is the number of subjects. The Aij and Pij are already calculated, and are obtained from N X N symmetrical matrices. K is an integer. The Mata code I'm currently using here is just one line:
Code:
V = sum(uppertriangle((A :* ln(P)) + (K :- A) :* ln( 1:- P),0))
So, this is sort of a speed challenge. Here's some code to create a data example if you want to mess with this:
Code:
mata: // given K = 20 N = 500 // Fake data for A and P A = makesymmetric(floor(K * lowertriangle(runiform(N,N)))) P = makesymmetric(lowertriangle(runiform(N,N))) // LL V = sum(uppertriangle((A :* ln(P)) + (K :- A) :* ln( 1:- P),0)) end
Comment