程序代写 practice – Problem 4

practice – Problem 4

Copyright By PowCoder代写 加微信 powcoder

import numpy as np
from scipy.stats import uniform

## record your uni here

Problem 4¶
Part I (10 points)¶
Let $f(x) = 0.5\sin(x) 1_{\{0< x < \pi\}}$ Implement the rejection sampling algorithm for this distribution. # we use Unif(0,pi) as our proposal distribution # c = max f(x) / g(x) = max 0.5 * sin(x) / (1 / pi) = 0.5 * pi # f(x) / (c g(x)) = sin(x) def rej_sampling(): U1 = np.pi * uniform.rvs() while True: U2 = uniform.rvs() if U2 < np.sin(U1): U1 = np.pi * uniform.rvs() Part II (10 points)¶ Using $n=10,000$ simulations, calculate a 95% confidence interval for $E[X^3]$ where $X$ has the density listed above. n = 10_000 X3 = [rej_sampling()**3 for i in range(n)] EX3 = np.mean(X3) sig = np.std(X3) lower = EX3 - 1.96 * sig / np.sqrt(n) upper = EX3 + 1.96 * sig / np.sqrt(n) print(lower, upper) 6.1383775342036895 5.895604202832638 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com