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

# Question 1

### a)

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

in which $p( heta)=1in(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)

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *