-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with sampling from Wishart distribution #247
Comments
I can confirm this unexpected behavior on linux. This appears to get worse as more correlation is present. E.g., using a diagonal matrix, the expected values of the diagonals are equal to the df. Using a matrix with a diagonal of 1, and covariances/correlations of .4, the expected values are way off, and in descending order. (e.g., 84, 27, 16, 12, 11, 10, 9, 9, 9, 8; when all should be 20). Is this an RcppArmadillo problem, or an Armadillo problem? |
We don't know at his point.
Can you guys look into this, please? |
We are looking into it, but also note that the unexpected behavior is not seen when using an identity matrix, or when the correlations are very small. If it was R I am thinking the problem would not depend on the kind of scale matrix. |
Good point -- agreed! Was just trying to be careful... |
This is a standalone program. It is indeed an Armadillo problem. We're using .4s here, but you can change it, compile/run it, and see in simOut.txt that the expected values of the diagonals are NOT the df, and are in descending order. They should be all approximately equal to the df. |
@eddelbuettel + @conradsnicta I can confirm a regression: From the issue ticket that incorporate the feature (#201), it seems like the diagonal isn't stable anymore. # Build the C++ function
Rcpp::cppFunction("arma::mat test_rwishart(arma::mat S, int df) {
return arma::wishrnd(S, df);
}", plugins = "cpp11", depends = "RcppArmadillo")
# Run through tests:
## Artificial
S = toeplitz((10:1)/10)
set.seed(11)
R = replicate(1000, test_rwishart(S, 20))
dim(R)
# 10 10 1000
mR = apply(R, 1:2, mean)
# ~= E[ Wish(S, 20) ] = 20 * S
stopifnot(all.equal(mR, 20*S, tolerance = .009))
# Error: mR and 20 * S are not equal:
# Mean relative difference: 0.5437535
## See Details, the variance is
Va = 20*(S^2 + tcrossprod(diag(S)))
vR = apply(R, 1:2, var)
stopifnot(all.equal(vR, Va, tolerance = 1/16))
# Error: vR and Va are not equal:
# Mean relative difference: 0.8660115 |
And thanks for carrying it over to GitLab where @conradsnicta is more likely to see it! |
Update. @conradsnicta is going to look into it when he has the time. My lab mate told me that it was not so easy to jump into the armadillo code and see readily all that is going on, and in particular, what might be causing this. Hopefully there solution soon. |
Looks like the latest push to Armadillo from @conradsnicta fixes the problem. |
Great -- and I just took that (well confined) commit and applied to the branch I just pushed (as I need to update the RNG settings for one test with Could you by chance test that? You could just drop in this file (and/or of course clone the repo / fetch the commit and build a local 0.9.200.7.1). Heck, I just put my tarball here . Feedback would be appreciated -- passes all tests so far. |
Glad to here. Is there a dev version of RcppArmadillo that I can install ? (could not locate this) |
~/tmp$ wget http://dirk.eddelbuettel.com/tmp/RcppArmadillo_0.9.200.7.1.tar.gz
--2019-03-07 14:29:36-- http://dirk.eddelbuettel.com/tmp/RcppArmadillo_0.9.200.7.1.tar.gz
Resolving dirk.eddelbuettel.com (dirk.eddelbuettel.com)... 73.120.251.142
Connecting to dirk.eddelbuettel.com (dirk.eddelbuettel.com)|73.120.251.142|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1253060 (1.2M) [application/x-gzip]
Saving to: ‘RcppArmadillo_0.9.200.7.1.tar.gz’
100%[======================================================>] 1,253,060 --.-K/s in 0.01s
2019-03-07 14:29:38 (110 MB/s) - ‘RcppArmadillo_0.9.200.7.1.tar.gz’ saved [1253060/1253060]
~/tmp$ Can please try again one of the redundant methods I provided, or kindly explain in more than ten words what you tried and what failed? |
I will give it a try, and report back to you. |
We used the bugfix/sample_test branch and confirmed it works. Thank you both for the update(s). |
Super, thanks so much for confirming. Should be on CRAN tomorrow, or, at the latest, by the weekend. |
Great ! Thank you for the excellent support. |
On CRAN now. Simulate away :) Thanks for the very helpful and detailed bug report, and of course to @conradsnicta for the quick fix. |
Hi:
I was wondering if there were changes made to the functions, as it does not seem to work for me as expected. More specifically, if taking the average of the diagonals everything appears OK, but each diagonal should be 20. This is note the case for the Rcpp implementation, and the values can be very far from the expected.
R solution
Rcpp solution
Finally, applying the stop if not all equal shows they are not equal as well.
Also note I posted this on a closed issue as well, but figured opening a new issue would be better.
Thanks !
The text was updated successfully, but these errors were encountered: