Jed Rembold
April 12, 2023
Now suppose we wanted to evaluate the area under the piece-wise function: \[ f(x) = \begin{cases}0 & x \leq 4;\\ 5x-20 & 4 < x \leq 5;\\ -5x+30 & 5 < x \leq 6;\\ 0 & x > 6;\end{cases} \] from \(x=0\) to \(x=10\), using Monte Carlo methods. Check into both uniform sampling and sampling targeted about the central peak.
Trying to select from a continuous probability distribution discretely is not ideal
For a better option, roll your own selector:
def sample_from_prob_dist(pdf, lower, upper, num):
samples = []
while len(samples) < num:
x = np.random.uniform(lower, upper)
p = pdf(x)
if np.random.uniform(0,1) <= p:
samples.append(x)
return np.array(samples)Now suppose we wanted to evaluate the area under the piece-wise function: \[ f(x) = \begin{cases}0 & x \leq 4;\\ 5x-20 & 4 < x \leq 5;\\ -5x+30 & 5 < x \leq 6;\\ 0 & x > 6;\end{cases} \] from \(x=0\) to \(x=10\), using Monte Carlo methods. Sample from the probability distribution defined as: \[ p(x) = \begin{cases} 0, & x <= 3 \\ 0.25x - 0.75, & 3 < x <= 5 \\ -0.25x + 1.75, & 5 < x <= 7 \\ 0, & x > 7 \end{cases} \]
Randomly choosing a nearby point inherently pulls from a probability distribution centered on the current point
Most frequently used is a Gaussian probability distribution
Working in higher dimensions? A multi-variate Gaussian will still work!
np.random.multivariate_normal(mean=VEC, cov=MAT)