Skip to content
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

Update var.h #320

Merged
merged 8 commits into from
Jul 11, 2015
Merged

Update var.h #320

merged 8 commits into from
Jul 11, 2015

Conversation

MattPD
Copy link
Contributor

@MattPD MattPD commented Jul 6, 2015

Variance: Change from the unstable formula back to the stable (two-pass) formula.
Details: #223

Variance: Change from the unstable one-pass formula back to the stable two-pass formula.
Details: RcppCore#223
const double
average = mean(object).get(),
sum_squared_deviations = sum( pow(object - average, 2.0) ).get();
return sum_squared_deviations / (object.size() - 1);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Could you do the same for the complex case (next few lines) and also add ChangeLog entry in the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added a comment, it's indeed next in line in .plan :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this might have introduced a regression for IntegerVector, investigating.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the reason.

For IntegerVector temporary object - average has type Rcpp::sugar::Minus_Vector_Primitive<13, true, Rcpp::Vector<13, Rcpp::PreserveStorage>>.

In contrast, for NumericVector temporary object - average has type Rcpp::sugar::Minus_Vector_Primitive<14, true, Rcpp::Vector<14, Rcpp::PreserveStorage>>.

/* Magic numbers corresponding to:
13 INTSXP integer vectors
14 REALSXP numeric vectors
http://cran.r-project.org/doc/manuals/r-release/R-ints.pdf */

This results in invalid deviations calculation (thus resulting in invalid sum_squared_deviations).

Rewriting.

Edit: done, complex included; if everything seems in order can also proceed with the ChangeLog.

Make this safer for IntegerVector
(Feature) Add tests for complex variance computation (unimplemented in R -- thus, comparing against known synthetic cases).
(Refactoring) Introduce names in test cases replacing magic numbers (improves Travis CI reports readability), apply DRY to test data, robustify.
Implement variance for complex numbers in accordance with its definition.

Reference: https://en.wikipedia.org/wiki/Variance#Generalizations
@eddelbuettel
Copy link
Member

I think this one is good to go (though I'd like a ChangeLog and/or inst/NEWS.Rd entry too).

Any seconds, @kevinushey or @jjallaire ?

@MattPD
Copy link
Contributor Author

MattPD commented Jul 11, 2015

@eddelbuettel: sounds great; done & done! :-)

@eddelbuettel
Copy link
Member

Sorry: I fixed this in 384e7e4 but I hope that doesn't create a merge conflict for you now.

@MattPD
Copy link
Contributor Author

MattPD commented Jul 11, 2015

Right! Can I fix this on my side?

@eddelbuettel
Copy link
Member

I would think so by re-syncing your fork with what I have now.

@MattPD
Copy link
Contributor Author

MattPD commented Jul 11, 2015

Let's hope this works :-)

@eddelbuettel
Copy link
Member

Looks good! While I usually wait for either/both of @kevinushey and @jjallaire to nod, I think this one is good to go -- thanks for the contribution!

eddelbuettel added a commit that referenced this pull request Jul 11, 2015
@eddelbuettel eddelbuettel merged commit 0010c56 into RcppCore:master Jul 11, 2015
@MattPD
Copy link
Contributor Author

MattPD commented Jul 11, 2015

:-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants