Monte Carlo
(a) Implement rejection sampling in Python 3 by editing the rejection_sampled_expectation function in the provided rejectionSampling.py. The function should compute and return the expectation of the provided function f on the provided target distribution using rejection sampling. The rejectionSampling.py file contains an example monte_carlo_expectation function, and main function that will automatically test your implementation. You may edit the main file to perform any additional tests that you like.
We will run your code by importing the rejectionSampling module and calling
rejection_sampled_expectation, so it is important that your code follow these naming conventions.
需求:请您帮我做(a)部分,也就是在文件rejectionSampling.py里, 添加rejection_sampling_expectation函数( 在#TODO: Add implementation here的地方)。 然后用rs_utils.py 文件,去跑完成的 rejectionSampling.py , 跑出rejection_sampling_expectation的期望值。编写rejection_sampling_expectation函数时用均匀分布,范围从0到1的均匀分布即可。
我将使用您帮我做好的程序(谢谢!),回答作业后面的内容:
(b) Estimate the expected value of a Gaussian distribution with mean 1.74 and
standard deviation 0.5, truncated below 0.75 and above 2.75. (This distribution is pro-
vided as trunc). Use your rejection sampling implementation to draw 2,000 samples
from the target distribution. Use a Uniform[-3,3] distribution (the U33 distribution) as a
proposal distribution.
i. What was the estimated expectation?
ii. How many samples were rejected?
(c) Estimate the expected value of a Gaussian distribution with mean 1.74 and
standard deviation 0.5, truncated below 0.75 and above 2.75. (This distribution is pro-
vided as trunc). Use your rejection sampling implementation to draw 2,000 samples from
the target distribution. Use an untruncated Gaussian distribution with mean 2.75 and
standard deviation 0.5 (the untrunc distribution) as a proposal distribution.
i. What was the estimated expectation?
ii. How many samples were rejected?
(d) —–
等等