package empirical_likelihood has all the EL related classes/functions. Can be found here

In [11]:
import csv, sys
import numpy as np
import matplotlib.pyplot as plt 
import empirical_likelihood as el 

Owens tree width data. Can be also found here

In [12]:
year,width = [],[]
with open('tree_ring.csv','rt') as file:
	f = csv.reader(file)
	for row in f:
		year.append(int(row[0]))
		width.append(float(row[1]))

Define the points for EL estimation and prepare the needed objects

In [13]:
data = el.data(np.array(width),block_length=50,block_shift=10)
#data = el.data(np.array(width)) ''' alternative for not employing the block structure '''

mean_try = np.linspace(0.014, 0.041, 50)
thetas = []
els = []

for t in mean_try:
	theta = el.parameter(t,mc='tree') #set the moment condition to tree_data
	els.append(theta.get_el(data)) #ACTUAL EVALUATION OF EL
	thetas.append(theta)

els = np.array(els)
theta_hat = thetas[np.argmax(els)]

Results analysis, plots ... etc

In [19]:
%matplotlib inline
plt.figure(1)
plt.plot(mean_try,els,'o-k',label='EL')
if any(over):
	plt.plot(mean_try[over],els[over],'o-r',label='%.2f%% confidence' % (conf_level))
plt.legend(loc='best')
plt.title('Pine tree width \n EL mean with block = %d' % data.block.length)
plt.grid(True)
plt.show()
No description has been provided for this image
In [18]:
# Confidence interval
conf_level = .95
over = els>(theta_hat.confidence(conf_level))
conf_interval = [np.min(mean_try[over]),np.max(mean_try[over])]

print('EL mean = %.3f, with block size =  %d' % (theta_hat.value,data.block.length))
print('Conf interval [%.4f, %.4f], width = %.3f' % (conf_interval[0],conf_interval[1], conf_interval[1]-conf_interval[0]))
EL mean = 0.027, with block size =  50
Conf interval [0.0217, 0.0322], width = 0.010