Mathematica Evaluating an integral in Mathematica, analytical form

Click For Summary
The discussion revolves around solving a complex integral using Mathematica and verifying the results obtained through manual calculations. The integral in question involves a multivariate Gaussian function, and the user has attempted to compute it using Mathematica but encountered difficulties in obtaining a simplified result. The initial command entered into Mathematica did not yield an error, but attempts to simplify or directly integrate the expression were unsuccessful. Further attempts included specifying the limits of integration from negative infinity to infinity, which eventually produced a more complex output that did not confirm the manual calculation. The user also noted a paste error involving the Greek letter pi, which could have contributed to the confusion in the results. A suggestion was made to avoid overly formatting expressions for desktop publishing, as this often leads to errors in Mathematica. The conversation highlights the importance of careful syntax and the potential pitfalls of formatting when using computational tools for mathematical verification.
fluidistic
Gold Member
Messages
3,931
Reaction score
281
So I've several huge integrals to solve by hands and I would like to check my result with Mathematica. However I don't really know how to do so.
Here is one of the several integrals I must solve:
\int \frac{1}{2\pi \sigma _x \sigma _y \sqrt{1-\rho ^2 }} \exp \{ \frac{-1}{2(1-\rho ^2)} \left ( \left ( \frac{x-\mu _x}{\sigma _x} \right ) ^2 +\left ( \frac{y-\mu _y}{\sigma _y} \right ) ^2 -\frac{2\rho (x-\mu _x)(y-\mu _y)}{\sigma _x \sigma _y} \right ] \} dx.
Solving by hands I reached \frac{1}{\sqrt{2\pi}\sigma _y} \exp \{ \left [ -\frac{1}{2 (1-\rho ^2)} \right ] \left [ \left ( \frac{y-\mu _y }{\sigma _y} \right ) ^2 +\left ( \frac{\mu _x }{\sigma _x} \right ) ^2 + \frac{2 \rho \mu _x y}{\sigma _x \sigma _y} +\frac{\mu _x}{2} +\frac{\rho \sigma _x (y-\mu _y)}{\sigma _y} \right ] \}.
I want to confirm this result via mathematica, I've tried the following command:
Code:
f[x_,y_]:=1/(2*\[Pi]*sigma_X*sigma_Y*Sqrt[1-rho^2])*E^(-1/(2(1-rho^2))*(((x-mu_X)/sigma_X)^2+((y-mu_Y)/sigma_Y)^2-(2*rho*(x-mu_X)*(y-mu_Y))/(sigma_X*sigma_Y)))
which produced no error.
Then
Code:
\[Integral]f[x,y]\[DifferentialD]x
returned \int \frac{e^{-\frac{\frac{(x-\text{mu$\_$X}){}^2}{\text{sigma$\_$X}{}^2}+\frac{(y-\text{mu$\_$Y}){}^2}{\text{sigma$\_$Y}{}^2}-\frac{2 \text{rho} (x-\text{mu$\_$X}) (y-\text{mu$\_$Y})}{\text{sigma$\_$X} \text{sigma$\_$Y}}}{2 \left(1-\text{rho}^2\right)}}}{2 \pi \sqrt{1-\text{rho}^2} \text{sigma$\_$X} \text{sigma$\_$Y}} \, dx
I've tried "Simplify", "FullSimplify" but the integral is never calculated.
Do you have any idea how to do so? Thanks!
 
Physics news on Phys.org
Does this verify your manual calculation?

In[1]:= Integrate[1/(2*π*sigmaX*sigmaY*Sqrt[1-rho^2])*E^(-1/(2(1-rho^2))*(((x-muX)/sigmaX)^2+((y-muY)/sigmaY)^2-(2*rho*(x-muX)*(y-muY))/(sigmaX*sigmaY))),x]

Out[1]= (Sqrt[-1 + rho^2]*Erfi[(muY*rho*sigmaX - muX*sigmaY + sigmaY*x - rho*sigmaX*y)/(Sqrt[2]*Sqrt[-1 + rho^2]*sigmaX*sigmaY)])/ (2*E^((muY - y)^2/(2*sigmaY^2))*Sqrt[2*Pi]*Sqrt[1 - rho^2]*sigmaY)
 
