0% found this document useful (0 votes)
14 views

Week 9 - Activity 6

Iitm..
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Week 9 - Activity 6

Iitm..
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#Detils about datatset. This Data has mainly two entities; i.

1. IMDB movie ID.

2. ratings out of 10.

Module used

1. Matplotlib for graph plot


2. Scipystats for various Distribution access
3. pandas for easy and fast access of csv(comma seprated files)
4. numpy for various mathematical functionalities.

Calculation of Alpha and Beta parameters of


Gamma Distribution
In [13]:
#calcultaing value of method of moment
# trying to set it with gamma distribution

#calculating probability of given distribution


# dict_probability

'''
as m1 = alpha/beta
m2 = a^2/b^2 + a/b^2

'''

alphamm = exp_1 * exp_1/var


betamm = exp_1/var
# print(alphamm, betamm)
print("Gamma Distribution, Alpha parameter value: ", alphamm)
print("Gamma Distribution, Beta Parameter value", betamm)

fig, ax = plt.subplots(1,1)
ax.hist(rec_imdb_arr, density=True, bins= 10)
xx = np.linspace(0,10,5000)
ax.plot(xx, st.gamma.pdf(xx,alphamm,scale = 1/betamm), label='gamm
ax.legend(loc='best')
plt.show()

Gamma Distribution, Alpha parameter value: 14.552182610027748


Gamma Distribution, Beta Parameter value 2.5360002633431473
Bootstrap calculation
In [7]:
N = 1000
n = len(rec_data)
print('Length of given Data is:', n)
alpha_hat = np.zeros(N)
beta_hat = np.zeros(N)
for i in np.arange(N):
x_i = st.gamma.rvs(alphamm, scale=1/betamm, size= n)
m1_i = np.average(x_i)
ss_i = np.var(x_i)
alpha_hat[i] = m1_i * m1_i /ss_i
beta_hat[i] = m1_i /ss_i

ax_1 = plt.subplot(121)
ax_1.hist(alpha_hat, density=True)
ax_2 = plt.subplot(122)
ax_2.hist(beta_hat, density=True)
#now calculating square standard deviation
print('variance of Alpha_hat', np.sqrt(np.var(alpha_hat)))
print('Variance of Beta_Hat', np.sqrt(np.var(beta_hat)))
plt.show()

Length of given Data is: 910


variance of Alpha_hat 0.7004493291001074
Variance of Beta_Hat 0.1235619160209917
In [8]:
#Now we are calculating the variance between estimator and actual
print('standard deviation of alpha_hat', np.sqrt(np.var(alpha_hat)
print('standard deviation of beta_hat', np.sqrt(np.var(beta_hat)))
#we Are getting very high value of variance let's see what happen

standard deviation of alpha_hat 0.7004493291001074


standard deviation of beta_hat 0.1235619160209917

Confidence interval calculation


In [9]:
del_1 = np.percentile(alpha_hat - alphamm, 97.5)
del_2 = np.percentile(alpha_hat - alphamm, 2.5)
print('Value of del_1 and del_2 is: ', [del_1, del_2])

#now calcultaing maximum likelihood


lm_1 = np.average(np.log(rec_imdb_arr))
print(lm_1)
print(np.log(exp_1))

from scipy.special import digamma


print(digamma(exp_1))
f_ML = lambda a: (np.log(a) - digamma(a) - np.log(exp_1)+ lm_1)

fig , ax = plt.subplots(1,1)
xx = np.linspace(0.01, 2, 50)
ax.plot(xx, f_ML(xx))
ax.grid(True)
plt.show()
# 95% of confidence interval for alpha using the method of moments
# Work out to be
#[14.552 - 1.689, 14.552 - (-1.349)]

Value of del_1 and del_2 is: [1.4538462751668666, -1.2336799314290299]


1.7065554690335327
1.7471528495126276
1.6574949043572276

95% of confidence interval for alpha using the method of moments estimator

Work out to be

[14.552 - 1.689, 14.552 - (-1.349)] = [12.869, 15.901]

In [10]:
# Now solving numerically
import scipy.optimize as spot
sol = spot.root_scalar(f_ML, bracket=[0.01,1000])
sol.root # don't Know why it is throwing this value

Out [10]: 12.480431978112412

In [11]:
alpha_ml = sol.root
beta_ml = alpha_ml/ exp_1
print('Alpha_ml :', alpha_ml)
print('Beta_ml:', beta_ml)
Alpha_ml : 12.480431978112412
Beta_ml: 2.174957505090638

Plotting graph with given maximum


likelihood
In [12]: fig, ax = plt.subplots(1, 1)
ax.hist(rec_imdb_arr, density= True, bins= 10)
xx = np.linspace(0,10, 5000)
ax.plot(xx, st.gamma.pdf(xx, alphamm, scale= 1/betamm), lw= '4', a
ax.plot(xx, st.gamma.pdf(xx, alpha_ml, scale= 1/beta_ml), lw= '1',
ax.legend(loc= 'best')
plt.show()
print('Here is the final Graph after calculation of ML and MM')

Here is the final Graph after calculation of ML and MM

Conclusion
From above graph it's clear that

Both ML and MM are not able to provide batter approximation for above data.

You might also like