def __entropy(self, pdf):
"""Calculate shannon entropy of posterior distribution.
Arguments
---------
pdf : ndarray (float64)
posterior distribution of psychometric curve parameters for each stimuli
Returns
-------
1D numpy array (float64) : Shannon entropy of posterior for each stimuli
"""
# Marginalize out all nuisance parameters, i.e. all except alpha and sigma
postDims = np.ndim(pdf)
if self.marginalize == True:
while postDims > 3: # marginalize out second-to-last dimension, last dim is x
pdf = np.sum(pdf, axis=-2)
postDims -= 1
# find expected entropy, suppress divide-by-zero and invalid value warnings
# as this is handled by the NaN redefinition to 0
with np.errstate(divide='ignore', invalid='ignore'):
entropy = np.multiply(pdf, np.log(pdf))
entropy[np.isnan(entropy)] = 0 # define 0*log(0) to equal 0
dimSum = tuple(range(postDims - 1)) # dimensions to sum over. also a Chinese dish
entropy = -(np.sum(entropy, axis=dimSum))
return entropy
评论列表
文章目录