c.jallet at laposte.net
2016-Jun-29 09:55 UTC
[R] Understanding and predict round-off errors sign on simple functions
Hi, May be it is a basic thing but I would like to know if we can anticipate round-off errors sign. Here is an example : # numerical matrix m <- matrix(data=cbind(rnorm(10, 0), rnorm(10, 2), rnorm(10, 5)), nrow=10, ncol=3)> m[,1] [,2] [,3] [1,] 0.4816247 1.1973502 3.855641 [2,] -1.2174937 0.7356427 4.393279 [3,] 0.8504074 2.5286509 2.689196 [4,] 1.8048642 1.8580804 6.665237 [5,] -0.6749397 1.0944277 4.838608 [6,] 0.8252034 1.5595268 3.681695 [7,] 1.3002208 0.9582693 4.561577 [8,] 1.6950923 3.5677921 6.005078 [9,] 0.6509285 0.9025964 5.082288 [10,] -0.5676040 1.3281102 4.446451 #weird moving average of period 1 ! mma <- apply(m, 2, SMA, n=1)> mma[,1] [,2] [,3] [1,] NA NA NA [2,] -1.2174937 0.7356427 4.393279 [3,] 0.8504074 2.5286509 2.689196 [4,] 1.8048642 1.8580804 6.665237 [5,] -0.6749397 1.0944277 4.838608 [6,] 0.8252034 1.5595268 3.681695 [7,] 1.3002208 0.9582693 4.561577 [8,] 1.6950923 3.5677921 6.005078 [9,] 0.6509285 0.9025964 5.082288 [10,] -0.5676040 1.3281102 4.446451 #difference should be 0 but here is the result> m - mma[,1] [,2] [,3] [1,] NA NA NA [2,] 0.000000e+00 0.000000e+00 -8.881784e-16 [3,] 0.000000e+00 0.000000e+00 -8.881784e-16 [4,] 0.000000e+00 4.440892e-16 -8.881784e-16 [5,] -1.110223e-16 4.440892e-16 -8.881784e-16 [6,] -1.110223e-16 2.220446e-16 -4.440892e-16 [7,] -2.220446e-16 2.220446e-16 0.000000e+00 [8,] -2.220446e-16 0.000000e+00 0.000000e+00 [9,] -3.330669e-16 2.220446e-16 -8.881784e-16 [10,] -3.330669e-16 4.440892e-16 -8.881784e-16 SMA function use runMean # TTR / R / MovingAverages.R "SMA" <- function(x, n=10, ...) { # Simple Moving Average ma <- runMean( x, n ) if(!is.null(dim(ma))) { colnames(ma) <- "SMA" } return(ma) } Can anyone explain me that round error type? Is it possible to reproduce this same error generation in another language like C++ or C# ? Thanks in advance for your answers Regards Chris [[alternative HTML version deleted]]
Bert Gunter
2016-Jun-29 15:13 UTC
[R] Understanding and predict round-off errors sign on simple functions
I am certainly no expert, but I would assume that: 1. Roundoff errors depend on the exact numerical libraries and versions that are used, and so general language comparisons are impossible without that information; 2. Roundoff errors depend on the exact calculations being done and machine precision and are very complicated to determine So I would say the answer to your questions is no. But you should probably address such a question to a numerical analyst for an authoritative answer. Maybe try stats.stackexchange.com . -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Jun 29, 2016 at 2:55 AM, Sirhc via R-help <r-help at r-project.org> wrote:> Hi, > > > > May be it is a basic thing but I would like to know if we can anticipate > round-off errors sign. > > > > Here is an example : > > > > # numerical matrix > > m <- matrix(data=cbind(rnorm(10, 0), rnorm(10, 2), rnorm(10, 5)), nrow=10, > ncol=3) > > > >> m > > [,1] [,2] [,3] > > [1,] 0.4816247 1.1973502 3.855641 > > [2,] -1.2174937 0.7356427 4.393279 > > [3,] 0.8504074 2.5286509 2.689196 > > [4,] 1.8048642 1.8580804 6.665237 > > [5,] -0.6749397 1.0944277 4.838608 > > [6,] 0.8252034 1.5595268 3.681695 > > [7,] 1.3002208 0.9582693 4.561577 > > [8,] 1.6950923 3.5677921 6.005078 > > [9,] 0.6509285 0.9025964 5.082288 > > [10,] -0.5676040 1.3281102 4.446451 > > > > #weird moving average of period 1 ! > > mma <- apply(m, 2, SMA, n=1) > > > >> mma > > [,1] [,2] [,3] > > [1,] NA NA NA > > [2,] -1.2174937 0.7356427 4.393279 > > [3,] 0.8504074 2.5286509 2.689196 > > [4,] 1.8048642 1.8580804 6.665237 > > [5,] -0.6749397 1.0944277 4.838608 > > [6,] 0.8252034 1.5595268 3.681695 > > [7,] 1.3002208 0.9582693 4.561577 > > [8,] 1.6950923 3.5677921 6.005078 > > [9,] 0.6509285 0.9025964 5.082288 > > [10,] -0.5676040 1.3281102 4.446451 > > > > > > #difference should be 0 but here is the result > >> m - mma > > [,1] [,2] [,3] > > [1,] NA NA NA > > [2,] 0.000000e+00 0.000000e+00 -8.881784e-16 > > [3,] 0.000000e+00 0.000000e+00 -8.881784e-16 > > [4,] 0.000000e+00 4.440892e-16 -8.881784e-16 > > [5,] -1.110223e-16 4.440892e-16 -8.881784e-16 > > [6,] -1.110223e-16 2.220446e-16 -4.440892e-16 > > [7,] -2.220446e-16 2.220446e-16 0.000000e+00 > > [8,] -2.220446e-16 0.000000e+00 0.000000e+00 > > [9,] -3.330669e-16 2.220446e-16 -8.881784e-16 > > [10,] -3.330669e-16 4.440892e-16 -8.881784e-16 > > > > SMA function use runMean > > # TTR / R / MovingAverages.R > > "SMA" <- function(x, n=10, ...) { # Simple Moving Average > > ma <- runMean( x, n ) > > if(!is.null(dim(ma))) { > > colnames(ma) <- "SMA" > > } > > return(ma) > > } > > > > > > Can anyone explain me that round error type? > > Is it possible to reproduce this same error generation in another language > like C++ or C# ? > > > > Thanks in advance for your answers > > > > Regards > > > > Chris > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Marc Schwartz
2016-Jun-29 18:06 UTC
[R] Understanding and predict round-off errors sign on simple functions
Hi, Just to augment Bert's comments, I presume that you are aware of the relevant R FAQ: https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f That you had an expectation of the difference being 0 suggested to me that you might not be, but my apologies if that is not the case. That being said, there are some higher precision CRAN packages that may offer some additional functionality, with the potential limitations that Bert references below. More information is available in the Numerical Mathematics CRAN Task View: https://cran.r-project.org/web/views/NumericalMathematics.html In addition, with the caveat that I have not used it, there is the 'propagate' package on CRAN that may be relevant to what you want to be able to anticipate, at some level: https://cran.r-project.org/web/packages/propagate/index.html It has not been updated in a while and there are some notes for the CRAN package checks, that suggest that the maintainer may not be active at this point. Regards, Marc> On Jun 29, 2016, at 10:13 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote: > > I am certainly no expert, but I would assume that: > > 1. Roundoff errors depend on the exact numerical libraries and > versions that are used, and so general language comparisons are > impossible without that information; > > 2. Roundoff errors depend on the exact calculations being done and > machine precision and are very complicated to determine > > So I would say the answer to your questions is no. > > But you should probably address such a question to a numerical analyst > for an authoritative answer. Maybe try stats.stackexchange.com . > > -- Bert > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Wed, Jun 29, 2016 at 2:55 AM, Sirhc via R-help <r-help at r-project.org> wrote: >> Hi, >> >> >> >> May be it is a basic thing but I would like to know if we can anticipate >> round-off errors sign. >> >> >> >> Here is an example : >> >> >> >> # numerical matrix >> >> m <- matrix(data=cbind(rnorm(10, 0), rnorm(10, 2), rnorm(10, 5)), nrow=10, >> ncol=3) >> >> >> >>> m >> >> [,1] [,2] [,3] >> >> [1,] 0.4816247 1.1973502 3.855641 >> >> [2,] -1.2174937 0.7356427 4.393279 >> >> [3,] 0.8504074 2.5286509 2.689196 >> >> [4,] 1.8048642 1.8580804 6.665237 >> >> [5,] -0.6749397 1.0944277 4.838608 >> >> [6,] 0.8252034 1.5595268 3.681695 >> >> [7,] 1.3002208 0.9582693 4.561577 >> >> [8,] 1.6950923 3.5677921 6.005078 >> >> [9,] 0.6509285 0.9025964 5.082288 >> >> [10,] -0.5676040 1.3281102 4.446451 >> >> >> >> #weird moving average of period 1 ! >> >> mma <- apply(m, 2, SMA, n=1) >> >> >> >>> mma >> >> [,1] [,2] [,3] >> >> [1,] NA NA NA >> >> [2,] -1.2174937 0.7356427 4.393279 >> >> [3,] 0.8504074 2.5286509 2.689196 >> >> [4,] 1.8048642 1.8580804 6.665237 >> >> [5,] -0.6749397 1.0944277 4.838608 >> >> [6,] 0.8252034 1.5595268 3.681695 >> >> [7,] 1.3002208 0.9582693 4.561577 >> >> [8,] 1.6950923 3.5677921 6.005078 >> >> [9,] 0.6509285 0.9025964 5.082288 >> >> [10,] -0.5676040 1.3281102 4.446451 >> >> >> >> >> >> #difference should be 0 but here is the result >> >>> m - mma >> >> [,1] [,2] [,3] >> >> [1,] NA NA NA >> >> [2,] 0.000000e+00 0.000000e+00 -8.881784e-16 >> >> [3,] 0.000000e+00 0.000000e+00 -8.881784e-16 >> >> [4,] 0.000000e+00 4.440892e-16 -8.881784e-16 >> >> [5,] -1.110223e-16 4.440892e-16 -8.881784e-16 >> >> [6,] -1.110223e-16 2.220446e-16 -4.440892e-16 >> >> [7,] -2.220446e-16 2.220446e-16 0.000000e+00 >> >> [8,] -2.220446e-16 0.000000e+00 0.000000e+00 >> >> [9,] -3.330669e-16 2.220446e-16 -8.881784e-16 >> >> [10,] -3.330669e-16 4.440892e-16 -8.881784e-16 >> >> >> >> SMA function use runMean >> >> # TTR / R / MovingAverages.R >> >> "SMA" <- function(x, n=10, ...) { # Simple Moving Average >> >> ma <- runMean( x, n ) >> >> if(!is.null(dim(ma))) { >> >> colnames(ma) <- "SMA" >> >> } >> >> return(ma) >> >> } >> >> >> >> >> >> Can anyone explain me that round error type? >> >> Is it possible to reproduce this same error generation in another language >> like C++ or C# ? >> >> >> >> Thanks in advance for your answers >> >> >> >> Regards >> >> >> >> Chris
MacQueen, Don
2016-Jun-29 22:31 UTC
[R] Understanding and predict round-off errors sign on simple functions
For all practical purposes, the differences are zero. If you want them to also look like zero, try round( m - mma , 3) or signif( m - mma , 3) (or some number of digits other than three; I picked 3 rather arbitrarily) For anticipating the sign of these minuscule differences, I doubt there is a way (but I don't know why it would matter, either). If you want them all positive, use abs(round(m-mma,3)) or similar. Other programming languages will produce similar very small numbers, depending on how the calculation is done, but I would not expect exactly the same very small numbers. Finally, it would appear that your round-off errors are being generated inside the runMean function, and we don't know where that function comes from. It's not in base R. You could try x <- rnorm(1) ; x - runMean(x , 1) many times and see how often runMean does not return the value it was supplied with, and then study the definition of runMean to try to understand why it does not always return the value it was supplied with. This all assumes I've accurately read your example code and and reduced it to its core behavior. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 6/29/16, 2:55 AM, "R-help on behalf of Sirhc via R-help" <r-help-bounces at r-project.org on behalf of r-help at r-project.org> wrote:>Hi, > > > >May be it is a basic thing but I would like to know if we can anticipate >round-off errors sign. > > > >Here is an example : > > > ># numerical matrix > >m <- matrix(data=cbind(rnorm(10, 0), rnorm(10, 2), rnorm(10, 5)), nrow=10, >ncol=3) > > > >> m > > [,1] [,2] [,3] > >[1,] 0.4816247 1.1973502 3.855641 > >[2,] -1.2174937 0.7356427 4.393279 > >[3,] 0.8504074 2.5286509 2.689196 > >[4,] 1.8048642 1.8580804 6.665237 > >[5,] -0.6749397 1.0944277 4.838608 > >[6,] 0.8252034 1.5595268 3.681695 > >[7,] 1.3002208 0.9582693 4.561577 > >[8,] 1.6950923 3.5677921 6.005078 > >[9,] 0.6509285 0.9025964 5.082288 > >[10,] -0.5676040 1.3281102 4.446451 > > > >#weird moving average of period 1 ! > >mma <- apply(m, 2, SMA, n=1) > > > >> mma > > [,1] [,2] [,3] > >[1,] NA NA NA > >[2,] -1.2174937 0.7356427 4.393279 > >[3,] 0.8504074 2.5286509 2.689196 > >[4,] 1.8048642 1.8580804 6.665237 > >[5,] -0.6749397 1.0944277 4.838608 > >[6,] 0.8252034 1.5595268 3.681695 > >[7,] 1.3002208 0.9582693 4.561577 > >[8,] 1.6950923 3.5677921 6.005078 > >[9,] 0.6509285 0.9025964 5.082288 > >[10,] -0.5676040 1.3281102 4.446451 > > > > > >#difference should be 0 but here is the result > >> m - mma > > [,1] [,2] [,3] > >[1,] NA NA NA > >[2,] 0.000000e+00 0.000000e+00 -8.881784e-16 > >[3,] 0.000000e+00 0.000000e+00 -8.881784e-16 > >[4,] 0.000000e+00 4.440892e-16 -8.881784e-16 > >[5,] -1.110223e-16 4.440892e-16 -8.881784e-16 > >[6,] -1.110223e-16 2.220446e-16 -4.440892e-16 > >[7,] -2.220446e-16 2.220446e-16 0.000000e+00 > >[8,] -2.220446e-16 0.000000e+00 0.000000e+00 > >[9,] -3.330669e-16 2.220446e-16 -8.881784e-16 > >[10,] -3.330669e-16 4.440892e-16 -8.881784e-16 > > > >SMA function use runMean > ># TTR / R / MovingAverages.R > >"SMA" <- function(x, n=10, ...) { # Simple Moving Average > > ma <- runMean( x, n ) > > if(!is.null(dim(ma))) { > > colnames(ma) <- "SMA" > > } > > return(ma) > >} > > > > > >Can anyone explain me that round error type? > >Is it possible to reproduce this same error generation in another language >like C++ or C# ? > > > >Thanks in advance for your answers > > > >Regards > > > >Chris > > > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.