The Mata code below is the best I came up with for speed. I'm interested in any suggestions or critique. Avoiding looping over measurements certainly helps, and it would be nice to avoid explicitly looping over observations as well.
Code:
// Simulate data
set seed 1243
mata mata clear
local nmeas = 15 // number of measurements
local top = 4 // measurements go from 1, ... top
local nobs = 500
local nreps = 50 // for timing only
mata: X = ceil(`top ' * runiform(`nobs', `nmeas'))
//
// Count up agreements
mata:
N = rows(X)
A = J(N, N, .)
timer_clear(1)
timer_on(1)
for (reps = 1; reps <= `nreps' ; reps++) { // outer loop just for timing
for (i = 1; i <= N - 1; i++) {
for (j = i+1; j <= N; j++) {
A[i,j] = rowsum(X[i,.] :== X[j,.]) // avoid loop over measurements
}
}
}
timer_off(1)
timer()
end
0 Response to Construct matrix counting pairwise agreements
Post a Comment