Assume I have a function $g(x)$ that I want to integrate $$ \int_{-\infty}^\infty g(x) dx.$$ Of course assuming $g(x)$ goes to zero at the endpoints, no blowups, nice function. One way that I've been fiddling with is to use the Metropolis-Hastings algorithm to generate a list of samples $x_1, x_2, \dots, x_n$ from the distribution proportional to $g(x)$, which is missing the normalization constant $$N = \int_{-\infty}^{\infty} g(x)dx $$ which I will call $p(x)$, and then computing some statistic $f(x)$ on these $x$'s: $$ \frac{1}{n} \sum_{i=0}^n f(x_i) \approx \int_{-\infty}^\infty f(x)p(x)dx.$$
Since $p(x) = g(x)/N$, I can substitute in $f(x) = U(x)/g(x)$ to cancel $g$ from the integral, resulting in an expression of the form $$ \frac{1}{N}\int_{-\infty}^{\infty}\frac{U(x)}{g(x)} g(x) dx = \frac{1}{N}\int_{-\infty}^\infty U(x) dx.$$ So provided that $U(x)$ integrates to $1$ along that region, I should get the result $1/N$, which I could just take the reciprocal to get the answer I want. Therefore I could take the range of my sample (to most effectively use the points) $r = x_\max - x_\min $ and let $U(x) = 1/r$ for each sample I've drawn. That way $U(x)$ evaluates to zero outside of the region where my samples aren't, but integrates to $1$ in that region. So if I now take the expected value, I should get: $$E\left [\frac{U(x)}{g(x)}\right ] = \frac{1}{N} \approx \frac{1}{n} \sum_{i=0}^n \frac{U(x)}{g(x)}. $$
I tried testing this in R for the sample function $g(x) = e^{-x^2}$. In this case I do not use Metropolis-Hastings to generate the samples but use the actual probabilities with rnorm
to generate samples (just to test). I do not quite get the results I am looking for. Basically the full expression of what I'd be calculating is:
$$\frac{1}{n(x_{\max} - x_\min)} \sum_{i=0}^n \frac{1}{ e^{-x_i^2}}. $$
This should in my theory evaluate to $1/\sqrt{\pi}$. It gets close but it certainly does not converge in the expected way, am I doing something wrong?
ys = rnorm(1000000, 0, 1/sqrt(2))
r = max(ys) - min(ys)
sum(sapply(ys, function(x) 1/( r * exp(-x^2))))/length(ys)
## evaluates to 0.6019741. 1/sqrt(pi) = 0.5641896
Edit for CliffAB
The reason I use the range is just to easily define a function that is non-zero over the region where my points are, but that integrates to $1$ on the range $[-\infty, \infty]$. The full specification of the function is: $$ U(x) = \begin{cases} \frac{1}{x_\max - x_\min} & x_\max > x > x_\min \\ 0 & \text{otherwise.} \end{cases} $$ I did not have to use $U(x)$ as this uniform density. I could have used some other density that integrated to $1$, for example the probability density $$ P(x) = \frac{1}{\sqrt{\pi}} e^{-x^2}.$$ However this would have made summing the individual samples trivial i.e. $$ \frac{1}{n} \sum_{i=0}^n \frac{P(x)}{g(x)} = \frac{1}{n} \sum_{i=0}^n \frac{e^{-x_i^2}/\sqrt{\pi}}{e^{-x_i^2} } = \frac{1}{n} \sum_{i=0}^n \frac{1}{\sqrt{\pi}} = \frac{1}{\sqrt{\pi}}.$$
I could try this technique for other distributions that integrate to $1$. However, I would still like to know why it doesn't work for a uniform distribution.