# Integrating the derivative of a distribution via Expectation

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.

• I'm not understanding why you're dividing by pdf. And the code presented results in an error when a specific distribution is given.
– JimB
Commented May 1 at 22:45
• @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]
– JTT
Commented May 2 at 7:17
• @JimB I guess they're dividing by pdf since Expectation will multiply by the pdf again when doing the integral. Commented May 2 at 15:00
• @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.
– JimB
Commented May 2 at 15:37

\$Version

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

Clear["Global*"]

(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] =
PDF[dist[n], Array[x, n]] // Simplify];

der[n_Integer?Positive] := der[n] =
Simplify[x[1]   D[pdf[n], x[1]]]]

exp[n_Integer?Positive] := exp[n] =
Expectation[der[n], Array[x, n] \[Distributed] dist[n]] //
Simplify]

exp /@ Range[4]


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?

• This is exactly what I am doing. This is efficient up to 2nd order derivative but starts becoming very slow afterwards
– 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)