Has std() enough precision?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Has std() enough precision?

Diego Javier Zea
The returned value from std should be zero if all the values are equal (since the distance between the mean and the values should be zero). However, the returned value isn't zero:

julia> total
0.11008615848501174

julia
> mean(Float64[total for i in 1:100])
0.11008615848501173

julia
> std(Float64[total for i in 1:100])
1.3947701538996772e-17

julia
> std(Float64[total for i in 1:100]) zero(Float64)
false

julia
> mean(Float64[total for i in 1:100]) total
true


How can I get a more precise result from std()?

Best,

Reply | Threaded
Open this post in threaded view
|

Re: Has std() enough precision?

John Myles White
This is not an appropriate question for julia-dev.

 -- John

On Sep 7, 2015, at 1:30 AM, Diego Javier Zea <[hidden email]> wrote:

The returned value from std should be zero if all the values are equal (since the distance between the mean and the values should be zero). However, the returned value isn't zero:

julia> total
0.11008615848501174

julia> mean(Float64[total for i in 1:100])
0.11008615848501173

julia> std(Float64[total for i in 1:100])
1.3947701538996772e-17

julia> std(Float64[total for i in 1:100]) zero(Float64)
false

julia> mean(Float64[total for i in 1:100]) total
true


How can I get a more precise result from std()?

Best,


Reply | Threaded
Open this post in threaded view
|

Re: Has std() enough precision?

Diego Javier Zea
Sorry for that... I confused this list with the julia-users when I sent it (they are next to each other on my google groups). Should I delete it from this list and send the same to the other or is fine enough leaving it here?

2015-09-06 20:07 GMT-03:00 John Myles White <[hidden email]>:
This is not an appropriate question for julia-dev.

 -- John

On Sep 7, 2015, at 1:30 AM, Diego Javier Zea <[hidden email]> wrote:

The returned value from std should be zero if all the values are equal (since the distance between the mean and the values should be zero). However, the returned value isn't zero:

julia> total
0.11008615848501174

julia> mean(Float64[total for i in 1:100])
0.11008615848501173

julia> std(Float64[total for i in 1:100])
1.3947701538996772e-17

julia> std(Float64[total for i in 1:100]) zero(Float64)
false

julia> mean(Float64[total for i in 1:100]) total
true


How can I get a more precise result from std()?

Best,





--
Diego Javier Zea

Departamento de Ciencia y Tecnología
Universidad Nacional de Quilmes

Roque Saenz Pena 182
1876 Bernal
Argentina
Phone:+541143657100 ext 4135

Reply | Threaded
Open this post in threaded view
|

Re: Has std() enough precision?

Andreas Noack
John is right that this isn't the right list for asking the question, but here is an answer anyway.

It is only true that the standard deviation of a vector with identical entries is zero when there is no rounding errors. When adding elements of different magnitude you will eventually get a rounding error, e.g.

julia> err = 0.11008615848501174
0.11008615848501174

julia> err + err + err + err + err + err - 6err
-1.1102230246251565e-16

bacause the sum grows large relative to err. If you instead split the sum in smaller chunks and sum the chunks first, you can avoid the error

julia> (err + err + err) + (err + err + err) - 6err
0.0

This is actually how our sum works and it reduces the error growth in the accumulation, but it has a threshold for the number of elements that is too large to avoid the small error you see in your example.

In practice, the safe solution is to never rely on a floting point value being exactly zero. The value 1.3947701538996772e-17 you got should simply be considered zero in application.

On Mon, Sep 7, 2015 at 1:27 AM, Diego Javier Zea <[hidden email]> wrote:
Sorry for that... I confused this list with the julia-users when I sent it (they are next to each other on my google groups). Should I delete it from this list and send the same to the other or is fine enough leaving it here?

2015-09-06 20:07 GMT-03:00 John Myles White <[hidden email]>:
This is not an appropriate question for julia-dev.

 -- John

On Sep 7, 2015, at 1:30 AM, Diego Javier Zea <[hidden email]> wrote:

The returned value from std should be zero if all the values are equal (since the distance between the mean and the values should be zero). However, the returned value isn't zero:

julia> total
0.11008615848501174

julia> mean(Float64[total for i in 1:100])
0.11008615848501173

julia> std(Float64[total for i in 1:100])
1.3947701538996772e-17

julia> std(Float64[total for i in 1:100]) zero(Float64)
false

julia> mean(Float64[total for i in 1:100]) total
true


How can I get a more precise result from std()?

Best,





--
Diego Javier Zea

Departamento de Ciencia y Tecnología
Universidad Nacional de Quilmes

Roque Saenz Pena 182
1876 Bernal
Argentina
Phone:<a href="tel:%2B541143657100%20ext%204135" value="+541143657100" target="_blank">+541143657100 ext 4135