I would like to differentiate the entropy H(T) of a discrete random variable T with respect to one of the masses q(t') using Sympy. The following implements the expression for H(T):
import sympy as sym
sym.init_printing(use_latex=True)
N_T = sym.Symbol("N_T")
qT = sym.Function("q")
t = sym.Symbol("t")
ht_expr = sym.summation( qT(t) * sym.log(qT(t)), (t, 1, N_T) )
ht_expr
This prints the correct expression for H(T), as expected:
Now, differentiating with respect to q(t') should result in the derivative of q(t)log(q(t)) evaluated at t' if t' is in 1...N_T, and 0 otherwise (this is a known result, easy to check). I.e. it should return:
Instead I get 0:
tprime = sym.Symbol("t'")
sym.diff(ht_expr, qT(tprime))
[Out]: 0
How can I make Sympy handle sums like this and give the correct result?
I found that the answer in sympy - taking derivative of sum of symbolic number of elements also applies to this case.
In short, we need to use
IndexedBase
instead ofFunction
. For completeness, here is the code solving this problem.Output: