For independent observations from some distribution, we end up doing a lot of

```
sum(logpdf.(myDist, x))
```

But in some cases, the algebra simplified a lot, and we can just track sufficient statistics. A common example of this is normal distributions, where we only need `(Σx,Σx²)`

. This kind of thing generalizes quite a bit - there’s a whole collection of exponential families that allow this kind of optimization.

I’ve been looking into this a little bit, current progress is here. But I’m a bit stuck on

```
function iid(d::NatExpFamDist{P,X} ) where {P,X}
logh(xs :: AbstractArray{<:X}) = sum(d.fam.logh.(xs))
t(xs :: AbstractArray{<:X}) = reduce((a,b) -> a .+ b,d.fam.t.(xs))
a(η::P) = d.fam.a(η)
fam = NaturalExponentialFamily{P,AbstractArray{X}}(logh, t, a)
NatExpFamDist(fam,d.η)
end
```

I already need to do some weird `reduce`

tricks, and I also traverse the data twice (a sum and a reduce), which is not great.

This seems like a natural place to use @tkf’s Transducers, but I’m not familiar with it enough yet to see how to go about it. Any ideas?

Even with these issues and without being too careful yet about the typing, the performance is pretty good. So I think there could be a big advantage once it’s cleaned up