Let's suppose to have a Multinormal distribution MultinormalDistribution[μ,Σ] . We can easily and very fast compute expectation values of the moments via the pre-implemented function Expectation.

I am wondering which is the most efficient way to compute quantities such $\int x^m \frac{\partial}{\partial x^n}f$. Intuitively I tried something like

pdf = PDF[f, {x1, x2}]
exp = Simplify[x1 * D[pdf, x1] /pdf]
Expectation[exp \[Distributed] f]

which seems although very slow. Nevertheless I do not think that evaluating the integral from $-\infty$ to $+\infty$ would be a good idea.

  • $\begingroup$ I'm not understanding why you're dividing by pdf. And the code presented results in an error when a specific distribution is given. $\endgroup$
    – JimB
    Commented May 1 at 22:45
  • $\begingroup$ @JimB I divide by the pdf since I want to get the result using Expectation. Suppose you want to evaluate only the integral of x*D[pdf,x]. Then I suppose that Expectation does something like evaluating the expectation value of what you give (so computing the integral of what you gives times f). So I think it does something like integral of x*D[pdf,x] * f. This is why I divide by the pdf. In such a way doing Expectation of x*D[pdf,x] / fgives you the integral from - infty yo +infty of x*D[pdf,x] $\endgroup$
    – JTT
    Commented May 2 at 7:17
  • $\begingroup$ @JimB I guess they're dividing by pdf since Expectation will multiply by the pdf again when doing the integral. $\endgroup$ Commented May 2 at 15:00
  • $\begingroup$ @SjoerdSmit Thanks. With your comment and the OP's response I see (and should have seen) that the OP wants the value of an integral which is not an "expectation" and just wanted to use Expectation as a means to an end. Your answer does exactly what the OP was asking for. $\endgroup$
    – JimB
    Commented May 2 at 15:37

3 Answers 3


(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)


(Format[#[n_]] := Subscript[#, n]) & /@ {μ, σ, x};

Assuming independence,

dist[n_Integer?Positive] := dist[n] =
   MultinormalDistribution[Array[μ, n],
    DiagonalMatrix[Array[σ, n]^2]];

pdf[n_Integer?Positive] := pdf[n] =
   Assuming[Thread[Array[σ, n] > 0],
    PDF[dist[n], Array[x, n]] // Simplify];

der[n_Integer?Positive] := der[n] =
  Assuming[Thread[Array[σ, n] > 0],
   Simplify[x[1]   D[pdf[n], x[1]]]]

The Expectation is

exp[n_Integer?Positive] := exp[n] =
  Assuming[Thread[Array[σ, n] > 0],
   Expectation[der[n], Array[x, n] \[Distributed] dist[n]] //

exp /@ Range[4]

enter image description here


This seems to work:

dist = BinormalDistribution[1/2]
pdf = PDF[dist, {x1, x2}]
exp = Simplify[x1*D[pdf, x1]/pdf]
Expectation[exp, {x1, x2} \[Distributed] dist]

Is that what you're after?

  • $\begingroup$ This is exactly what I am doing. This is efficient up to 2nd order derivative but starts becoming very slow afterwards $\endgroup$
    – JTT
    Commented May 3 at 8:18

One trick is to work with the equality

$$ \int g(x) \frac{\partial f(x)}{\partial x} dx = \int g(x) \frac{\partial \log f(x)}{\partial x} f(x) dx$$

So now you're just computing the expected value of $g(x)\frac{\partial \log f(x)}{\partial x}$, which you can approximate with a power series (if you want to use the series of moments)


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.