Ask Your Question

Alessandro Verzasconi's profile - activity

2017-12-06 15:42:13 -0500 received badge  Nice Question (source)
2017-11-16 04:46:53 -0500 commented question Is it normal, that we need to redeclare a variable inside a function?

Oh my, I'm so ashamed, you are right! Thanks a lot!

2017-11-16 03:54:37 -0500 asked a question Is it normal, that we need to redeclare a variable inside a function?

I defined this function

def Evaluatep1(y):
b = (y*(y+1))/2
d = Integerd(y)
print d
c=1
var('r')
for i in range(1,4):
    a = min(i*256-0.5,b)
    print("a = "),
    print a
    b = min(i*256+0.5,b)
    print("b = "),
    print b
    print integrate(1,r,a,b)

The output if I evaluate it in 33 is

sage: Evaluatep1(33)
3
a =  255.500000000000
b =  256.500000000000
1.0
a =  256.500000000000
b =  256.500000000000
0
a =  256.500000000000
b =  256.500000000000
0

, which is not correct, because the second integral, when i = 2, should also be 1.

Now I realised that if I added the line b= (y*(y+1))/2 inside the for-loop, everything worked fine, indeed with the function

def Evaluatep1(y):
b = (y*(y+1))/2
d = Integerd(y)
print d
c=1
var('r')
for i in range(1,4):
    b = (y*(y+1))/2
    a = min(i*256-0.5,b)
    print("a = "),
    print a
    b = min(i*256+0.5,b)
    print("b = "),
    print b
    print integrate(1,r,a,b)

The output is

sage: Evaluatep1(33)
3
a =  255.500000000000
b =  256.500000000000
1.0
a =  511.500000000000
b =  512.500000000000
1.0
a =  561
b =  561
0

Why do we need to redeclare the variable b inside the for-loop? With a program like Matlab, this is not necessary, is this a special feature of Sage, should I always redeclare variables when opening for,while,if-statements?

2017-11-15 07:10:21 -0500 commented answer Problem evaluating a very tiny integral

Thank you for your very well written answer!

2017-11-15 07:10:05 -0500 commented answer Problem evaluating a very tiny integral

Thank you for your answer, I really appreciate it! I knew the integral I posted above, was a very tiny one, it is actually a simpler version of the one I need to evaluate, because I tried to simplify the problem, in order to make it readable. I like both answers, but I accepted the other one, because it allows me to solve my problem using the converse error function, which I found being preciser than the error function itself. I would also like to thank mforets for his/her (?) hints.

2017-11-15 07:07:28 -0500 received badge  Scholar (source)
2017-11-15 07:07:26 -0500 received badge  Supporter (source)
2017-11-14 09:31:55 -0500 commented question Problem evaluating a very tiny integral

@mforets I don't know what you mean. If you tell Sage to compute the difference between erf(256.5) and erf(255.5), with the code print erf(256.5)-erf(255.5), it tells you that it is 0. Is there a way to make it more precise?

2017-11-14 01:42:58 -0500 received badge  Student (source)
2017-11-14 01:40:26 -0500 received badge  Editor (source)
2017-11-13 22:42:22 -0500 asked a question Problem evaluating a very tiny integral

Hi everyone,

I'm pretty new with sage. I was forced to change from MATLAB to Sage, because I was told Sage does approximate very tiny numbers better as it can work with sqrt(2) as sqrt(2) and not as the rational number approximating it.

Approximations are very important for my problem. I need to evaluate this integral $$\sum_{c=1}^{d}\int_{\min(256c-0.5,\frac{y(y+1)}{2})}^{\min(256c+0.5,\frac{y(y+1)}{2})}\frac{1}{\sqrt{2\pi}}e^{-\frac{x^2}{2}}\, dx$$

Suppose d = 1, then this is simply the integral

$$ \int_{255.5}^{256.5}\frac{1}{\sqrt{2\pi}}e^{-\frac{x^2}{2}}\, dx$$

To evaluate this integral I wrote the following code

T = RealDistribution('gaussian,1) print T.cum_distribution_function(256.5)-T.cum_distribution_function(255.5)

because the integral above is the same as the difference of the distribution function of a standard distributed gaussian random variable between the boundaries of the integral. However, and you can check yourselves if you don't believe it, the result I get with sage is 0.

I guess that this is due to some approximation, which sage does. Indeed the exact value of the integral is pretty close (and with pretty I mean a lot) to 0. My problem is that I need to be able to have the exact value, because the sum of integrals I'm working with and my whole work behind this integral requires me to be very careful with those little tiny numbers.

I tried to use the function

integrate

to deal with this problem, but something funny and apparently inexplicable happened when I was trying to use it.

To be precise I defined this code:

def VarianceOfvy(y):
temp1 = 0
temp2 = 0
for r in range(0,y+1):
    for x in range(0,r+1):
        temp1 = temp1 + (255/256)^x * 1/256 * (r-x)^2
for r in range(0,y+1):
    for x in range(0,r+1):
        temp2 = temp2 + ((255/256)^x * 1/256 * (r-x))^2
sigma = temp1 - temp2
return sqrt(sigma)

def Integerd(y):
b = y*(y+1)/2
d = 1
c = 0
while min((c+1)*256-0.5,b) == (c+1)*256-0.5:
    d = d+1
    c = c+1
return d

def Probabilityvynequiv(y):
var('c')
b = (y*(y+1))/2
sigma = 2
mu = 1
d = Integerd(y)
factor = 1/(integrate(1/sqrt(2*pi)*e^(-(x/sigma)^2/2),x,-oo,(b-mu)/sigma) - integrate(1/sqrt(2*pi)*e^(-(x)^2/2),x,-oo,(-mu)/sigma))
p = sum(factor*1/sigma*integrate(1/sqrt(2*pi)*e^((-x^2)/(2)),x,c*256+0.5,min((c+1)*256-0.5,b)),c,0,d)
return p

And if I let it run, the result I get is

1/2*(erf(255.75*sqrt(2)) - erf(128.25*sqrt(2)) + erf(127.75*sqrt(2)) - erf(0.25*sqrt(2)))/(3*erf(1/4*sqrt(2)) + 1)

which I assume is correct, and at least it tells me that Sage is able to read my code and output a result. If I call the function VarianceOfvy(2), the result I get is 3/65536*sqrt(11133895), which is also correct. Now, if I'm changing the command

sigma = 2

with

sigma = VarianceOfvy(2)

and try to let the whole program run again, Sage is not able anymore to output a result.

I'm really lost and I don't know what to do. Could someone advise me and give me some hints on how to evaluate those tiny integrals, in such a way that I don't loose any approximation?