What is the general formula for $\int h(x)$ if $h(x) = f(x)g(x)$?

The product rule is great for the derivatives, but I'm wondering if there is a similar general rule for integration when you have a function $$h(x)$$ that is the product of two functions that you know the integral for.

The context of this question is that I have a normal distribution and a linear function, and I want to know the integral of their product.

For example:

$$\text{NORMAL} = \mathcal{N}(100,12) = \frac{1}{\sqrt{2 \pi \cdot 12^2}}e^{-\frac{(x-100)^2}{2\cdot 12^2}}$$

$$\text{OTHER} = 12.95x + 26$$

I want the general way to find the integral of the product of two functions such as this.

A note on how this is being used, because it limits the types of solutions that are acceptable for my situation:

This formula is going to be implemented in a computer program, so all functions that I use must either be able to be evaluated with arithmetic, or have a convergent sum representation.

I have an implementation for the CDF for a normal distribution already created, so the integral of the normal function is something which I have available to use.

Here's an example of the limitations:

$$u = f(x | \mu,\sigma^2) = \frac{1}{\sqrt{2 \pi \cdot \sigma^2}}e^{-\frac{(x-\mu)^2}{2\cdot \sigma^2}}$$

$$v' = g(x|a,b) = ax + b$$

$$h(x) = f(x) \cdot g(x)$$

From integration by parts, we know that:

$$\int{uv' \,dx} = uv - \int{u'v \,dx}$$

So we know that:

$$v = \int{ax+b \,dx} = \frac{ax^2}{2} + bx + C$$

$$u' = \frac{(\mu - x)}{\sigma^2 \cdot \sqrt{2 \pi \cdot \sigma^2}}e^{-\frac{(x-\mu)^2}{2\cdot \sigma^2}}$$

Now if I use integration by parts, I end up with this:

$$\int h(x) = \frac{\frac{ax^2}{2}+bx + C}{\sqrt{2 \pi \cdot \sigma^2}}e^{-\frac{(x-\mu)^2}{2\cdot \sigma^2}} - \int{\frac{(\mu - x)(\frac{ax^2}{2}+bx + C)}{\sigma^2 \cdot \sqrt{2 \pi \cdot \sigma^2}}e^{-\frac{(x-\mu)^2}{2\cdot \sigma^2}} \,dx}$$

But the integral on the right is just as difficult to calculate, and doesn't conform to the existing $$\text{erf}$$ I have already implemented for the normal distribution.

If I use the opposite form:

$$v' = f(x | \mu,\sigma^2) = \frac{1}{\sqrt{2 \pi \cdot \sigma^2}}e^{-\frac{(x-\mu)^2}{2\cdot \sigma^2}}$$

$$u = g(x|a,b) = ax + b$$

$$v = \frac{1}{2}\left[1 + \operatorname{erf}\left( \frac{x-\mu}{\sigma\sqrt{2}}\right)\right]$$

$$u' = a$$

Then:

$$\int h(x) = \frac{ax+b}{2}\left[1 + \operatorname{erf}\left( \frac{x-\mu}{\sigma\sqrt{2}}\right)\right] - \int{\frac{a}{\sqrt{2 \pi \cdot \sigma^2}}e^{-\frac{(x-\mu)^2}{2\cdot \sigma^2}}}$$

Is certainly simpler, but runs into the same problem of calculating the integral on the right.

Note that $$a$$, $$b$$, $$\mu$$, and $$\sigma$$ may all have arbitrary values, so I can't simplify them out of the formula.

EDIT: I believe I've found a workable answer that satisfies my problem, but would appreciate it if someone who understands the math better could check it for me:

$$\int h(x) = \frac{ax+b}{2}\left[1 + \operatorname{erf}\left( \frac{x-\mu}{\sigma\sqrt{2}}\right)\right] - \frac{a}{2}\left[1 + \operatorname{erf}\left( \frac{x-\mu}{\sigma\sqrt{2}}\right)\right] + \frac{1}{2}\left[1 + \operatorname{erf}\left( \frac{x-\mu}{\sigma\sqrt{2}}\right)\right]$$

Simplified:

$$\int h(x) = \frac{ax-a+b+1}{2}\left[1 + \operatorname{erf}\left( \frac{x-\mu}{\sigma\sqrt{2}}\right)\right]$$

Or:

$$\int h(x) = (ax-a+b+1) \cdot \frac{1}{2}\left[1 + \operatorname{erf}\left( \frac{x-\mu}{\sigma\sqrt{2}}\right)\right]$$

Or:

$$\int h(x) = (ax-a+b+1) \cdot \Phi (x)$$

