1
$\begingroup$

I want to compute a numerical integral for a gaussian trivariate distribution. Say

NIntegrate[
  X* PDF[MultinormalDistribution[{0,0,1}, {{1, 1/10, 1/5}, {1/10, 1,1/10}, {1/5, 1/10, 1}}], {X, Y, Z}],
  {X, -Infinity, Infinity}, {Y, 0, Infinity}, {Z, -Infinity, -1}
]

(*Out: -0.00365287 *)

This is quite expensive to calculate, so I'd like to make use of parameterisations available for the Gaussian distribution. Specifically, the fact that:

$$ E[X\mid Y\geq y, Z\leq z]=E[E[X \mid Y,Z]\mid Y\geq y, Z\leq z]\\ =\beta_{XY;Z} E[Y\mid Y\geq y, Z\leq z]+\beta_{XZ;Y} E[Z \mid Y\geq y, Z\leq z] $$

where $\beta_{XY;Z}$ notes the regression coefficient of $X$ on $Y$ conditional on $Z$ (and $\beta_{XZ;Y}$ is analogously defined).

Applying this parameterisation to calculate the same integral from above gives

ρXY = 1/10;
ρXZ = 2/10;
ρYZ = 1/10;

βXYz = (ρXY - ρXZ*ρYZ)/(Sqrt[(1 - (ρXZ^2))] Sqrt[(1 - (ρYZ^2))]);
βXZy = (ρXZ - ρXY*ρYZ)/(Sqrt[(1 - ρXY^2)] Sqrt[(1 - ρYZ^2)]);

βXYz*NIntegrate[Y*PDF[BinormalDistribution[{0, 1}, {1, 1}, ρYZ], {Y, Z}], {Y, 0, Infinity}, {Z, -Infinity, -1}] 
+ 
βXZy*NIntegrate[Z*PDF[BinormalDistribution[{0, 1}, {1, 1}, ρYZ], {Y, Z}], {Y, 0, Infinity}, {Z, -Infinity, -1}]


(* Out: -0.00187282 *)

Is it possible that this discrepancy is coming from the precision of NIntegrate? Or am I just doing something wrong here?

NB. The two computational procedures coincide perfectly if ρXY= ρXZ= ρYZ


UPDATE: I am interested in speeding up the first integral, because what I would ultimately like to do is fix ρXY and ρXZ and calculate something like

   ContourPlot[ NIntegrate[
 X*PDF[MultinormalDistribution[{0,0,µ}, {{1, ρXY, ρXZ}, {ρXY, 1, ρYZ}, {ρXZ, ρYZ, 1}}], {X, Y, Z}], {X, -Infinity, Infinity}, {Y, a, Infinity}, {Z, -Infinity,b}],{µ,-2,2},{ρYZ,-.9,-9} ]

where the limits $a$ and $b$ are themselves numerical calculations (FindRoots). In other words, I would ideally like a symbolic expression of the first integral in terms of µ, ρYZ, $a$ and $b$

$\endgroup$
4
  • $\begingroup$ In your first NIntegrate expression you probably have a typo: the first Z variable range specification should actually refer to X. $\endgroup$
    – MarcoB
    Commented Mar 1, 2016 at 5:10
  • $\begingroup$ @MarcoB: Yes, it is an X. Thanks for pointing it out. The typo does not affect the question though$-$the typo was introduced when I was renaming variables for the OP question. The output corresponds to X in the first integration range. $\endgroup$
    – OO_SE
    Commented Mar 1, 2016 at 13:58
  • $\begingroup$ If it's just that one integral, it doesn't take that long, less than a second. Seems pointless to speed it up after you have gotten the answer. If it's just one of a family, then what is the family? This particular one has an exact, symbolic answer, (-((2 Erfc[Sqrt[2/11]/3])/E^2) + Erfc[(10 Sqrt[2/11])/3])/(20 Sqrt[2 \[Pi]]). Perhaps there is a good strategy for the family. $\endgroup$
    – Michael E2
    Commented Mar 1, 2016 at 14:24
  • $\begingroup$ @MichaelE2: Thanks for pointing it out. I've included an update to provide more detail as to why the parameterisation might help. Suggestions as to how to obtain symbolic answers to the complete problem would be great. I've tried "folding" the symbolic integral, but it's still not working... $\endgroup$
    – OO_SE
    Commented Mar 1, 2016 at 14:44

1 Answer 1

2
$\begingroup$

A one-time lengthy (one-minute) integral of the general integrand can reduce the dimension of NIntegrate by 2 to a fairly long expression (containing Erfi and Sqrt). Doing the Z integral symbolically seemed impossible. The dimension reduction speeds up NIntegrate by a factor of around 70 on my machine.

intXY = Integrate[
  X*PDF[MultinormalDistribution[{0, 0, μ},
     {{1, ρXY, ρXZ}, {ρXY, 1, ρYZ}, {ρXZ, ρYZ, 1}}], {X, Y, Z}],
  {Y, a, Infinity}, {X, -Infinity, Infinity}, 
  Assumptions -> {a, b, μ, ρXY, ρXZ, ρYZ} ∈ Reals, 
  GenerateConditions -> False]
(*  (E^(-(1/2) (Z - μ)^2) (<...>))/(4 Sqrt[2] π <...>)  *)

Check of OP's example: Something causes NIntegrate to use complex numbers, but the imaginary part comes out to be zero, which it should be. You can get twice the speed by applying Re to the integrand.

Block[{ρXY = 1/10, ρXZ = 2/10, ρYZ = 1/10,
   a = 0, b = -1, μ = 1},
 NIntegrate[Re@intXY, {Z, -Infinity, b}] // AbsoluteTiming]
(*  {0.014633, -0.00365287}  *)

No idea about appropriate parameters for the ContourPlot, but here goes:

Block[{ρXY, ρXZ, ρYZ, a, b, μ, NIntegrate},
  obj[
   ρXY_?NumericQ, ρXZ_?NumericQ, ρYZ_?NumericQ, a_?NumericQ, b_?NumericQ, μ_?NumericQ] = 
     NIntegrate[Re@intXY, {Z, -Infinity, b}];
  ];
ContourPlot[obj[1/10, 2/10, ρYZ, 0, -1, μ],
  {μ, -2, 2}, {ρYZ, -.9, .9}, 
  MaxRecursion -> 1] // AbsoluteTiming

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Thanks. Definitely improves. Can you comment on why the parameterisation yields different results? I guess I'm just curious... $\endgroup$
    – OO_SE
    Commented Mar 1, 2016 at 19:59
  • $\begingroup$ @EOO Are you sure the math is right? (I'd don't know off the top of my head.) When I integrate the first integral just over {X, -Infinity, Infinity}, the result is not equivalent to the linear combination of the other two integrands. I think it should be, since the limits of integration over Y and Z are the same. $\endgroup$
    – Michael E2
    Commented Mar 1, 2016 at 20:46
  • $\begingroup$ Hm. It seem mysterious to me. I'll check the maths again. Thanks! $\endgroup$
    – OO_SE
    Commented Mar 2, 2016 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.