I'm solving a numerical problem in Matlab and I need to generate a sample from a non-standard probability density function. I'm fully aware of that old trick of inverting the cumulative distribution function and applying it on random numbers from an Uniform $[0,1]$ distribution but I think this won't do the trick here, since there's no closed form for the desired cdf.
Let $J$ be a fixed integer (odd). Define $\theta_{j} = j - (J+1)/2$, for all $j = 1, 2, ..., J$. Let $f(x, \theta)$ be the probability density function of a Normal $(\theta, 1)$ distribution. Now we define the following density:
$g(x) = \frac{\sum_{j=1}^{J}f(x,\theta_{j})}{J}$
It's easy to see that $g( ) $ has all of the good properties of pdf. My question is how to generate a sample coming from this density. I'm working on Matlab but I think the same procedure would work everywhere. Any ideas or hints?
Pick $j$ uniformly, and then pick $x$ from the $N(\theta_j,1)$ distribution.
ADDED: this works because the density function of the output of this process matches your $g$ formula. For any set $A$, we have $P(x \in A)$ is given by $\sum_{i=1}^J \int_A f(y,\theta_j) \,dy$, by conditioning on the randomly chosen $j$. But this is exactly what your $\int_A g(y)\,dy$ gives.