• There is integration by parts. – Arthur Nov 1 '19 at 20:58
• That would be challenging, considering that the derivative of the normal function is something I don't have an easy general solution to. I'm writing a computer program, so I'd need to simplify any equations I work with into a convergent sum or something that can be directly evaluated. – JRL Nov 1 '19 at 21:03
• Does "numeric integration" count as "something that can be directly evaluated"? That's usually the best way to tackle this in software if you need any level of generality - of course, if you're just integrating polynomials against the PDFs of common probability distributions (or tasks like that), you can just pre-compute the values you need and look them up at run time (e.g. store a table of moments of your distribution) – Milo Brandt Nov 1 '19 at 21:15
• I could do numeric integration if I really have to, however I'm attempting to build an implementation that allows for arbitrary precision in the answer, so numeric integration would be challenging to say the least. Also unfortunate is that the probability distributions have a wide variety of means and standard deviations, so precomputing those is not an option. – JRL Nov 1 '19 at 21:22

This is surely a duplicate in spirit, but it's apparently tricky to find a suitable question to redirect you to.

There is indeed an analogue of the product rule for integration: If $$u, v$$ are functions of $$x$$, we can write the usual product rule as $$(uv)' = u v' + u' v,$$ and integrating gives $$u v = \int u v' \,dx + \int u' v \,dx = \int u \,dv + \int v \,du$$ Rearranging gives the integration by parts formula: $$\int u v' \,dx = uv - \int u' v \,dx .$$ We can see that if we wish to apply this formula, we must in particular integrate one of the factors of the original product.

To integrate our particular function, however, we don't need to use integration by parts: By making an appropriate affine change of variable $$x \rightsquigarrow u$$, we can rewrite the product $$\frac{1}{\sqrt{2 \pi} \sigma} \exp\left[-\frac{(x - \mu)^2}{2 \sigma^2}\right] \cdot (a x + b)$$ as $$A u e^{-u^2} + B e^{-u^2}$$ for some constants $$A, B$$. The first term can be integrated via substitution, but the second term has no closed-form antiderivative; often we express that antiderivative in terms of the error function, $$\operatorname{erf}(u) = \frac{2}{\sqrt \pi} \int_0^u e^{-t^2} dt.$$

Putting this all together we find $$\begin{multline}\color{#bf0000}{\int \frac{1}{\sqrt{2 \pi} \sigma} \exp\left[-\frac{(x - \mu)^2}{2 \sigma^2}\right] \cdot (a x + b) \,dx} \\ \color{#bf0000}{= \frac{1}{2} (a \mu + b) \operatorname{erf} \left(\frac{x - \mu}{\sqrt{2} \sigma}\right) - \frac{a \sigma}{\sqrt{2 \pi}} \exp \left[- \frac{(x - \mu)^2}{2 \sigma^2}\right] + C} . \end{multline}$$ We can alternatively write the second term on the right-hand side in your notation as $$-a \sigma^2 f(x | \mu, \sigma^2)$$.

• I actually explored this option, however I ran into the issue that I'm using these solutions as part of a computer program. This limits the solutions I can use to things which have a convergent sum representation or can be directly evaluated. – JRL Nov 1 '19 at 21:12
• Perhaps you can be more specific (ideally in your question statement) about your constraints? There are many implementations of $\operatorname{erf}$. – Travis Willse Nov 1 '19 at 21:32
• I have an implementation of $\text{erf}$, the main problem is that the coefficients on the linear equation and the mean and standard deviation in the normal function will both change every time the application is run, so nothing can be pre-computed. I think the main blocker to using your answer for me is that I don't have a derivative for $\phi$ where $\mu$ and $\sigma$ are variable. – JRL Nov 1 '19 at 22:04
• Alright, please see the expanded notes and explanation given in my question. – JRL Nov 1 '19 at 22:52
• I've updated my answer to include an explicit formula for the antiderivative in terms of the given parameters. (In particular, the don't think the expression you give for the same in your edit to the question is quite right.) – Travis Willse Nov 3 '19 at 6:08

In the particular case of $$f(x)=u'(v(x))$$ and $$g(x)=v'(x)$$ we have

$$\int h(x)dx = \int f(x)g(x)dx=\int u'(v(x))v'(x)dx=u(v(x)) +C$$

and as a more general method from the product rule for derivative

$$[u(x)v(x)]'=u'(x)v(x)+u(x)v'(x)$$

we can show that the following holds

$$\int u'(x)v(x)dx =u(x)v(x) -\int u(x)v'(x)dx$$

which is the well known formula for integration by parts.