Let
- $(E,\mathcal E,\lambda)$ be a measure space
- $f:E\to[0,\infty)$ be $\mathcal E$-measurable
- $q$ be a probability density on $(E,\mathcal E,\lambda)$ with $$\{q=0\}\subseteq\{f=0\}\tag1$$
- $(\Omega,\mathcal A,\operatorname P)$ be a probability space
- $(X_n)_{n\in\mathbb N}$ be an $(E,\mathcal E)$-valued independent process on $(\Omega,\mathcal A,\operatorname P)$ with $$X_n\sim q\lambda\;\;\;\text{for all }n\in\mathbb N\tag2$$
- $B\in\mathcal E$ with $$c:=(q\lambda)(B)\in(0,1)\tag3$$
- $\tau_0:=0$, $$\tau_k:=\inf\left\{n>\tau_{k-1}:X_n\in B\right\}\;\;\;\text{for }k\in\mathbb N$$ and $$Y_k:=X_{\tau_k}\;\;\;\text{for }k\in\mathbb N$$
Note that $(Y_k)_{k\in\mathbb N}$ is independent with $$Y_n\sim (q\lambda)\left[\;\cdot\mid B\right]\;\;\;\text{for all }k\in\mathbb N\tag4.$$ Let $$g(x):=1_B(x)\left.\begin{cases}\displaystyle\frac fq(x)&\text{, if }q(x)>0\\0&\text{, otherwise}\end{cases}\right\}\;\;\;\text{for }x\in E.$$ Note that $$\frac1k\sum_{i=1}^kg(Y_i)\xrightarrow{k\to\infty}(q\lambda)\left[g\mid B\right]=\frac1c\int_Bf\:{\rm d}\lambda\;\;\;\text{almost surely}.\tag5$$
Say we want to use $(Y_k)_{k\in\mathbb N}$ to estimate $$I:=\int_Bf\:{\rm d}\lambda.$$ In practice, I'm independently drawing $X_1,\ldots,X_n\sim q\lambda$, where $$n=\frac{n_0}c\tag6$$ for some $n_0\in\mathbb N$, and estimate $I$ by $$\frac1{n_0}\sum_{i=1}^ng(X_i).\tag7$$ How can we prove rigorously that $(7)$ is a sensible estimator of $I$?
We may consider the number $$N_n:=\sum_{i=1}^n1_B(X_i)$$ of visits to $B$ up to time $n$ and note that $$\operatorname E\left[N_n\right]=nc\tag8$$ and $$\frac{N_n}n\xrightarrow{n\to\infty}c\;\;\;\text{almost surely}.\tag9$$ Now, $(7)$ is equal to $$\frac1{\operatorname E\left[N_n\right]}\sum_{i=1}^{N_n}g(Y_i)\tag{10}.$$
I first thought $(10)$ might tend to $I$ as $n$ tends to $\infty$, but since $$\frac1m\sum_{i=1}^mg(X_i)\xrightarrow{m\to\infty}I\tag{11}$$ this cannot be true (since the $(10)$ and the left-hand side of $(11)$ are off by factor of $c$). So, does maybe $(10)$ tend to $I$ as $n_0$ tends to $\infty$?
According to Wald's lemma, if $N$ is a nonegative integer valued random variable with $EN <\infty$ and $X_n$ is an iid process, then under some not too restrictive conditions $$ E[ \sum_{i=1}^N X_i] = EN EX_1, $$ hence $$E\bigg[ \frac1{EN_n } \sum_{i=1}^{N_n} g(X_i)\bigg] = Eg(X_1) = \int_B fd\lambda$$ Then you can apply SLLN, since the the process $g(X_i)$ is iid and the expectation is finite. Actually you don't necessarily need the $Y_i$'s, since your $g$ takes care of the case when $X_i\notin B$. $$ E[g(X_i)] = \int_B f d\lambda$$ so due to the SSLN $$ \frac1n \sum_{i=1}^n g(X_i) \to \int_B fd\lambda\quad a.s. $$ Note that $$ \sum_{i=1}^n g(X_i) = \sum_{i=1}^{N_n} g(Y_i) $$ hence $$ \frac1{nc} \sum_{i=1}^n g(Y_i) \to \frac1c \int_B f\ d\lambda$$