def est_pmf_from_mpps(self, other, samples, eps=1e-10):
"""Estimate probability mass function from MPPovmList samples
:param MPPovmList other: An :class:`MPPovmList` instance
:param samples: Iterable of samples (e.g. from
:func:`MPPovmList.samples()`)
:returns: `(p_est, n_samples_used)`, both are shape
`self.nsoutdims` ndarrays. `p_est` provides estimated
probabilities and `n_samples_used` provides the effective
number of samples used for each probability.
"""
assert len(other.mpps) == len(samples)
pmf_ests = np.zeros((len(other.mpps),) + self.nsoutdims, float)
n_samples = np.zeros(len(other.mpps), int)
for pos, other_mpp, other_samples in zip(it.count(), other.mpps, samples):
pmf_ests[pos, ...], n_samples[pos] = self.est_pmf_from(
other_mpp, other_samples, eps)
n_out = np.prod(self.nsoutdims)
pmf_ests = pmf_ests.reshape((len(other.mpps), n_out))
given = ~np.isnan(pmf_ests)
n_samples_used = (given * n_samples[:, None]).sum(0)
# Weighted average over available estimates according to the
# number of samples underlying each estimate. Probabilities
# without any estimates produce 0.0 / 0 = nan in `pmf_est`.
pmf_est = np.nansum(pmf_ests * n_samples[:, None], 0) / n_samples_used
return (pmf_est.reshape(self.nsoutdims),
n_samples_used.reshape(self.nsoutdims))
评论列表
文章目录