Is this a bug?

99 views
Skip to first unread message

Mihai

unread,
Apr 16, 2020, 7:39:39 AM4/16/20
to sage-devel
Hi, I'm very new to Sage, thus it's likely I'm using it wrongly, but is this a bug?

Using:

I entered:

F(x)=1/(1 + exp(-x))
FN(x,n) = F(x)**n
assume(n, 'integer')
assume(log(exp(n)+1) - n > 0)
fn = diff(FN(x,n), x)
mean(n) = integral(fn(x, n), (x, 0, oo))
mean(1)

and I get:
1/((e + 1)*log(e + 1)^2 - 2*(e + 1)*log(e + 1) + e + 1)

The answer I would expect is:
1/2


Also, it seems the expression I get as answer could be more compactly formatted as:
1/(e + 1)/(log(e + 1) - 1)^2
but that's secondary, the main point being whether the result is correct.

Cheers,
Mihai

Michael Orlitzky

unread,
Apr 17, 2020, 9:56:03 PM4/17/20
to sage-...@googlegroups.com
On 4/16/20 7:24 AM, Mihai wrote:
> Hi, I'm very new to Sage, thus it's likely I'm using it wrongly, but is
> this a bug?
>
> Using:
> https://sagecell.sagemath.org/
>
> I entered:
>
> F(x)=1/(1 + exp(-x))
> FN(x,n) = F(x)**n
> assume(n, 'integer')
> assume(log(exp(n)+1) - n > 0)
> fn = diff(FN(x,n), x)
> mean(n) = integral(fn(x, n), (x, 0, oo))
> mean(1)
>
> and I get:
>
> 1/((e + 1)*log(e + 1)^2 - 2*(e + 1)*log(e + 1) + e + 1)
>
> The answer I would expect is:
> 1/2

This is how I would have done this integral, but my output raises only
more questions:

sage: x = SR.var('x',domain='real')
sage: n = SR.var('n',domain='integer')
sage: assume(n > 1)
sage: F = 1/(1 + exp(-x))
sage: mean = integral((F^n).diff(x), (x, 0, infinity))
sage: mean
-n*(1/(2^n*n) - 1/((W2490^(1/n) + 1)^n*n))
sage: mean(n=1)
1/(W2490 + 1) - 1/2

Note that the answer is what you expect if you set W2490 (whatever that
is, wherever it came from) to zero:

sage: mean(n=1,W2490=0)
1/2

Michael Orlitzky

unread,
Apr 17, 2020, 9:58:01 PM4/17/20
to sage-...@googlegroups.com
On 4/17/20 9:55 PM, Michael Orlitzky wrote:
> sage: assume(n > 1)

Oops, that should be assume(n > 0), but it doesn't change anything else.

Markus Wageringel

unread,
Apr 18, 2020, 4:00:50 AM4/18/20
to sage-devel
Am Donnerstag, 16. April 2020 13:39:39 UTC+2 schrieb Mihai:
fn = diff(FN(x,n), x)
mean(n) = integral(fn(x, n), (x, 0, oo))
 
The main problem with this is that `fn` is just an expression, not a function of `(x, n)`. The call `fn(x, n)` switches the position of x and n, as apparently the arguments of the expression are listed in alphabetical order.

sage: fn
n
*(1/(e^(-x) + 1))^(n - 1)*e^(-x)/(e^(-x) + 1)^2
sage
: fn.arguments()
(n, x)
sage
: fn(x, n)
x
*(e^(-n) + 1)^(-x + 1)*e^(-n)/(e^(-n) + 1)^2

With that in mind, replacing `fn(x, n)` by just `fn` in the integral leads to the output that Michael obtained, so apparently Sage (or Maxima) has difficulties to compute this integral symbolically for all n. However, if you replace n by an actual integer before integrating, you get better results:

sage: F(x)=1/(1 + exp(-x))
sage
: FN(x,n) = F(x)**n
sage
: fn = diff(FN(x,n), x)
sage
: mean = lambda k: integral(fn.subs({n: k}), (x, 0, oo))
sage
: [mean(k) for k in (1..10)]
[1/2, 3/4, 7/8, 15/16, 31/32, 63/64, 127/128, 255/256, 511/512, 1023/1024]

Mihai

unread,
Apr 18, 2020, 8:30:58 AM4/18/20
to sage-devel
Thank you, that explains the behavior.
- Mihai
Reply all
Reply to author
Forward
0 new messages