ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 06 Jan 2019 15:27:14 -0600Working with sums/products of listshttp://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/I need to work symbolically with expressions such as this
$L(x; a,b) = \prod_{i=1}^n{abx_i^{a-1}(1-x_i^a)^{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:
$-\frac{\left(-1\right)^{n} a^{n} b^{n} X\left(0\right) X\left(-1\right) X\left(-2\right) X\left(-3\right) X\left(-4\right) X\left(-5\right) X\left(-6\right) X\left(-7\right) X\left(-8\right) X\left(-9\right) {\prod_{i=1}^{n} {\left(-X\left(i\right)^{a} + 1\right)}^{b}} {\prod_{i=1}^{n} X\left(i\right)^{a}}}{X\left(n - 1\right) X\left(n - 2\right) X\left(n - 3\right) X\left(n - 4\right) X\left(n - 5\right) X\left(n - 6\right) X\left(n - 7\right) X\left(n - 8\right) X\left(n - 9\right) X\left(n\right) {\prod_{i=1}^{n} X\left(i\right)^{a} - 1} {\prod_{i=1}^{n} X\left(i - 10\right)}}$
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$.Sat, 05 Jan 2019 08:23:28 -0600http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/Comment by galio for <p>I need to work symbolically with expressions such as this</p>
<p>$L(x; a,b) = \prod_{i=1}^n{abx_i^{a-1}(1-x_i^a)^{b-1}}$</p>
<p>where $x$ would be a random sample of size $n$.</p>
<p>I don't know of any way to express the indexing of the sample $x$ by each element...</p>
<p>The closest I got was defining the variables $n$ and $i$ and representing $x$ as a function</p>
<pre><code>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)
</code></pre>
<p>But this seems to inmediately assume that $X(i) = i$ and $L$ is represented as:</p>
<p>$-\frac{\left(-1\right)^{n} a^{n} b^{n} X\left(0\right) X\left(-1\right) X\left(-2\right) X\left(-3\right) X\left(-4\right) X\left(-5\right) X\left(-6\right) X\left(-7\right) X\left(-8\right) X\left(-9\right) {\prod_{i=1}^{n} {\left(-X\left(i\right)^{a} + 1\right)}^{b}} {\prod_{i=1}^{n} X\left(i\right)^{a}}}{X\left(n - 1\right) X\left(n - 2\right) X\left(n - 3\right) X\left(n - 4\right) X\left(n - 5\right) X\left(n - 6\right) X\left(n - 7\right) X\left(n - 8\right) X\left(n - 9\right) X\left(n\right) {\prod_{i=1}^{n} X\left(i\right)^{a} - 1} {\prod_{i=1}^{n} X\left(i - 10\right)}}$</p>
<p>I don't know how to deal with this expression, and it seems to me like it should be straight forward.</p>
<p>In case it's meaningful, after defining the expression, I will be differentiating it with respect to both $a$ and $b$.</p>
http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44920#post-id-44920@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? ThanksSun, 06 Jan 2019 15:27:14 -0600http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44920#post-id-44920Comment by galio for <p>I need to work symbolically with expressions such as this</p>
<p>$L(x; a,b) = \prod_{i=1}^n{abx_i^{a-1}(1-x_i^a)^{b-1}}$</p>
<p>where $x$ would be a random sample of size $n$.</p>
<p>I don't know of any way to express the indexing of the sample $x$ by each element...</p>
<p>The closest I got was defining the variables $n$ and $i$ and representing $x$ as a function</p>
<pre><code>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)
</code></pre>
<p>But this seems to inmediately assume that $X(i) = i$ and $L$ is represented as:</p>
<p>$-\frac{\left(-1\right)^{n} a^{n} b^{n} X\left(0\right) X\left(-1\right) X\left(-2\right) X\left(-3\right) X\left(-4\right) X\left(-5\right) X\left(-6\right) X\left(-7\right) X\left(-8\right) X\left(-9\right) {\prod_{i=1}^{n} {\left(-X\left(i\right)^{a} + 1\right)}^{b}} {\prod_{i=1}^{n} X\left(i\right)^{a}}}{X\left(n - 1\right) X\left(n - 2\right) X\left(n - 3\right) X\left(n - 4\right) X\left(n - 5\right) X\left(n - 6\right) X\left(n - 7\right) X\left(n - 8\right) X\left(n - 9\right) X\left(n\right) {\prod_{i=1}^{n} X\left(i\right)^{a} - 1} {\prod_{i=1}^{n} X\left(i - 10\right)}}$</p>
<p>I don't know how to deal with this expression, and it seems to me like it should be straight forward.</p>
<p>In case it's meaningful, after defining the expression, I will be differentiating it with respect to both $a$ and $b$.</p>
http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44919#post-id-44919That 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.Sun, 06 Jan 2019 15:22:01 -0600http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44919#post-id-44919Comment by nbruin for <p>I need to work symbolically with expressions such as this</p>
<p>$L(x; a,b) = \prod_{i=1}^n{abx_i^{a-1}(1-x_i^a)^{b-1}}$</p>
<p>where $x$ would be a random sample of size $n$.</p>
<p>I don't know of any way to express the indexing of the sample $x$ by each element...</p>
<p>The closest I got was defining the variables $n$ and $i$ and representing $x$ as a function</p>
<pre><code>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)
</code></pre>
<p>But this seems to inmediately assume that $X(i) = i$ and $L$ is represented as:</p>
<p>$-\frac{\left(-1\right)^{n} a^{n} b^{n} X\left(0\right) X\left(-1\right) X\left(-2\right) X\left(-3\right) X\left(-4\right) X\left(-5\right) X\left(-6\right) X\left(-7\right) X\left(-8\right) X\left(-9\right) {\prod_{i=1}^{n} {\left(-X\left(i\right)^{a} + 1\right)}^{b}} {\prod_{i=1}^{n} X\left(i\right)^{a}}}{X\left(n - 1\right) X\left(n - 2\right) X\left(n - 3\right) X\left(n - 4\right) X\left(n - 5\right) X\left(n - 6\right) X\left(n - 7\right) X\left(n - 8\right) X\left(n - 9\right) X\left(n\right) {\prod_{i=1}^{n} X\left(i\right)^{a} - 1} {\prod_{i=1}^{n} X\left(i - 10\right)}}$</p>
<p>I don't know how to deal with this expression, and it seems to me like it should be straight forward.</p>
<p>In case it's meaningful, after defining the expression, I will be differentiating it with respect to both $a$ and $b$.</p>
http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44917#post-id-44917Why 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
L = product(a*b*X(i)^(a-1)*(1-X(i)^a)^(b-1), i, 1, n)
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.Sun, 06 Jan 2019 15:14:52 -0600http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44917#post-id-44917Comment by rburing for <p>I need to work symbolically with expressions such as this</p>
<p>$L(x; a,b) = \prod_{i=1}^n{abx_i^{a-1}(1-x_i^a)^{b-1}}$</p>
<p>where $x$ would be a random sample of size $n$.</p>
<p>I don't know of any way to express the indexing of the sample $x$ by each element...</p>
<p>The closest I got was defining the variables $n$ and $i$ and representing $x$ as a function</p>
<pre><code>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)
</code></pre>
<p>But this seems to inmediately assume that $X(i) = i$ and $L$ is represented as:</p>
<p>$-\frac{\left(-1\right)^{n} a^{n} b^{n} X\left(0\right) X\left(-1\right) X\left(-2\right) X\left(-3\right) X\left(-4\right) X\left(-5\right) X\left(-6\right) X\left(-7\right) X\left(-8\right) X\left(-9\right) {\prod_{i=1}^{n} {\left(-X\left(i\right)^{a} + 1\right)}^{b}} {\prod_{i=1}^{n} X\left(i\right)^{a}}}{X\left(n - 1\right) X\left(n - 2\right) X\left(n - 3\right) X\left(n - 4\right) X\left(n - 5\right) X\left(n - 6\right) X\left(n - 7\right) X\left(n - 8\right) X\left(n - 9\right) X\left(n\right) {\prod_{i=1}^{n} X\left(i\right)^{a} - 1} {\prod_{i=1}^{n} X\left(i - 10\right)}}$</p>
<p>I don't know how to deal with this expression, and it seems to me like it should be straight forward.</p>
<p>In case it's meaningful, after defining the expression, I will be differentiating it with respect to both $a$ and $b$.</p>
http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44916#post-id-44916Very strange behavior. It seems to have something to do with the minus signs in the exponents. A workaround is to change variables: `a` to `a+1` and `b` to `b+1` (and hence `a>=0, b>=0`): `L = product((a+1)*(b+1)*X(i)^a*(1-X(i)^(a+1))^b, i, 1, n)` works fine.Sun, 06 Jan 2019 14:52:53 -0600http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44916#post-id-44916Answer by tmonteil for <p>I need to work symbolically with expressions such as this</p>
<p>$L(x; a,b) = \prod_{i=1}^n{abx_i^{a-1}(1-x_i^a)^{b-1}}$</p>
<p>where $x$ would be a random sample of size $n$.</p>
<p>I don't know of any way to express the indexing of the sample $x$ by each element...</p>
<p>The closest I got was defining the variables $n$ and $i$ and representing $x$ as a function</p>
<pre><code>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)
</code></pre>
<p>But this seems to inmediately assume that $X(i) = i$ and $L$ is represented as:</p>
<p>$-\frac{\left(-1\right)^{n} a^{n} b^{n} X\left(0\right) X\left(-1\right) X\left(-2\right) X\left(-3\right) X\left(-4\right) X\left(-5\right) X\left(-6\right) X\left(-7\right) X\left(-8\right) X\left(-9\right) {\prod_{i=1}^{n} {\left(-X\left(i\right)^{a} + 1\right)}^{b}} {\prod_{i=1}^{n} X\left(i\right)^{a}}}{X\left(n - 1\right) X\left(n - 2\right) X\left(n - 3\right) X\left(n - 4\right) X\left(n - 5\right) X\left(n - 6\right) X\left(n - 7\right) X\left(n - 8\right) X\left(n - 9\right) X\left(n\right) {\prod_{i=1}^{n} X\left(i\right)^{a} - 1} {\prod_{i=1}^{n} X\left(i - 10\right)}}$</p>
<p>I don't know how to deal with this expression, and it seems to me like it should be straight forward.</p>
<p>In case it's meaningful, after defining the expression, I will be differentiating it with respect to both $a$ and $b$.</p>
http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?answer=44912#post-id-44912I am not sure to understand your request, but if it is OK to work with `n` fixed, does the following help you ?
sage: n = 3
....: var('a','b')
....: assume(a>0,b>0)
....: X = [SR.var('X_{}'.format(i)) for i in range(n)]
....: L = prod(a*b*X[i]^(a-1)*(1-X[i]^a)^(b-1) for i in range(n))
sage: L
X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*b^3
sage: L.diff(a)
-X_0^(a - 1)*X_0^a*(-X_0^a + 1)^(b - 2)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*(b - 1)*b^3*log(X_0) - X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*X_1^a*(-X_1^a + 1)^(b - 2)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*(b - 1)*b^3*log(X_1) - X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*X_2^a*(-X_2^a + 1)^(b - 2)*a^3*(b - 1)*b^3*log(X_2) + X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*b^3*log(X_0) + X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*b^3*log(X_1) + X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*b^3*log(X_2) + 3*X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^2*b^3
Sun, 06 Jan 2019 08:15:37 -0600http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?answer=44912#post-id-44912Comment by galio for <p>I am not sure to understand your request, but if it is OK to work with <code>n</code> fixed, does the following help you ?</p>
<pre><code>sage: n = 3
....: var('a','b')
....: assume(a>0,b>0)
....: X = [SR.var('X_{}'.format(i)) for i in range(n)]
....: L = prod(a*b*X[i]^(a-1)*(1-X[i]^a)^(b-1) for i in range(n))
sage: L
X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*b^3
sage: L.diff(a)
-X_0^(a - 1)*X_0^a*(-X_0^a + 1)^(b - 2)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*(b - 1)*b^3*log(X_0) - X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*X_1^a*(-X_1^a + 1)^(b - 2)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*(b - 1)*b^3*log(X_1) - X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*X_2^a*(-X_2^a + 1)^(b - 2)*a^3*(b - 1)*b^3*log(X_2) + X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*b^3*log(X_0) + X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*b^3*log(X_1) + X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^3*b^3*log(X_2) + 3*X_0^(a - 1)*(-X_0^a + 1)^(b - 1)*X_1^(a - 1)*(-X_1^a + 1)^(b - 1)*X_2^(a - 1)*(-X_2^a + 1)^(b - 1)*a^2*b^3
</code></pre>
http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44913#post-id-44913No, x has variable length, so that's not really a possibility. It shouldn't be that hard to generalize this case. And even if it doesn't go straight to my problem I'd have guessed there should be a way to work with variable-length variables... Perhaps I'm thinking too much like a programmer.Sun, 06 Jan 2019 10:12:42 -0600http://ask.sagemath.org/question/44906/working-with-sumsproducts-of-lists/?comment=44913#post-id-44913