Suponga que tengo una función g(x) que deseo integrar
∫∞−∞g(x)dx.
Por supuesto, suponiendo que
g(x) va a cero en los puntos finales, sin ampliaciones, buena función. Una forma con la que he estado jugando es usar el algoritmo Metropolis-Hastings para generar una lista de muestras
x1,x2,…,xn partir de la distribución
proporcional a
g(x), al que le falta la constante de normalización
N=∫∞−∞g(x)dx
que llamaré
p(x) , y luego calcularé alguna estadística
f(x) en estas
x 's:
1n∑i=0nf(xi)≈∫∞−∞f(x)p(x)dx.
Como p(x)=g(x)/N , puedo sustituir en f(x)=U(x)/g(x) para cancelar g de la integral, lo que resulta en una expresión de la forma
Entonces, siempre queU(x) seintegre a1 a lolargo de esa región, debería obtener el resultado1/N, que podría tomar el recíproco para obtener la respuesta que quiero. Por lo tanto, podría tomar el rango de mi muestra (para usar los puntos de manera más efectiva)r=xmax-xminy dejarU(x)=1/rpara cada muestra que he dibujado. De esa maneraU
1N∫∞−∞U(x)g(x)g(x)dx=1N∫∞−∞U(x)dx.
U(x)11/Nr=xmax−xminU(x)=1/rU(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[U(x)g(x)]=1N≈1n∑i=0nU(x)g(x).
I tried testing this in R for the sample function g(x)=e−x2. 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:
1n(xmax−xmin)∑i=0n1e−x2i.
This should in my theory evaluate to
1/π−−√. 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 [−∞,∞]. The full specification of the function is:
U(x)={1xmax−xmin0xmax>x>xminotherwise.
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)=1π−−√e−x2.
However this would have made summing the individual samples trivial i.e.
1n∑i=0nP(x)g(x)=1n∑i=0ne−x2i/π−−√e−x2i=1n∑i=0n1π−−√=1π−−√.
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.
Respuestas:
This is a most interesting question, which relates to the issue of approximating a normalising constant of a densityg based on an MCMC output from the same density g . (A side remark is that the correct assumption to make is that g is integrable, going to zero at infinity is not sufficient.)
In my opinion, the most relevant entry on this topic in regard to your suggestion is a paper by Gelfand and Dey (1994, JRSS B), where the authors develop a very similar approach to find
Your idea of using the range of your sample(min(xi),max(xi)) and the uniform over that range is connected with the harmonic mean issue: this estimator does not have a variance if only because because of the exp{x2} appearing in the numerator (I suspect it could always be the case for an unbounded support!) and it thus converges very slowly to the normalising constant. For instance, if you rerun your code several times, you get very different numerical values after 10⁶ iterations. This means you cannot even trust the magnitude of the answer.
A generic fix to this infinite variance issue is to use forα a more concentrated density, using for instance the quartiles of your sample (q.25(xi),q.75(xi)) , because g then remains lower-bounded over this interval.
When adapting your code to this new density, the approximation is much closer to1/π−−√ :
We discuss this method in details in two papers with Darren Wraith and with Jean-Michel Marin.
fuente