# Multi_normal_lcdf?

Hi, I’m looking for a multivariate normal CDF function for censored multivariate normal observations. I.e. with observations (x_1, \ldots, x_k) I have

(X_1,\ldots, X_k) \sim \text{multi_normal}(\mu, \Sigma)

and I want to calculate

P(X_1 < x_1\text{ and }\ldots X_k < x_k)

But I don’t see a multi_normal_lcdf in the docs. Is there a reason for that? I think I could write multi_normal_lcdf in terms of normal_lcdf (c.f. this stackoverflow answer for the bivariate case). Is that the best way? Thanks.

Hey @potash!

Unfortunately, multi_normal_lcdf doesn’t exist in Stan. I’m working (slowly) on getting a bivariate normal cdf and lcdf into the math library. The algorithm is more complex than the stack overflow answer you linked to, we’re implementing something like the algorithm presented in section 2.2 here http://www.math.wsu.edu/faculty/genz/papers/bvnt.pdf

Higher dimensional (like d > 3 essentially) multi_normal_lcdf needs to be computed via MC integration. If you need something useful now I’d follow @bgoodri’s advice here: https://groups.google.com/d/msg/stan-users/GuWUJogum1o/LvxjlUBnBwAJ

Essentially, you’ll declare a parameter vector<lower=0,upper=1>[k] u[N] that you’ll be able to connect to your observations vector[k] x[N] with some clever inverse transform sampling laid out in that google groups post.

2 Likes

That technique is more commonly referred to as the GHK algorithm

Different G.

4 Likes

Got it. Thanks @rtrangucci and @bgoodri!

Hey @rtrangucci,

I need 16 dimensional normal CDF for my model. I’m very interested in the advice you linked (https://groups.google.com/d/msg/stan-users/GuWUJogum1o/LvxjlUBnBwAJ). That group chat is now private so I can’t see it. Could you please paste it here? Thanks.

The linked conversation was me asking for a truncated MVN, way back when. Ben provided the attached solutions.

tMVN.pdf (2.4 MB)
tMVN.stan (1.3 KB)

1 Like

Thanks, @aornugent.

I hate to necro an old thread, but I’ve been playing with Ben’s solution and while it works great to sample from a truncated normal, Ben suggests in the PDF you can use it to learn the parameters of the truncated normal, but that doesn’t work because you still need the normalizing constant of the truncation. You can prove this mathematically, but the easiest way to see it is to take the example and add priors to mu and Sigma, so that make_stuff specifies the conditional density of y | mu, Sigma. You’ll have to make the priors tight to prevent divergent transitions (I used K=2 with mu ~ N(0, 0.1), diag(Sigma) ~ N(1, 0.1), and assumed Sigma was diagonal). Then the marginal of mu is just its prior, but Stan will subtly sample from a slightly different distribution - in my examples you can only detect this with a large sample size, and it looks OK according to the naked eye, but a KS test will catch it.

Hi, @Matthew_Simpson, do you have the solution to correct for what you describe? Like math or stan code? Indeed when I sample from mu ~ std_normal(), the marginal of mu should be its prior but it’s not. I don’t observe any problem in the HMC diagnostics.

1 Like

There is no known solution @samlevy. You need the normalizing constant of the multivariate truncated normal as a function of mu and sigma, and this is simply not analytically tractable for 3+ dimensions. Or at least no one knows how to use NUTS/HMC in this case, AFAIK. If you manage to figure it out, you could have a nice publication - this would have applications to multinomial probit discrete choice models, for example.

What you can do is use NUTS/HMC for the latents conditional on the parameters (y|mu, sigma), and for the parameters conditional on the latents (mu, sigma | y), and construct a NUTS-within-Gibbs sampler in this way. But this is entering original research territory - e.g. I’m not sure how well the tuning algorithms Stan and other NUTS implementations use will work in the context of a Gibbs sampler, and I’m not sure if you can use Stan in this way.

1 Like

Maybe @spinkney has something to add to this last point by @Matthew_Simpson ?

I see. I was trying to use @bgoodri code to infer the mean and/or covariance of a truncated multivariate normal distribution. My ultimate goal was to use NUTS to sample from a constrained Gaussian process in which I write the covariance function as a joint kernel of the GP and its derivative GP, and imposing a certain number of constraints on the derivative GP part (to have a shape-constrained original GP) is akin to sampling from a large truncated multivariate normal. The parameters mu and the hyperparameters of the covariance function are unknown and should be inferred. But as far as I understand what you said, it’s not feasible, unless I place mu and cov as data and not as parameters.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9067966,"math_prob":0.7409487,"size":3538,"snap":"2022-05-2022-21","text_gpt3_token_len":822,"char_repetition_ratio":0.10837578,"word_repetition_ratio":0.006655574,"special_character_ratio":0.21763708,"punctuation_ratio":0.09916201,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95748526,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-16T22:59:15Z\",\"WARC-Record-ID\":\"<urn:uuid:6f93ff4d-b482-4bd7-a1e2-5932196406ce>\",\"Content-Length\":\"49818\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:61b06563-5e8d-4a22-8d6d-2535c7297182>\",\"WARC-Concurrent-To\":\"<urn:uuid:f4550ab1-4dbc-4fbd-b068-4ff4cc75a192>\",\"WARC-IP-Address\":\"64.62.250.111\",\"WARC-Target-URI\":\"https://discourse.mc-stan.org/t/multi-normal-lcdf/6652\",\"WARC-Payload-Digest\":\"sha1:ZPYDVW7EMLVNYXUORGDK7HE2QJ7L4354\",\"WARC-Block-Digest\":\"sha1:SAMLHPZYOOEDVO5ZBENSZBKWUUG6FR7N\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662512249.16_warc_CC-MAIN-20220516204516-20220516234516-00357.warc.gz\"}"}