Evaluating an integral in Mathematica, analytical form

Click For Summary

Discussion Overview

The discussion revolves around evaluating a complex integral using Mathematica, specifically focusing on a Gaussian-type integral involving parameters such as means, standard deviations, and correlation coefficients. Participants are sharing their attempts to confirm their manual calculations with Mathematica's output.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant presents a specific integral to verify against their manual calculation and shares their Mathematica command.
  • Another participant provides a Mathematica command that yields a different result, questioning whether it confirms the original manual calculation.
  • A third participant attempts to compute the integral over the entire real line and expresses uncertainty about the results aligning with their manual work.
  • A later reply introduces assumptions regarding the parameters and provides a new output from Mathematica, while also noting a previous error in the input due to formatting issues.
  • General advice is given about the challenges of using Mathematica for calculations that resemble textbook formatting, suggesting that clarity in input is crucial for obtaining correct results.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether the Mathematica outputs confirm the manual calculations, as various results are presented and some participants express uncertainty about their correctness.

Contextual Notes

There are noted formatting errors in the Mathematica commands that may affect the results, and assumptions about the parameters are introduced in later posts. The discussion highlights the complexity of integrating functions with multiple variables and parameters.

fluidistic
Gold Member
Messages
3,932
Reaction score
283
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 19 ·
Replies
19
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K