Better confidence interval in kreg_demo2

master
Per.Andreas.Brodtkorb 13 years ago
parent ef856af7ae
commit f6c3219870

@ -3217,7 +3217,7 @@ def test_smoothn_1d():
z = smoothn(y) # Regular smoothing z = smoothn(y) # Regular smoothing
zr = smoothn(y,robust=True) # Robust smoothing zr = smoothn(y,robust=True) # Robust smoothing
plt.subplot(121), plt.subplot(121),
h = plt.plot(x,y,'r.',x,z,'k',linewidth=2) unused_h = plt.plot(x,y,'r.',x,z,'k',linewidth=2)
plt.title('Regular smoothing') plt.title('Regular smoothing')
plt.subplot(122) plt.subplot(122)
plt.plot(x,y,'r.',x,zr,'k',linewidth=2) plt.plot(x,y,'r.',x,zr,'k',linewidth=2)
@ -3476,7 +3476,7 @@ def kreg_demo2(n=100, hs=None, symmetric=False, fun='hisj'):
x = x.ravel() x = x.ravel()
kernel = Kernel('gauss',fun=fun) kernel = Kernel('gauss',fun=fun)
hopt = kernel.get_smoothing(x)/2 hopt = kernel.get_smoothing(x)
if hs is None: if hs is None:
hs = hopt hs = hopt
if symmetric: if symmetric:
@ -3499,6 +3499,9 @@ def kreg_demo2(n=100, hs=None, symmetric=False, fun='hisj'):
#plt.scatter(xi,logyi) #plt.scatter(xi,logyi)
#return #return
#print(logyi) #print(logyi)
dx = xi[1]-xi[0]
ckreg = KDE(x,hs=hs)
ci = ckreg.eval_grid(xi)*n*dx
gkreg = KRegression(xi, yi, hs=hs, xmin=xmin-2*hopt,xmax=xmax+2*hopt) gkreg = KRegression(xi, yi, hs=hs, xmin=xmin-2*hopt,xmax=xmax+2*hopt)
fg = gkreg.eval_grid(xi,output='plotobj', title='Kernel regression', plotflag=1) fg = gkreg.eval_grid(xi,output='plotobj', title='Kernel regression', plotflag=1)
@ -3506,9 +3509,13 @@ def kreg_demo2(n=100, hs=None, symmetric=False, fun='hisj'):
alpha=0.05 alpha=0.05
z0 = -_invnorm(alpha/2) z0 = -_invnorm(alpha/2)
# ref Casella and Berger (1990) "Statistical inference" pp444
pup = (pi + z0*np.sqrt(pi*(1-pi)/c)).clip(min=0,max=1) a = 2*pi + z0**2/(ci+1e-16)
plo = (pi - z0*np.sqrt(pi*(1-pi)/c)).clip(min=0,max=1) b = 2*(1+z0**2/(ci+1e-16))
plo2 = ((a-sqrt(a**2-2*pi**2*b))/b).clip(min=0,max=1)
pup2 = ((a+sqrt(a**2-2*pi**2*b))/b).clip(min=0,max=1)
pup = (pi + z0*np.sqrt(pi*(1-pi)/ci)).clip(min=0,max=1)
plo = (pi - z0*np.sqrt(pi*(1-pi)/ci)).clip(min=0,max=1)
#print(fg.data) #print(fg.data)
#fg.data = np.exp(fg.data) #fg.data = np.exp(fg.data)
@ -3519,6 +3526,7 @@ def kreg_demo2(n=100, hs=None, symmetric=False, fun='hisj'):
f.plot(label='KRegression') f.plot(label='KRegression')
plt.plot(xi, pup,'r--', xi, plo,'r--', label='%d CI' % (int(100*(1-alpha)))) plt.plot(xi, pup,'r--', xi, plo,'r--', label='%d CI' % (int(100*(1-alpha))))
plt.plot(xi, pup2,'r:', xi, plo2,'r:', label='%d CI2' % (int(100*(1-alpha))))
plt.plot(xi, 0.5*np.cos(xi)+.5, label='True model') plt.plot(xi, 0.5*np.cos(xi)+.5, label='True model')
plt.scatter(xi,yi, label='data') plt.scatter(xi,yi, label='data')
print(np.nanmax(f.data)) print(np.nanmax(f.data))
@ -3590,6 +3598,6 @@ if __name__ == '__main__':
#kde_demo2() #kde_demo2()
#kreg_demo1(fast=True) #kreg_demo1(fast=True)
#kde_gauss_demo() #kde_gauss_demo()
#kreg_demo2() kreg_demo2(n=100)
#test_smoothn_2d() #test_smoothn_2d()
test_smoothn_cardioid() #test_smoothn_cardioid()
Loading…
Cancel
Save