OK, I can see why you want a Kronecker product function after
having to write:
// MVCAR_prec <- Kronecker_product( D - A , MVCAR_scale );
temp <- (D - A);
for (a in 1:I){
for (b in 1:4){
rowNr <- (a-1)*4 +b;
for (c in 1:I){
for (d in 1:4){
colNr <-(c-1)*4 +d;
MVCAR_prec[rowNr,colNr] <- temp[a,c]*MVCAR_scale[b,d];
}}}}
f_star ~ multi_normal_prec( zeros, MVCAR_prec );
I didn't follow all the indexing closely, but after you construct
MVCAR_prec, you can check yourself if it's symmetric:
for (m in 1:rows(MVCAR_prec))
for (n in (m+1):cols(MVCAR_prec))
if (fabs(MVCAR_prec[m,n] - MVCAR_prec[n,m]) > 1e-8)
print("ASYMMETRY FOUND: m=", m, " n=", n);
If it's close but not exact, you can enforce symmetry by doing
this:
for (m in 1:rows(MVCAR_prec))
for (n in (m+1):cols(MVCAR_prec))
MVCAR_prec[m,n] <- MVCAR[n,m];
As to the second issue, you get a std::bad_alloc when the
runtime can't allocate enough memory. Can you check how your memory
grows as you run? I'm really hoping it's not a leak on our side!
One other issue is that you can vectorize (and simplify the code)
that looks like this:
HC1 ~ normal( HC_const[1] + HC_loading[1] * F[1] , HC_var[1]);
HC2 ~ normal( HC_const[2] + HC_loading[2] * F[1] , HC_var[2]);
HC3 ~ normal( HC_const[3] + HC_loading[3] * F[1] , HC_var[3]);
I'd rather see a loop with HC made into a 3D array of vectors,
for (i in 1:3)
HC[i] ~ normal(HC_const[i] + HC_loading[i] * F[1], HC_var[i]);
Although our code hasn't caught up with our designs, we plan to
vectorize further so that the above loop could be written as:
HC ~ normal(HC_const + HC_loading * F[1], HC_var);
You should get some feedback on those with more knowledge of stats
about this choice of prior:
HC_var ~ gamma(0.001, 0.001);
It may be pushing variances to zero a little too firmly.
Also note that the naming is off or you're missing a sqrt, because
Stan parameterizes basic normal() with standard deviation (i.e., scale),
not variance.
- Bob
On 4/29/13 3:09 PM, M.F. Jonker wrote:
> Hi Bob, Ben,
>
> With your help I've implemented the MVCAR distribution in two different models. One estimates age-specific mortality
> rates for a group of areas (e.g. neighborhoods in a city) and the other one correlates observed life expectancy with
> several area-specific characteristics. Both models compile, but apparently there is still something wrong.. One model
> throws a bad::alloc during runtime, and the other one throws an error that the covariance matrix is at some point no
> longer symmetric.. The latter doesn't happen with a smaller number of iterations. Apologies for such an open question,
> but do you perhaps have an idea what is going wrong? I really do not know where to look (anymore).
>
> Thanks,
> Marcel
>
> ------------------------------------------------------------------------------------------------------------------------
> From:
mfjo...@hotmail.com
> To:
stan-...@googlegroups.com
> Subject: RE: [stan-users] Re: GeoBUGS distributions
> Date: Thu, 25 Apr 2013 17:48:12 -0400
>
> Hi Ben,
>
> Having a "dense" Kronecker product would already be very convenient (!) On the other hand, sparse products are indeed an
> order of magnitude faster (especially with spatial distributions) and optimizing the order of the areas can further
> improve computation speed; see the attached graph of the pre- and post-optimized spatial matrix (using Matlab's -symrcm-
> function).
>
> Marcel
>
> ------------------------------------------------------------------------------------------------------------------------