MATLAB: How to do a double integration of a multivariate normal probability density function

double integralmultivariate normal pdf

Hi,
I'm trying to do a double integral on a nultivariate normal probability density function. The two variables I want to integrate over is U and K. I created a function (fun) with the formula for multivariate normal pdf and used this to performe the double integral. When I do it like this I only get errors. Is there an easier approach?
My code:
clc; clear all; close all;
C = [23.875 15.75281; 15.75281 93.9842]; % C = [sigma_u^2 ro*sigma_u*sigma_k;...
ro*sigma_u*sigma_k sigma_k]
mu_u = 1788.2058;
mu_k = 70.8489;
mu = [mu_u;mu_k];
fun = @(U,K) ((1/sqrt(det(C)*(2*pi)^2))*exp(-0.5*transpose([U;K]-mu)*inv(C)*([U;K]-mu)));
q = integral2(fun,1700,1900,30,120);
Thanks!

Best Answer

If you have the statistic toolbox, you'll want to use MVNCDF for a multivariate normal distribution. However, in case not everything you want to do is multivariate normal, let's fix your current approach. INTEGRAL, INTEGRAL2, and INTEGRAL3 assume that they can send in many U and K values at once and that you will return an array of corresponding p values. Yours doesn't work with non-scalar inputs. Assuming p = fun(U,K) works properly for all scalar U and K in the desired range of inputs, you can make it work with arrays using ARRAYFUN.
q = integral2(@(U,K)arrayfun(fun,U,K),1700,1900,30,120);
ARRAYFUN just takes the array inputs and loops over the entries to construct an array output.