-
-
Notifications
You must be signed in to change notification settings - Fork 368
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
ragged array assignment (not declaration) #1369
Comments
Okay, it seems that
is a correct code comment for what the compiler interprets as a "match"; i.e. |
I'm still not convinced this is a good idea. I think it's Is the plan to throw out size matches altogether in Stan's When you say "compiler", do you mean Stan's or C++'s? Eigen
|
I think having functions are able to return an array of matrices would be a good thing, and I am not seeing another way to accomplish that right now. Possibly we could make a push to allow simultaneous declaration and definitions, in which case things wouldn't really be "resized" but just given different assignments from the outset. But we would still need heterogenous sizes. If we do resizing, it probably makes sense to only allow Eigen-style assignment if the lvalue has size 0 but it is I think more confusing to the user to have to write
than to write
although
would probably be better eventually. Although maybe when we go to C++, stanc could just use auto behind the scenes and then it could just be
The "compiler" thing that threw me was that the code comment was refering to Eigen's matching on Eigen::Dynamic whereas I was thinking in terms of algebra. |
That's on my near-term to-do list, but as you point out, it won't solve
Maybe we need an R list-like structure. The only
Agreed. So that would involve a change in the language The user-defined function definitions also use bare We have to think in terms of generality here, so a user matrix a[3]; to get an array of matrix[0,0], each of which would then We then have to think through what this does to things
I don't like that idea because I want to preserve static typing,
Ah, right. You can use fixed sizes > 1, but they get implemented
|
The closest thing to an R list is a std::tuple but then we would have to wait for C++11. I think if someone does
|
Boost has already implemented tuples. They tend to The problem I see is that if we have a list of size 100, Small tuples would be OK, though.
|
I think tuples would be useful eventually (and hopefully the C++11 version is easier to compile), but they are more general than what is needed in this case where the heterogenous dynamic-sized matrices are "homogenous" from Eigen's perspective. If
is equivalent to
it would work like user-defined functions and not disorient users much. I guess the danger is that someone acceidentally declares a 0x0 matrix and then assigns some other matrix to it like
But that might actually be the intended behavior and the user just meant for foo to me JxJ or something. |
What is the right Stan syntax to do promotion inside here:
I need to implement this locally for a conference paper. |
Is this in a user-defined function in Stan? It should For instance, if you write this real foo(real x, real y) { and you pass in data (double) x and parameter (var) y,
|
No, I meant in assign.hpp to allow .stan programs with std::vectors of Eigen matrices with different sizes:
|
Before going ahead with this, I'd like some feedback from matrix[2,3] y; or matrix[2,3] y; I'm less happy with the former, but if we allow the latter,
|
Do you need to do anything other than This is all going to be a bit confusing because we'll
|
Maybe. But check_matching_dims is called in several places besides goodrich@T540p:/opt/stan$ git grep -F -n "check_matching_dims" src/stan | and I didn't want to create problems if 0x0 matrices are passed to any of On Wed, Mar 18, 2015 at 8:00 PM, Bob Carpenter notifications@github.com
|
I agree that it seems hard to disallow the former. But a matrix[0,0] is currently not useful for anything substantive; it just On Wed, Mar 18, 2015 at 7:56 PM, Bob Carpenter notifications@github.com
|
Sorry for taking so long to catch up. First off, I disagree with allowing Regarding Rather than just saying no, my suggestion would be to create a new type, something called In short: I don't like |
This wouldn't limit the ability to use a size zero matrix as a placeholder I'm not sure what the code is almost ready to do with regards to error matrix[0,0] X; matrix X; // equivalent to matrix[0,0] X; matrix X; // equivalent to matrix[0,0] X; I don't think adding the Stan keyword dynamic would hurt anything but how On Fri, Mar 20, 2015 at 10:46 AM, Daniel Lee notifications@github.com
|
I agree that if a type is meant to be resizable On Mar 20, 2015, at 3:25 PM, bgoodri notifications@github.com wrote:
|
Do you mean, just for users to remind themselves that it is resizeable? resizeable matrix X; That's fine by me, but how would it be different on the C++ as matrix[0,0] X; On Fri, Mar 20, 2015 at 11:49 AM, Michael Betancourt <
|
Yeah, I’m just thinking about the users — the On Mar 20, 2015, at 3:58 PM, bgoodri notifications@github.com wrote:
|
If Bob wants to add a dynamic or resizeable keyword, go ahead. One concern On Fri, Mar 20, 2015 at 12:00 PM, Michael Betancourt <
|
Adding a no-op keyword is easy, but I don't think that's Right now, we allow size-zero vectors and matrices, they print So Ben's right that if we hack in a solution by allowing
Anything else is going to take more work, like adding I was thinking we'd have ragged arrays with declared array sizes. int Ms[K]; where a[k] is of size Ms[k] x Ns[k]. That's consistent with how
|
I think Bob's suggestion is what makes the most sense. I'm guessing will take a long time to figure out, so, let's talk about what we can do in the short term. @bgoodri, I still think allowing I know, I didn't come up with a decent counter-example, but perhaps something like:
That'll work if M is 0. Seems odd and error-prone to me. |
If we implement true ragged arrays, that would be better because it would On Wed, Mar 25, 2015 at 3:13 PM, Daniel Lee notifications@github.com
|
This project has to be a compromise. I don't like this Don't forget doc and testing. It's may be 4 lines of By the way, is this only for matrices, or also for vectors I think it'll actually require a fair amount of doc and testing. Maybe we can come up with an intermediate solution, such as having matrix A; And then only use this as a local variable. Or maybe in transformed Then when using that on the LHS of an array, we'd not check sizes.
|
If we are going to support genuine ragged arrays like matrix[rows,cols] X[elements]; where rows and cols are vectors, then I think we should allow that anywhere. If it is just the original hack where a 0x0 matrix or 0x1 vector can get More generally, I am not a fan of limiting language features that lots of transformed parameters { currently checks at the end of the block that X is, in fact, KxK. If so, I On Wed, Mar 25, 2015 at 7:05 PM, Bob Carpenter notifications@github.com
|
I agree about allowing declared ragged arrays anywhere. And I I strongly disagree about turning off size checks at the ends of Maybe we can discuss at the next Stan meeting?
|
If there were some way to turn checks off instead of taking them out of the transformed parameters { is infeasible and may very well fail the positive definiteness check for model { And if the "best" thing to do is to do something that is inconsistent with If someone defines a corr_matrix or cov_matrix in the transformed Ben On Wed, Mar 25, 2015 at 8:21 PM, Bob Carpenter notifications@github.com
|
I'm OK with being able to disable all checks if someone wants to do it.
You can't declare a corr_matrix in the model, but you could So if you define Sigma as a matrix[K,K] in the transformed parameters
Agreed. This seems like a problem with our enforcing positive
Where would it be OK to use a correlation matrix that doesn't pass
What I'm missing is how the code is correct if they're Is the problem just that we need a different test?
I'm confused, because this seems to be arguing that we should enforce We could go further and just terminate the run if we get a "divergent"
|
Ben, if you want us to turn off all checks, that isn't the hardest thing to If you're ok with that, we can go ahead and find a way to disable all
I have the same question: what should we do? |
On Wed, Mar 25, 2015 at 10:21 PM, Bob Carpenter notifications@github.com
It is a dilemma that is self-inflicted by the user who writes
So, the only way to get sound posterior draws is to only use For positive definiteness, there are ways of circumventing the check by transformed parameters { and export that single make_stuff function to R to use in posterior Ben |
@bgoodri, do you have an example of a positive definite matrix that is numerically indefinite? (I'm trying to search online for one, but can't find one easily) |
You can make one in Stan with something like L <- lkj_cholesky_corr_rng(K); On Wed, Mar 25, 2015 at 11:01 PM, Daniel Lee notifications@github.com
|
I've managed to get an example running and I see it's failing, but is that
an artifact of going to disk and back?
Could we have issues with:
- multiply_lower_tri_self_transpose?
- diag_pre_multiply?
or do you really believe those are solid enough to not produce numerically
unstable results?
There are too many moving parts, so I was hoping you could provide an
example that I could look at. (and hopefully it's smaller than the one I
generated?)
|
Well, it is that multiply_lower_tri_self_transpose produces numerically cov_matrix[2] Sigma; will underflow the off-diagonal element and result in a numerically On Thu, Mar 26, 2015 at 12:45 AM, Daniel Lee notifications@github.com
|
How about we add this Eigen::Matrix constructor by adding this to MatrixAddons.h
Then amend the Stan language to accept
and that parses using the range constructor
|
I think that looks great. Think we'd be able to declare matrix sizes using int indices[2]; Or would this just be available an array of matrices so the equivalent On Fri, Mar 27, 2015 at 5:51 PM, bgoodri notifications@github.com wrote:
|
I guess we could also support On Fri, Mar 27, 2015 at 6:05 PM, Daniel Lee notifications@github.com
|
I like the basic idea --- it's along the lines of what I've been I don't quite understand the details, though. I think I'd prefer to not use 2D arrays as implicit codings. int J; I'll have to think through whatever else is going to come up from this. I think we should just code this up directly in Stan rather than trying to
P.S. I'm struggling to just tread water with keeping existing things
|
On Fri, Mar 27, 2015 at 7:31 PM, Bob Carpenter notifications@github.com
Fine with me, but ...
No one else can make non-trivial changes to the Stan language, so I am only int J; Ben |
Daniel says he understands it enough to do so, but I don't know if I think we really need to agree on priorities. I just feel like
But your suggested change would also require changing the parser and
|
On Fri, Mar 27, 2015 at 7:49 PM, Bob Carpenter notifications@github.com
I don't know how many files would have to change, but I assumed the for (j in 1:J) validate(X[j]); in which case it seems like it should work the same whether the elements of Ragged arrays have been one of the most requested features since 2011. They
and probably a bunch of other use cases. If anyone else could have Ben |
I think you're right here, but I'd have to look and we'd need
Right --- we don't check that because we don't allow resizing
Stan 1.0 was only released August 2012! Adding line numbers to error messages was also on the list I did add user-defined functions last year and all the language If I could make more time to do things, I would. I really feel Right now, my priorities are two grant proposals and finishing the See my recently added longer-term to-do list on the stan-dev Wiki. I'm going to start by not answering any more issues on stan-users I also spend a lot of time trying to stay on top of issues like Having said that, I really don't want to keep throwing down hacks.
Mitzi's saying she'll try to take it on. First, we need a design
|
On Fri, Mar 27, 2015 at 8:25 PM, Bob Carpenter notifications@github.com
It was on the TODO list well before 1.0 https://groups.google.com/d/msg/stan-dev/TrvVDfQ9uMc/Yt9n0dbeSHwJ If Mitzi can get the ball rolling, that could make the difference. Ben |
From Ben Goodrich on stan-dev:
OK. I invented a new rule (locally): You are allowed to assign anything to a matrix with 0 rows and 0 columns. So, this now works:
But it would probably be better to support (outside of the data and parameters blocks)
rather than making users declare
I think we're good in that case in the sense that this runs so I guess the column vector gets promoted:
However, the comment in
src/stan/math/prim/mat/fun/assign.hpp
does not seem to match what the code does. I had to do trial and error to figure out that this was being called rather than the one above it that allegedly fires when the dimensions do not match.The text was updated successfully, but these errors were encountered: