X-Git-Url: https://git.auder.net/?p=morpheus.git;a=blobdiff_plain;f=pkg%2Ftests%2Ftestthat%2Ftest-Moments.R;fp=pkg%2Ftests%2Ftestthat%2Ftest-Moments.R;h=67a90c45b3ee0f086717d543d88392f8e36f9c2e;hp=0000000000000000000000000000000000000000;hb=2b3a6af5c55ac121405e3a8da721626ddf46b28b;hpb=2989133a738da99b78594cb00afdd3d87916b8bd diff --git a/pkg/tests/testthat/test-Moments.R b/pkg/tests/testthat/test-Moments.R new file mode 100644 index 0000000..67a90c4 --- /dev/null +++ b/pkg/tests/testthat/test-Moments.R @@ -0,0 +1,28 @@ +context("Moments") + +test_that("both versions of Moments_Mi agree on various inputs", +{ + for (n in c(20,200)) + { + for (d in c(2,10,20)) + { + E <- diag(d) + Id <- as.double(E) + X <- matrix( rnorm(n*d), nrow=n ) + Y <- rbinom(n, 1, .5) + M2 <- as.double(.Moments_M2(X,Y)) + M2_R <- colMeans(Y * t( apply(X, 1, function(Xi) Xi %o% Xi - Id) )) + expect_equal(max(abs(M2 - M2_R)), 0) + M3 <- as.double(.Moments_M3(X,Y)) + M3_R <- colMeans(Y * t( apply(X, 1, function(Xi) { + return (Xi %o% Xi %o% Xi - + Reduce('+', lapply(1:d, function(j) + as.double(Xi %o% E[j,] %o% E[j,])), rep(0, d*d*d)) - + Reduce('+', lapply(1:d, function(j) + as.double(E[j,] %o% Xi %o% E[j,])), rep(0, d*d*d)) - + Reduce('+', lapply(1:d, function(j) + as.double(E[j,] %o% E[j,] %o% Xi)), rep(0, d*d*d))) } ) )) + expect_equal(max(abs(M3 - M3_R)), 0) + } + } +})