Given a finite support x1,…,xn, feature functions fk and target expectations Fk, find the distribution closest to a base measure q in KL divergence that matches every target:
p=argminpDKL(p∥q)s.t.∑ip(xi)fk(xi)=Fk for all k
With a uniform q this is the maximum-entropy distribution. Its solution has the exponential-family form:
p(x)=Z(λ)q(x)exp(−∑kλkfk(x))
Implement maxent_dist(feats, targets, q=None):
feats: array of shape (n, K) with feats[i, k] =fk(xi); targets: shape (K,); q: base measure of shape (n,) (nonnegative, normalize it), or None for uniform.
- Find the multipliers λ∈RK that make the expectations of p match the targets to within 10−9 (the targets are strictly feasible and the features are not collinear on the support of q).
- Return
(p, lam) as float arrays of shapes (n,) and (K,), using the sign convention above.