Importance sampling - approximation of an integral in R So I am given this integral $$\mathrm{I}=\int_{38}^{\infty} \mathrm{f}(\mathrm{x}) \mathrm{d} \mathrm{x}=\int_{38}^{\infty} \mathrm{e}^{-\mathrm{x}} \mathrm{x}^{2} \mathrm{dx}$$ 
and i am also given this p.d.f $$g(x)=e^{-(x-38)}, \quad x \geq 38$$ from where i am supposed to sample from.
I have seen the formula of that inverse sampling is all about which is the following one:
$$\int h(x) \frac{\pi(x)}{g(x)} g(x) d x$$ .
I cannot understand how this can be applied to my integral above. I guess that h(x) if my original $\mathrm{e}^{-\mathrm{x}} \mathrm{x}^{2}$ function and that g(x) is where i sample from but what is $\pi(x)$ ?
How can I get an estimate of this? 
Here is my code in R so far
original_function <- function(x){exp(-x)*x^2}

#inverse sampling to sample from g(x)
u <- runif(1000, -1, 0)
inverse_function <- function(u){-log(-u)+38}
values_derived_from_inverse_CDF <- inverse_function(u)

#i use R's function integrate to calculate the integral 
integrate(original_function, 38, Inf)

#results
4.777715e-14 with absolute error < 1.3e-14



#I plug the X that came from g(x) to the original function in the numerator 
#and in the 
#denominator I plug in the values that derived from g(x). The results are not 
#even close.


mean(original_function(values_derived_from_inverse_CDF) / 
values_derived_from_inverse_CDF)

#results 
6.237939e-16

 A: Importance sampling approximates the integral $\mathbb E_{\pi(x)}[h(x)]$, so $\pi(x)$ is the original distribution. It makes sense if the original problem is to calculate an expected value of a function wrt distribution, $\pi(x)$. Instead of sampling from $\pi(x)$, we sample from a proposal distribution, $g(x)$ and calculate the expected value of the ratio $\frac{h(x)\pi(x)}{g(x)}$. In integral estimation, the actual $h(x)$ and $\pi(x)$ doesn't matter. You'll again find the expected value of the ratio above.
Your code needs the following change:
mean(original_function(values_derived_from_inverse_CDF) / exp(-values_derived_from_inverse_CDF+38))

You were averaging $\frac{h(x)\pi(x)}{x}$, but you should have averaged $\frac{h(x)\pi(x)}{g(x)}$.
A: There may be confusion between inverse (cdf) sampling which means simulating from a distribution with cdf G by taking the quantile transform of a uniform $$X=G^{-1}(U)\qquad U\sim\mathcal U(0,1)$$
and importance sampling which means replacing the expression of an expectation under a given distribution with pdf $f$ by an identical expectation under a different distribution with pdf $g$ :
$$\int h(x) \pi(x)\,\text{d} x= \int h(x) \frac{\pi(x)}{g(x)} g(x) \,\text{d} x$$
and its Monte Carlo approximation
$$\int h(x)\frac{\pi(x)}{g(x)} g(x) \,\text{d} x\approx \frac{1}{N} \sum_{i=1}^N \frac{h(x_i)\pi(x_i)}{g(x_i)}\qquad x_i\sim g(x)\quad i=1,\ldots,N$$
which can be rewritten as
$$\int h(x)\frac{\pi(x)}{g(x)} g(x) \,\text{d} x\approx \frac{1}{N} \sum_{i=1}^N \frac{h(G^{-1}(u_i))\pi(G^{-1}(u_i))}{g(G^{-1}(u_i))}\qquad u_i\sim \mathcal U(0,1)\quad i=1,\ldots,N$$
to merge inverse sampling and importance sampling. The correction in the R code is obvious.
A: In principle Monte Carlo works as follows for integration:
$$I=\int_a^bf(x)dx=\int_a^bf(x)\frac{b-a}{b-a}dx=(b-a)\int_a^bf(x)\pi(x)dx= (b-a)E[f(x)]$$ where $\pi(x)$ is PDF of uniform distribution on $[a,b]$. Therefore, we can approximate the integral by sampling from uniform:
$$I\approx \frac {b-a} n \sum_{i=1}^n f(\xi_i)$$ where $\xi\sim U(a,b)$
Now you can change the variable, but sampling from a different distribution with PDF $g(x)$:
$$I=(b-a)\int_a^bf(x)\frac{\pi(x)}{g(x)}g(x)dx= E[f(y)\frac{1}{g(y)}]$$
where $y$ is sampled from $g(y)$. 
Again, we can approximate the integral by sampling:
$$I\approx \frac 1 n \sum_{i=1}^n \frac{f(y_i)}{g(y_i)}$$ where $y_i$ is from distribution PDF $g(y)$
