程序代写代做代考 python # Question 1

# Question 1

### a)

$$
p(\theta|n,y) = \frac{p(\theta)p(y|\theta,n)}{\int_0^1p(\theta)p(y|\theta,n)d\theta}=(n+1)\binom{n}{y}\theta^y(1-\theta)^{n-y}\\
$$

in which $p(\theta)=1\in(0,1)$ is the prior.

### b)

“`python
import matplotlib.pyplot as plt
import numpy as np
from math import factorial as fact
def fun(n,the,y):
return fact(n)/(fact(y)*fact(n-y)) * the ** y * (1-the)**(n-y)

x = list(range(5))
y = [fun(4,0.5,i) for i in x]
#print(y)

plt.plot(x, y,’bo-‘)
plt.title(‘likelyhood’)
plt.xlabel(‘y’)
plt.ylabel(‘probability’)
plt.show()

“`

![Figure_1.png](Figure_1.png)

### c)

“`python 
import matplotlib.pyplot as plt
import numpy as np
from math import factorial as fact

def fun1(n,the,y):
return fact(n+1)/(fact(y)*fact(n-y)) * the ** y * (1-the)**(n-y)
heads = [1,2,2,3]

for i, head in enumerate(heads):
x = np.linspace(0,1, 1000)
y = [fun1(i+1,t, head) for t in x]
plt.plot(x,y)
s = ‘Posterior for ‘+str(head) + ‘ heads in ‘+ str(i+1)+’ trials’
plt.title(s)
plt.xlabel(‘\\theta’)
plt.ylabel(‘posterior’)
plt.show()
“`

![](Figure_1-1.png)

![](Figure_1-2.png)

![](Figure_1-3.png)

![](Figure_1-4.png)