Hi,
I have a struct as follows:
struct P
df::DataFrame
end
where the DataFrame
contains labels (qs
) & prior probabilities (probs
). I’ve implemented multiplication & normalization:
Base.:(*)(p::P, v) = P(DataFrame(qs=p.df.qs, probs=p.df.probs .* v))
normalize!(p::P) = p.df.probs = p.df.probs ./ sum(p.df.probs)
which allows Bayesian updating:
prior = P(DataFrame(qs = ["heads", "tail"], probs=[0.5, 0.5]) # illustrative. Not how it's done
likelihood = [0.75, 0.5]
posterior = prior * likelihood
normalize!(posterior)
Now for the question: say I want to create an update
function that will update a probability distribution with new data (likelihood):
# Does not work
function update!(p::P, likelihood)
p *= likehood
normalize!(p)
end
This doesn’t work because although p
is passed by reference, p *= likelihood
is actually translated to p = p * likelihood
, which results in a new p
being allocated.
OK, fine. As the multiplication is really a broadcast operation - what about supporting broadcast for the new type, so I can write:
p .*= likelihood
This (if it works) should result in modification of the elements of the DataFrame
, which should preserve the pass-by-ref semantics.
Trouble is, I can’t figure out how to actually implement the broadcasting interface. I’m lost in a sea of broadcastable
, and BroadcastStyle
s…
How do I define a simple broadcast interface for this type? Is there any other way of to get update!
to work?
(obviously, I can just write:
p.probs .*= likelihood
or define a function that will do that for me:
mul!(p::P, likelihood) = p.probs .*= likelihood
but that’s a cop out)
Thanks
DD