# Sparse matrix use cases (resolving declaration issues)

**URL:** <https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141>\
**Category:** Developers\
**Created:** [November 28, 2016, 11:13pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141 "2016-11-28T23:13:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [November 28, 2016, 11:13pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/1 "2016-11-28T23:13:47Z")

</div>

I’m trying to figure out what the use cases are for sparse  
matrices (not ragged data structures, not structured matrices  
like triangular or tri-diagonal or anything else) before trying  
to implement them.

So far, the only use case I can think of is to have a sparse  
data matrix X, a dense parameter matrix beta, and compute  
X \* beta. That’s what we already have hacked in with CSR.

What else are sparse matrices good for?

The problem I’m running into is that for parameters, transformed  
parameters, and generated quantities, we need to fix the sparsity  
pattern once and for all because Stan requires a fixed set of  
parameters for I/O. Are there cases where local variables  
or transformed data have varying sparsity over their scope?  
Function arguments should be no problem, because those don’t take sizes.

I’m imagining by analogy to matrices, e.g.,

matrix[4, 3] x;

I’d implement a sparse matrix the same way:

sparse\_matrix[4, 3] x;

But that won’t work for parameters because it doesn’t fix the  
sparsity pattern. Instead, I could take an array of non-zero  
indexes

int nz[J, 2];

that indicates J non-zero positions, then declare a sparse  
matrix like:

sparse\_matrix[M, N, nz];

to say that it’s M x N and the non-zero entries are as given in nz.  
Then we’d throw exceptions if anything is assigned to the  
zero positions or if any of the nz[j] are outside M x N.  
But that’d prohibit doing things like adding or multiplying sparse  
matrices unless we could compute the sparsity pattern ahead of time  
for the declaration.

- Bob

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [December 1, 2016, 3:42am UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/2 "2016-12-01T03:42:28Z")

</div>

> [@Bob\_Carpenter](#):
>
> What else are sparse matrices good for?

Kronecker products often involve sparse matrices.

> [@Bob\_Carpenter](#):
>
> The problem I’m running into is that for parameters, transformed parameters, and generated quantities, we need to fix the sparsity pattern once and for all because Stan requires a fixed set of parameters for I/O. Are there cases where local variables or transformed data have varying sparsity over their scope?

I doubt there are cases (that are amenable to HMC) where the sparseness pattern is not fixed. But there are cases where the sparsity pattern is not obvious, such as the matrix product of two sparse matrices. For the data block and parameters blocks, it seems like we have to specify the sparsity pattern, but for the other blocks it would be great if we could just let Eigen figure it out.

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [December 2, 2016, 3:26pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/3 "2016-12-02T15:26:33Z")

</div>

Dan Simpson would be a perfect person to tell about beauty of sparse matrices for Markov Random Fields etc., but it seems that he has no useful internet connection at Mongolia, so we have to wait him to get back.

Aki

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [July 17, 2017, 2:24am UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/4 "2017-07-17T02:24:18Z")

</div>

Hi all!

Let’s resurrect this thread (because apparently I think about this every eight months).

**Use cases and where we’re at**  
Now that @mitzimorris’s got the basic ICAR models working the first use case would be to re-implement the sparse inner product as a function native in Stan to reduce the size of the expression tree.

The next step would be to implement a multi\_normal\_prec that takes a sparse precision matrix. This matrix would, in general, depend on a parameter, so it would need a sparse specialisation of the appropriate reverse mode functions.

My job for this week is to write something similar (actually the helper function for forward mode) so that we can lose an INLA dependency in the ICAR case study. So I’m happy to have a shot at these.

This would allow @imadmali to implement the Leroux model (not [La Roux](https://www.youtube.com/watch?v=Kk8eJh4i8Lo) as I typed before I caught myself). It will also let Wakefield do a bunch of mmodels that he’s currently hand-rolling MCMC for.

The precision matrices in these cases look like Q = \sum\_{i=1}^k \phi\_i Q\_i for fixed sparse matrices Q\_i and potentially unknown parameters \phi\_i.

**Some interesting challenges**

(I’m going to assume through all of this that, while it will be exposed, the initial users will be rstanarm, as you’ll see below that these might be a beast

- From previous conversations, and now Mitzi’s experience, we can implement this without a specialised type by passing some vectors that define the sparse matrix. It’s not pretty, but it might be the easiest place to start. This would be similar to replacing the matrix type with 1 array and 2 (fixed) dimension parameters

- The only difference between dense and sparse matrices is that typically you _reorder_ sparse matrices rather than use them in their given order. This is because it reduces the computational cost of the algorithms massively. This means that the cholesky decomposition Q = LL’ no longer has lower-triangular matrix L. Instead L is a _permutation_ of a lower triangular matrix. Obviously we need to know this permutation to do solves etc. Thankfully this can be fixed throughout the problem, so maybe a function in the _transformed data_ block to compute the permutation from the sparsity pattern would would be enough to start with?

- If we make a proper sparse matrix class, we can do a few optimisation (as well as making a lot more pleasing syntactically). In particular, we can do the reordering in place and hold onto a _symbolic factorisation_, which is the first phase of a sparse cholesky that’s common to _any_ cholesky factorisation with the same sparsity structure. Given that we’ll be doing a few thousand of these suckers, the saving will be real.

- If you dig into Eigen’s SimplicialCholeskyBase you can see that they’re storing the pattern and the elimination tree (ie the symbolic factorisation). As these things are **not** parameter dependent, it seems to me that they should be kept. Does this fit with the logic of Stan? (ie you instantiate a sparseMatrix and it computes these two things and _keeps_ them until the object is destroyed?)

If you want to talk further, let me know. I’m also always happy to go south of the border to have a chat…

D

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [July 17, 2017, 11:38am UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/5 "2017-07-17T11:38:01Z")

</div>

You can use this pattern:

> [@Sparse double matrix times vector of vars](http://discourse.mc-stan.org/t/sparse-double-matrix-times-vector-of-vars/353/2):
>
> Here is an improved version of the C++ code #include \<Rcpp.h\> #include \<RcppEigen.h\> // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::export]] Eigen::VectorXd smv(int rows, int cols, const std::vector\<double\>& Values, const std::vector\<int\>& InnerIndices, const std::vector\<int\>& OuterStarts, const Eigen::VectorXd& beta) { Eigen::Map\<const Eigen::SparseMatrix\<double,Eigen::RowMajor\> \> sm(rows, cols, Values.size(), &OuterStarts[0], &InnerIndices[0], &Values[0]); // To do d…

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [August 1, 2017, 6:16pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/6 "2017-08-01T18:16:23Z")

</div>

> [@anon75146577](#):
>
> The next step would be to implement a multi\_normal\_prec that takes a sparse precision matrix. This matrix would, in general, depend on a parameter, so it would need a sparse specialisation of the appropriate reverse mode functions.

I missed the followup for this somehow.

Are you suggesting implementing a different `multi_normal_prec` for each form of parametric sparse precision matrix? Otherwise, wouldn’t we just autodiff through the parametric specification?

> [@anon75146577](#):
>
> we can do the reordering in place and hold onto a symbolic factorisation, which is the first phase of a sparse cholesky that’s common to any cholesky factorisation with the same sparsity structure.

That sounds super cool. Ben was urging us in the past to keep the factorization of a matrix cached, but we never figured out how to do that. The entire function/object design is stateless. The problem is that if you let users construct that matrix, how do you update the cached solutions as the values change? You’d need something stateful that kept track of whether the factorization is up to date. Not impossible. Especially if the sparse matrix class has a fixed sparsity pattern known at compile time. We’d need something like that for parameters anyway, but not in general.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [August 1, 2017, 6:25pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/7 "2017-08-01T18:25:41Z")

</div>

> [@Bob\_Carpenter](#):
>
> That sounds super cool. Ben was urging us in the past to keep the factorization of a matrix cached, but we never figured out how to do that. The entire function/object design is stateless. The problem is that if you let users construct that matrix, how do you update the cached solutions as the values change? You’d need something stateful that kept track of whether the factorization is up to date. Not impossible. Especially if the sparse matrix class has a fixed sparsity pattern known at compile time. We’d need something like that for parameters anyway, but not in general.

Have you looked at how Eigen implements its SparseMatrix class? (Or possibly its SimplicicalLDLT class). It keeps an indicator of whether or not the sparse matrix has a reordering, symbolic factorisation, and numerical factorisation as well as the appropriate things.

> [@Bob\_Carpenter](#):
>
> Are you suggesting implementing a different multi\_normal\_prec for each form of parametric sparse precision matrix? Otherwise, wouldn’t we just autodiff through the parametric specification?

The operations for the normal lpdf are the sparse specialisation of the inner product x’Qx and the log determinant sum\_i(log(L[i,i])). So, unless I’m missing my understanding, we need a reverse mode autodiff of the sparse cholesky and the sparse inner product for this to work.

Or am I _way_ off base?

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [August 1, 2017, 6:34pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/8 "2017-08-01T18:34:45Z")

</div>

> [@anon75146577](#):
>
> The operations for the normal lpdf are the sparse specialisation of the inner product x’Qx and the log determinant sum\_i(log(L[i,i])). So, unless I’m missing my understanding, we need a reverse mode autodiff of the sparse cholesky and the sparse inner product for this to work.
> 
> Or am I way off base?

I think that’s right. But that only autodiffs the normal, not through any parametric construction of the precision matrix. I think we’re on the same page.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [August 1, 2017, 6:38pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/9 "2017-08-01T18:38:33Z")

</div>

Yes I think so. The big challenge here is the implementation of the autodiff for the cholesky. It’s easy to do inefficiently (actually you can just use a variant of the old Smith/Giles code Rob implemented). It’s more delicate to actually do it properly. The engineering challenge is that it’s random access to L[i,j] is very expensive, so you’ve got to iterate through carefully. This is already done for computing the Cholesky in the SimplicalLDLT\_impl class, so all the bits needed to do it are there, it’s just taking the time to understand them and put them together.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [August 1, 2017, 6:40pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/10 "2017-08-01T18:40:50Z")

</div>

> [@anon75146577](#):
>
> The operations for the normal lpdf are the sparse specialisation of the inner product x’Qx and the log determinant sum\_i(log(L[i,i])). So, unless I’m missing my understanding, we need a reverse mode autodiff of the sparse cholesky and the sparse inner product for this to work.

We can do `y = Qx` for sparse `Q` and treat `y` as dense. At that point you can do `x'y` to get the value and you have `2y` as the derivatives with respect to `x`. So, I am not sure about the need for a sparse vector product, but yeah we would have to do some sparse factorization of `Q` to get its log determinant.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [August 14, 2017, 5:24pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/11 "2017-08-14T17:24:53Z")

</div>

> [@bgoodri](#):
>
> We can do y = Qx for sparse Q and treat y as dense. At that point you can do x’y to get the value and you have 2y as the derivatives with respect to x. So, I am not sure about the need for a sparse vector product, but yeah we would have to do some sparse factorization of Q to get its log determinant.

So @imadmali tried this out last week. It was slow. @rtrangucci suggested that it was a combination of:

- The copy that occurs when the transpose happens
- The lack of specialised autodiff for the matrix-vector product.

The second point can be fixed (I mean, it’s pretty easy!), but the first is always there, i.e. `dot_product(x,y)` is always faster than `x' * y`. So there’s a solid case for some quadratic form code.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [August 14, 2017, 5:29pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/12 "2017-08-14T17:29:15Z")

</div>

> [@anon75146577](#):
>
> The big challenge here is the implementation of the autodiff for the cholesky.

Ok. There’s one more thing that I didn’t think of about this. The basic problem is that at compile time, we don’t have easy access to the number of non-zero elements in the Cholesky factorisation and hence we know that we need a vector of vars but we don’t know how long that vector will be.

I don’t understand all of this enough to know if it will be a problem. Will we be saved by the fact that this vector of vars represents one node on the expression tree, or do we still need to know how many there are? Any thoughts @Bob_Carpenter?

If the latter is true, it’s easy enough to _compute_ the number of vars needed using a separate program. Would this be ok?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [August 14, 2017, 5:55pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/13 "2017-08-14T17:55:31Z")

</div>

> [@anon75146577](#):
>
> the first is always there, i.e. dot\_product(x,y) is always faster than x’ \* y.

Yes, but why not call `dot_product(x, y)`?

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [August 14, 2017, 5:57pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/14 "2017-08-14T17:57:34Z")

</div>

Good point.

@imadmali am I remembering correctly that

> y = csr\_matrix\_times\_vector(…, x);  
> result = dot\_product(x,y);

was still slower than Mitzi’s code?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [August 14, 2017, 6:00pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/15 "2017-08-14T18:00:17Z")

</div>

> [@Daniel\_Simpson](#):
>
> csr\_matrix\_times\_vector(…, x);

is slower than an abacus. This might be faster:

> [@Sparse double matrix times vector of vars](http://discourse.mc-stan.org/t/sparse-double-matrix-times-vector-of-vars/353/2):
>
> Here is an improved version of the C++ code #include \<Rcpp.h\> #include \<RcppEigen.h\> // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::export]] Eigen::VectorXd smv(int rows, int cols, const std::vector\<double\>& Values, const std::vector\<int\>& InnerIndices, const std::vector\<int\>& OuterStarts, const Eigen::VectorXd& beta) { Eigen::Map\<const Eigen::SparseMatrix\<double,Eigen::RowMajor\> \> sm(rows, cols, Values.size(), &OuterStarts[0], &InnerIndices[0], &Values[0]); // To do d…

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [August 14, 2017, 6:03pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/16 "2017-08-14T18:03:19Z")

</div>

Yes. That will be much faster (and is basically what I want to push through the language). Mainly because sparse matrix-vector product, and sparse quadratic form should be easy enough for even me to do the whole thing on relatively quickly.

Then we’re just a sparse cholesky (and an internal type) away from world domination.

---

<div class="post-metadata">

**Author:** ![imadmali](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/imadmali/32/3111_2.png) [@imadmali](https://discourse.mc-stan.org/u/imadmali)\
**Post date:** [August 14, 2017, 6:07pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/17 "2017-08-14T18:07:40Z")

</div>

@anon75146577 Yeah. It went something like pairwise difference was faster than the Cholesky decomposition version `dot_self(csr(L*phi)) - e * dot_self(phi)` which was moderately faster than `dot_product(phi, csr(Q,phi))`.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [August 15, 2017, 7:39pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/18 "2017-08-15T19:39:28Z")

</div>

> [@anon75146577](#):
>
> So there’s a solid case for some quadratic form code.

What function do you want specifically? A sparse version of `quad_form(y, Sigma) = y' * Sigma * y`?

> [@anon75146577](#):
>
> The basic problem is that at compile time, we don’t have easy access to the number of non-zero elements in the Cholesky factorisation and hence we know that we need a vector of vars but we don’t know how long that vector will be.

Yes, this will be a problem. Every Stan variable other than function arguments is declared with a static size. This is one of the serious issues with sparse arithmetic and the current Stan design.

> [@bgoodri](#):
>
> csr\_matrix\_times\_vector(…, x);
> 
> is slower than an abacus.

Then if it’s so critical, we should work on speeding it up. Is it slow in the way that all sparse matrix arithmetic is slow compared to dense arithmetic, or is it written in such a way that we could optimize somehow?

> [@anon75146577](#):
>
> Then we’re just a sparse cholesky (and an internal type) away from world domination.

If there’s one in Eigen and you can figure out how to pass it arguments using our current data types, it should be relatively easy to add. It’ll be faster if there’s a decent algorithm for the gradients.

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [August 15, 2017, 7:49pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/19 "2017-08-15T19:49:00Z")

</div>

> [@Bob\_Carpenter](#):
>
> Then if it’s so critical, we should work on speeding it up. Is it slow in the way that all sparse matrix arithmetic is slow compared to dense arithmetic, or is it written in such a way that we could optimize somehow?

This isn’t hard to do. Ben’s got partial code in the other thread. Passing it to eigen and actually coding a reverse autodiff ffor it isn’t hard and should make a difference.

If we’re happy with building row-major sparse matrices (not the Eigen default) it should be possible without changing the function signature.

> [@Bob\_Carpenter](#):
>
> If there’s one in Eigen and you can figure out how to pass it arguments using our current data types, it should be relatively easy to add. It’ll be faster if there’s a decent algorithm for the gradients.

The problem is that the number of non-zeros in the cholesky factor (ie the number of vars) is not easy to get at compile time (you have to write a different program to compute it). But once it’s computed it’s fixed.

This is **only** a problem for the Cholesky.

One possible way though this might be to code up the derivatives of `log_det(sparse)' and `solve( chol(Q)‘, vector)’ directly, which might be possible without computing the intimidate derivatives. (Although I’m not sure).

In that case, we’d just not specialise chol(Q) to vars.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [August 15, 2017, 8:20pm UTC](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141/20 "2017-08-15T20:20:43Z")

</div>

> [@anon75146577](#):
>
> The problem is that the number of non-zeros in the cholesky factor (ie the number of vars) is not easy to get at compile time (you have to write a different program to compute it). But once it’s computed it’s fixed.

OK, that may not be so bad. When I said all the sizes need to be declared statically, they won’t actually be instantiated to actual numbers until you see the data. So if that sizing calculation can happen with a Stan function call, it should be possible.

> [@anon75146577](#):
>
> without computing the intimidate derivatives.

All of those matrix derivatives intimidate me!

[Next page](https://discourse.mc-stan.org/t/sparse-matrix-use-cases-resolving-declaration-issues/141.md?page=2)
