Likelihood of a set of events originating from a set of sources

I’m hoping there’s a more performant way to do this, but I’m missing something.

I’m migrating some MATLAB code that uses reversible-jump MCMC to produce optimal sets of point sources to explain observed events (2d coordinates + precision).

The way a model is evaluated for a move to be accepted in MATLAB is to generate a matrix of all combinations of sources and coordinates, with each value being the normpdf described by the modeled source coordinate and event precision evaluated at the event coordinate. This is done separately for each axis and multiplied, and then the values for each localization are summed across sources, then the log of these are summed to produce the score for the model.

This is what the code looks like in Julia, partially optimized:

    xworking = zeros(μxs)
    yworking = zeros(μys)
    ws = 1 / nemitters
    p_alloc = 0.0
    for i ∈ eachindex(xs)
        xworking .= normpdf.(μxs .+ αxs .* frames[i], σxs[i], xs[i])
        yworking .= normpdf.(μys .+ αys .* frames[i], σys[i], ys[i])
        xworking .*= yworking
        p_emitter = sum(xworking)
        p_alloc += log(p_emitter * ws)
    return p_alloc

(the alpha values allow for linear drift)

Profiling shows normpdf, within the broadcast copy call, is the bottleneck.

I feel like there must be a more efficient approach to this. Are there particular packages I should look into? Can KernelDensity.jl be used?

The original MATLAB code is here for reference:

function LogL = p_Alloc(SMD,Mu_X,Mu_Y,Alpha_X,Alpha_Y,Ws)
%This function calculated the probability of a given allocation set.
    T = repmat(SMD.FrameNum,[1,length(Mu_X)]);
    Lx = length(X);
    Lmu = length(Mu_X);
    LogL = log(sum(repmat(Ws,[Lx,1]).*normpdf(repmat(X,[1,Lmu]),...
    LogL = sum(LogL);