Bill Simpson said:
Does this verify your manual calculation?

In[1]:= Integrate[1/(2*π*sigmaX*sigmaY*Sqrt[1-rho^2])*E^(-1/(2(1-rho^2))*(((x-muX)/sigmaX)^2+((y-muY)/sigmaY)^2-(2*rho*(x-muX)*(y-muY))/(sigmaX*sigmaY))),x]

Out[1]= (Sqrt[-1 + rho^2]*Erfi[(muY*rho*sigmaX - muX*sigmaY + sigmaY*x - rho*sigmaX*y)/(Sqrt[2]*Sqrt[-1 + rho^2]*sigmaX*sigmaY)])/ (2*E^((muY - y)^2/(2*sigmaY^2))*Sqrt[2*Pi]*Sqrt[1 - rho^2]*sigmaY)
Hmm nice thank you, that's a way to integrate that works. I forgot to mention I did the integral from -\infty to \infty.
So I tried
Code:
Integrate[
 1/(2*\[Pi]*sigmaX*sigmaY*Sqrt[1 - rho^2])*
  E^(-1/(2 (1 - rho^2))*(((x - muX)/sigmaX)^2 + ((y - muY)/
          sigmaY)^2 - (2*rho*(x - muX)*(y - muY))/(sigmaX*
          sigmaY))), {x, -\[Infinity], \[Infinity]}]
which, after a long time, returned
Code:
If[Re[1/((-1 + rho^2) sigmaX^2)] < 0 && 
   Re[(-1 + rho^2) sigmaX^2] <= 0, 
  E^(-((muY - y)^2/(2 sigmaY^2))) Sqrt[2 \[Pi]]
    Sqrt[-(-1 + rho^2) sigmaX^2], 
  Integrate[
   E^(-(((muX - x)^2/sigmaX^2 - (2 rho (muX - x) (muY - y))/(
     sigmaX sigmaY) + (muY - y)^2/sigmaY^2)/(
    2 (1 - rho^2)))), {x, -\[Infinity], \[Infinity]}, 
   Assumptions -> 
    Re[1/((-1 + rho^2) sigmaX^2)] >= 0 || 
     Re[sigmaX^2 - rho^2 sigmaX^2] < 0]]/(2 \[Pi] Sqrt[1 - rho^2]
    sigmaX sigmaY)
. In all cases I don't think it confirms my result by hands so I guess I have made some errors.
 
If I make some hopefully reasonable assumptions, that you should verify, I get

In[1]:= Assuming[Element[rho,Reals]&& Element[sigmaX,Reals]&& -1<rho<1&&sigmaX≠0,
Integrate[1/(2*Pi*sigmaX*sigmaY*Sqrt[1-rho^2])*E^(-1/(2 (1-rho^2))*(((x-muX)/sigmaX)^2+((y-muY)/sigmaY)^2-(2*rho*(x-muX)*(y-muY))/(sigmaX*sigmaY))),{x,-Infinity,Infinity}]]

Out[1]= Abs[sigmaX]/(E^((muY - y)^2/(2*sigmaY^2))*Sqrt[2*Pi]*sigmaX*sigmaY)

Warning: If you look carefully at my previous post you will notice there is "Integrate[1/(2*π" and you might wonder where in the world I came up with "n"?? That is a scrape-n-paste error where the desktop published Greek Pi got mangled into an n in the process of going from the forum screen to the clipboard to Mathematica to the clipboard to the forum screen. This leads me to my next sentence.

One bit of general advice for using Mathematica, the more of the "I absolutely must desktop publish my every keystroke" Koolaid(tm) you have drunk, just like befor your first post, the more difficulties you will tend to have with Mathematica. In the beginning Mathematica was a calculation engine. Only much later did they try to glue on a veneer of desktop publishing and that almost never works. Again and again here we see posts "Why doesn't this work?" and the answer is often that they are confusing making the expression look like a textbook with making the expression something they can get a correct answer from. There are long lists of things that look like a textbook but don't work to get answers.

My apologies for not catching that. Hopefully there are no more errors in this. Damn. There was another of exactly the same error in this post which I have now manually fixed.
 
Last edited:

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K