Working with sums/products of lists
I need to work symbolically with expressions such as this
L(x;a,b)=∏ni=1abxa−1i(1−xai)b−1
where x would be a random sample of size n.
I don't know of any way to express the indexing of the sample x by each element...
The closest I got was defining the variables n and i and representing x as a function
var('a','b','x','n','i')
assume(x>0,a>0,b>0,i>0,n>0)
X = function('X',nargs=1)
L = product(a*b*X(i)^(a-1)*(1-X(i)^a)^(b-1), i, 1, n)
But this seems to inmediately assume that X(i)=i and L is represented as:
−(−1)nanbnX(0)X(−1)X(−2)X(−3)X(−4)X(−5)X(−6)X(−7)X(−8)X(−9)∏ni=1(−X(i)a+1)b∏ni=1X(i)aX(n−1)X(n−2)X(n−3)X(n−4)X(n−5)X(n−6)X(n−7)X(n−8)X(n−9)X(n)∏ni=1X(i)a−1∏ni=1X(i−10)
I don't know how to deal with this expression, and it seems to me like it should be straight forward.
In case it's meaningful, after defining the expression, I will be differentiating it with respect to both a and b.
Very strange behavior. It seems to have something to do with the minus signs in the exponents. A workaround is to change variables:
a
toa+1
andb
tob+1
(and hencea>=0, b>=0
):L = product((a+1)*(b+1)*X(i)^a*(1-X(i)^(a+1))^b, i, 1, n)
works fine.Why do you think it's assuming that X(i)=i? I see no indication of that. It is doing some ill-advised simplification (by the looks of it, an invalid one--Bug report?), apparently triggered by the b−1 exponent. So if you're willing to shift b, you can use
However, you'll quickly find that sage currently doesn't know how to differentiate symbolic products, so you might want to do this yourself via a logarithmic derivative.
That is very strange... However you found an issue with my code. a and b should be greater than or equal to 0, not strictly greater. In that case the result remains the same.
@nbruin I thought it assumed X(i)=i from the expansion to (−1)n...X(0)X(1)... but now that I see it again, it might have come to mind from some other tests I run (but can't replicate now). The next thing I'll do is take the logarithm of L, so differentiating shouldn't be a problem so long as it understands the product will turn into a sum (not sure it will since log of the product simply puts a log around the whole expression even when simplified). Perhaps you could expand on an answer? Thanks