Parameter vector outer product

737 views
Skip to first unread message

John Reid

unread,
Sep 29, 2014, 6:01:28 AM9/29/14
to stan-...@googlegroups.com
Is it possible to create a matrix of the outer product between two vectors of parameters? I.e. something like?

parameters {                                                                                                                                                                                                      
    row_vector[M] colsizes;                                                                                                                                                                                       
    vector[N] rowrates;                                                                                                                                                                                           
}                                                                                                                                                                                                                 
transformed parameters {                                                                                                                                                                                          
    matrix[N,M] rates;                                                                                                                                                                                            
    rates <- rowrates * colsizes;                                                                                                                                                                                 
}

If it were possible would it be more efficient than looping in the model? That is:

model {
    for (n in 1:N) {
        for (m in 1:M) {
             rate[n,m] <- rowrates[n] * colsizes[m];
        }
    }
}

Thanks,
John.

Bob Carpenter

unread,
Sep 29, 2014, 2:19:28 PM9/29/14
to stan-...@googlegroups.com
Yes, it's possible. Check out the doc for operator* to see
what the possible arguments are. For products, we have both:

outer product:

matrix operator*(vector,row_vector)

inner product:

real operator*(row_vector,vector)

and both are written just as (a * b) with a and b of different
types. Basically, Stan figures out what the operation entails and
the tightest return type that can be inferred. That way, you can
just use matrix notation for everything.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

John Reid

unread,
Sep 30, 2014, 2:37:00 AM9/30/14
to stan-...@googlegroups.com
Great. I'll try to use the documentation better.
Thanks,
John.
Reply all
Reply to author
Forward
0 new messages