edited title
BruceET
• 46.5k
• 2
• 27
• 73

colorlace
• 1k
• 10
• 25

# Numerically calculate the mean of a KDE (approximated by 2 vectors)

I have a 1-d kernel density estimate in the form of two vectors:
x_grid is a vector of x-values at which the density function was sampled
density is a vector of corresponding density values (estimated by a kernel density estimate) at each x value in the x_grid.

If I want the median value of this density function ($$m$$ where $$\int_{-\infty}^{m}{f(x) =0.5}$$), then I can approximate this by taking the integral (using a numerical technique such as Trapezoidal rule) from $$-\infty$$ to $$x_0, x_1, x_2, ...$$ until I get (close enough to) $$0.5$$.

Essentially, I'm just summing up the areas of trapezoids under the curve until I reach a cumulative sum of $$0.5$$.

How can I do something similar, but for the mean/expected value of the density function?