(Moving to stan-dev.)
I think this also relates to the older question of being able to
inspect the length of an array and not having to specify it as a
separate variable. It'd be really nice to be able to write something
like
data {
int[int[]] x;
}
model {
for (i in 1:x.size)
for (j in 1:x[i].size)
x ~ normal(mu, 1);
}
to load an Rdump that looks like
x <-
list(c(1, 2, 3), 1, c(2, 8))
Not sure how high this goes on the to-do list, though.
Matt
On Mon, Oct 29, 2012 at 12:55 PM, Bob Carpenter <
ca...@alias-i.com> wrote:
> It would be nice. Does R let you specify ragged
> arrays some way?
>
> There's always the explicit DB-like coding of a ragged
> array using the indices. Suppose x is a ragged array
> of size I, where the entry for index i in 1:I is
> an array of size N[i].
>
> K | ii nn word
> ------------------
> 1 | 1 1 17
> 2 | 1 2 192
> 3 | 1 3 42
> ...
> 92| 1 92 598
> 93| 2 1 17
> 94| 2 2 3
> 95| 2 3 971
>
> The matrix x is determined by:
>
> x[ ii[k], nn[k] ] = word[k]
>
> If we assume the ii and nn are sorted as above, we can
> use an index structure to indicate where the entries
> for x[i,] start:
>
> I| kk
> -------
> 1| 1
> 2| 93
> ...
>
> So kk[i] is the entry where the entries for x[i,] start
> and size(x[i,]) in the ragged array is (kk[i+1] - kk[i])
> (assuming you pad one on the end).
>
> With this secondary kk structure, you can do away with the
> i and n indices and just keep the word array.
>
> For a start, it'd be easy to write a Stan operation
>
> ragged_array_of_arrays(word,k)
>
> that would take those word and start arrays and create a
> ragged array of arrays. The same kind of thing would work
> for a vector.
>
> But there's no good type for the result in Stan. We
> could always add something like:
>
> int x[I,];
>
> for a ragged array of arrays of ints.
>
> I also could imagine specifying a ragged array in Stan
> by providing its indexes.
>
> int<lower=0> N; // size of ragged array
> int<lower=0> x_sizes[N]; // size of each entry of ragged array
> ragged_array x[x_sizes]; // ragged array
>
> That's more than a bit clunky, but it would be
> workable given what we have now.
>
> Essentially x_sizes[N] is just the diff of kk with itself
> offset by one.
>
> - Bob
>
>
> On 10/29/12 2:56 PM, Matt Hoffman wrote:
>>
>> I'd personally love to have ragged arrays in Stan. I'm looking at
>> modeling large numbers of sequences, most of which are very short but
>> some of which are longish.
>>
>> Matt
>>
>> On Sun, Oct 28, 2012 at 6:39 PM, Ben Goodrich <
goodri...@gmail.com>
>> wrote:
>>>
>>> Actually, you would need to use max rather than fmax. Correct patch
>>> attached.
>>>
>>> Ben
>>>
>>>
>>> On Sunday, October 28, 2012 9:36:47 PM UTC-4, Ben Goodrich wrote:
>>>>
>>>>
>>>> Maybe the attached patch will make it work? If the patch doesn't apply
>>>> for
>>>> whatever reason, just change
>>>>
>>>>
>>>>
>>>> STAN_HOME/src/stan/prob/distributions/univariate/discrete/ordered_logistic.hpp
>>>>
>>>> so that the line that says
>>>>
>>>> int K = c.size() + 1;
>>>>
>>>> becomes
>>>>
>>>> int K = fmax(y);
>>>>
>>>> Ben
>>>>
>>>> On Sunday, October 28, 2012 9:12:33 PM UTC-4, tds151 wrote:
>>>>>
>>>>>
>>>>> the problem is in use of the ordered_logistic likelihood. the
>>>>> probability masses assigned to each observed category are keyed off of
>>>>> the
>>>>> length of the cutpoint vector (which we know is the number of
>>>>> categories
>>>>> minus one) so this isn't a case where a square matrix may be used in a
>>>>> fashion that allows the data to ignore some positions (and all will be
>>>>> well
>>>>> under a proper prior). if is the max number of cutpoints is 5 over all
>>>>> dimensions and dimension "d" has 3 categories, then I need something
>>>>> like,
>>>>> y[i,d] ~ ordered_logistic(c[i,d],cutpoints[d,1:2]), where "cutpoints"
>>>>> is a D
>>>>> x 5 matrix.
>>>>>
>>>>> On Sun, Oct 28, 2012 at 1:10 PM, Ben Goodrich <
goodri...@gmail.com>
>>>>> wrote:
>>>>>>
>>>>>>
>>>>>> Is it so bad to make all variables have the maximum number of
>>>>>> cutpoints?
>>>>>> I don't know for a fact that Stan would still work, but I hope it
>>>>>> would just
>>>>>> make the posterior distribution of the extra cutpoints for each
>>>>>> variable be
>>>>>> the same as the prior distribution.
>>>>>>
>>>>>> Ben
>>>>>>
>>>>>>
>>>>>> On Sunday, October 28, 2012 4:04:47 PM UTC-4, tds151 wrote:
>>>>>>>
>>>>>>>
>>>>>>> i'd really appreciate any advice on the forwarded topic, below, about
>>>>>>> dealing with a ragged array in stan. in my case, it is an array of
>>>>>>> cutpoints where the number of "rows" depends on the the column index.
>>>>>>>
>>>>>>> ---------- Forwarded message ----------
>>>>>>> From: terrance savitsky <
tds...@gmail.com>
>>>>>>> Date: Sat, Oct 27, 2012 at 7:43 PM
>>>>>>> Subject: employing a ragged array of cutpoints
>>>>>>> To:
stan-...@googlegroups.com
>>>>>>>
>>>>>>>
>>>>>>> I have an multvariate ordered outcome, y[N,D], where the number of
>>>>>>> categories depends on dimension d = 1, ..., D. I'd like to set an
>>>>>>> ordered_logistic likelihood for each y[i,d], where the length of each
>>>>>>> row of
>>>>>>> the cutpoint "matrix", gamma[d,] depends on "d". I know this is not
>>>>>>> literally possible, but i'm hoping there is a way to do so without
>>>>>>> brute
>>>>>>> force definition of distinct vector objects for each dimension, d,
>>>>>>> with no
>>>>>>> dimension index tie between them. What would make this go easily
>>>>>>> would be
>>>>>>> if I could define matrix[D,max(ncuts)] gamma and then use
>>>>>>> gamma[d,1:ncuts[d]] in the ordered_logistic statement, but I know we
>>>>>>> may not
>>>>>>> use partial indexing.
>>>>>>>
>>>>>>> Thanks for your help.
>>>>>>>
>>>>>>> --
>>>>>>> Thank you, Terrance Savitsky
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Thank you, Terrance Savitsky
>>>>>>
>>>>>>
>>>>>> --
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Thank you, Terrance Savitsky
>>>
>>>
>>> --
>>>
>>>
>>
>
> --
>
>