Skip to content

Conversation

@bgoodri
Copy link
Contributor

@bgoodri bgoodri commented Nov 2, 2021

fixes #534

@bgoodri bgoodri requested a review from jgabry November 2, 2021 16:58
@bgoodri
Copy link
Contributor Author

bgoodri commented Nov 2, 2021

I can't bring myself to merge this but if @jgabry does, then I'll live with it.

@jgabry
Copy link
Member

jgabry commented Nov 2, 2021

I can't bring myself to merge this but if @jgabry does, then I'll live with it.

Can you elaborate? I'm not sure what the pros and cons of this are, so not sure how to evaluate.

@bgoodri
Copy link
Contributor Author

bgoodri commented Nov 2, 2021 via email

@jgabry
Copy link
Member

jgabry commented Nov 2, 2021

Ok I think I follow most of that. If I understand you correctly, this part

Presumably, there is some way to construct X such that the columns are perfectly collinear but not numerically perfectly collinear enough to go through the code path that implements the new behavior and conversely some way to construct an X that is not perfectly collinear but is sufficiently numerically collinear to go through the branch that the code path that implements the new behavior.

means that this doesn't actually guarantee that Andrew gets the desired behavior when there's perfect collinearity and it also doesn't guarantee that when there's no collinearity we get exactly the same result as before this PR. Is that right? (If that's the case then I think we should probably discuss this more with Andrew because I don't think he's necessarily aware of these subtleties. I definitely wasn't aware until now.)

@jgabry
Copy link
Member

jgabry commented Nov 2, 2021

I'm gonna send an email to you and Andrew about this. I'm not entirely comfortable merging this yet (for the reasons you described). That said, I did just test it and it does work well (or at least it runs well) on the test case you included. So this seems good to go if we decide to use it.

@hsbadr
Copy link
Member

hsbadr commented Dec 23, 2021

The problem with this kind of change is that it needs to be maintained separately. For example, changes in Stan/Math or RcppEigen may affect the templates for CODOLS, such as the broken csr_matrix_times_vector2 with Stan ~v2.28. While this might be an easy task, I'd rather prefer to completely rely on Stan/Math here (and, BTW, remove csr_matrix_times_vector2 too by replacing it with csr_matrix_times_vector in the Stan code). Any required changes for performance would be better included in Math, probably with a simple interface to disable time-consumin checks, for example.

@bgoodri bgoodri merged commit 5a7715e into master Mar 14, 2022
@bgoodri bgoodri deleted the CODOLS branch March 14, 2022 18:40
@hsbadr
Copy link
Member

hsbadr commented Mar 15, 2022

@bgoodri FYI. With the development version of RStan + master branch of RStanARM:

In file included from stan_files/continuous.cc(3):
stan_files/continuous.hpp(6025): error: no instance of overloaded function "model_continuous_namespace::CODOLS" matches the argument list
            argument types are: (Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Map<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::Stride<0, 0>>, std::ostream *)
          stan::model::assign(OLS, CODOLS(X_, y, pstream__),
                                   ^
../inst/include/CODOLS.hpp(14): note: this candidate was rejected because at least one template argument could not be deduced
  CODOLS(const Eigen::Matrix<T2__, Eigen::Dynamic, Eigen::Dynamic>& X,
  ^
stan_files/continuous.hpp(3665): note: this candidate was rejected because at least one template argument could not be deduced
    CODOLS(const T0__& X_arg__, const T1__& y_arg__, std::ostream* pstream__) ;
    ^

In file included from stan_files/continuous.cc(3):
stan_files/continuous.hpp(6025): error: no instance of overloaded function "stan::model::assign" matches the argument list
            argument types are: (Eigen::Map<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::Stride<0, 0>>, <error-type>, const char [23])
          stan::model::assign(OLS, CODOLS(X_, y, pstream__),
          ^

#558 compiles successfuly.

@andrjohns
Copy link
Contributor

This is a pretty easy fix - the templates just need to be updated for Eigen expressions (and calling to_ref on the inputs and possibly returning via make_holder). Should I make the change or did you want to handle this Ben?

@bgoodri
Copy link
Contributor Author

bgoodri commented Mar 16, 2022 via email

@hsbadr
Copy link
Member

hsbadr commented Mar 16, 2022

Yes, fixed by b7b2579.

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.

Fix for perfect collinearity in linear regression

5 participants