#!/usr/bin/env python3 # Assignment n 1, given in lesson 03 on 01/10/19 # Author: Leonardo Tamiano import math import numpy as np # DESCRIPTION # # This function computes the entropy for a given discrete random # variable X. # # ARGUMENTS: # # 1) pmf is a numpy array describing the probability mass function of # a discrete random variable. In particular we have, # # pmf[i] = P(X = x_i) # def entropy(pmf): N_x = pmf.shape[0] result = 0 for i in range(0, N_x): if pmf[i] != 0: result += pmf[i] * math.log(pmf[i], 2) return -result