Is it possible to get the KL divergence figure from a model fitted using variational bayes on RStan?

I believe that is `fit@sim$diaganostics$log_g__`

where `fit`

is produced by `vb`

.

Itâ€™s one part which is needed to compute KL, but you need also `log_p__`

and KL equation

Hi @avehtari and @bgoodri, thanks for the quick responses.

Using the notation here https://en.wikipedia.org/wiki/Kullbackâ€“Leibler_divergence, would the log_g__ refer to p(x) or q(x) in the KL equation?

Depends in which direction you want to compute that KL, and remember that it has log. The draws \theta^{(s)} used to compute `log_g__`

and `log_p__`

are from g(\theta) which is the approximate distribution. `log_q__`

and `log_p__`

are \log g(\theta^{(s)}) and \log p(\theta^{(s)}). Empirical estimate of KL from g to p (the one minimized by ADVI) would be then \frac{1}{S}\sum\left(\log g(\theta^{(s)})-\log p(\theta^{(s)}\right). This estimate is likely to have very high or infinite variance.

After studying the above, I would like to verify the following.

The \log p(\theta) densities used are not from the actual posterior densities, but rather, the posterior density up to a normalizing constant. This is since Stan usually has no way of knowing what the normalizing constants are from the likelihood alone.

Yes.

â€¦from the likelihood and prior alone.

Hi,

I have a model which has n distributions and I want them to be as different as possible from one another. So I want to maximize the KL divergence between them. Am I on the right track? Also, how do I maximize KL divergence in my model using stan?

Since VI computes the KL divergence up to a normalizing constant, it is meaningless to compare the KL divergence of 2 VI fits. You can consider using the R package philentropy to estimate this, or some other statistical test like the KS test

Could someone help me understand how to calculate the KL divergence from a Stanfit Object? Thank you very much!