Block Diagonals with gaussian_dlm_obs function

250 views
Skip to first unread message

Sam Weiss

unread,
Aug 9, 2014, 9:42:12 AM8/9/14
to stan-...@googlegroups.com
Hello,

I'm trying to run a DLM with block diagonals for W matrix. I intend for most of the off diagonal elements to be zero. However, when I attempt to declare these to be zero in transformed parameters section, the error comes out:

Invalid value of W: Error in function validate transformed params N4stan5agrad3varE: WW is not positive definite. W(0,0) is nan:0.Rejecting proposed initial value with zero density.


Is there a way to ensure certain elements in cov_matrix are zero?

Thanks
Sam


mod.dat=list(
  n=n,
  k=k,
  y=y,
  F=t(F),
  G=t(G),
  C0=C0,
  m0=m0,
  param=dim(G)[1]
  )



code="
data {
    int n;
    int k;
    int param;
    matrix[k, n] y;
    vector[param] m0;
    matrix[param,param] C0;
    matrix[param,k] F;
    matrix[param,param] G;

}
parameters {
    cov_matrix[k] V;
  }

transformed parameters {
  cov_matrix[param] W;
  for (k1 in 1:2)
    for (k2 in 3:param)
      W[k1,k2]<-0;
  for (k1 in 1:2)
    for (k2 in 3:param)
      W[k2,k1]<-0;
}

model {
  y ~ gaussian_dlm_obs(F, G, V, W,m0,C0);
  }
"

fit1 <- stan(model_code = code, data = mod.dat,  iter = 5, chains = 1)

Daniel Lee

unread,
Aug 9, 2014, 10:18:00 AM8/9/14
to stan-...@googlegroups.com
Hi Sam,

You've got a couple issues here. First, there's some misinterpretation of the error message. (Sorry -- we're still cleaning up the error reporting and hopefully it'll get clearer with future versions.)

---------


"Invalid value of W: Error in function validate transformed params N4stan5agrad3varE: WW is not positive definite. W(0,0) is nan:0.Rejecting proposed initial value with zero density."

The above error message is telling you that the W[1,1] element is nan (we've fixed the off-by-one indexing in the error messages and it'll be out next version). Transformed parameters, including every element in matrices, are initialized to nan. They need to be set before you use them.

If you take a look at your loops, k1 goes from 1:2 and k2 goes from 3:param. So, elements [1,1], [1,2], [2,1], [2,2] are never set. I'm not sure what you're intending to do with those elements.

----------

If you know your covariance matrix is diagonal, I think you've got the right idea, but you want to parameterize it differently. Perhaps you're interested in something like:

    ....
    parameters {
      cov_matrix[2] V;
      vector<lower=-1,upper=1>[param-3] covariance;
    }
    transformed parameters {
      cov_matrix[param] W;
      W <- rep_matrix(0, param, param);
      for (i in 1:2)
        for (j in 1:2)
          W[i,j] <- V[i,j];
      for (i in 3:param)
        W[i,i] <- covariance[i - 2];       
    }
    ...

Or... if they're really independent, there might be a better way to reparameterize your model as independent univariate components in a model as opposed to a multivariate model.







--
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.

Sam Weiss

unread,
Aug 10, 2014, 5:31:00 AM8/10/14
to stan-...@googlegroups.com
Hi Daniel,

With DLM's the covariance matrix W does have many elements that are set to zero, and are supposed to be independent. In this example, I'd like all elements to be zero except for [1,1],[1,2],[2,1],[2,2], [3,3], [4,3], [3,4], [4,4], [5,5] and [16,16]. 

Here's updated code that tries to follow your suggestion for transforming the parameters. However, It still gives errors that says:

"
Invalid value of W: Error in function validate transformed params N4stan5agrad3varE: WW is not positive definite. W(0,0) is 0.209502:0.Rejecting proposed initial value with zero density.
Initialization failed after 100 attempts.  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
error occurred during calling the sampler; sampling not done
"

Is my syntax correct?

Thanks
Sam

code="
data {
    int n;
    int k;
    int param;
    matrix[k, n] y;
    vector[param] m0;
    matrix[param,param] C0;
    matrix[param,k] F;
    matrix[param,param] G;

}
parameters {
    cov_matrix[k] V;
    cov_matrix[k] beta;
    cov_matrix[k] mu;
    vector<lower=0>[k] seasonal;
  }
transformed parameters{
  cov_matrix[param] W;
  W <- rep_matrix(0, param, param);
    
  for(i in 1:k)
  for(j in 1:k)
  W[i,j]<-beta[i,j];

  for(i in 1:k)
  for(j in 1:k)
  W[i+k,j+k]<-mu[i,j];

W[5,5]<-seasonal[1];
W[16,16]<-seasonal[2];

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/H0iaSlJRKRM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.

Michael Betancourt

unread,
Aug 10, 2014, 5:48:26 AM8/10/14
to stan-...@googlegroups.com
This isn’t a Stan problem — that matrix isn’t going to be positive-definite with zeroes
along the higher diagonal elements.

Sam Weiss

unread,
Aug 10, 2014, 6:13:56 AM8/10/14
to stan-...@googlegroups.com
Ok. Thank you.

Bob Carpenter

unread,
Aug 11, 2014, 4:51:36 PM8/11/14
to stan-...@googlegroups.com
And to answer the Stan question, it's not a syntactic issue, but
a semantic one. You probably don't want to parameterize like this:

> parameters {
>> cov_matrix[k] V;
>> cov_matrix[k] beta;
>> cov_matrix[k] mu;
>> vector<lower=0>[k] seasonal;
>> }
>> transformed parameters{
>> cov_matrix[param] W;
>> W <- rep_matrix(0, param, param);
>>
>> for(i in 1:k)
>> for(j in 1:k)
>> W[i,j]<-beta[i,j];
>>
>> for(i in 1:k)
>> for(j in 1:k)
>> W[i+k,j+k]<-mu[i,j];
>>
>> W[5,5]<-seasonal[1];
>> W[16,16]<-seasonal[2];

I don't know what k is, but it's problematic no matter what
it is.

The first two loops would be fine if param = 2 * k and you stopped
there. You'd get a block diagonal matrix made up out of covariance
matrices, so it'll still be positive definite.

But when you set W[5,5] and W[16,16], all bets are off (assumm

I'd suggest doing this the other way around, and setting
seasonal as a transformed parameter and setting

seasonal[1] <- W[5,5];
seasonal[2] <- W[16,16];

I also wouldn't recommend hard-coding numbers like 5 and 16 and
then using values like k. Either do everything relative to k or
hard-code k, too.

Unless gaussian_dlm_obs puts priors on the covariance matrices,
you'll also have improper priors (which is OK as long as
the posteriors are proper, but can cause computational difficulties
if estimates tend toward boundaries).

- Bob
Reply all
Reply to author
Forward
0 new messages